001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.util.test;
023:
024: import java.io.File;
025: import java.util.Properties;
026:
027: import org.jboss.test.JBossTestCase;
028: import org.jboss.util.StringPropertyReplacer;
029:
030: /** Unit tests for the StringPropertyReplacer utility class
031: *
032: * @see org.jboss.util.StringPropertyReplacer
033: * @author Scott.Stark@jboss.org
034: * @author Dimitris.Andreadis@jboss.org
035: *
036: * @version $Revision: 57211 $
037: */
038: public class StringPropertyReplacerUnitTestCase extends JBossTestCase {
039: public StringPropertyReplacerUnitTestCase(String name) {
040: super (name);
041: }
042:
043: /** Tests of the ${x} property replacement
044: *
045: * @throws Exception
046: */
047: public void testPropReplacement() throws Exception {
048: getLog().debug("+++ testPropReplacement");
049: String xref = "${x}";
050: String xval = StringPropertyReplacer.replaceProperties(xref);
051: assertTrue("xval == xref", xval.equals(xref));
052:
053: System.setProperty("x", "testPropReplacement");
054: xval = StringPropertyReplacer.replaceProperties(xref);
055: assertTrue("xval == 'xval'", xval.equals("testPropReplacement"));
056: }
057:
058: /** Tests of the ${x} property replacement with a non-System Properties
059: *
060: * @throws Exception
061: */
062: public void testNonSystemPropReplacement() throws Exception {
063: getLog().debug("+++ testNonSystemPropReplacement");
064: String xref = "${xx}";
065:
066: Properties props = new Properties();
067: props.setProperty("xx", "testNonSystemPropReplacement");
068: String xval = StringPropertyReplacer.replaceProperties(xref,
069: props);
070: assertTrue("xval == 'xval'", xval
071: .equals("testNonSystemPropReplacement"));
072: }
073:
074: /** Test the specified default value is used
075: * when the system property could not be replaced,
076: * and ignored when the system property is replaced.
077: */
078: public void testDefaultValueSystemPropReplacement()
079: throws Exception {
080: getLog().debug("+++ testDefaultValueSystemPropReplacement");
081:
082: String xref = "${xxx:d}";
083: String xdef = "d";
084: String xval = StringPropertyReplacer.replaceProperties(xref);
085: assertTrue("xval == xdef", xval.equals(xdef));
086:
087: System.setProperty("xxx", "testPropReplacement");
088: xval = StringPropertyReplacer.replaceProperties(xref);
089: assertTrue("xval == 'xval'", xval.equals("testPropReplacement"));
090: }
091:
092: /** Test the specified default value is used
093: * when the non-system property could not be replaced
094: * and ignored when the non-property is replaced.
095: */
096: public void testDefaultValueNonSystemPropReplacement()
097: throws Exception {
098: getLog().debug("+++ testDefaultValueNonSystemPropReplacement");
099:
100: Properties props = new Properties();
101:
102: String xref = "${xxx:d}";
103: String xdef = "d";
104: String xval = StringPropertyReplacer.replaceProperties(xref,
105: props);
106: assertTrue("xval == xdef", xval.equals(xdef));
107:
108: props.setProperty("xxx", "testNonSystemPropReplacement");
109: xval = StringPropertyReplacer.replaceProperties(xref, props);
110: assertTrue("xval == 'xval'", xval
111: .equals("testNonSystemPropReplacement"));
112: }
113:
114: /**
115: * Test the scenario where a primary and a secondary
116: * system property is specified.
117: */
118: public void testSecondarySystemPropReplacement() throws Exception {
119: getLog().debug("+++ testSecondarySystemPropReplacement");
120:
121: String xref = "${x1,x2}";
122: String xval = StringPropertyReplacer.replaceProperties(xref);
123: assertTrue("xval == 'xref'", xval.equals(xref));
124:
125: System.setProperty("x2", "secondaryPropReplacement");
126: xval = StringPropertyReplacer.replaceProperties(xref);
127: assertTrue("xval == 'xval'", xval
128: .equals("secondaryPropReplacement"));
129:
130: System.setProperty("x1", "primaryPropReplacement");
131: xval = StringPropertyReplacer.replaceProperties(xref);
132: assertTrue("xval == 'xval'", xval
133: .equals("primaryPropReplacement"));
134: }
135:
136: /**
137: * Test the scenario where a primary and a secondary
138: * non-system property, plus a default value are specified
139: */
140: public void testSecondaryNonSystemPropReplacementWithDefault()
141: throws Exception {
142: getLog().debug(
143: "+++ testSecondaryNonSystemPropReplacementWithDefault");
144:
145: Properties props = new Properties();
146:
147: String xref = "${x1,x2:d}";
148: String xdef = "d";
149: String xval = StringPropertyReplacer.replaceProperties(xref,
150: props);
151: assertTrue("xval == 'xdef'", xval.equals(xdef));
152:
153: props.setProperty("x2", "secondaryPropReplacement");
154: xval = StringPropertyReplacer.replaceProperties(xref, props);
155: assertTrue("xval == 'xval'", xval
156: .equals("secondaryPropReplacement"));
157:
158: props.setProperty("x1", "primaryPropReplacement");
159: xval = StringPropertyReplacer.replaceProperties(xref, props);
160: assertTrue("xval == 'xval'", xval
161: .equals("primaryPropReplacement"));
162: }
163:
164: /**
165: * Test that we first check if the property is set before
166: * trying to apply a default or a resolving secondary property
167: */
168: public void testPathologicalNonSystemPropReplacement()
169: throws Exception {
170: getLog().debug("+++ testPathologicalNonSystemPropReplacement");
171:
172: Properties props = new Properties();
173:
174: String xref = "${x1,x2:d}";
175: props.setProperty("x1,x2:d", "pathologicalPropReplacement");
176: String xval = StringPropertyReplacer.replaceProperties(xref,
177: props);
178: assertTrue("xval == 'xval'", xval
179: .equals("pathologicalPropReplacement"));
180: }
181:
182: /**
183: * Test that a composite property gets resolved
184: * when the secondary property is missing.
185: */
186: public void testPathologicalMissingSecondaryProperty()
187: throws Exception {
188: getLog().debug("+++ testPathologicalMissingSecondaryProperty");
189:
190: Properties props = new Properties();
191:
192: String xref = "${x1,}";
193: String xval = StringPropertyReplacer.replaceProperties(xref,
194: props);
195: assertTrue("xval == 'xref'", xval.equals(xref));
196:
197: props.setProperty("x1", "primaryPropReplacement");
198: xval = StringPropertyReplacer.replaceProperties(xref, props);
199: assertTrue("xval == 'xval'", xval
200: .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 {
209: getLog()
210: .debug(
211: "+++ testPathologicalMissingSecondaryPropertyWithDefault");
212:
213: Properties props = new Properties();
214:
215: String xref = "${x1,:d}";
216: String xdef = "d";
217: String xval = StringPropertyReplacer.replaceProperties(xref,
218: props);
219: assertTrue("xval == 'xdef'", xval.equals(xdef));
220:
221: props.setProperty("x1", "primaryPropReplacement");
222: xval = StringPropertyReplacer.replaceProperties(xref, props);
223: assertTrue("xval == 'xval'", xval
224: .equals("primaryPropReplacement"));
225: }
226:
227: /**
228: * Test that a composite property gets resolved
229: * when the primary property is missing.
230: */
231: public void testPathologicalMissingPrimaryProperty()
232: throws Exception {
233: getLog().debug("+++ testPathologicalMissingPrimaryProperty");
234:
235: Properties props = new Properties();
236:
237: String xref = "${,x2}";
238: String xval = StringPropertyReplacer.replaceProperties(xref,
239: props);
240: assertTrue("xval == 'xref'", xval.equals(xref));
241:
242: props.setProperty("x2", "secondaryPropReplacement");
243: xval = StringPropertyReplacer.replaceProperties(xref, props);
244: assertTrue("xval == 'xval'", xval
245: .equals("secondaryPropReplacement"));
246: }
247:
248: /**
249: * Test that a composite property with a default gets resolved
250: * when the primary property is missing.
251: */
252: public void testPathologicalMissingPrimaryPropertyWithDefault()
253: throws Exception {
254: getLog()
255: .debug(
256: "+++ testPathologicalMissingPrimaryPropertyWithDefault");
257:
258: Properties props = new Properties();
259:
260: String xref = "${,x2:d}";
261: String xdef = "d";
262: String xval = StringPropertyReplacer.replaceProperties(xref,
263: props);
264: assertTrue("xval == 'xdef'", xval.equals(xdef));
265:
266: props.setProperty("x2", "secondaryPropReplacement");
267: xval = StringPropertyReplacer.replaceProperties(xref, props);
268: assertTrue("xval == 'xval'", xval
269: .equals("secondaryPropReplacement"));
270: }
271:
272: /**
273: * Test that with an empty default value we get the
274: * property evaluating to an empty string "", when
275: * the property is undefined
276: */
277: public void testEmptyDefaultNonSystemPropReplacement()
278: throws Exception {
279: getLog().debug("+++ testEmptyDefaultNonSystemPropReplacement");
280:
281: Properties props = new Properties();
282:
283: String xref = "${x1:}";
284: String xval = StringPropertyReplacer.replaceProperties(xref,
285: props);
286: assertTrue("xval == ''", xval.equals(""));
287:
288: props.setProperty("x1", "primaryPropReplacement");
289: xval = StringPropertyReplacer.replaceProperties(xref, props);
290: assertTrue("xval == 'xval'", xval
291: .equals("primaryPropReplacement"));
292: }
293:
294: /** Test that ${/} and ${:} refs are replaced with
295: *
296: * @throws Exception
297: */
298: public void testFilePropReplacement() throws Exception {
299: getLog().debug("+++ testFilePropReplacement");
300: String pathSeparatorRef = "${:}";
301: String pathSeparator = StringPropertyReplacer
302: .replaceProperties(pathSeparatorRef);
303: String separatorRef = "${/}";
304: String separator = StringPropertyReplacer
305: .replaceProperties(separatorRef);
306: getLog().debug(
307: "File.pathSeparator='" + File.pathSeparator + "'");
308: getLog().debug("File.separator='" + File.separator + "'");
309: assertTrue("${:}(" + pathSeparator + ") == File.pathSeparator",
310: pathSeparator.equals(File.pathSeparator));
311: assertTrue("${/}(" + separator + ") == File.separator",
312: separator.equals(File.separator));
313: }
314:
315: }
|