1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.util;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30 public class StringUtilTest {
31
32
33
34 @Test
35 public void testDefaultStringSubstitution() throws Exception {
36 final Map<String, String> vars = new HashMap<>();
37 vars.put("foo", "FOO");
38 vars.put("bar", "BAR");
39
40 assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
41 assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString());
42 }
43
44
45
46
47 @Test
48 public void testErroneousTemplate() throws Exception {
49 final Map<String, String> vars = new HashMap<>();
50 vars.put("foo", "FOO");
51
52 assertEquals("This is a FOO & ${}} test", StringUtils.stringSubstitution("This is a ${foo} & ${}} test", vars, true).toString());
53 }
54
55
56
57
58 @Test
59 public void testIncompleteSubstitution() throws Exception {
60
61 final Map<String, String> vars = new HashMap<>();
62 vars.put("foo", "FOO");
63
64 assertEquals("This is a FOO & ${bar} test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
65
66 try {
67 StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString();
68 fail();
69 } catch (final RuntimeException e) {
70
71 }
72 }
73
74
75
76
77 @Test
78 public void testNoStringSubstitution() throws Exception {
79 final Map<String, String> vars = new HashMap<>();
80 vars.put("foo", "FOO");
81 vars.put("bar", "BAR");
82
83 assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a FOO & BAR test", vars, true).toString());
84 }
85 }