0001: package org.apache.turbine.util.parser;
0002:
0003: /*
0004: * Licensed to the Apache Software Foundation (ASF) under one
0005: * or more contributor license agreements. See the NOTICE file
0006: * distributed with this work for additional information
0007: * regarding copyright ownership. The ASF licenses this file
0008: * to you under the Apache License, Version 2.0 (the
0009: * "License"); you may not use this file except in compliance
0010: * with the License. You may obtain a copy of the License at
0011: *
0012: * http://www.apache.org/licenses/LICENSE-2.0
0013: *
0014: * Unless required by applicable law or agreed to in writing,
0015: * software distributed under the License is distributed on an
0016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0017: * KIND, either express or implied. See the License for the
0018: * specific language governing permissions and limitations
0019: * under the License.
0020: */
0021:
0022: import java.math.BigDecimal;
0023:
0024: import junit.framework.TestSuite;
0025:
0026: import org.apache.turbine.TurbineConstants;
0027: import org.apache.turbine.test.BaseTurbineTest;
0028: import org.apache.turbine.util.DateSelector;
0029: import org.apache.turbine.util.TimeSelector;
0030:
0031: /**
0032: * test whether the Default parameter parser returns its uploaded file items
0033: * in the keySet().
0034: *
0035: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
0036: * @version $Id: BaseValueParserTest.java 534527 2007-05-02 16:10:59Z tv $
0037: */
0038:
0039: public class BaseValueParserTest extends BaseTurbineTest {
0040: public BaseValueParserTest(String name) throws Exception {
0041: super (name, "conf/test/TurbineResources.properties");
0042: }
0043:
0044: public static TestSuite suite() {
0045: return new TestSuite(BaseValueParserTest.class);
0046: }
0047:
0048: public void testSetup() {
0049: BaseValueParser vp = new BaseValueParser();
0050: assertFalse(vp.isDisposed());
0051:
0052: assertEquals("Wrong Character Encoding",
0053: TurbineConstants.PARAMETER_ENCODING_DEFAULT, vp
0054: .getCharacterEncoding());
0055: }
0056:
0057: public void testSetupWithEncoding() {
0058: String encoding = "ISO-8859-2";
0059:
0060: BaseValueParser vp = new BaseValueParser(encoding);
0061: assertFalse(vp.isDisposed());
0062:
0063: assertEquals("Wrong Character Encoding", encoding, vp
0064: .getCharacterEncoding());
0065: }
0066:
0067: public void testChangeEncoding() {
0068: ValueParser vp = new BaseValueParser();
0069:
0070: assertEquals("Wrong Character Encoding",
0071: TurbineConstants.PARAMETER_ENCODING_DEFAULT, vp
0072: .getCharacterEncoding());
0073:
0074: String encoding = "ISO-8859-2";
0075: vp.setCharacterEncoding(encoding);
0076:
0077: assertEquals("Wrong Character Encoding", encoding, vp
0078: .getCharacterEncoding());
0079: }
0080:
0081: public void testClear() {
0082: ValueParser vp = new BaseValueParser();
0083:
0084: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0085:
0086: vp.add("foo", "bar");
0087:
0088: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0089:
0090: vp.clear();
0091:
0092: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0093: }
0094:
0095: public void testDispose() {
0096: BaseValueParser vp = new BaseValueParser();
0097:
0098: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0099:
0100: vp.add("foo", "bar");
0101:
0102: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0103:
0104: vp.dispose();
0105:
0106: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0107: assertTrue(vp.isDisposed());
0108: }
0109:
0110: public void testKeyArray() {
0111: ValueParser vp = new BaseValueParser();
0112:
0113: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0114:
0115: vp.add("foo", "bar");
0116:
0117: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0118:
0119: vp.add("bar", "foo");
0120:
0121: assertEquals("Wrong number of keys", 2, vp.keySet().size());
0122:
0123: vp.add("bar", "baz");
0124:
0125: assertEquals("Wrong number of keys", 2, vp.keySet().size());
0126: }
0127:
0128: public void testDoubleAdd() {
0129: ValueParser vp = new BaseValueParser();
0130:
0131: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0132:
0133: double testValue = 2.0;
0134:
0135: vp.add("foo", testValue);
0136:
0137: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0138:
0139: assertEquals("Wrong string value", "2.0", vp.getString("foo"));
0140: assertEquals("Wrong double value", (double) testValue, vp
0141: .getDouble("foo"), 0.001);
0142: assertEquals("Wrong Double value", (double) testValue, vp
0143: .getDoubleObject("foo").doubleValue(), 0.001);
0144:
0145: double[] doubles = vp.getDoubles("foo");
0146: assertEquals("Wrong Array Size", 1, doubles.length);
0147:
0148: assertEquals("Wrong double array value", testValue, doubles[0],
0149: 0.001);
0150:
0151: Double[] doubleObjs = vp.getDoubleObjects("foo");
0152: assertEquals("Wrong Array Size", 1, doubleObjs.length);
0153:
0154: assertEquals("Wrong Double array value", testValue,
0155: doubleObjs[0].doubleValue(), 0.001);
0156: }
0157:
0158: public void testIntAdd() {
0159: ValueParser vp = new BaseValueParser();
0160:
0161: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0162:
0163: int testValue = 123;
0164:
0165: vp.add("foo", testValue);
0166:
0167: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0168:
0169: assertEquals("Wrong string value", "123", vp.getString("foo"));
0170: assertEquals("Wrong int value", (int) testValue, vp
0171: .getInt("foo"));
0172: assertEquals("Wrong Int value", (int) testValue, vp
0173: .getIntObject("foo").intValue());
0174:
0175: int[] ints = vp.getInts("foo");
0176: assertEquals("Wrong Array Size", 1, ints.length);
0177:
0178: assertEquals("Wrong int array value", testValue, ints[0]);
0179:
0180: Integer[] intObjs = vp.getIntObjects("foo");
0181: assertEquals("Wrong Array Size", 1, intObjs.length);
0182:
0183: assertEquals("Wrong Int array value", testValue, intObjs[0]
0184: .intValue());
0185: }
0186:
0187: public void testIntegerAdd() {
0188: ValueParser vp = new BaseValueParser();
0189:
0190: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0191:
0192: Integer testValue = new Integer(123);
0193:
0194: vp.add("foo", testValue);
0195:
0196: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0197:
0198: assertEquals("Wrong string value", "123", vp.getString("foo"));
0199: assertEquals("Wrong int value", (int) testValue.intValue(), vp
0200: .getInt("foo"));
0201: assertEquals("Wrong Int value", (int) testValue.intValue(), vp
0202: .getIntObject("foo").intValue());
0203:
0204: int[] ints = vp.getInts("foo");
0205: assertEquals("Wrong Array Size", 1, ints.length);
0206:
0207: assertEquals("Wrong int array value", testValue.intValue(),
0208: ints[0]);
0209:
0210: Integer[] intObjs = vp.getIntObjects("foo");
0211: assertEquals("Wrong Array Size", 1, intObjs.length);
0212:
0213: assertEquals("Wrong Int array value", testValue.intValue(),
0214: intObjs[0].intValue());
0215: }
0216:
0217: public void testLongAdd() {
0218: ValueParser vp = new BaseValueParser();
0219:
0220: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0221:
0222: long testValue = 9223372036854775807l;
0223:
0224: vp.add("foo", testValue);
0225:
0226: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0227:
0228: assertEquals("Wrong string value", "9223372036854775807", vp
0229: .getString("foo"));
0230: assertEquals("Wrong long value", (long) testValue, vp
0231: .getLong("foo"));
0232: assertEquals("Wrong Long value", (long) testValue, vp
0233: .getLongObject("foo").longValue());
0234:
0235: long[] longs = vp.getLongs("foo");
0236: assertEquals("Wrong Array Size", 1, longs.length);
0237:
0238: assertEquals("Wrong long array value", testValue, longs[0]);
0239:
0240: Long[] longObjs = vp.getLongObjects("foo");
0241: assertEquals("Wrong Array Size", 1, longObjs.length);
0242:
0243: assertEquals("Wrong Long array value", testValue, longObjs[0]
0244: .longValue());
0245: }
0246:
0247: public void testLongToInt() {
0248: ValueParser vp = new BaseValueParser();
0249:
0250: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0251:
0252: long testValue = 1234l;
0253:
0254: vp.add("foo", testValue);
0255:
0256: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0257:
0258: assertEquals("Wrong string value", "1234", vp.getString("foo"));
0259: assertEquals("Wrong int value", (int) testValue, vp
0260: .getInt("foo"));
0261: assertEquals("Wrong Int value", (int) testValue, vp
0262: .getIntObject("foo").intValue());
0263:
0264: int[] ints = vp.getInts("foo");
0265: assertEquals("Wrong Array Size", 1, ints.length);
0266:
0267: assertEquals("Wrong int array value", testValue, ints[0]);
0268:
0269: Integer[] intObjs = vp.getIntObjects("foo");
0270: assertEquals("Wrong Array Size", 1, intObjs.length);
0271:
0272: assertEquals("Wrong Int array value", testValue, intObjs[0]
0273: .intValue());
0274: }
0275:
0276: public void testIntToLong() {
0277: ValueParser vp = new BaseValueParser();
0278:
0279: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0280:
0281: int testValue = 123;
0282:
0283: vp.add("foo", testValue);
0284:
0285: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0286:
0287: assertEquals("Wrong string value", "123", vp.getString("foo"));
0288: assertEquals("Wrong long value", (long) testValue, vp
0289: .getLong("foo"));
0290: assertEquals("Wrong Long value", (long) testValue, vp
0291: .getLongObject("foo").longValue());
0292:
0293: long[] longs = vp.getLongs("foo");
0294: assertEquals("Wrong Array Size", 1, longs.length);
0295:
0296: assertEquals("Wrong long array value", testValue, longs[0]);
0297:
0298: Long[] longObjs = vp.getLongObjects("foo");
0299: assertEquals("Wrong Array Size", 1, longObjs.length);
0300:
0301: assertEquals("Wrong Long array value", testValue, longObjs[0]
0302: .longValue());
0303: }
0304:
0305: public void testIntToDouble() {
0306: ValueParser vp = new BaseValueParser();
0307:
0308: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0309:
0310: int testValue = 123;
0311:
0312: vp.add("foo", testValue);
0313:
0314: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0315:
0316: assertEquals("Wrong string value", "123", vp.getString("foo"));
0317: assertEquals("Wrong double value", (double) testValue, vp
0318: .getDouble("foo"), 0.001);
0319: assertEquals("Wrong Double value", (double) testValue, vp
0320: .getDoubleObject("foo").doubleValue(), 0.001);
0321:
0322: double[] doubles = vp.getDoubles("foo");
0323: assertEquals("Wrong Array Size", 1, doubles.length);
0324:
0325: assertEquals("Wrong double array value", testValue, doubles[0],
0326: 0.001);
0327:
0328: Double[] doubleObjs = vp.getDoubleObjects("foo");
0329: assertEquals("Wrong Array Size", 1, doubleObjs.length);
0330:
0331: assertEquals("Wrong Double array value", testValue,
0332: doubleObjs[0].doubleValue(), 0.001);
0333: }
0334:
0335: public void testLongToDouble() {
0336: ValueParser vp = new BaseValueParser();
0337:
0338: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0339:
0340: long testValue = 9223372036854775807l;
0341:
0342: vp.add("foo", testValue);
0343:
0344: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0345:
0346: assertEquals("Wrong string value", "9223372036854775807", vp
0347: .getString("foo"));
0348: assertEquals("Wrong double value", (double) testValue, vp
0349: .getDouble("foo"), 0.001);
0350: assertEquals("Wrong Double value", (double) testValue, vp
0351: .getDoubleObject("foo").doubleValue(), 0.001);
0352:
0353: double[] doubles = vp.getDoubles("foo");
0354: assertEquals("Wrong Array Size", 1, doubles.length);
0355:
0356: assertEquals("Wrong double array value", testValue, doubles[0],
0357: 0.001);
0358:
0359: Double[] doubleObjs = vp.getDoubleObjects("foo");
0360: assertEquals("Wrong Array Size", 1, doubleObjs.length);
0361:
0362: assertEquals("Wrong Double array value", testValue,
0363: doubleObjs[0].doubleValue(), 0.001);
0364: }
0365:
0366: public void testStringAdd() {
0367: ValueParser vp = new BaseValueParser();
0368:
0369: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0370:
0371: String testValue = "the quick brown fox";
0372:
0373: vp.add("foo", testValue);
0374:
0375: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0376:
0377: assertEquals("Wrong string value", testValue, vp
0378: .getString("foo"));
0379:
0380: String[] Strings = vp.getStrings("foo");
0381: assertEquals("Wrong Array Size", 1, Strings.length);
0382:
0383: assertEquals("Wrong String array value", testValue, Strings[0]);
0384: }
0385:
0386: public void testStringToInt() {
0387: ValueParser vp = new BaseValueParser();
0388:
0389: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0390:
0391: String testValue = "123456";
0392:
0393: vp.add("foo", testValue);
0394:
0395: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0396:
0397: assertEquals("Wrong string value", testValue, vp
0398: .getString("foo"));
0399:
0400: assertEquals("Wrong int value", Integer.parseInt(testValue), vp
0401: .getInt("foo"));
0402: assertEquals("Wrong Int value", Integer.valueOf(testValue)
0403: .intValue(), vp.getIntObject("foo").intValue());
0404:
0405: int[] ints = vp.getInts("foo");
0406: assertEquals("Wrong Array Size", 1, ints.length);
0407:
0408: assertEquals("Wrong int array value", Integer
0409: .parseInt(testValue), ints[0]);
0410:
0411: Integer[] intObjs = vp.getIntObjects("foo");
0412: assertEquals("Wrong Array Size", 1, intObjs.length);
0413:
0414: assertEquals("Wrong Int array value", Integer
0415: .valueOf(testValue).intValue(), intObjs[0].intValue());
0416: }
0417:
0418: public void testStringToLong() {
0419: ValueParser vp = new BaseValueParser();
0420:
0421: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0422:
0423: String testValue = "123456";
0424:
0425: vp.add("foo", testValue);
0426:
0427: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0428:
0429: assertEquals("Wrong string value", testValue, vp
0430: .getString("foo"));
0431:
0432: assertEquals("Wrong long value", Long.parseLong(testValue), vp
0433: .getLong("foo"));
0434: assertEquals("Wrong Long value", Long.valueOf(testValue)
0435: .longValue(), vp.getLongObject("foo").longValue());
0436:
0437: long[] longs = vp.getLongs("foo");
0438: assertEquals("Wrong Array Size", 1, longs.length);
0439:
0440: assertEquals("Wrong long array value", Long
0441: .parseLong(testValue), longs[0]);
0442:
0443: Long[] longObjs = vp.getLongObjects("foo");
0444: assertEquals("Wrong Array Size", 1, longObjs.length);
0445:
0446: assertEquals("Wrong Long array value", Long.valueOf(testValue)
0447: .longValue(), longObjs[0].longValue());
0448: }
0449:
0450: public void testStringArray() {
0451: ValueParser vp = new BaseValueParser();
0452:
0453: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0454:
0455: String[] testValue = new String[] { "foo", "bar", "baz" };
0456:
0457: vp.add("foo", testValue);
0458:
0459: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0460:
0461: String[] res = vp.getStrings("foo");
0462:
0463: assertEquals("Wrong number of elements", 3, res.length);
0464:
0465: for (int i = 0; i < res.length; i++) {
0466: assertEquals("Wrong value", res[i], testValue[i]);
0467: }
0468:
0469: assertEquals("Wrong element returned", testValue[0], vp
0470: .getString("foo"));
0471:
0472: vp.add("foo", "xxx");
0473:
0474: res = vp.getStrings("foo");
0475:
0476: assertEquals("Wrong number of elements", 4, res.length);
0477:
0478: for (int i = 0; i < 3; i++) {
0479: assertEquals("Wrong value", res[i], testValue[i]);
0480: }
0481:
0482: assertEquals(res[3], "xxx");
0483:
0484: // should append at the end.
0485: assertEquals("Wrong element returned", testValue[0], vp
0486: .getString("foo"));
0487: }
0488:
0489: public void testRemove() {
0490: ValueParser vp = new BaseValueParser();
0491:
0492: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0493:
0494: String testValue = "the quick brown fox";
0495:
0496: vp.add("foo", testValue);
0497:
0498: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0499:
0500: assertEquals("Wrong string value", testValue, vp
0501: .getString("foo"));
0502:
0503: assertNotNull(vp.remove("foo"));
0504:
0505: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0506:
0507: assertNull(vp.getString("foo"));
0508:
0509: // Test non-existing key
0510: assertNull(vp.remove("baz"));
0511:
0512: // Test removing null value
0513: assertNull(vp.remove(null));
0514: }
0515:
0516: public void testRemoveArray() {
0517: ValueParser vp = new BaseValueParser();
0518:
0519: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0520:
0521: String testValue = "the quick brown fox";
0522:
0523: vp.add("foo", testValue);
0524:
0525: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0526:
0527: vp.add("foo", testValue);
0528:
0529: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0530:
0531: assertEquals("Wrong string value", testValue, vp
0532: .getString("foo"));
0533:
0534: String[] res = vp.getStrings("foo");
0535:
0536: assertEquals("Wrong number of elements", 2, res.length);
0537:
0538: for (int i = 0; i < res.length; i++) {
0539: assertEquals("Wrong value", res[i], testValue);
0540: }
0541:
0542: vp.remove("foo");
0543:
0544: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0545:
0546: assertNull(vp.getString("foo"));
0547: }
0548:
0549: public void testContainsKey() {
0550: ValueParser vp = new BaseValueParser();
0551:
0552: vp.add("foo", "bar");
0553: vp.add("bar", new String[] { "foo", "bar" });
0554:
0555: assertTrue(vp.containsKey("foo"));
0556: assertTrue(vp.containsKey("bar"));
0557: assertFalse(vp.containsKey("baz"));
0558: }
0559:
0560: public void testDateSelector() {
0561: BaseValueParser vp = new BaseValueParser();
0562:
0563: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0564: assertFalse(vp.containsDateSelectorKeys("foo"));
0565:
0566: vp.add("foo" + DateSelector.DAY_SUFFIX, "1");
0567:
0568: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0569: assertFalse(vp.containsDateSelectorKeys("foo"));
0570:
0571: vp.add("foo" + DateSelector.MONTH_SUFFIX, "1");
0572:
0573: assertEquals("Wrong number of keys", 2, vp.keySet().size());
0574: assertFalse(vp.containsDateSelectorKeys("foo"));
0575:
0576: vp.add("foo" + DateSelector.YEAR_SUFFIX, "2005");
0577:
0578: assertEquals("Wrong number of keys", 3, vp.keySet().size());
0579: assertTrue(vp.containsDateSelectorKeys("foo"));
0580: }
0581:
0582: public void testTimeSelector() {
0583: BaseValueParser vp = new BaseValueParser();
0584:
0585: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0586: assertFalse(vp.containsTimeSelectorKeys("foo"));
0587:
0588: vp.add("foo" + TimeSelector.HOUR_SUFFIX, "22");
0589:
0590: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0591: assertFalse(vp.containsTimeSelectorKeys("foo"));
0592:
0593: vp.add("foo" + TimeSelector.MINUTE_SUFFIX, "58");
0594:
0595: assertEquals("Wrong number of keys", 2, vp.keySet().size());
0596: assertFalse(vp.containsTimeSelectorKeys("foo"));
0597:
0598: vp.add("foo" + TimeSelector.SECOND_SUFFIX, "0");
0599:
0600: assertEquals("Wrong number of keys", 3, vp.keySet().size());
0601: assertTrue(vp.containsTimeSelectorKeys("foo"));
0602: }
0603:
0604: public void testBooleanObject() {
0605: ValueParser vp = new BaseValueParser();
0606:
0607: vp.add("t1", "true");
0608: vp.add("t2", "yes");
0609: vp.add("t3", "on");
0610: vp.add("t4", "1");
0611: vp.add("t5", 1);
0612:
0613: vp.add("f1", "false");
0614: vp.add("f2", "no");
0615: vp.add("f3", "off");
0616: vp.add("f4", "0");
0617: vp.add("f5", 0);
0618:
0619: vp.add("e1", "nix");
0620: vp.add("e2", "weg");
0621: vp.add("e3", 200);
0622: vp.add("e4", -2.5);
0623:
0624: assertEquals("Value is not true", Boolean.TRUE, vp
0625: .getBooleanObject("t1"));
0626: assertEquals("Value is not true", Boolean.TRUE, vp
0627: .getBooleanObject("t2"));
0628: assertEquals("Value is not true", Boolean.TRUE, vp
0629: .getBooleanObject("t3"));
0630: assertEquals("Value is not true", Boolean.TRUE, vp
0631: .getBooleanObject("t4"));
0632: assertEquals("Value is not true", Boolean.TRUE, vp
0633: .getBooleanObject("t5"));
0634:
0635: assertEquals("Value is not false", Boolean.FALSE, vp
0636: .getBooleanObject("f1"));
0637: assertEquals("Value is not false", Boolean.FALSE, vp
0638: .getBooleanObject("f2"));
0639: assertEquals("Value is not false", Boolean.FALSE, vp
0640: .getBooleanObject("f3"));
0641: assertEquals("Value is not false", Boolean.FALSE, vp
0642: .getBooleanObject("f4"));
0643: assertEquals("Value is not false", Boolean.FALSE, vp
0644: .getBooleanObject("f5"));
0645:
0646: assertNull(vp.getBooleanObject("e1"));
0647: assertNull(vp.getBooleanObject("e2"));
0648: assertNull(vp.getBooleanObject("e3"));
0649: assertNull(vp.getBooleanObject("e4"));
0650:
0651: assertNull(vp.getBooleanObject("does-not-exist"));
0652: }
0653:
0654: public void testBoolDefault() {
0655: ValueParser vp = new BaseValueParser();
0656:
0657: vp.add("t1", "true");
0658: vp.add("f1", "false");
0659:
0660: assertTrue(vp.getBoolean("t1"));
0661: assertFalse(vp.getBoolean("f1"));
0662:
0663: assertFalse(vp.getBoolean("does not exist"));
0664:
0665: assertTrue(vp.getBoolean("t1", false));
0666: assertFalse(vp.getBoolean("f1", true));
0667:
0668: assertFalse(vp.getBoolean("does not exist", false));
0669: assertTrue(vp.getBoolean("does not exist", true));
0670: }
0671:
0672: public void testBooleanDefault() {
0673: ValueParser vp = new BaseValueParser();
0674:
0675: vp.add("t1", "true");
0676: vp.add("f1", "false");
0677:
0678: assertEquals("Value is not true", Boolean.TRUE, vp
0679: .getBooleanObject("t1"));
0680: assertEquals("Value is not false", Boolean.FALSE, vp
0681: .getBooleanObject("f1"));
0682:
0683: assertNull(vp.getBooleanObject("does not exist"));
0684:
0685: assertEquals("Value is not true", Boolean.TRUE, vp
0686: .getBooleanObject("t1", Boolean.FALSE));
0687: assertEquals("Value is not true", Boolean.TRUE, vp
0688: .getBooleanObject("t1", null));
0689: assertEquals("Value is not false", Boolean.FALSE, vp
0690: .getBooleanObject("f1", Boolean.TRUE));
0691: assertEquals("Value is not false", Boolean.FALSE, vp
0692: .getBooleanObject("f1", null));
0693:
0694: assertNull(vp.getBooleanObject("does not exist", null));
0695: }
0696:
0697: public void testDoubleArray() {
0698: ValueParser vp = new BaseValueParser();
0699:
0700: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0701:
0702: double[] testValue = { 1.0, 2.0, 3.0 };
0703:
0704: for (int i = 0; i < testValue.length; i++) {
0705: vp.add("foo", testValue[i]);
0706:
0707: String[] res = vp.getStrings("foo");
0708: assertEquals("Wrong number of elements", res.length, i + 1);
0709: }
0710:
0711: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0712:
0713: double[] res = vp.getDoubles("foo");
0714:
0715: assertEquals("Wrong number of elements", 3, res.length);
0716:
0717: for (int i = 0; i < res.length; i++) {
0718: assertEquals("Wrong value", res[i], testValue[i], 0.001);
0719: }
0720:
0721: Double[] resObj = vp.getDoubleObjects("foo");
0722:
0723: assertEquals("Wrong number of elements", 3, resObj.length);
0724:
0725: for (int i = 0; i < resObj.length; i++) {
0726: assertEquals("Wrong value", resObj[i].doubleValue(),
0727: testValue[i], 0.001);
0728: }
0729:
0730: assertEquals("Wrong element returned", testValue[0], vp
0731: .getDoubleObject("foo").doubleValue(), 0.001);
0732:
0733: vp.add("foo", 4.0);
0734:
0735: res = vp.getDoubles("foo");
0736:
0737: assertEquals("Wrong number of elements", 4, res.length);
0738:
0739: for (int i = 0; i < 3; i++) {
0740: assertEquals("Wrong value", res[i], testValue[i], 0.001);
0741: }
0742:
0743: assertEquals(res[3], 4.0, 0.001);
0744:
0745: resObj = vp.getDoubleObjects("foo");
0746:
0747: assertEquals("Wrong number of elements", 4, resObj.length);
0748:
0749: for (int i = 0; i < 3; i++) {
0750: assertEquals("Wrong value", resObj[i].doubleValue(),
0751: testValue[i], 0.001);
0752: }
0753:
0754: assertEquals(resObj[3].doubleValue(), 4.0, 0.001);
0755:
0756: // should append at the end.
0757: assertEquals("Wrong element returned", testValue[0], vp
0758: .getDouble("foo"), 0.001);
0759: }
0760:
0761: public void testFloatArray() {
0762: ValueParser vp = new BaseValueParser();
0763:
0764: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0765:
0766: float[] testValue = { 1.0f, 2.0f, 3.0f };
0767:
0768: for (int i = 0; i < testValue.length; i++) {
0769: vp.add("foo", testValue[i]);
0770:
0771: String[] res = vp.getStrings("foo");
0772: assertEquals("Wrong number of elements", res.length, i + 1);
0773: }
0774:
0775: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0776:
0777: float[] res = vp.getFloats("foo");
0778:
0779: assertEquals("Wrong number of elements", 3, res.length);
0780:
0781: for (int i = 0; i < res.length; i++) {
0782: assertEquals("Wrong value", res[i], testValue[i], 0.001f);
0783: }
0784:
0785: Float[] resObj = vp.getFloatObjects("foo");
0786:
0787: assertEquals("Wrong number of elements", 3, resObj.length);
0788:
0789: for (int i = 0; i < resObj.length; i++) {
0790: assertEquals("Wrong value", resObj[i].floatValue(),
0791: testValue[i], 0.001f);
0792: }
0793:
0794: assertEquals("Wrong element returned", testValue[0], vp
0795: .getFloatObject("foo").floatValue(), 0.001f);
0796:
0797: vp.add("foo", 4.0f);
0798:
0799: res = vp.getFloats("foo");
0800:
0801: assertEquals("Wrong number of elements", 4, res.length);
0802:
0803: for (int i = 0; i < 3; i++) {
0804: assertEquals("Wrong value", res[i], testValue[i], 0.001f);
0805: }
0806:
0807: assertEquals(res[3], 4.0f, 0.001f);
0808:
0809: resObj = vp.getFloatObjects("foo");
0810:
0811: assertEquals("Wrong number of elements", 4, resObj.length);
0812:
0813: for (int i = 0; i < 3; i++) {
0814: assertEquals("Wrong value", resObj[i].floatValue(),
0815: testValue[i], 0.001f);
0816: }
0817:
0818: assertEquals(resObj[3].floatValue(), 4.0f, 0.001f);
0819:
0820: // should append at the end.
0821: assertEquals("Wrong element returned", testValue[0], vp
0822: .getFloat("foo"), 0.001f);
0823: }
0824:
0825: public void testBigDecimalArray() {
0826: ValueParser vp = new BaseValueParser();
0827:
0828: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0829:
0830: long[] testValue = { 12345678, 87654321, 1092837465, };
0831:
0832: for (int i = 0; i < testValue.length; i++) {
0833: vp.add("foo", testValue[i]);
0834:
0835: String[] res = vp.getStrings("foo");
0836: assertEquals("Wrong number of elements", res.length, i + 1);
0837: }
0838:
0839: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0840:
0841: BigDecimal[] res = vp.getBigDecimals("foo");
0842:
0843: assertEquals("Wrong number of elements", 3, res.length);
0844:
0845: for (int i = 0; i < res.length; i++) {
0846: assertEquals("Wrong value", res[i].longValue(),
0847: testValue[i]);
0848: }
0849:
0850: assertEquals("Wrong element returned", testValue[0], vp
0851: .getBigDecimal("foo").longValue());
0852:
0853: vp.add("foo", 77777777);
0854:
0855: res = vp.getBigDecimals("foo");
0856:
0857: assertEquals("Wrong number of elements", 4, res.length);
0858:
0859: for (int i = 0; i < 3; i++) {
0860: assertEquals("Wrong value", res[i].longValue(),
0861: testValue[i], 0.001);
0862: }
0863:
0864: assertEquals(res[3].longValue(), 77777777);
0865:
0866: // should append at the end.
0867: assertEquals("Wrong element returned", testValue[0], vp
0868: .getBigDecimal("foo").longValue());
0869: }
0870:
0871: public void testIntegerArray() {
0872: ValueParser vp = new BaseValueParser();
0873:
0874: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0875:
0876: int[] testValue = { 1, 2, 3 };
0877:
0878: for (int i = 0; i < testValue.length; i++) {
0879: vp.add("foo", testValue[i]);
0880:
0881: String[] res = vp.getStrings("foo");
0882: assertEquals("Wrong number of elements", res.length, i + 1);
0883: }
0884:
0885: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0886:
0887: int[] res = vp.getInts("foo");
0888:
0889: assertEquals("Wrong number of elements", 3, res.length);
0890:
0891: for (int i = 0; i < res.length; i++) {
0892: assertEquals("Wrong value", res[i], testValue[i]);
0893: }
0894:
0895: Integer[] resObj = vp.getIntObjects("foo");
0896:
0897: assertEquals("Wrong number of elements", 3, resObj.length);
0898:
0899: for (int i = 0; i < resObj.length; i++) {
0900: assertEquals("Wrong value", resObj[i].intValue(),
0901: testValue[i]);
0902: }
0903:
0904: assertEquals("Wrong element returned", testValue[0], vp
0905: .getIntObject("foo").intValue());
0906:
0907: vp.add("foo", 4);
0908:
0909: res = vp.getInts("foo");
0910:
0911: assertEquals("Wrong number of elements", 4, res.length);
0912:
0913: for (int i = 0; i < 3; i++) {
0914: assertEquals("Wrong value", res[i], testValue[i]);
0915: }
0916:
0917: assertEquals(res[3], 4);
0918:
0919: resObj = vp.getIntObjects("foo");
0920:
0921: assertEquals("Wrong number of elements", 4, resObj.length);
0922:
0923: for (int i = 0; i < 3; i++) {
0924: assertEquals("Wrong value", resObj[i].intValue(),
0925: testValue[i]);
0926: }
0927:
0928: assertEquals(resObj[3].intValue(), 4);
0929:
0930: // should append at the end.
0931: assertEquals("Wrong element returned", testValue[0], vp
0932: .getInt("foo"));
0933: }
0934:
0935: public void testLongArray() {
0936: ValueParser vp = new BaseValueParser();
0937:
0938: assertEquals("Wrong number of keys", 0, vp.keySet().size());
0939:
0940: long[] testValue = { 1l, 2l, 3l };
0941:
0942: for (int i = 0; i < testValue.length; i++) {
0943: vp.add("foo", testValue[i]);
0944:
0945: String[] res = vp.getStrings("foo");
0946: assertEquals("Wrong number of elements", res.length, i + 1);
0947: }
0948:
0949: assertEquals("Wrong number of keys", 1, vp.keySet().size());
0950:
0951: long[] res = vp.getLongs("foo");
0952:
0953: assertEquals("Wrong number of elements", 3, res.length);
0954:
0955: for (int i = 0; i < res.length; i++) {
0956: assertEquals("Wrong value", res[i], testValue[i]);
0957: }
0958:
0959: Long[] resObj = vp.getLongObjects("foo");
0960:
0961: assertEquals("Wrong number of elements", 3, resObj.length);
0962:
0963: for (int i = 0; i < resObj.length; i++) {
0964: assertEquals("Wrong value", resObj[i].longValue(),
0965: testValue[i]);
0966: }
0967:
0968: assertEquals("Wrong element returned", testValue[0], vp
0969: .getLongObject("foo").longValue());
0970:
0971: vp.add("foo", 4);
0972:
0973: res = vp.getLongs("foo");
0974:
0975: assertEquals("Wrong number of elements", 4, res.length);
0976:
0977: for (int i = 0; i < 3; i++) {
0978: assertEquals("Wrong value", res[i], testValue[i]);
0979: }
0980:
0981: assertEquals(res[3], 4);
0982:
0983: resObj = vp.getLongObjects("foo");
0984:
0985: assertEquals("Wrong number of elements", 4, resObj.length);
0986:
0987: for (int i = 0; i < 3; i++) {
0988: assertEquals("Wrong value", resObj[i].longValue(),
0989: testValue[i]);
0990: }
0991:
0992: assertEquals(resObj[3].longValue(), 4);
0993:
0994: // should append at the end.
0995: assertEquals("Wrong element returned", testValue[0], vp
0996: .getLong("foo"));
0997: }
0998:
0999: public void testByteArray() throws Exception {
1000: ValueParser vp = new BaseValueParser();
1001:
1002: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1003:
1004: String testValue = "abcdefg";
1005:
1006: vp.add("foo", testValue);
1007:
1008: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1009:
1010: byte[] res = vp.getBytes("foo");
1011:
1012: assertEquals("Wrong number of elements", 7, res.length);
1013:
1014: for (int i = 0; i < res.length; i++) {
1015: byte[] testByte = testValue.substring(i, i + 1).getBytes(
1016: vp.getCharacterEncoding());
1017: assertEquals("More than one byte for a char!", 1,
1018: testByte.length);
1019: assertEquals("Wrong value", res[i], testByte[0]);
1020: }
1021: }
1022:
1023: public void testByte() {
1024: ValueParser vp = new BaseValueParser();
1025:
1026: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1027:
1028: String[] testValue = { "0", "127", "-1", "0", "-127", "100" };
1029:
1030: for (int i = 0; i < testValue.length; i++) {
1031: vp.add("foo" + i, testValue[i]);
1032: }
1033:
1034: assertEquals("Wrong number of keys", 6, vp.keySet().size());
1035:
1036: assertEquals("Wrong value", (byte) 0, vp.getByte("foo0"));
1037: assertEquals("Wrong value", (byte) 127, vp.getByte("foo1"));
1038: assertEquals("Wrong value", (byte) -1, vp.getByte("foo2"));
1039: assertEquals("Wrong value", (byte) 0, vp.getByte("foo3"));
1040: assertEquals("Wrong value", (byte) -127, vp.getByte("foo4"));
1041: assertEquals("Wrong value", (byte) 100, vp.getByte("foo5"));
1042:
1043: assertEquals("Wrong value", new Byte((byte) 0), vp
1044: .getByteObject("foo0"));
1045: assertEquals("Wrong value", new Byte((byte) 127), vp
1046: .getByteObject("foo1"));
1047: assertEquals("Wrong value", new Byte((byte) -1), vp
1048: .getByteObject("foo2"));
1049: assertEquals("Wrong value", new Byte((byte) 0), vp
1050: .getByteObject("foo3"));
1051: assertEquals("Wrong value", new Byte((byte) -127), vp
1052: .getByteObject("foo4"));
1053: assertEquals("Wrong value", new Byte((byte) 100), vp
1054: .getByteObject("foo5"));
1055:
1056: }
1057:
1058: public void testStringDefault() {
1059: ValueParser vp = new BaseValueParser();
1060:
1061: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1062:
1063: vp.add("foo", "bar");
1064:
1065: assertEquals("Wrong value found", "bar", vp.getString("foo",
1066: "xxx"));
1067: assertEquals("Wrong value found", "bar", vp.getString("foo",
1068: null));
1069:
1070: assertEquals("Wrong value found", "baz", vp.getString(
1071: "does-not-exist", "baz"));
1072: assertNull(vp.getString("does-not-exist", null));
1073: }
1074:
1075: public void testSetString() {
1076: ValueParser vp = new BaseValueParser();
1077:
1078: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1079:
1080: vp.add("foo", "bar");
1081:
1082: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1083:
1084: vp.add("bar", "foo");
1085:
1086: assertEquals("Wrong number of keys", 2, vp.keySet().size());
1087:
1088: vp.add("bar", "baz");
1089:
1090: assertEquals("Wrong number of keys", 2, vp.keySet().size());
1091:
1092: String[] res = vp.getStrings("bar");
1093: assertEquals("Wrong number of values", 2, res.length);
1094: assertEquals("Wrong value found", "foo", res[0]);
1095: assertEquals("Wrong value found", "baz", res[1]);
1096:
1097: vp.setString("bar", "xxx");
1098:
1099: assertEquals("Wrong number of keys", 2, vp.keySet().size());
1100:
1101: res = vp.getStrings("bar");
1102: assertEquals("Wrong number of values", 1, res.length);
1103: assertEquals("Wrong value found", "xxx", res[0]);
1104: }
1105:
1106: public void testSetStrings() {
1107: ValueParser vp = new BaseValueParser();
1108:
1109: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1110:
1111: vp.add("foo", "bar");
1112:
1113: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1114:
1115: vp.add("bar", "foo");
1116:
1117: assertEquals("Wrong number of keys", 2, vp.keySet().size());
1118:
1119: vp.add("bar", "baz");
1120:
1121: assertEquals("Wrong number of keys", 2, vp.keySet().size());
1122:
1123: String[] res = vp.getStrings("bar");
1124: assertEquals("Wrong number of values", 2, res.length);
1125: assertEquals("Wrong value found", "foo", res[0]);
1126: assertEquals("Wrong value found", "baz", res[1]);
1127:
1128: String[] newValues = new String[] { "aaa", "bbb", "ccc", "ddd" };
1129:
1130: vp.setStrings("bar", newValues);
1131:
1132: assertEquals("Wrong number of keys", 2, vp.keySet().size());
1133:
1134: res = vp.getStrings("bar");
1135: assertEquals("Wrong number of values", newValues.length,
1136: res.length);
1137:
1138: for (int i = 0; i < newValues.length; i++) {
1139: assertEquals("Wrong value found", newValues[i], res[i]);
1140: }
1141: }
1142:
1143: public void testSetProperties() throws Exception {
1144: ValueParser vp = new BaseValueParser();
1145:
1146: vp.add("longvalue", 12345l);
1147: vp.add("doublevalue", 2.0);
1148: vp.add("intValue", 200);
1149: vp.add("stringvalue", "foobar");
1150: vp.add("booleanvalue", "true");
1151:
1152: PropertyBean bp = new PropertyBean();
1153: bp.setDoNotTouchValue("abcdef");
1154:
1155: vp.setProperties(bp);
1156:
1157: assertEquals("Wrong value in bean", "abcdef", bp
1158: .getDoNotTouchValue());
1159: assertEquals("Wrong value in bean", "foobar", bp
1160: .getStringValue());
1161: assertEquals("Wrong value in bean", 200, bp.getIntValue());
1162: assertEquals("Wrong value in bean", 2.0, bp.getDoubleValue(),
1163: 0.001);
1164: assertEquals("Wrong value in bean", 12345l, bp.getLongValue());
1165: assertEquals("Wrong value in bean", Boolean.TRUE, bp
1166: .getBooleanValue());
1167: }
1168:
1169: public void testAddNulls() {
1170: ValueParser vp = new BaseValueParser();
1171:
1172: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1173:
1174: vp.add("foo", (Integer) null);
1175:
1176: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1177:
1178: vp.add("foo", (String) null);
1179:
1180: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1181:
1182: vp.add("bar", "null");
1183:
1184: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1185:
1186: }
1187:
1188: public void testAddNullArrays() {
1189: String[] res = null;
1190:
1191: ValueParser vp = new BaseValueParser();
1192: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1193:
1194: vp.add("foo", new String[] { "foo", "bar" });
1195: res = vp.getStrings("foo");
1196: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1197: assertEquals("Wrong number of values", 2, res.length);
1198:
1199: // null value should not change contents
1200: vp.add("foo", (String) null);
1201: res = vp.getStrings("foo");
1202: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1203: assertEquals("Wrong number of values", 2, res.length);
1204:
1205: // null value should not change contents
1206: vp.add("foo", (String[]) null);
1207: res = vp.getStrings("foo");
1208: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1209: assertEquals("Wrong number of values", 2, res.length);
1210:
1211: // empty String array should not change contents
1212: vp.add("foo", new String[0]);
1213: res = vp.getStrings("foo");
1214: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1215: assertEquals("Wrong number of values", 2, res.length);
1216:
1217: // String array with null value should not change contents
1218: vp.add("foo", new String[] { null });
1219: res = vp.getStrings("foo");
1220: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1221: assertEquals("Wrong number of values", 2, res.length);
1222:
1223: // String array with null value should only add non-null values
1224: vp.add("foo", new String[] { "bla", null, "foo" });
1225: res = vp.getStrings("foo");
1226: assertEquals("Wrong number of keys", 1, vp.keySet().size());
1227: assertEquals("Wrong number of values", 4, res.length);
1228: }
1229:
1230: public void testNonExistingResults() {
1231: ValueParser vp = new BaseValueParser();
1232: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1233:
1234: assertEquals("Wrong value for non existing key", 0.0, vp
1235: .getDouble("foo"), 0.001);
1236: assertNull(vp.getDoubles("foo"));
1237: assertNull(vp.getDoubleObject("foo"));
1238: assertNull(vp.getDoubleObjects("foo"));
1239:
1240: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1241:
1242: assertNull(vp.getString("foo"));
1243: assertNull(vp.getStrings("foo"));
1244:
1245: assertEquals("Wrong value for non existing key", 0.0f, vp
1246: .getFloat("foo"), 0.001);
1247: assertNull(vp.getFloats("foo"));
1248: assertNull(vp.getFloatObject("foo"));
1249: assertNull(vp.getFloatObjects("foo"));
1250:
1251: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1252:
1253: assertEquals("Wrong value for non existing key", 0.0, vp
1254: .getBigDecimal("foo").doubleValue(), 0.001);
1255: assertNull(vp.getBigDecimals("foo"));
1256:
1257: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1258:
1259: assertEquals("Wrong value for non existing key", 0, vp
1260: .getInt("foo"));
1261: assertNull(vp.getInts("foo"));
1262: assertNull(vp.getIntObject("foo"));
1263: assertNull(vp.getIntObjects("foo"));
1264:
1265: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1266:
1267: assertEquals("Wrong value for non existing key", 0, vp
1268: .getLong("foo"));
1269: assertNull(vp.getLongs("foo"));
1270: assertNull(vp.getLongObject("foo"));
1271: assertNull(vp.getLongObjects("foo"));
1272:
1273: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1274:
1275: assertEquals("Wrong value for non existing key", 0, vp
1276: .getByte("foo"));
1277: assertNull(vp.getByteObject("foo"));
1278:
1279: assertEquals("Wrong number of keys", 0, vp.keySet().size());
1280: }
1281: }
|