1 package org.codehaus.plexus.interpolation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.IOException;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29 import static org.junit.jupiter.api.Assertions.fail;
30
31 public class RegexBasedInterpolatorTest {
32
33 @BeforeEach
34 public void setUp() {
35 EnvarBasedValueSource.resetStatics();
36 }
37
38 public String getVar() {
39 return "testVar";
40 }
41
42 @Test
43 public void testShouldFailOnExpressionCycle() {
44 Properties props = new Properties();
45 props.setProperty("key1", "${key2}");
46 props.setProperty("key2", "${key1}");
47
48 RegexBasedInterpolator rbi = new RegexBasedInterpolator();
49 rbi.addValueSource(new PropertiesBasedValueSource(props));
50
51 try {
52 rbi.interpolate("${key1}", new SimpleRecursionInterceptor());
53
54 fail("Should detect expression cycle and fail.");
55 } catch (InterpolationException e) {
56
57 }
58 }
59
60 @Test
61 public void testShouldResolveByMy_getVar_Method() throws InterpolationException {
62 RegexBasedInterpolator rbi = new RegexBasedInterpolator();
63 rbi.addValueSource(new ObjectBasedValueSource(this));
64 String result = rbi.interpolate("this is a ${this.var}", "this");
65
66 assertEquals("this is a testVar", result);
67 }
68
69 @Test
70 public void testShouldResolveByContextValue() throws InterpolationException {
71 RegexBasedInterpolator rbi = new RegexBasedInterpolator();
72
73 Map context = new HashMap();
74 context.put("var", "testVar");
75
76 rbi.addValueSource(new MapBasedValueSource(context));
77
78 String result = rbi.interpolate("this is a ${this.var}", "this");
79
80 assertEquals("this is a testVar", result);
81 }
82
83 @Test
84 public void testShouldResolveByEnvar() throws IOException, InterpolationException {
85 OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.EnvVarSource() {
86 public Map<String, String> getEnvMap() {
87 HashMap<String, String> map = new HashMap<String, String>();
88 map.put("SOME_ENV", "variable");
89 return map;
90 }
91 });
92
93 RegexBasedInterpolator rbi = new RegexBasedInterpolator();
94
95 rbi.addValueSource(new EnvarBasedValueSource());
96
97 String result = rbi.interpolate("this is a ${env.SOME_ENV}", "this");
98
99 assertEquals("this is a variable", result);
100 }
101
102 @Test
103 public void testUseAlternateRegex() throws Exception {
104 RegexBasedInterpolator rbi = new RegexBasedInterpolator("\\@\\{(", ")?([^}]+)\\}@");
105
106 Map context = new HashMap();
107 context.put("var", "testVar");
108
109 rbi.addValueSource(new MapBasedValueSource(context));
110
111 String result = rbi.interpolate("this is a @{this.var}@", "this");
112
113 assertEquals("this is a testVar", result);
114 }
115
116 @Test
117 public void testNPEFree() throws Exception {
118 RegexBasedInterpolator rbi = new RegexBasedInterpolator("\\@\\{(", ")?([^}]+)\\}@");
119
120 Map context = new HashMap();
121 context.put("var", "testVar");
122
123 rbi.addValueSource(new MapBasedValueSource(context));
124
125 String result = rbi.interpolate(null);
126
127 assertEquals("", result);
128 }
129
130 @Test
131 public void testUsePostProcessor_DoesNotChangeValue() throws InterpolationException {
132 RegexBasedInterpolator rbi = new RegexBasedInterpolator();
133
134 Map context = new HashMap();
135 context.put("test.var", "testVar");
136
137 rbi.addValueSource(new MapBasedValueSource(context));
138
139 rbi.addPostProcessor(new InterpolationPostProcessor() {
140 public Object execute(String expression, Object value) {
141 return null;
142 }
143 });
144
145 String result = rbi.interpolate("this is a ${test.var}", "");
146
147 assertEquals("this is a testVar", result);
148 }
149
150 @Test
151 public void testUsePostProcessor_ChangesValue() throws InterpolationException {
152
153 int loopNumber = 200000;
154
155 long start = System.currentTimeMillis();
156
157 RegexBasedInterpolator rbi = new RegexBasedInterpolator();
158
159 Map context = new HashMap();
160 context.put("test.var", "testVar");
161
162 rbi.addValueSource(new MapBasedValueSource(context));
163
164 rbi.addPostProcessor(new InterpolationPostProcessor() {
165 public Object execute(String expression, Object value) {
166 return value + "2";
167 }
168 });
169
170 for (int i = 0, number = loopNumber; i < number; i++) {
171
172 String result = rbi.interpolate("this is a ${test.var}", "");
173
174 assertEquals("this is a testVar2", result);
175 }
176 long end = System.currentTimeMillis();
177
178 System.out.println("time without pattern reuse and RegexBasedInterpolator instance reuse " + (end - start));
179
180 System.gc();
181
182 start = System.currentTimeMillis();
183
184 rbi = new RegexBasedInterpolator(true);
185
186 rbi.addPostProcessor(new InterpolationPostProcessor() {
187 public Object execute(String expression, Object value) {
188 return value + "2";
189 }
190 });
191
192 rbi.addValueSource(new MapBasedValueSource(context));
193
194 for (int i = 0, number = loopNumber; i < number; i++) {
195
196 String result = rbi.interpolate("this is a ${test.var}", "");
197
198 assertEquals("this is a testVar2", result);
199 }
200 end = System.currentTimeMillis();
201
202 System.out.println("time with pattern reuse and RegexBasedInterpolator instance reuse " + (end - start));
203 }
204 }