001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.*;
030:
031: import junit.framework.TestCase;
032: import junit.framework.*;
033:
034: public class TestPropertyTree extends TestCase {
035: private String str(Object o) {
036: return (o == null) ? "null" : o.toString();
037: }
038:
039: public void test_PropertyTree() {
040: PropertyTree pt = new PropertyTree();
041: pt.put("foo", "bar");
042: List x = new ArrayList();
043: x.add("elemA");
044: x.add("elemB");
045: pt.put("testList", x);
046:
047: PropertyTree subPT = new PropertyTree(1);
048: subPT.put("sub1", "val1");
049: subPT.put("sub2", "val2");
050: subPT.put("sub3", "val3");
051: pt.put("subPT", subPT);
052:
053: assertEquals(
054: "initialize",
055: "{foo=bar, testList=[elemA, elemB], subPT={sub1=val1, sub2=val2, sub3=val3}}",
056: str(pt.toString()));
057: assertEquals("get(foo)", "bar", pt.get("foo"));
058: assertEquals("get(testList)", "[elemA, elemB]", str(pt
059: .get("testList")));
060: assertEquals("get(subPT)", "{sub1=val1, sub2=val2, sub3=val3}",
061: str(pt.get("subPT")));
062:
063: assertEquals("get(subPT).get(sub1)", "val1",
064: str(((PropertyTree) pt.get("subPT")).get("sub1")));
065: assertEquals("get(xxx)", "null", str(pt.get("xxx")));
066: assertEquals("get(null)", "null", str(pt.get(null)));
067: try {
068: pt.put(null, "val");
069: fail("allows illegal key");
070: } catch (Exception e) {
071: assertTrue("denies illegal key (null)", true);
072: }
073:
074: try {
075: pt.put("obj", new Object());
076: fail("allows illegal value");
077: } catch (Exception e) {
078: assertTrue("denies illegal value (new Object())", true);
079: }
080:
081: PropertyTree dupPt = (PropertyTree) pt.clone();
082: assertEquals(
083: "clone",
084: "{foo=bar, testList=[elemA, elemB], subPT={sub1=val1, sub2=val2, sub3=val3}}",
085: str(dupPt.toString()));
086: // .equals is probably not worth implementing properly
087: //assertEquals("original.equals(clone)", pt, dupPt);
088: try {
089: java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(
090: 100);
091: java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(
092: baos);
093: oos.writeObject(pt);
094: byte buf[] = baos.toByteArray();
095: oos.close();
096: //System.out.println("serialized to byte["+buf.length+"]");
097: //System.out.println("attempt deserialize...");
098: java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(
099: buf);
100: java.io.ObjectInputStream ois = new java.io.ObjectInputStream(
101: bais);
102: PropertyTree newPT = (PropertyTree) ois.readObject();
103: ois.close();
104: //System.out.println("view deserialized result:");
105: assertEquals(
106: "piped",
107: "{foo=bar, testList=[elemA, elemB], subPT={sub1=val1, sub2=val2, sub3=val3}}",
108: str(newPT.toString()));
109: } catch (Exception e) {
110: fail("serialization failure:" + e);
111: }
112: }
113: }
|