1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.exec.util;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.fail;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.junit.jupiter.api.Test;
30
31
32
33 class StringUtilTest {
34
35
36
37 @Test
38 void testDefaultStringSubstitution() throws Exception {
39 final Map<String, String> vars = new HashMap<>();
40 vars.put("foo", "FOO");
41 vars.put("bar", "BAR");
42
43 assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
44 assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString());
45 }
46
47
48
49
50 @Test
51 void testErroneousTemplate() throws Exception {
52 final Map<String, String> vars = new HashMap<>();
53 vars.put("foo", "FOO");
54
55 assertEquals("This is a FOO & ${}} test", StringUtils.stringSubstitution("This is a ${foo} & ${}} test", vars, true).toString());
56 }
57
58
59
60
61 @Test
62 void testIncompleteSubstitution() throws Exception {
63
64 final Map<String, String> vars = new HashMap<>();
65 vars.put("foo", "FOO");
66
67 assertEquals("This is a FOO & ${bar} test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
68
69 try {
70 StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString();
71 fail();
72 } catch (final RuntimeException e) {
73
74 }
75 }
76
77
78
79
80 @Test
81 void testNoStringSubstitution() throws Exception {
82 final Map<String, String> vars = new HashMap<>();
83 vars.put("foo", "FOO");
84 vars.put("bar", "BAR");
85
86 assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a FOO & BAR test", vars, true).toString());
87 }
88
89 @Test
90 void testQuoteArgument() throws Exception {
91 assertEquals("hi", StringUtils.quoteArgument("'hi'"));
92 assertEquals("hi", StringUtils.quoteArgument("\"hi\""));
93
94
95 assertEquals("\"echo 'hi'\"", StringUtils.quoteArgument("echo 'hi'"));
96 assertEquals("'echo \"hi\"'", StringUtils.quoteArgument("echo \"hi\""));
97
98 assertThrows(IllegalArgumentException.class, () -> StringUtils.quoteArgument("echo \"hi 'world'\""),
99 "Can't handle single and double quotes in same argument");
100 }
101
102 }