KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > test > StringPropertyReplacerUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.util.test;
23
24 import java.io.File JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.jboss.test.JBossTestCase;
28 import org.jboss.util.StringPropertyReplacer;
29
30 /** Unit tests for the StringPropertyReplacer utility class
31  *
32  * @see org.jboss.util.StringPropertyReplacer
33  * @author Scott.Stark@jboss.org
34  * @author Dimitris.Andreadis@jboss.org
35  *
36  * @version $Revision: 37406 $
37  */

38 public class StringPropertyReplacerUnitTestCase extends JBossTestCase
39 {
40    public StringPropertyReplacerUnitTestCase(String JavaDoc name)
41    {
42       super(name);
43    }
44
45    /** Tests of the ${x} property replacement
46     *
47     * @throws Exception
48     */

49    public void testPropReplacement()
50       throws Exception JavaDoc
51    {
52       getLog().debug("+++ testPropReplacement");
53       String JavaDoc xref = "${x}";
54       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref);
55       assertTrue("xval == xref", xval.equals(xref));
56       
57       System.setProperty("x", "testPropReplacement");
58       xval = StringPropertyReplacer.replaceProperties(xref);
59       assertTrue("xval == 'xval'", xval.equals("testPropReplacement"));
60    }
61
62    /** Tests of the ${x} property replacement with a non-System Properties
63     *
64     * @throws Exception
65     */

66    public void testNonSystemPropReplacement()
67       throws Exception JavaDoc
68    {
69       getLog().debug("+++ testNonSystemPropReplacement");
70       String JavaDoc xref = "${xx}";
71
72       Properties JavaDoc props = new Properties JavaDoc();
73       props.setProperty("xx", "testNonSystemPropReplacement");
74       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
75       assertTrue("xval == 'xval'", xval.equals("testNonSystemPropReplacement"));
76    }
77
78    /** Test the specified default value is used
79     * when the system property could not be replaced,
80     * and ignored when the system property is replaced.
81     */

82    public void testDefaultValueSystemPropReplacement()
83       throws Exception JavaDoc
84    {
85       getLog().debug("+++ testDefaultValueSystemPropReplacement");
86
87       String JavaDoc xref = "${xxx:d}";
88       String JavaDoc xdef = "d";
89       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref);
90       assertTrue("xval == xdef", xval.equals(xdef));
91       
92       System.setProperty("xxx", "testPropReplacement");
93       xval = StringPropertyReplacer.replaceProperties(xref);
94       assertTrue("xval == 'xval'", xval.equals("testPropReplacement"));
95    }
96    
97
98    /** Test the specified default value is used
99     * when the non-system property could not be replaced
100     * and ignored when the non-property is replaced.
101     */

102    public void testDefaultValueNonSystemPropReplacement()
103       throws Exception JavaDoc
104    {
105       getLog().debug("+++ testDefaultValueNonSystemPropReplacement");
106
107       Properties JavaDoc props = new Properties JavaDoc();
108       
109       String JavaDoc xref = "${xxx:d}";
110       String JavaDoc xdef = "d";
111       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
112       assertTrue("xval == xdef", xval.equals(xdef));
113       
114       props.setProperty("xxx", "testNonSystemPropReplacement");
115       xval = StringPropertyReplacer.replaceProperties(xref, props);
116       assertTrue("xval == 'xval'", xval.equals("testNonSystemPropReplacement"));
117    }
118    
119    /**
120     * Test the scenario where a primary and a secondary
121     * system property is specified.
122     */

123    public void testSecondarySystemPropReplacement()
124       throws Exception JavaDoc
125    {
126       getLog().debug("+++ testSecondarySystemPropReplacement");
127       
128       String JavaDoc xref = "${x1,x2}";
129       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref);
130       assertTrue("xval == 'xref'", xval.equals(xref));
131       
132       System.setProperty("x2", "secondaryPropReplacement");
133       xval = StringPropertyReplacer.replaceProperties(xref);
134       assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement"));
135       
136       System.setProperty("x1", "primaryPropReplacement");
137       xval = StringPropertyReplacer.replaceProperties(xref);
138       assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement"));
139    }
140    
141    /**
142     * Test the scenario where a primary and a secondary
143     * non-system property, plus a default value are specified
144     */

145    public void testSecondaryNonSystemPropReplacementWithDefault()
146       throws Exception JavaDoc
147    {
148       getLog().debug("+++ testSecondaryNonSystemPropReplacementWithDefault");
149       
150       Properties JavaDoc props = new Properties JavaDoc();
151       
152       String JavaDoc xref = "${x1,x2:d}";
153       String JavaDoc xdef = "d";
154       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
155       assertTrue("xval == 'xdef'", xval.equals(xdef));
156       
157       props.setProperty("x2", "secondaryPropReplacement");
158       xval = StringPropertyReplacer.replaceProperties(xref, props);
159       assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement"));
160       
161       props.setProperty("x1", "primaryPropReplacement");
162       xval = StringPropertyReplacer.replaceProperties(xref, props);
163       assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement"));
164    }
165
166    /**
167     * Test that we first check if the property is set before
168     * trying to apply a default or a resolving secondary property
169     */

170    public void testPathologicalNonSystemPropReplacement()
171       throws Exception JavaDoc
172    {
173       getLog().debug("+++ testPathologicalNonSystemPropReplacement");
174       
175       Properties JavaDoc props = new Properties JavaDoc();
176       
177       String JavaDoc xref = "${x1,x2:d}";
178       props.setProperty("x1,x2:d", "pathologicalPropReplacement");
179       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
180       assertTrue("xval == 'xval'", xval.equals("pathologicalPropReplacement"));
181    }
182    
183    /**
184     * Test that a composite property gets resolved
185     * when the secondary property is missing.
186     */

187    public void testPathologicalMissingSecondaryProperty()
188       throws Exception JavaDoc
189    {
190       getLog().debug("+++ testPathologicalMissingSecondaryProperty");
191       
192       Properties JavaDoc props = new Properties JavaDoc();
193       
194       String JavaDoc xref = "${x1,}";
195       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
196       assertTrue("xval == 'xref'", xval.equals(xref));
197       
198       props.setProperty("x1", "primaryPropReplacement");
199       xval = StringPropertyReplacer.replaceProperties(xref, props);
200       assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement"));
201    }
202    
203    /**
204     * Test that a composite property with a default gets resolved
205     * when the secondary property is missing.
206     */

207    public void testPathologicalMissingSecondaryPropertyWithDefault()
208       throws Exception JavaDoc
209    {
210       getLog().debug("+++ testPathologicalMissingSecondaryPropertyWithDefault");
211       
212       Properties JavaDoc props = new Properties JavaDoc();
213       
214       String JavaDoc xref = "${x1,:d}";
215       String JavaDoc xdef = "d";
216       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
217       assertTrue("xval == 'xdef'", xval.equals(xdef));
218       
219       props.setProperty("x1", "primaryPropReplacement");
220       xval = StringPropertyReplacer.replaceProperties(xref, props);
221       assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement"));
222    }
223    
224    /**
225     * Test that a composite property gets resolved
226     * when the primary property is missing.
227     */

228    public void testPathologicalMissingPrimaryProperty()
229       throws Exception JavaDoc
230    {
231       getLog().debug("+++ testPathologicalMissingPrimaryProperty");
232       
233       Properties JavaDoc props = new Properties JavaDoc();
234       
235       String JavaDoc xref = "${,x2}";
236       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
237       assertTrue("xval == 'xref'", xval.equals(xref));
238       
239       props.setProperty("x2", "secondaryPropReplacement");
240       xval = StringPropertyReplacer.replaceProperties(xref, props);
241       assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement"));
242    }
243    
244    /**
245     * Test that a composite property with a default gets resolved
246     * when the primary property is missing.
247     */

248    public void testPathologicalMissingPrimaryPropertyWithDefault()
249       throws Exception JavaDoc
250    {
251       getLog().debug("+++ testPathologicalMissingPrimaryPropertyWithDefault");
252       
253       Properties JavaDoc props = new Properties JavaDoc();
254       
255       String JavaDoc xref = "${,x2:d}";
256       String JavaDoc xdef = "d";
257       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
258       assertTrue("xval == 'xdef'", xval.equals(xdef));
259       
260       props.setProperty("x2", "secondaryPropReplacement");
261       xval = StringPropertyReplacer.replaceProperties(xref, props);
262       assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement"));
263    }
264    
265    /**
266     * Test that with an empty default value we get the
267     * property evaluating to an empty string "", when
268     * the property is undefined
269     */

270    public void testEmptyDefaultNonSystemPropReplacement()
271       throws Exception JavaDoc
272    {
273       getLog().debug("+++ testEmptyDefaultNonSystemPropReplacement");
274       
275       Properties JavaDoc props = new Properties JavaDoc();
276       
277       String JavaDoc xref="${x1:}";
278       String JavaDoc xval = StringPropertyReplacer.replaceProperties(xref, props);
279       assertTrue("xval == ''", xval.equals(""));
280       
281       props.setProperty("x1", "primaryPropReplacement");
282       xval = StringPropertyReplacer.replaceProperties(xref, props);
283       assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement"));
284    }
285    
286    /** Test that ${/} and ${:} refs are replaced with
287     *
288     * @throws Exception
289     */

290    public void testFilePropReplacement()
291       throws Exception JavaDoc
292    {
293       getLog().debug("+++ testFilePropReplacement");
294       String JavaDoc pathSeparatorRef = "${:}";
295       String JavaDoc pathSeparator = StringPropertyReplacer.replaceProperties(pathSeparatorRef);
296       String JavaDoc separatorRef = "${/}";
297       String JavaDoc separator = StringPropertyReplacer.replaceProperties(separatorRef);
298       getLog().debug("File.pathSeparator='"+File.pathSeparator+"'");
299       getLog().debug("File.separator='"+File.separator+"'");
300       assertTrue("${:}("+pathSeparator+") == File.pathSeparator", pathSeparator.equals(File.pathSeparator));
301       assertTrue("${/}("+separator+") == File.separator", separator.equals(File.separator));
302    }
303
304 }
305
Popular Tags