0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: TestConfig.java 3668 2007-02-24 00:13:22Z gbevin $
0007: */
0008: package com.uwyn.rife.config;
0009:
0010: import java.io.File;
0011: import java.io.Serializable;
0012: import java.util.Collection;
0013: import java.util.Iterator;
0014: import java.util.prefs.BackingStoreException;
0015: import java.util.prefs.Preferences;
0016:
0017: import junit.framework.TestCase;
0018:
0019: import com.uwyn.rife.config.exceptions.ConfigErrorException;
0020: import com.uwyn.rife.resources.ResourceFinderClasspath;
0021: import com.uwyn.rife.tools.ExceptionUtils;
0022: import com.uwyn.rife.tools.SerializationUtils;
0023: import com.uwyn.rife.tools.exceptions.SerializationUtilsErrorException;
0024:
0025: public class TestConfig extends TestCase {
0026: public TestConfig(String name) {
0027: super (name);
0028: }
0029:
0030: public void testInstantiation() {
0031: Config config = new Config();
0032:
0033: assertNotNull(config);
0034: }
0035:
0036: public void testValidParameters() {
0037: Config config = new Config();
0038:
0039: assertEquals(config.countParameters(), 0);
0040: config.setParameter("paramstring", "astring");
0041: config.setParameter("parambool1", "0");
0042: config.setParameter("parambool2", "1");
0043: config.setParameter("parambool3", "false");
0044: config.setParameter("parambool4", "true");
0045: config.setParameter("parambool5", "f");
0046: config.setParameter("parambool6", "t");
0047: config.setParameter("paramchar", "C");
0048: config.setParameter("paramint", "5133");
0049: config.setParameter("paramlong", "8736478");
0050: config.setParameter("paramfloat", "545.2546");
0051: config.setParameter("paramdouble", "7863.3434353");
0052:
0053: assertTrue(config.hasParameter("paramstring"));
0054: assertTrue(config.hasParameter("parambool1"));
0055: assertTrue(config.hasParameter("parambool2"));
0056: assertTrue(config.hasParameter("parambool3"));
0057: assertTrue(config.hasParameter("parambool4"));
0058: assertTrue(config.hasParameter("parambool5"));
0059: assertTrue(config.hasParameter("parambool6"));
0060: assertTrue(config.hasParameter("paramchar"));
0061: assertTrue(config.hasParameter("paramint"));
0062: assertTrue(config.hasParameter("paramlong"));
0063: assertTrue(config.hasParameter("paramfloat"));
0064: assertTrue(config.hasParameter("paramdouble"));
0065: assertEquals(config.countParameters(), 12);
0066:
0067: assertFalse(config.isFinalParameter("paramstring"));
0068: assertFalse(config.isFinalParameter("parambool1"));
0069: assertFalse(config.isFinalParameter("parambool2"));
0070: assertFalse(config.isFinalParameter("parambool3"));
0071: assertFalse(config.isFinalParameter("parambool4"));
0072: assertFalse(config.isFinalParameter("parambool5"));
0073: assertFalse(config.isFinalParameter("parambool6"));
0074: assertFalse(config.isFinalParameter("paramchar"));
0075: assertFalse(config.isFinalParameter("paramint"));
0076: assertFalse(config.isFinalParameter("paramlong"));
0077: assertFalse(config.isFinalParameter("paramfloat"));
0078: assertFalse(config.isFinalParameter("paramdouble"));
0079:
0080: assertEquals(config.getString("paramstring"), "astring");
0081: assertEquals(config.getBool("parambool1"), false);
0082: assertEquals(config.getBool("parambool2"), true);
0083: assertEquals(config.getBool("parambool3"), false);
0084: assertEquals(config.getBool("parambool4"), true);
0085: assertEquals(config.getBool("parambool5"), false);
0086: assertEquals(config.getBool("parambool6"), true);
0087: assertEquals(config.getChar("paramchar"), 'C');
0088: assertEquals(config.getInt("paramint"), 5133);
0089: assertEquals(config.getLong("paramlong"), 8736478L);
0090: assertEquals(config.getFloat("paramfloat"), 545.2546f, 0);
0091: assertEquals(config.getDouble("paramdouble"), 7863.3434353d, 0);
0092:
0093: config.removeParameter("paramstring");
0094: assertFalse(config.hasParameter("paramstring"));
0095: assertNull(config.getString("paramstring"));
0096: }
0097:
0098: public void testInvalidParameters() {
0099: Config config = new Config();
0100:
0101: assertEquals(config.countParameters(), 0);
0102: assertEquals(config.hasParameter("paramstring"), false);
0103: assertEquals(config.hasParameter("parambool"), false);
0104: assertEquals(config.hasParameter("paramchar"), false);
0105: assertEquals(config.hasParameter("paramint"), false);
0106: assertEquals(config.hasParameter("paramlong"), false);
0107: assertEquals(config.hasParameter("paramfloat"), false);
0108: assertEquals(config.hasParameter("paramdouble"), false);
0109:
0110: assertEquals(config.getString("paramstring"), null);
0111: assertEquals(config.getBool("parambool"), false);
0112: assertEquals(config.getChar("paramchar"), 0);
0113: assertEquals(config.getInt("paramint"), 0);
0114: assertEquals(config.getLong("paramlong"), 0);
0115: assertEquals(config.getFloat("paramfloat"), 0, 0);
0116: assertEquals(config.getDouble("paramdouble"), 0, 0);
0117: }
0118:
0119: public void testDefaultParameters() {
0120: Config config = new Config();
0121:
0122: config.setParameter("paramstring", "astring");
0123: config.setParameter("parambool", true);
0124: config.setParameter("paramchar", 'C');
0125: config.setParameter("paramint", 5133);
0126: config.setParameter("paramlong", 8736478L);
0127: config.setParameter("paramfloat", 545.2546f);
0128: config.setParameter("paramdouble", 7863.3434353d);
0129:
0130: assertEquals(config.getString("paramstring", "defaultstring"),
0131: "astring");
0132: assertEquals(config.getBool("parambool", false), true);
0133: assertEquals(config.getChar("paramchar", 'H'), 'C');
0134: assertEquals(config.getInt("paramint", 834), 5133);
0135: assertEquals(config.getLong("paramlong", 349875L), 8736478L);
0136: assertEquals(config.getFloat("paramfloat", 354.9457f),
0137: 545.2546f, 0);
0138: assertEquals(config.getDouble("paramdouble", 9347.784578d),
0139: 7863.3434353d, 0);
0140:
0141: assertEquals(config.getString("paramstring2", "defaultstring"),
0142: "defaultstring");
0143: assertEquals(config.getBool("parambool2", false), false);
0144: assertEquals(config.getChar("paramchar2", 'H'), 'H');
0145: assertEquals(config.getInt("paramint2", 834), 834);
0146: assertEquals(config.getLong("paramlong2", 349875L), 349875L);
0147: assertEquals(config.getFloat("paramfloat2", 354.9457f),
0148: 354.9457f, 0);
0149: assertEquals(config.getDouble("paramdouble2", 9347.784578d),
0150: 9347.784578d, 0);
0151: }
0152:
0153: public void testFinalParameters() {
0154: Config config = new Config();
0155:
0156: config.setParameter("final", "first");
0157: assertFalse(config.isFinalParameter("final"));
0158: assertEquals(config.getString("final"), "first");
0159:
0160: config.setFinalParameter("final", true);
0161: assertTrue(config.isFinalParameter("final"));
0162: config.setParameter("final", "second");
0163: assertEquals(config.getString("final"), "first");
0164:
0165: config.removeParameter("final");
0166: assertTrue(config.isFinalParameter("final"));
0167: assertEquals(config.getString("final"), "first");
0168:
0169: config.setFinalParameter("final", false);
0170: assertFalse(config.isFinalParameter("final"));
0171: config.setParameter("final", "second");
0172: assertEquals(config.getString("final"), "second");
0173:
0174: config.removeParameter("final");
0175: assertNull(config.getString("final"));
0176: }
0177:
0178: public void testSerialization() {
0179: Config config = new Config();
0180:
0181: SerializableClass serializable1 = new SerializableClass(459,
0182: "thestring");
0183: SerializableClass serializable2 = new SerializableClass(680824,
0184: "thesecondstring");
0185: SerializableClass serializable3 = null;
0186: try {
0187: config.setParameter("paramserializable", serializable1);
0188: serializable3 = config.getSerializable("paramserializable");
0189: } catch (ConfigErrorException e) {
0190: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0191: }
0192: assertEquals(serializable1, serializable3);
0193: assertNotSame(serializable1, serializable3);
0194:
0195: config.setParameter("paramstring", "astring");
0196: serializable3 = config.getSerializable("paramstring");
0197: assertNull(serializable3);
0198: serializable3 = config.getSerializable("paramstring",
0199: serializable2);
0200: assertEquals(serializable3, serializable2);
0201: }
0202:
0203: public void testLists() {
0204: Config config = new Config();
0205:
0206: assertEquals(config.countLists(), 0);
0207: config.addListItem("list1", "item1");
0208: config.addListItem("list1", "item2");
0209: config.addListItem("list1", "item3");
0210: config.addListItem("list2", "item4");
0211: config.addListItem("list2", "item5");
0212:
0213: assertTrue(config.hasList("list1"));
0214: assertTrue(config.hasList("list2"));
0215: assertEquals(config.countLists(), 2);
0216:
0217: assertFalse(config.isFinalList("list1"));
0218: assertFalse(config.isFinalList("list2"));
0219:
0220: Collection<String> items = null;
0221: Iterator<String> item_it = null;
0222:
0223: items = config.getStringItems("list1");
0224: assertEquals(items.size(), 3);
0225: item_it = items.iterator();
0226: assertEquals(item_it.hasNext(), true);
0227: assertEquals(item_it.next(), "item1");
0228: assertEquals(item_it.hasNext(), true);
0229: assertEquals(item_it.next(), "item2");
0230: assertEquals(item_it.hasNext(), true);
0231: assertEquals(item_it.next(), "item3");
0232: assertEquals(item_it.hasNext(), false);
0233:
0234: config.addListItem("list1", "item3");
0235: assertEquals(items.size(), 4);
0236: config.addListItem("list1", "item6");
0237: assertEquals(items.size(), 5);
0238:
0239: items = config.getStringItems("list2");
0240: assertEquals(items.size(), 2);
0241: item_it = items.iterator();
0242: assertEquals(item_it.hasNext(), true);
0243: assertEquals(item_it.next(), "item4");
0244: assertEquals(item_it.hasNext(), true);
0245: assertEquals(item_it.next(), "item5");
0246: assertEquals(item_it.hasNext(), false);
0247:
0248: config.clearList("list1");
0249: assertEquals(config.getStringItems("list1").size(), 0);
0250:
0251: config.removeList("list2");
0252: assertTrue(config.hasList("list1"));
0253: assertTrue(!config.hasList("list2"));
0254: assertEquals(config.countLists(), 1);
0255: }
0256:
0257: public void testListBool() {
0258: Config config = new Config();
0259:
0260: Collection<Boolean> booleans = null;
0261: config.addListItem("booleanlist", true);
0262: config.addListItem("booleanlist", false);
0263: config.addListItem("booleanlist", true);
0264: booleans = config.getBoolItems("booleanlist");
0265:
0266: assertEquals(3, booleans.size());
0267: Iterator<Boolean> booleans_it = booleans.iterator();
0268: assertEquals(booleans_it.hasNext(), true);
0269: assertEquals(booleans_it.next().booleanValue(), true);
0270: assertEquals(booleans_it.hasNext(), true);
0271: assertEquals(booleans_it.next().booleanValue(), false);
0272: assertEquals(booleans_it.hasNext(), true);
0273: assertEquals(booleans_it.next().booleanValue(), true);
0274: assertEquals(booleans_it.hasNext(), false);
0275:
0276: config.addListItem("stringlist", "thestring");
0277: config.addListItem("stringlist", "anotherstring");
0278: config.addListItem("stringlist", true);
0279: config.addListItem("stringlist", "athirdstring");
0280: booleans = config.getBoolItems("stringlist");
0281: assertEquals(4, booleans.size());
0282: booleans_it = booleans.iterator();
0283: assertEquals(booleans_it.hasNext(), true);
0284: assertEquals(booleans_it.next().booleanValue(), false);
0285: assertEquals(booleans_it.hasNext(), true);
0286: assertEquals(booleans_it.next().booleanValue(), false);
0287: assertEquals(booleans_it.hasNext(), true);
0288: assertEquals(booleans_it.next().booleanValue(), true);
0289: assertEquals(booleans_it.hasNext(), true);
0290: assertEquals(booleans_it.next().booleanValue(), false);
0291: assertEquals(booleans_it.hasNext(), false);
0292:
0293: Collection<String> items = config.getStringItems("stringlist");
0294: assertEquals(items.size(), 4);
0295: Iterator<String> item_it = items.iterator();
0296: assertEquals(item_it.hasNext(), true);
0297: assertEquals(item_it.next(), "thestring");
0298: assertEquals(item_it.hasNext(), true);
0299: assertEquals(item_it.next(), "anotherstring");
0300: assertEquals(item_it.hasNext(), true);
0301: assertEquals(item_it.next(), "true");
0302: assertEquals(item_it.hasNext(), true);
0303: assertEquals(item_it.next(), "athirdstring");
0304: assertEquals(item_it.hasNext(), false);
0305: }
0306:
0307: public void testListChar() {
0308: Config config = new Config();
0309:
0310: Collection<Character> chars = null;
0311: config.addListItem("charlist", 'G');
0312: config.addListItem("charlist", 'i');
0313: config.addListItem("charlist", 'M');
0314: chars = config.getCharItems("charlist");
0315:
0316: assertEquals(3, chars.size());
0317: Iterator<Character> chars_it = chars.iterator();
0318: assertEquals(chars_it.hasNext(), true);
0319: assertEquals(chars_it.next().charValue(), 'G');
0320: assertEquals(chars_it.hasNext(), true);
0321: assertEquals(chars_it.next().charValue(), 'i');
0322: assertEquals(chars_it.hasNext(), true);
0323: assertEquals(chars_it.next().charValue(), 'M');
0324: assertEquals(chars_it.hasNext(), false);
0325:
0326: config.addListItem("stringlist", "thestring");
0327: config.addListItem("stringlist", "anotherstring");
0328: config.addListItem("stringlist", 'O');
0329: config.addListItem("stringlist", "athirdstring");
0330: chars = config.getCharItems("stringlist");
0331: assertEquals(4, chars.size());
0332: chars_it = chars.iterator();
0333: assertEquals(chars_it.hasNext(), true);
0334: assertEquals(chars_it.next().charValue(), 't');
0335: assertEquals(chars_it.hasNext(), true);
0336: assertEquals(chars_it.next().charValue(), 'a');
0337: assertEquals(chars_it.hasNext(), true);
0338: assertEquals(chars_it.next().charValue(), 'O');
0339: assertEquals(chars_it.hasNext(), true);
0340: assertEquals(chars_it.next().charValue(), 'a');
0341: assertEquals(chars_it.hasNext(), false);
0342:
0343: Collection<String> items = config.getStringItems("stringlist");
0344: assertEquals(items.size(), 4);
0345: Iterator<String> item_it = items.iterator();
0346: assertEquals(item_it.hasNext(), true);
0347: assertEquals(item_it.next(), "thestring");
0348: assertEquals(item_it.hasNext(), true);
0349: assertEquals(item_it.next(), "anotherstring");
0350: assertEquals(item_it.hasNext(), true);
0351: assertEquals(item_it.next(), "O");
0352: assertEquals(item_it.hasNext(), true);
0353: assertEquals(item_it.next(), "athirdstring");
0354: assertEquals(item_it.hasNext(), false);
0355: }
0356:
0357: public void testListInt() {
0358: Config config = new Config();
0359:
0360: Collection<Integer> ints = null;
0361: config.addListItem("intlist", 96285);
0362: config.addListItem("intlist", 1596);
0363: config.addListItem("intlist", 4174);
0364: ints = config.getIntItems("intlist");
0365:
0366: assertEquals(3, ints.size());
0367: Iterator<Integer> ints_it = ints.iterator();
0368: assertEquals(ints_it.hasNext(), true);
0369: assertEquals(ints_it.next().intValue(), 96285);
0370: assertEquals(ints_it.hasNext(), true);
0371: assertEquals(ints_it.next().intValue(), 1596);
0372: assertEquals(ints_it.hasNext(), true);
0373: assertEquals(ints_it.next().intValue(), 4174);
0374: assertEquals(ints_it.hasNext(), false);
0375:
0376: config.addListItem("stringlist", "thestring");
0377: config.addListItem("stringlist", "anotherstring");
0378: config.addListItem("stringlist", 88646);
0379: config.addListItem("stringlist", "athirdstring");
0380: ints = config.getIntItems("stringlist");
0381: assertEquals(1, ints.size());
0382: ints_it = ints.iterator();
0383: assertEquals(ints_it.hasNext(), true);
0384: assertEquals(ints_it.next().intValue(), 88646);
0385: assertEquals(ints_it.hasNext(), false);
0386:
0387: Collection<String> items = config.getStringItems("stringlist");
0388: assertEquals(items.size(), 4);
0389: Iterator<String> item_it = items.iterator();
0390: assertEquals(item_it.hasNext(), true);
0391: assertEquals(item_it.next(), "thestring");
0392: assertEquals(item_it.hasNext(), true);
0393: assertEquals(item_it.next(), "anotherstring");
0394: assertEquals(item_it.hasNext(), true);
0395: assertEquals(item_it.next(), "88646");
0396: assertEquals(item_it.hasNext(), true);
0397: assertEquals(item_it.next(), "athirdstring");
0398: assertEquals(item_it.hasNext(), false);
0399: }
0400:
0401: public void testListLong() {
0402: Config config = new Config();
0403:
0404: Collection<Long> longs = null;
0405: config.addListItem("longlist", 69778249L);
0406: config.addListItem("longlist", 6687792094L);
0407: config.addListItem("longlist", 24425829L);
0408: longs = config.getLongItems("longlist");
0409:
0410: assertEquals(3, longs.size());
0411: Iterator<Long> longs_it = longs.iterator();
0412: assertEquals(longs_it.hasNext(), true);
0413: assertEquals(longs_it.next().longValue(), 69778249L);
0414: assertEquals(longs_it.hasNext(), true);
0415: assertEquals(longs_it.next().longValue(), 6687792094L);
0416: assertEquals(longs_it.hasNext(), true);
0417: assertEquals(longs_it.next().longValue(), 24425829L);
0418: assertEquals(longs_it.hasNext(), false);
0419:
0420: config.addListItem("stringlist", "thestring");
0421: config.addListItem("stringlist", "anotherstring");
0422: config.addListItem("stringlist", 7098634812L);
0423: config.addListItem("stringlist", "athirdstring");
0424: longs = config.getLongItems("stringlist");
0425: assertEquals(1, longs.size());
0426: longs_it = longs.iterator();
0427: assertEquals(longs_it.hasNext(), true);
0428: assertEquals(longs_it.next().longValue(), 7098634812L);
0429: assertEquals(longs_it.hasNext(), false);
0430:
0431: Collection<String> items = config.getStringItems("stringlist");
0432: assertEquals(items.size(), 4);
0433: Iterator<String> item_it = items.iterator();
0434: assertEquals(item_it.hasNext(), true);
0435: assertEquals(item_it.next(), "thestring");
0436: assertEquals(item_it.hasNext(), true);
0437: assertEquals(item_it.next(), "anotherstring");
0438: assertEquals(item_it.hasNext(), true);
0439: assertEquals(item_it.next(), "7098634812");
0440: assertEquals(item_it.hasNext(), true);
0441: assertEquals(item_it.next(), "athirdstring");
0442: assertEquals(item_it.hasNext(), false);
0443: }
0444:
0445: public void testListFloat() {
0446: Config config = new Config();
0447:
0448: Collection<Float> floats = null;
0449: config.addListItem("floatlist", 425.68f);
0450: config.addListItem("floatlist", 9682.54f);
0451: config.addListItem("floatlist", 134.98f);
0452: floats = config.getFloatItems("floatlist");
0453:
0454: assertEquals(3, floats.size());
0455: Iterator<Float> floats_it = floats.iterator();
0456: assertEquals(floats_it.hasNext(), true);
0457: assertEquals(floats_it.next(), 425.68f);
0458: assertEquals(floats_it.hasNext(), true);
0459: assertEquals(floats_it.next(), 9682.54f);
0460: assertEquals(floats_it.hasNext(), true);
0461: assertEquals(floats_it.next(), 134.98f);
0462: assertEquals(floats_it.hasNext(), false);
0463:
0464: config.addListItem("stringlist", "thestring");
0465: config.addListItem("stringlist", "anotherstring");
0466: config.addListItem("stringlist", 4512.78f);
0467: config.addListItem("stringlist", "athirdstring");
0468: floats = config.getFloatItems("stringlist");
0469: assertEquals(1, floats.size());
0470: floats_it = floats.iterator();
0471: assertEquals(floats_it.hasNext(), true);
0472: assertEquals(floats_it.next(), 4512.78f);
0473: assertEquals(floats_it.hasNext(), false);
0474:
0475: Collection<String> items = config.getStringItems("stringlist");
0476: assertEquals(items.size(), 4);
0477: Iterator<String> item_it = items.iterator();
0478: assertEquals(item_it.hasNext(), true);
0479: assertEquals(item_it.next(), "thestring");
0480: assertEquals(item_it.hasNext(), true);
0481: assertEquals(item_it.next(), "anotherstring");
0482: assertEquals(item_it.hasNext(), true);
0483: assertEquals(item_it.next(), "4512.78");
0484: assertEquals(item_it.hasNext(), true);
0485: assertEquals(item_it.next(), "athirdstring");
0486: assertEquals(item_it.hasNext(), false);
0487: }
0488:
0489: public void testListDouble() {
0490: Config config = new Config();
0491:
0492: Collection<Double> doubles = null;
0493: config.addListItem("doublelist", 69978.23524d);
0494: config.addListItem("doublelist", 413387.23451d);
0495: config.addListItem("doublelist", 441534.79798d);
0496: doubles = config.getDoubleItems("doublelist");
0497:
0498: assertEquals(3, doubles.size());
0499: Iterator<Double> doubles_it = doubles.iterator();
0500: assertEquals(doubles_it.hasNext(), true);
0501: assertEquals(doubles_it.next(), 69978.23524d);
0502: assertEquals(doubles_it.hasNext(), true);
0503: assertEquals(doubles_it.next(), 413387.23451d);
0504: assertEquals(doubles_it.hasNext(), true);
0505: assertEquals(doubles_it.next(), 441534.79798d);
0506: assertEquals(doubles_it.hasNext(), false);
0507:
0508: config.addListItem("stringlist", "thestring");
0509: config.addListItem("stringlist", "anotherstring");
0510: config.addListItem("stringlist", 551348.7986d);
0511: config.addListItem("stringlist", "athirdstring");
0512: doubles = config.getDoubleItems("stringlist");
0513: assertEquals(1, doubles.size());
0514: doubles_it = doubles.iterator();
0515: assertEquals(doubles_it.hasNext(), true);
0516: assertEquals(doubles_it.next(), 551348.7986d);
0517: assertEquals(doubles_it.hasNext(), false);
0518:
0519: Collection<String> items = config.getStringItems("stringlist");
0520: assertEquals(items.size(), 4);
0521: Iterator<String> item_it = items.iterator();
0522: assertEquals(item_it.hasNext(), true);
0523: assertEquals(item_it.next(), "thestring");
0524: assertEquals(item_it.hasNext(), true);
0525: assertEquals(item_it.next(), "anotherstring");
0526: assertEquals(item_it.hasNext(), true);
0527: assertEquals(item_it.next(), "551348.7986");
0528: assertEquals(item_it.hasNext(), true);
0529: assertEquals(item_it.next(), "athirdstring");
0530: assertEquals(item_it.hasNext(), false);
0531: }
0532:
0533: public void testListSerialization()
0534: throws SerializationUtilsErrorException {
0535: Config config = new Config();
0536:
0537: SerializableClass serializable1 = new SerializableClass(459,
0538: "thestring");
0539: SerializableClass serializable2 = new SerializableClass(69823,
0540: "anotherstring");
0541: SerializableClass serializable3 = new SerializableClass(499417,
0542: "athirdstring");
0543: Collection<SerializableClass> serializables = null;
0544: try {
0545: config.addListItem("serializablelist", serializable1);
0546: config.addListItem("serializablelist", serializable2);
0547: config.addListItem("serializablelist", serializable3);
0548: serializables = config
0549: .getSerializableItems("serializablelist");
0550: } catch (ConfigErrorException e) {
0551: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0552: }
0553:
0554: assertEquals(3, serializables.size());
0555: Iterator<SerializableClass> serializables_it = serializables
0556: .iterator();
0557: assertEquals(serializables_it.hasNext(), true);
0558: assertEquals(serializables_it.next(), serializable1);
0559: assertEquals(serializables_it.hasNext(), true);
0560: assertEquals(serializables_it.next(), serializable2);
0561: assertEquals(serializables_it.hasNext(), true);
0562: assertEquals(serializables_it.next(), serializable3);
0563: assertEquals(serializables_it.hasNext(), false);
0564:
0565: config.addListItem("stringlist", "thestring");
0566: config.addListItem("stringlist", "anotherstring");
0567: try {
0568: config.addListItem("stringlist", serializable2);
0569: } catch (ConfigErrorException e) {
0570: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0571: }
0572: config.addListItem("stringlist", "athirdstring");
0573: serializables = config.getSerializableItems("stringlist");
0574: assertEquals(1, serializables.size());
0575: serializables_it = serializables.iterator();
0576: assertEquals(serializables_it.hasNext(), true);
0577: assertEquals(serializables_it.next(), serializable2);
0578: assertEquals(serializables_it.hasNext(), false);
0579:
0580: Collection<String> items = config.getStringItems("stringlist");
0581: assertEquals(items.size(), 4);
0582: Iterator<String> item_it = items.iterator();
0583: assertEquals(item_it.hasNext(), true);
0584: assertEquals(item_it.next(), "thestring");
0585: assertEquals(item_it.hasNext(), true);
0586: assertEquals(item_it.next(), "anotherstring");
0587: assertEquals(item_it.hasNext(), true);
0588: String serialized = item_it.next();
0589: assertEquals(SerializationUtils
0590: .serializeToString(serializable2), serialized);
0591: assertEquals(item_it.hasNext(), true);
0592: assertEquals(item_it.next(), "athirdstring");
0593: assertEquals(item_it.hasNext(), false);
0594: }
0595:
0596: public void testFinalLists() {
0597: Config config = new Config();
0598:
0599: Collection<String> items = null;
0600: Iterator<String> item_it = null;
0601:
0602: config.addListItem("final", "item1");
0603: config.addListItem("final", "item2");
0604: config.addListItem("final", "item3");
0605: assertFalse(config.isFinalList("final"));
0606:
0607: items = config.getStringItems("final");
0608: assertEquals(items.size(), 3);
0609: item_it = items.iterator();
0610: assertEquals(item_it.hasNext(), true);
0611: assertEquals(item_it.next(), "item1");
0612: assertEquals(item_it.hasNext(), true);
0613: assertEquals(item_it.next(), "item2");
0614: assertEquals(item_it.hasNext(), true);
0615: assertEquals(item_it.next(), "item3");
0616: assertEquals(item_it.hasNext(), false);
0617:
0618: config.setFinalList("final", true);
0619: assertTrue(config.isFinalList("final"));
0620: config.addListItem("final", "item4");
0621: items = config.getStringItems("final");
0622: assertEquals(items.size(), 3);
0623: item_it = items.iterator();
0624: assertEquals(item_it.hasNext(), true);
0625: assertEquals(item_it.next(), "item1");
0626: assertEquals(item_it.hasNext(), true);
0627: assertEquals(item_it.next(), "item2");
0628: assertEquals(item_it.hasNext(), true);
0629: assertEquals(item_it.next(), "item3");
0630: assertEquals(item_it.hasNext(), false);
0631:
0632: config.clearList("final");
0633: assertEquals(config.getStringItems("final").size(), 3);
0634:
0635: config.removeList("final");
0636: assertTrue(config.hasList("final"));
0637: assertEquals(config.getStringItems("final").size(), 3);
0638:
0639: config.setFinalList("final", false);
0640: assertFalse(config.isFinalList("final"));
0641: config.addListItem("final", "item4");
0642: items = config.getStringItems("final");
0643: assertEquals(items.size(), 4);
0644: item_it = items.iterator();
0645: assertEquals(item_it.hasNext(), true);
0646: assertEquals(item_it.next(), "item1");
0647: assertEquals(item_it.hasNext(), true);
0648: assertEquals(item_it.next(), "item2");
0649: assertEquals(item_it.hasNext(), true);
0650: assertEquals(item_it.next(), "item3");
0651: assertEquals(item_it.hasNext(), true);
0652: assertEquals(item_it.next(), "item4");
0653: assertEquals(item_it.hasNext(), false);
0654:
0655: config.clearList("final");
0656: assertEquals(config.getStringItems("final").size(), 0);
0657:
0658: config.removeList("final");
0659: assertFalse(config.hasList("final"));
0660: }
0661:
0662: public void testXmlOutput() throws SerializationUtilsErrorException {
0663: Config config = new Config();
0664:
0665: config.setParameter("paramstring", "astring");
0666: config.setFinalParameter("paramstring", true);
0667: config.setParameter("parambool", true);
0668: config.setParameter("paramchar", 'C');
0669: config.setParameter("paramint", 5133);
0670: config.setParameter("paramlong", 8736478L);
0671: config.setParameter("paramfloat", 545.2546f);
0672: config.setParameter("paramdouble", 7863.3434353d);
0673: config.addListItem("list1", "item1");
0674: config.addListItem("list1", "item2");
0675: config.addListItem("list1", "item3");
0676: config.addListItem("list2", "item4");
0677: config.addListItem("list2", "item5");
0678: config.setFinalList("list2", true);
0679: SerializableClass serializable1 = new SerializableClass(69823,
0680: "anotherstring");
0681: SerializableClass serializable2 = new SerializableClass(459,
0682: "thestring");
0683: SerializableClass serializable3 = new SerializableClass(499417,
0684: "athirdstring");
0685: try {
0686: config.setParameter("paramserializable", serializable1);
0687: config.addListItem("serializablelist", serializable2);
0688: config.addListItem("serializablelist", serializable3);
0689: } catch (ConfigErrorException e) {
0690: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0691: }
0692:
0693: String xml = config.toXml();
0694: assertEquals(
0695: xml,
0696: "<config>\n"
0697: + "\t<list name=\"list1\">\n"
0698: + "\t\t<item>item1</item>\n"
0699: + "\t\t<item>item2</item>\n"
0700: + "\t\t<item>item3</item>\n"
0701: + "\t</list>\n"
0702: + "\t<list name=\"list2\" final=\"true\">\n"
0703: + "\t\t<item>item4</item>\n"
0704: + "\t\t<item>item5</item>\n"
0705: + "\t</list>\n"
0706: + "\t<list name=\"serializablelist\">\n"
0707: + "\t\t<item>"
0708: + SerializationUtils
0709: .serializeToString(serializable2)
0710: + "</item>\n"
0711: + "\t\t<item>"
0712: + SerializationUtils
0713: .serializeToString(serializable3)
0714: + "</item>\n"
0715: + "\t</list>\n"
0716: + "\t<param name=\"parambool\">true</param>\n"
0717: + "\t<param name=\"paramchar\">C</param>\n"
0718: + "\t<param name=\"paramdouble\">7863.3434353</param>\n"
0719: + "\t<param name=\"paramfloat\">545.2546</param>\n"
0720: + "\t<param name=\"paramint\">5133</param>\n"
0721: + "\t<param name=\"paramlong\">8736478</param>\n"
0722: + "\t<param name=\"paramserializable\">"
0723: + SerializationUtils
0724: .serializeToString(serializable1)
0725: + "</param>\n"
0726: + "\t<param name=\"paramstring\" final=\"true\">astring</param>\n"
0727: + "</config>\n");
0728: }
0729:
0730: public void testStoreXml() {
0731: try {
0732: Config config = new Config();
0733:
0734: config.setParameter("paramstring", "astring");
0735: config.setFinalParameter("paramstring", true);
0736: config.setParameter("parambool", true);
0737: config.setParameter("paramchar", 'C');
0738: config.setParameter("paramint", 5133);
0739: config.setParameter("paramlong", 8736478L);
0740: config.setParameter("paramfloat", 545.2546f);
0741: config.setParameter("paramdouble", 7863.3434353d);
0742: config.addListItem("list1", "item1");
0743: config.addListItem("list1", "item2");
0744: config.addListItem("list1", "item3");
0745: config.addListItem("list2", "item4");
0746: config.addListItem("list2", "item5");
0747: config.setFinalList("list2", true);
0748:
0749: String xml_filename = "config_storexml_test.xml";
0750: String xml_path = RifeConfig.Global.getTempPath()
0751: + File.separator + xml_filename;
0752: File xml_file = new File(xml_path);
0753: config.storeToXml(xml_file);
0754:
0755: Config config_stored = new Config(xml_filename,
0756: ResourceFinderClasspath.getInstance());
0757:
0758: assertEquals(config_stored.countParameters(), 7);
0759:
0760: assertTrue(config_stored.hasParameter("paramstring"));
0761: assertTrue(config_stored.hasParameter("parambool"));
0762: assertTrue(config_stored.hasParameter("paramchar"));
0763: assertTrue(config_stored.hasParameter("paramint"));
0764: assertTrue(config_stored.hasParameter("paramlong"));
0765: assertTrue(config_stored.hasParameter("paramfloat"));
0766: assertTrue(config_stored.hasParameter("paramdouble"));
0767: assertEquals(config_stored.getString("paramstring"),
0768: "astring");
0769: assertEquals(config_stored.getBool("parambool"), true);
0770: assertEquals(config_stored.getChar("paramchar"), 'C');
0771: assertEquals(config_stored.getInt("paramint"), 5133);
0772: assertEquals(config_stored.getLong("paramlong"), 8736478L);
0773: assertEquals(config_stored.getFloat("paramfloat"),
0774: 545.2546f, 0);
0775: assertEquals(config_stored.getDouble("paramdouble"),
0776: 7863.3434353d, 0);
0777: assertTrue(config_stored.isFinalParameter("paramstring"));
0778: assertFalse(config_stored.isFinalParameter("parambool"));
0779: assertFalse(config_stored.isFinalParameter("paramchar"));
0780: assertFalse(config_stored.isFinalParameter("paramint"));
0781: assertFalse(config_stored.isFinalParameter("paramlong"));
0782: assertFalse(config_stored.isFinalParameter("paramfloat"));
0783: assertFalse(config_stored.isFinalParameter("paramdouble"));
0784:
0785: assertEquals(config_stored.countLists(), 2);
0786:
0787: assertTrue(config_stored.hasList("list1"));
0788: assertTrue(config_stored.hasList("list2"));
0789: assertFalse(config_stored.isFinalList("list1"));
0790: assertTrue(config_stored.isFinalList("list2"));
0791:
0792: Collection<String> items = null;
0793: Iterator<String> item_it = null;
0794:
0795: items = config_stored.getStringItems("list1");
0796: assertEquals(items.size(), 3);
0797: item_it = items.iterator();
0798: assertEquals(item_it.hasNext(), true);
0799: assertEquals(item_it.next(), "item1");
0800: assertEquals(item_it.hasNext(), true);
0801: assertEquals(item_it.next(), "item2");
0802: assertEquals(item_it.hasNext(), true);
0803: assertEquals(item_it.next(), "item3");
0804: assertEquals(item_it.hasNext(), false);
0805:
0806: items = config_stored.getStringItems("list2");
0807: assertEquals(items.size(), 2);
0808: item_it = items.iterator();
0809: assertEquals(item_it.hasNext(), true);
0810: assertEquals(item_it.next(), "item4");
0811: assertEquals(item_it.hasNext(), true);
0812: assertEquals(item_it.next(), "item5");
0813: assertEquals(item_it.hasNext(), false);
0814:
0815: config_stored.setParameter("paramstring2", "anotherstring");
0816: config_stored.addListItem("list3", "item6");
0817: config_stored.addListItem("list3", "item7");
0818:
0819: assertTrue(config_stored.hasList("list3"));
0820:
0821: config_stored.storeToXml();
0822:
0823: Config config_stored2 = new Config(xml_filename,
0824: ResourceFinderClasspath.getInstance());
0825: assertEquals(config_stored2.countParameters(), 8);
0826: assertEquals(config_stored2.getString("paramstring2"),
0827: "anotherstring");
0828: assertEquals(config_stored2.countLists(), 3);
0829: assertTrue(config_stored2.hasList("list1"));
0830: assertTrue(config_stored2.hasList("list2"));
0831: assertTrue(config_stored2.hasList("list3"));
0832: items = config_stored.getStringItems("list3");
0833: assertEquals(items.size(), 2);
0834: item_it = items.iterator();
0835: assertEquals(item_it.hasNext(), true);
0836: assertEquals(item_it.next(), "item6");
0837: assertEquals(item_it.hasNext(), true);
0838: assertEquals(item_it.next(), "item7");
0839: assertEquals(item_it.hasNext(), false);
0840:
0841: xml_file.delete();
0842: } catch (ConfigErrorException e) {
0843: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0844: }
0845: }
0846:
0847: public void testStorePreferences() {
0848: String preferences_path = "test_rifeconfig_preferences_integration";
0849: Preferences preferences = Preferences.userRoot().node(
0850: preferences_path);
0851:
0852: try {
0853: Config config = new Config();
0854:
0855: config.setParameter("paramstring", "astring");
0856: config.setParameter("paramstring2", "astring2");
0857: config.setParameter("parambool", true);
0858: config.setParameter("paramchar", 'C');
0859: config.setParameter("paramint", 5133);
0860: config.setParameter("paramlong", 8736478L);
0861: config.setParameter("paramfloat", 545.2546f);
0862: config.setParameter("paramdouble", 7863.3434353d);
0863: config.addListItem("list1", "item1");
0864: config.addListItem("list1", "item2");
0865: config.addListItem("list1", "item3");
0866: config.addListItem("list2", "item4");
0867: config.addListItem("list2", "item5");
0868: config.addListItem("list3", "item6");
0869: config.addListItem("list3", "item7");
0870: config.addListItem("list3", "item8");
0871:
0872: config.storeToPreferences(preferences);
0873:
0874: Config config_other = new Config();
0875:
0876: assertEquals(config_other.countParameters(), 0);
0877:
0878: config_other.setParameter("paramstring", "afirststring");
0879: config_other.setParameter("paramstring2", "afirststring2");
0880: config_other.setFinalParameter("paramstring2", true);
0881: config_other.setParameter("paramstring3", "afirststring3");
0882: config_other.setParameter("parambool", false);
0883: config_other.setParameter("paramchar", 'D');
0884: config_other.setParameter("paramint", 698);
0885: config_other.setParameter("paramlong", 985835L);
0886: config_other.setParameter("paramfloat", 978.14898f);
0887: config_other.setParameter("paramdouble", 6098.1439724d);
0888: config_other.addListItem("list1", "item1a");
0889: config_other.addListItem("list1", "item2a");
0890: config_other.addListItem("list2", "item3a");
0891: config_other.setFinalList("list2", true);
0892:
0893: assertEquals(config_other.countParameters(), 9);
0894:
0895: assertTrue(config_other.hasList("list1"));
0896: assertTrue(config_other.hasList("list2"));
0897:
0898: assertTrue(config_other.hasParameter("paramstring"));
0899: assertTrue(config_other.hasParameter("paramstring2"));
0900: assertTrue(config_other.hasParameter("paramstring3"));
0901: assertTrue(config_other.hasParameter("parambool"));
0902: assertTrue(config_other.hasParameter("paramchar"));
0903: assertTrue(config_other.hasParameter("paramint"));
0904: assertTrue(config_other.hasParameter("paramlong"));
0905: assertTrue(config_other.hasParameter("paramfloat"));
0906: assertTrue(config_other.hasParameter("paramdouble"));
0907: assertEquals(config_other.getString("paramstring"),
0908: "afirststring");
0909: assertEquals(config_other.getString("paramstring2"),
0910: "afirststring2");
0911: assertEquals(config_other.getString("paramstring3"),
0912: "afirststring3");
0913: assertEquals(config_other.getBool("parambool"), false);
0914: assertEquals(config_other.getChar("paramchar"), 'D');
0915: assertEquals(config_other.getInt("paramint"), 698);
0916: assertEquals(config_other.getLong("paramlong"), 985835L);
0917: assertEquals(config_other.getFloat("paramfloat"),
0918: 978.14898f, 0);
0919: assertEquals(config_other.getDouble("paramdouble"),
0920: 6098.1439724d, 0);
0921:
0922: assertEquals(config_other.countLists(), 2);
0923:
0924: Collection<String> items = null;
0925: Iterator<String> item_it = null;
0926:
0927: items = config_other.getStringItems("list1");
0928: assertEquals(items.size(), 2);
0929: item_it = items.iterator();
0930: assertEquals(item_it.hasNext(), true);
0931: assertEquals(item_it.next(), "item1a");
0932: assertEquals(item_it.hasNext(), true);
0933: assertEquals(item_it.next(), "item2a");
0934: assertEquals(item_it.hasNext(), false);
0935:
0936: items = config_other.getStringItems("list2");
0937: assertEquals(items.size(), 1);
0938: item_it = items.iterator();
0939: assertEquals(item_it.hasNext(), true);
0940: assertEquals(item_it.next(), "item3a");
0941: assertEquals(item_it.hasNext(), false);
0942:
0943: config_other.setPreferencesNode(preferences);
0944:
0945: assertEquals(config_other.countParameters(), 10);
0946:
0947: assertTrue(config_other.hasParameter("paramstring"));
0948: assertTrue(config_other.hasParameter("paramstring2"));
0949: assertTrue(config_other.hasParameter("paramstring3"));
0950: assertTrue(config_other.hasParameter("parambool"));
0951: assertTrue(config_other.hasParameter("paramchar"));
0952: assertTrue(config_other.hasParameter("paramint"));
0953: assertTrue(config_other.hasParameter("paramlong"));
0954: assertTrue(config_other.hasParameter("paramfloat"));
0955: assertTrue(config_other.hasParameter("paramdouble"));
0956: assertEquals(config_other.getString("paramstring"),
0957: "astring");
0958: assertEquals(config_other.getString("paramstring2"),
0959: "afirststring2");
0960: assertEquals(config_other.getString("paramstring3"),
0961: "afirststring3");
0962: assertEquals(config_other.getBool("parambool"), true);
0963: assertEquals(config_other.getChar("paramchar"), 'C');
0964: assertEquals(config_other.getInt("paramint"), 5133);
0965: assertEquals(config_other.getLong("paramlong"), 8736478L);
0966: assertEquals(config_other.getFloat("paramfloat"),
0967: 545.2546f, 0);
0968: assertEquals(config_other.getDouble("paramdouble"),
0969: 7863.3434353d, 0);
0970:
0971: assertEquals(config_other.countLists(), 3);
0972:
0973: assertTrue(config_other.hasList("list1"));
0974: assertTrue(config_other.hasList("list2"));
0975: assertTrue(config_other.hasList("list3"));
0976:
0977: items = config_other.getStringItems("list1");
0978: assertEquals(items.size(), 3);
0979: item_it = items.iterator();
0980: assertEquals(item_it.hasNext(), true);
0981: assertEquals(item_it.next(), "item1");
0982: assertEquals(item_it.hasNext(), true);
0983: assertEquals(item_it.next(), "item2");
0984: assertEquals(item_it.hasNext(), true);
0985: assertEquals(item_it.next(), "item3");
0986: assertEquals(item_it.hasNext(), false);
0987:
0988: items = config_other.getStringItems("list2");
0989: assertEquals(items.size(), 1);
0990: item_it = items.iterator();
0991: assertEquals(item_it.hasNext(), true);
0992: assertEquals(item_it.next(), "item3a");
0993: assertEquals(item_it.hasNext(), false);
0994:
0995: items = config_other.getStringItems("list3");
0996: assertEquals(items.size(), 3);
0997: item_it = items.iterator();
0998: assertEquals(item_it.hasNext(), true);
0999: assertEquals(item_it.next(), "item6");
1000: assertEquals(item_it.hasNext(), true);
1001: assertEquals(item_it.next(), "item7");
1002: assertEquals(item_it.hasNext(), true);
1003: assertEquals(item_it.next(), "item8");
1004: assertEquals(item_it.hasNext(), false);
1005:
1006: config_other.removeParameter("paramint");
1007: config_other.setParameter("paramstring3", "anotherstring");
1008: config_other.addListItem("list1", "item9");
1009: config_other.addListItem("list4", "item10");
1010: config_other.addListItem("list4", "item11");
1011:
1012: assertTrue(config_other.hasList("list4"));
1013:
1014: config_other.storeToPreferences();
1015:
1016: Config config_other2 = new Config();
1017: config_other2.setParameter("paramstring2",
1018: "oncemoreastring");
1019: config_other2.setPreferencesNode(preferences);
1020:
1021: assertEquals(config_other2.countParameters(), 9);
1022: assertFalse(config_other.hasParameter("paramint"));
1023: assertNull(config_other.getString("paramint"));
1024: assertTrue(config_other.hasParameter("paramstring2"));
1025: assertEquals(config_other2.getString("paramstring2"),
1026: "astring2");
1027: config_other2.setFinalParameter("paramstring2", true);
1028: assertEquals(config_other2.getString("paramstring2"),
1029: "oncemoreastring");
1030: assertTrue(config_other.hasParameter("paramstring3"));
1031: assertEquals(config_other2.getString("paramstring3"),
1032: "anotherstring");
1033:
1034: assertEquals(config_other2.countLists(), 4);
1035: assertTrue(config_other2.hasList("list1"));
1036: assertTrue(config_other2.hasList("list2"));
1037: assertTrue(config_other2.hasList("list3"));
1038: assertTrue(config_other2.hasList("list4"));
1039:
1040: items = config_other2.getStringItems("list1");
1041: assertEquals(items.size(), 4);
1042: item_it = items.iterator();
1043: assertEquals(item_it.hasNext(), true);
1044: assertEquals(item_it.next(), "item1");
1045: assertEquals(item_it.hasNext(), true);
1046: assertEquals(item_it.next(), "item2");
1047: assertEquals(item_it.hasNext(), true);
1048: assertEquals(item_it.next(), "item3");
1049: assertEquals(item_it.hasNext(), true);
1050: assertEquals(item_it.next(), "item9");
1051: assertEquals(item_it.hasNext(), false);
1052:
1053: items = config_other2.getStringItems("list2");
1054: assertEquals(items.size(), 2);
1055: item_it = items.iterator();
1056: assertEquals(item_it.hasNext(), true);
1057: assertEquals(item_it.next(), "item4");
1058: assertEquals(item_it.hasNext(), true);
1059: assertEquals(item_it.next(), "item5");
1060: assertEquals(item_it.hasNext(), false);
1061:
1062: items = config_other.getStringItems("list3");
1063: assertEquals(items.size(), 3);
1064: item_it = items.iterator();
1065: assertEquals(item_it.hasNext(), true);
1066: assertEquals(item_it.next(), "item6");
1067: assertEquals(item_it.hasNext(), true);
1068: assertEquals(item_it.next(), "item7");
1069: assertEquals(item_it.hasNext(), true);
1070: assertEquals(item_it.next(), "item8");
1071: assertEquals(item_it.hasNext(), false);
1072:
1073: items = config_other2.getStringItems("list4");
1074: assertEquals(items.size(), 2);
1075: item_it = items.iterator();
1076: assertEquals(item_it.hasNext(), true);
1077: assertEquals(item_it.next(), "item10");
1078: assertEquals(item_it.hasNext(), true);
1079: assertEquals(item_it.next(), "item11");
1080: assertEquals(item_it.hasNext(), false);
1081:
1082: Config config_other3 = new Config();
1083: config_other3.setPreferencesNode(preferences);
1084:
1085: assertEquals(config_other3.countLists(), 4);
1086: assertTrue(config_other3.hasList("list1"));
1087: assertTrue(config_other3.hasList("list2"));
1088: assertTrue(config_other3.hasList("list3"));
1089: assertTrue(config_other3.hasList("list4"));
1090:
1091: items = config_other3.getStringItems("list1");
1092: assertEquals(items.size(), 4);
1093: items = config_other3.getStringItems("list2");
1094: assertEquals(items.size(), 2);
1095: items = config_other3.getStringItems("list3");
1096: assertEquals(items.size(), 3);
1097: items = config_other3.getStringItems("list4");
1098: assertEquals(items.size(), 2);
1099:
1100: config_other3.clearList("list2");
1101: config_other3.removeList("list3");
1102:
1103: assertEquals(config_other3.countLists(), 3);
1104: assertTrue(config_other3.hasList("list1"));
1105: assertTrue(config_other3.hasList("list2"));
1106: assertFalse(config_other3.hasList("list3"));
1107: assertTrue(config_other3.hasList("list4"));
1108: items = config_other3.getStringItems("list1");
1109: assertEquals(items.size(), 4);
1110: items = config_other3.getStringItems("list2");
1111: assertNull(items);
1112: items = config_other3.getStringItems("list4");
1113: assertEquals(items.size(), 2);
1114:
1115: Config config_other4 = new Config();
1116: config_other4.setPreferencesNode(preferences);
1117:
1118: assertEquals(config_other4.countLists(), 3);
1119: assertTrue(config_other4.hasList("list1"));
1120: assertTrue(config_other4.hasList("list2"));
1121: assertFalse(config_other4.hasList("list3"));
1122: assertTrue(config_other4.hasList("list4"));
1123:
1124: items = config_other4.getStringItems("list1");
1125: assertEquals(items.size(), 4);
1126: items = config_other4.getStringItems("list2");
1127: assertNull(items);
1128: items = config_other4.getStringItems("list4");
1129: assertEquals(items.size(), 2);
1130: } catch (ConfigErrorException e) {
1131: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
1132: } finally {
1133: try {
1134: preferences.removeNode();
1135: } catch (BackingStoreException e) {
1136: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
1137: false);
1138: }
1139: }
1140: }
1141:
1142: public void testClone() {
1143: Config config1 = new Config();
1144:
1145: config1.setParameter("paramstring", "astring");
1146: config1.setFinalParameter("paramstring", true);
1147: config1.setParameter("parambool", true);
1148: config1.setParameter("paramchar", 'C');
1149: config1.setParameter("paramint", 5133);
1150: config1.setParameter("paramlong", 8736478L);
1151: config1.setParameter("paramfloat", 545.2546f);
1152: config1.setParameter("paramdouble", 7863.3434353d);
1153: config1.addListItem("list1", "item1");
1154: config1.addListItem("list1", "item2");
1155: config1.addListItem("list1", "item3");
1156: config1.addListItem("list2", "item4");
1157: config1.addListItem("list2", "item5");
1158: config1.setFinalList("list2", true);
1159:
1160: Config config2 = config1.clone();
1161:
1162: assertNotSame(config1, config2);
1163:
1164: assertEquals(config1.toXml(), config2.toXml());
1165:
1166: config2.setFinalParameter("paramstring", false);
1167: config2.setParameter("paramstring", "astring2");
1168: config2.setParameter("parambool", false);
1169: config2.setFinalParameter("parambool", true);
1170: config2.setParameter("paramchar", 'D');
1171: config2.setParameter("paramint", 442);
1172: config2.setParameter("paramlong", 9121L);
1173: config2.setParameter("paramfloat", 112.87f);
1174: config2.setParameter("paramdouble", 1313.2232887d);
1175: config2.setParameter("paramnew", 2223);
1176: config2.addListItem("list1", "item6");
1177: config2.setFinalList("list1", true);
1178: config2.setFinalList("list2", false);
1179: config2.addListItem("list2", "item7");
1180: config2.addListItem("list3", "item8");
1181:
1182: assertFalse(config1.toXml().equals(config2.toXml()));
1183:
1184: assertEquals(config1.countParameters(), 7);
1185:
1186: assertTrue(config1.hasParameter("paramstring"));
1187: assertTrue(config1.hasParameter("parambool"));
1188: assertTrue(config1.hasParameter("paramchar"));
1189: assertTrue(config1.hasParameter("paramint"));
1190: assertTrue(config1.hasParameter("paramlong"));
1191: assertTrue(config1.hasParameter("paramfloat"));
1192: assertTrue(config1.hasParameter("paramdouble"));
1193: assertEquals(config1.getString("paramstring"), "astring");
1194: assertEquals(config1.getBool("parambool"), true);
1195: assertEquals(config1.getChar("paramchar"), 'C');
1196: assertEquals(config1.getInt("paramint"), 5133);
1197: assertEquals(config1.getLong("paramlong"), 8736478L);
1198: assertEquals(config1.getFloat("paramfloat"), 545.2546f, 0);
1199: assertEquals(config1.getDouble("paramdouble"), 7863.3434353d, 0);
1200: assertTrue(config1.isFinalParameter("paramstring"));
1201: assertFalse(config1.isFinalParameter("parambool"));
1202: assertFalse(config1.isFinalParameter("paramchar"));
1203: assertFalse(config1.isFinalParameter("paramint"));
1204: assertFalse(config1.isFinalParameter("paramlong"));
1205: assertFalse(config1.isFinalParameter("paramfloat"));
1206: assertFalse(config1.isFinalParameter("paramdouble"));
1207:
1208: assertEquals(config1.countLists(), 2);
1209:
1210: assertTrue(config1.hasList("list1"));
1211: assertTrue(config1.hasList("list2"));
1212: assertFalse(config1.isFinalList("list1"));
1213: assertTrue(config1.isFinalList("list2"));
1214:
1215: Collection<String> items = null;
1216: Iterator<String> item_it = null;
1217:
1218: items = config1.getStringItems("list1");
1219: assertEquals(items.size(), 3);
1220: item_it = items.iterator();
1221: assertEquals(item_it.hasNext(), true);
1222: assertEquals(item_it.next(), "item1");
1223: assertEquals(item_it.hasNext(), true);
1224: assertEquals(item_it.next(), "item2");
1225: assertEquals(item_it.hasNext(), true);
1226: assertEquals(item_it.next(), "item3");
1227: assertEquals(item_it.hasNext(), false);
1228:
1229: items = config1.getStringItems("list2");
1230: assertEquals(items.size(), 2);
1231: item_it = items.iterator();
1232: assertEquals(item_it.hasNext(), true);
1233: assertEquals(item_it.next(), "item4");
1234: assertEquals(item_it.hasNext(), true);
1235: assertEquals(item_it.next(), "item5");
1236: assertEquals(item_it.hasNext(), false);
1237:
1238: assertEquals(config2.countParameters(), 8);
1239:
1240: assertTrue(config2.hasParameter("paramstring"));
1241: assertTrue(config2.hasParameter("parambool"));
1242: assertTrue(config2.hasParameter("paramchar"));
1243: assertTrue(config2.hasParameter("paramint"));
1244: assertTrue(config2.hasParameter("paramlong"));
1245: assertTrue(config2.hasParameter("paramfloat"));
1246: assertTrue(config2.hasParameter("paramdouble"));
1247: assertEquals(config2.getString("paramstring"), "astring2");
1248: assertEquals(config2.getBool("parambool"), false);
1249: assertEquals(config2.getChar("paramchar"), 'D');
1250: assertEquals(config2.getInt("paramint"), 442);
1251: assertEquals(config2.getLong("paramlong"), 9121L);
1252: assertEquals(config2.getFloat("paramfloat"), 112.87f, 0);
1253: assertEquals(config2.getDouble("paramdouble"), 1313.2232887d, 0);
1254: assertEquals(config2.getInt("paramnew"), 2223);
1255: assertFalse(config2.isFinalParameter("paramstring"));
1256: assertTrue(config2.isFinalParameter("parambool"));
1257: assertFalse(config2.isFinalParameter("paramchar"));
1258: assertFalse(config2.isFinalParameter("paramint"));
1259: assertFalse(config2.isFinalParameter("paramlong"));
1260: assertFalse(config2.isFinalParameter("paramfloat"));
1261: assertFalse(config2.isFinalParameter("paramdouble"));
1262: assertFalse(config2.isFinalParameter("paramnew"));
1263:
1264: assertEquals(config2.countLists(), 3);
1265:
1266: assertTrue(config2.hasList("list1"));
1267: assertTrue(config2.hasList("list2"));
1268: assertTrue(config2.hasList("list3"));
1269: assertTrue(config2.isFinalList("list1"));
1270: assertFalse(config2.isFinalList("list2"));
1271: assertFalse(config2.isFinalList("list3"));
1272:
1273: items = config2.getStringItems("list1");
1274: assertEquals(items.size(), 4);
1275: item_it = items.iterator();
1276: assertEquals(item_it.hasNext(), true);
1277: assertEquals(item_it.next(), "item1");
1278: assertEquals(item_it.hasNext(), true);
1279: assertEquals(item_it.next(), "item2");
1280: assertEquals(item_it.hasNext(), true);
1281: assertEquals(item_it.next(), "item3");
1282: assertEquals(item_it.hasNext(), true);
1283: assertEquals(item_it.next(), "item6");
1284: assertEquals(item_it.hasNext(), false);
1285:
1286: items = config2.getStringItems("list2");
1287: assertEquals(items.size(), 3);
1288: item_it = items.iterator();
1289: assertEquals(item_it.hasNext(), true);
1290: assertEquals(item_it.next(), "item4");
1291: assertEquals(item_it.hasNext(), true);
1292: assertEquals(item_it.next(), "item5");
1293: assertEquals(item_it.hasNext(), true);
1294: assertEquals(item_it.next(), "item7");
1295: assertEquals(item_it.hasNext(), false);
1296:
1297: items = config2.getStringItems("list3");
1298: assertEquals(items.size(), 1);
1299: item_it = items.iterator();
1300: assertEquals(item_it.hasNext(), true);
1301: assertEquals(item_it.next(), "item8");
1302: assertEquals(item_it.hasNext(), false);
1303: }
1304: }
1305:
1306: class SerializableClass implements Serializable {
1307: private int mNumber = -1;
1308: private String mString = null;
1309:
1310: public SerializableClass(int number, String string) {
1311: mNumber = number;
1312: mString = string;
1313: }
1314:
1315: public void setNumber(int number) {
1316: mNumber = number;
1317: }
1318:
1319: public int getNumber() {
1320: return mNumber;
1321: }
1322:
1323: public void setString(String string) {
1324: mString = string;
1325: }
1326:
1327: public String getString() {
1328: return mString;
1329: }
1330:
1331: public boolean equals(Object other) {
1332: if (this == other) {
1333: return true;
1334: }
1335:
1336: if (null == other) {
1337: return false;
1338: }
1339:
1340: if (!(other instanceof SerializableClass)) {
1341: return false;
1342: }
1343:
1344: SerializableClass other_datalink = (SerializableClass) other;
1345: if (!other_datalink.getString().equals(getString())) {
1346: return false;
1347: }
1348: if (other_datalink.getNumber() != getNumber()) {
1349: return false;
1350: }
1351:
1352: return true;
1353: }
1354: }
|