View Javadoc
1   /**
2    * Copyright (C) 2008-2010 Matt Gumbley, DevZendo.org <http://devzendo.org>;
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests correct interpolation of name=value pairs from a
28   * Properties object into a String, using Ant-style references
29   * to names - e.g ${name}.
30   * 
31   * @author matt
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         // it isn't cloned in the constructor, so can modify input props after construction :)
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 }