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      /**
41       * 
42       */
43      @Before
44      public void getPrerequisites() {
45          mProps = new Properties();
46          mProps.put("key", "value");
47          mProps.put("long.key.name", "long value");
48          mProps.put("return", "line1" + lineSeparator + "line2");
49          
50          final StringBuilder sb = new StringBuilder();
51          sb.append("            <string>$JAVAROOT/lib/BeanCounter-0.1.0-SNAPSHOT.jar</string>" + lineSeparator);
52          sb.append("            <string>$JAVAROOT/lib/MiniMiser-0.1.0-SNAPSHOT.jar</string>" + lineSeparator);
53          mLibsString = sb.toString();
54          mProps.put("xplp.macosxclasspatharray", mLibsString);
55          
56          mInterpolator = new PropertiesInterpolator(mProps);
57      }
58      
59      /**
60       * 
61       */
62      @Test
63      public void nullAndEmptyPassedStraightThrough() {
64          Assert.assertNull(mInterpolator.interpolate(null));
65          Assert.assertEquals(0, mInterpolator.interpolate("").length());
66      }
67      
68      /**
69       * 
70       */
71      @Test
72      public void noInterpolation() {
73          Assert.assertEquals("verbatim text", mInterpolator.interpolate("verbatim text"));
74      }
75      
76      /**
77       * 
78       */
79      @Test
80      public void replaceOneInstance() {
81          Assert.assertEquals("check value test", mInterpolator.interpolate("check ${key} test"));
82          Assert.assertEquals("check long value test", mInterpolator.interpolate("check ${long.key.name} test"));
83      }
84      
85      /**
86       * 
87       */
88      @Test
89      public void replaceMultipleOccurrences() {
90          Assert.assertEquals("check value test value investigate", mInterpolator.interpolate("check ${key} test ${key} investigate"));
91      }
92      
93      /**
94       * 
95       */
96      @Test
97      public void replaceMultipleOccurrencesMultipleKeys() {
98          Assert.assertEquals("check value test long value foo long value",
99              mInterpolator.interpolate("check ${key} test ${long.key.name} foo ${long.key.name}"));
100     }
101 
102     /**
103      * 
104      */
105     @Test
106     public void replaceMultipleOccurrencesMultipleKeysRightNextToEachOther() {
107         Assert.assertEquals("valuelong valuelong value",
108             mInterpolator.interpolate("${key}${long.key.name}${long.key.name}"));
109     }
110 
111     /**
112      * 
113      */
114     @Test
115     public void replaceMultipleOccurrencesMultipleKeysRightNextToEachOtherIncludingNewlines() {
116         Assert.assertEquals("valueline1" + lineSeparator + "line2long value",
117             mInterpolator.interpolate("${key}${return}${long.key.name}"));
118     }
119 
120     /**
121      * 
122      */
123     @Test(expected = IllegalStateException.class)
124     public void variableNotFound() {
125         mInterpolator.interpolate("wahey ${nonexistant} frugal!");
126     }
127     
128     /**
129      * 
130      */
131     @Test
132     public void replaceLongString() {
133         Assert.assertEquals(mLibsString, mInterpolator.interpolate("${xplp.macosxclasspatharray}"));
134     }
135 
136     /**
137      * 
138      */
139     @Test
140     public void dontInterpolateInComments() {
141         Assert.assertEquals("# ${env.HOME}", mInterpolator.interpolate("# ${env.HOME}"));
142     }
143 }