1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.devzendo.xplp;
18
19 import java.util.Properties;
20
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24
25
26
27
28
29
30
31
32
33
34 public final class TestPropertiesInterpolation {
35 private final String lineSeparator = System.getProperty("line.separator");
36 private Properties mProps;
37 private PropertiesInterpolator mInterpolator;
38 private String mLibsString;
39
40 @Before
41 public void getPrerequisites() {
42 mProps = new Properties();
43 mProps.put("key", "value");
44 mProps.put("long.key.name", "long value");
45 mProps.put("return", "line1" + lineSeparator + "line2");
46
47 final StringBuilder sb = new StringBuilder();
48 sb.append(" <string>$JAVAROOT/lib/BeanCounter-0.1.0-SNAPSHOT.jar</string>" + lineSeparator);
49 sb.append(" <string>$JAVAROOT/lib/MiniMiser-0.1.0-SNAPSHOT.jar</string>" + lineSeparator);
50 mLibsString = sb.toString();
51 mProps.put("xplp.macosxclasspatharray", mLibsString);
52
53 mInterpolator = new PropertiesInterpolator(mProps);
54 }
55
56 @Test
57 public void nullAndEmptyPassedStraightThrough() {
58 Assert.assertNull(mInterpolator.interpolate(null));
59 Assert.assertEquals(0, mInterpolator.interpolate("").length());
60 }
61
62 @Test
63 public void noInterpolation() {
64 Assert.assertEquals("verbatim text", mInterpolator.interpolate("verbatim text"));
65 }
66
67 @Test
68 public void replaceOneInstance() {
69 Assert.assertEquals("check value test", mInterpolator.interpolate("check ${key} test"));
70 Assert.assertEquals("check long value test", mInterpolator.interpolate("check ${long.key.name} test"));
71 }
72
73 @Test
74 public void replaceAtStart() {
75 Assert.assertEquals("value test", mInterpolator.interpolate("${key} test"));
76 }
77
78 @Test
79 public void replaceAtEnd() {
80 Assert.assertEquals("test value", mInterpolator.interpolate("test ${key}"));
81 }
82
83 @Test
84 public void replaceMultipleOccurrences() {
85 Assert.assertEquals("check value test value investigate", mInterpolator.interpolate("check ${key} test ${key} investigate"));
86 }
87
88 @Test
89 public void replaceMultipleOccurrencesMultipleKeys() {
90 Assert.assertEquals("check value test long value foo long value",
91 mInterpolator.interpolate("check ${key} test ${long.key.name} foo ${long.key.name}"));
92 }
93
94 @Test
95 public void replaceMultipleOccurrencesMultipleKeysRightNextToEachOther() {
96 Assert.assertEquals("valuelong valuelong value",
97 mInterpolator.interpolate("${key}${long.key.name}${long.key.name}"));
98 }
99
100 @Test
101 public void replaceMultipleOccurrencesMultipleKeysRightNextToEachOtherIncludingNewlines() {
102 Assert.assertEquals("valueline1" + lineSeparator + "line2long value",
103 mInterpolator.interpolate("${key}${return}${long.key.name}"));
104 }
105
106 @Test(expected = IllegalStateException.class)
107 public void variableNotFound() {
108 mInterpolator.interpolate("wahey ${nonexistant} frugal!");
109 }
110
111 @Test
112 public void replaceLongString() {
113 Assert.assertEquals(mLibsString, mInterpolator.interpolate("${xplp.macosxclasspatharray}"));
114 }
115
116 @Test
117 public void dontInterpolateInComments() {
118 Assert.assertEquals("# ${env.HOME}", mInterpolator.interpolate("# ${env.HOME}"));
119 }
120
121 @Test
122 public void doNotReplaceNoInterpolatedVariables() {
123 mInterpolator.doNotInterpolate("VERBATIM");
124 Assert.assertEquals("check value test value don't replace ${VERBATIM} investigate", mInterpolator.interpolate("check ${key} test ${key} don't replace ${VERBATIM} investigate"));
125 }
126
127 @Test
128 public void janelSpecialVerbatimVariablesAreNotReplaced() {
129
130 mProps.put("FOUND_EXE_FOLDER", "${FOUND_EXE_FOLDER}");
131 Assert.assertEquals("check value test value don't replace ${FOUND_EXE_FOLDER} investigate", mInterpolator.interpolate("check ${key} test ${key} don't replace ${FOUND_EXE_FOLDER} investigate"));
132 }
133 }