001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.configuration;
018:
019: import java.math.BigDecimal;
020: import java.util.List;
021: import java.util.Iterator;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * Test class for PropertyConverter.
027: *
028: * @author Emmanuel Bourg
029: * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
030: */
031: public class TestPropertyConverter extends TestCase {
032: public void testSplit() {
033: String s = "abc, xyz , 123";
034: List list = PropertyConverter.split(s, ',');
035:
036: assertEquals("size", 3, list.size());
037: assertEquals("1st token for '" + s + "'", "abc", list.get(0));
038: assertEquals("2nd token for '" + s + "'", "xyz", list.get(1));
039: assertEquals("3rd token for '" + s + "'", "123", list.get(2));
040: }
041:
042: public void testSplitWithEscapedSeparator() {
043: String s = "abc\\,xyz, 123";
044: List list = PropertyConverter.split(s, ',');
045:
046: assertEquals("size", 2, list.size());
047: assertEquals("1st token for '" + s + "'", "abc,xyz", list
048: .get(0));
049: assertEquals("2nd token for '" + s + "'", "123", list.get(1));
050: }
051:
052: public void testSplitEmptyValues() {
053: String s = ",,";
054: List list = PropertyConverter.split(s, ',');
055:
056: assertEquals("size", 3, list.size());
057: assertEquals("1st token for '" + s + "'", "", list.get(0));
058: assertEquals("2nd token for '" + s + "'", "", list.get(1));
059: assertEquals("3rd token for '" + s + "'", "", list.get(2));
060: }
061:
062: public void testSplitWithEndingSlash() {
063: String s = "abc, xyz\\";
064: List list = PropertyConverter.split(s, ',');
065:
066: assertEquals("size", 2, list.size());
067: assertEquals("1st token for '" + s + "'", "abc", list.get(0));
068: assertEquals("2nd token for '" + s + "'", "xyz\\", list.get(1));
069: }
070:
071: public void testSplitNull() {
072: List list = PropertyConverter.split(null, ',');
073: assertNotNull(list);
074: assertTrue(list.isEmpty());
075: }
076:
077: public void testToIterator() {
078: int[] array = new int[] { 1, 2, 3 };
079:
080: Iterator it = PropertyConverter.toIterator(array, ',');
081:
082: assertEquals("1st element", new Integer(1), it.next());
083: assertEquals("2nd element", new Integer(2), it.next());
084: assertEquals("3rd element", new Integer(3), it.next());
085: }
086:
087: /**
088: * Tests the interpolation features.
089: */
090: public void testInterpolateString() {
091: PropertiesConfiguration config = new PropertiesConfiguration();
092: config.addProperty("animal", "quick brown fox");
093: config.addProperty("target", "lazy dog");
094: assertEquals("Wrong interpolation",
095: "The quick brown fox jumps over the lazy dog.",
096: PropertyConverter.interpolate(
097: "The ${animal} jumps over the ${target}.",
098: config));
099: }
100:
101: /**
102: * Tests interpolation of an object. Here nothing should be substituted.
103: */
104: public void testInterpolateObject() {
105: assertEquals("Object was not correctly interpolated",
106: new Integer(42), PropertyConverter.interpolate(
107: new Integer(42), new PropertiesConfiguration()));
108: }
109:
110: /**
111: * Tests complex interpolation where the variables' values contain in turn
112: * other variables.
113: */
114: public void testInterpolateRecursive() {
115: PropertiesConfiguration config = new PropertiesConfiguration();
116: config.addProperty("animal", "${animal_attr} fox");
117: config.addProperty("target", "${target_attr} dog");
118: config.addProperty("animal_attr", "quick brown");
119: config.addProperty("target_attr", "lazy");
120: assertEquals("Wrong complex interpolation",
121: "The quick brown fox jumps over the lazy dog.",
122: PropertyConverter.interpolate(
123: "The ${animal} jumps over the ${target}.",
124: config));
125: }
126:
127: /**
128: * Tests an interpolation that leads to a cycle. This should throw an
129: * exception.
130: */
131: public void testCyclicInterpolation() {
132: PropertiesConfiguration config = new PropertiesConfiguration();
133: config.addProperty("animal", "${animal_attr} ${species}");
134: config.addProperty("animal_attr", "quick brown");
135: config.addProperty("species", "${animal}");
136: try {
137: PropertyConverter
138: .interpolate("This is a ${animal}", config);
139: fail("Cyclic interpolation was not detected!");
140: } catch (IllegalStateException iex) {
141: // ok
142: }
143: }
144:
145: /**
146: * Tests interpolation if a variable is unknown. Then the variable won't be
147: * substituted.
148: */
149: public void testInterpolationUnknownVariable() {
150: PropertiesConfiguration config = new PropertiesConfiguration();
151: config.addProperty("animal", "quick brown fox");
152: assertEquals("Wrong interpolation",
153: "The quick brown fox jumps over ${target}.",
154: PropertyConverter.interpolate(
155: "The ${animal} jumps over ${target}.", config));
156: }
157:
158: /**
159: * Tests conversion to numbers when the passed in objects are already
160: * numbers.
161: */
162: public void testToNumberDirect() {
163: Integer i = new Integer(42);
164: assertSame("Wrong integer", i, PropertyConverter.toNumber(i,
165: Integer.class));
166: BigDecimal d = new BigDecimal("3.1415");
167: assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d,
168: Integer.class));
169: }
170:
171: /**
172: * Tests conversion to numbers when the passed in objects have a compatible
173: * string representation.
174: */
175: public void testToNumberFromString() {
176: assertEquals("Incorrect Integer value", new Integer(42),
177: PropertyConverter.toNumber("42", Integer.class));
178: assertEquals("Incorrect Short value", new Short((short) 10),
179: PropertyConverter.toNumber(new StringBuffer("10"),
180: Short.class));
181: }
182:
183: /**
184: * Tests conversion to numbers when the passed in objects are strings with
185: * prefixes for special radices.
186: */
187: public void testToNumberFromHexString() {
188: Number n = PropertyConverter.toNumber("0x10", Integer.class);
189: assertEquals("Incorrect Integer value", 16, n.intValue());
190: }
191:
192: /**
193: * Tests conversion to numbers when an invalid Hex value is passed in. This
194: * should cause an exception.
195: */
196: public void testToNumberFromInvalidHexString() {
197: try {
198: PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
199: fail("Could convert invalid hex value!");
200: } catch (ConversionException cex) {
201: // ok
202: }
203: }
204:
205: /**
206: * Tests conversion to numbers when the passed in objects have no numeric
207: * String representation. This should cause an exception.
208: */
209: public void testToNumberFromInvalidString() {
210: try {
211: PropertyConverter.toNumber("Not a number", Byte.class);
212: fail("Could convert invalid String!");
213: } catch (ConversionException cex) {
214: // ok
215: }
216: }
217:
218: /**
219: * Tests conversion to numbers when the passed in target class is invalid.
220: * This should cause an exception.
221: */
222: public void testToNumberWithInvalidClass() {
223: try {
224: PropertyConverter.toNumber("42", Object.class);
225: fail("Could convert to invalid target class!");
226: } catch (ConversionException cex) {
227: //ok
228: }
229: }
230: }
|