001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.collections;
020:
021: import java.util.Arrays;
022: import java.util.Collection;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.jorphan.logging.LoggingManager;
027: import org.apache.log.Logger;
028:
029: public class PackageTest extends TestCase {
030: public PackageTest(String name) {
031: super (name);
032: }
033:
034: public void testAdd1() throws Exception {
035: Logger log = LoggingManager.getLoggerForClass();
036: Collection treePath = Arrays.asList(new String[] { "1", "2",
037: "3", "4" });
038: HashTree tree = new HashTree();
039: log.debug("treePath = " + treePath);
040: tree.add(treePath, "value");
041: log.debug("Now treePath = " + treePath);
042: log.debug(tree.toString());
043: assertEquals(1, tree.list(treePath).size());
044: assertEquals("value", tree.getArray(treePath)[0]);
045: }
046:
047: public void testEqualsAndHashCode1() throws Exception {
048: HashTree tree1 = new HashTree("abcd");
049: HashTree tree2 = new HashTree("abcd");
050: HashTree tree3 = new HashTree("abcde");
051: HashTree tree4 = new HashTree("abcde");
052:
053: assertTrue(tree1.equals(tree1));
054: assertTrue(tree1.equals(tree2));
055: assertTrue(tree2.equals(tree1));
056: assertTrue(tree2.equals(tree2));
057: assertTrue(tree1.hashCode() == tree2.hashCode());
058:
059: assertTrue(tree3.equals(tree3));
060: assertTrue(tree3.equals(tree4));
061: assertTrue(tree4.equals(tree3));
062: assertTrue(tree4.equals(tree4));
063: assertTrue(tree3.hashCode() == tree4.hashCode());
064:
065: assertNotSame(tree1, tree2);
066: assertNotSame(tree1, tree3);
067: assertNotSame(tree1, tree4);
068: assertNotSame(tree2, tree3);
069: assertNotSame(tree2, tree4);
070:
071: assertFalse(tree1.equals(tree3));
072: assertFalse(tree1.equals(tree4));
073: assertFalse(tree2.equals(tree3));
074: assertFalse(tree2.equals(tree4));
075:
076: assertNotNull(tree1);
077: assertNotNull(tree2);
078:
079: tree1.add("abcd", tree3);
080: assertFalse(tree1.equals(tree2));
081: assertFalse(tree2.equals(tree1));// Check reflexive
082: if (tree1.hashCode() == tree2.hashCode()) {
083: // This is not a requirement
084: System.out
085: .println("WARN: unequal HashTrees should not have equal hashCodes");
086: }
087: tree2.add("abcd", tree4);
088: assertTrue(tree1.equals(tree2));
089: assertTrue(tree2.equals(tree1));
090: assertTrue(tree1.hashCode() == tree2.hashCode());
091: }
092:
093: public void testAddObjectAndTree() throws Exception {
094: ListedHashTree tree = new ListedHashTree("key");
095: ListedHashTree newTree = new ListedHashTree("value");
096: tree.add("key", newTree);
097: assertEquals(tree.list().size(), 1);
098: assertEquals("key", tree.getArray()[0]);
099: assertEquals(1, tree.getTree("key").list().size());
100: assertEquals(0, tree.getTree("key").getTree("value").size());
101: assertEquals(tree.getTree("key").getArray()[0], "value");
102: assertNotNull(tree.getTree("key").get("value"));
103: }
104:
105: public void testEqualsAndHashCode2() throws Exception {
106: ListedHashTree tree1 = new ListedHashTree("abcd");
107: ListedHashTree tree2 = new ListedHashTree("abcd");
108: ListedHashTree tree3 = new ListedHashTree("abcde");
109: ListedHashTree tree4 = new ListedHashTree("abcde");
110:
111: assertTrue(tree1.equals(tree1));
112: assertTrue(tree1.equals(tree2));
113: assertTrue(tree2.equals(tree1));
114: assertTrue(tree2.equals(tree2));
115: assertTrue(tree1.hashCode() == tree2.hashCode());
116:
117: assertTrue(tree3.equals(tree3));
118: assertTrue(tree3.equals(tree4));
119: assertTrue(tree4.equals(tree3));
120: assertTrue(tree4.equals(tree4));
121: assertTrue(tree3.hashCode() == tree4.hashCode());
122:
123: assertNotSame(tree1, tree2);
124: assertNotSame(tree1, tree3);
125: assertFalse(tree1.equals(tree3));
126: assertFalse(tree3.equals(tree1));
127: assertFalse(tree1.equals(tree4));
128: assertFalse(tree4.equals(tree1));
129:
130: assertFalse(tree2.equals(tree3));
131: assertFalse(tree3.equals(tree2));
132: assertFalse(tree2.equals(tree4));
133: assertFalse(tree4.equals(tree2));
134:
135: tree1.add("abcd", tree3);
136: assertFalse(tree1.equals(tree2));
137: assertFalse(tree2.equals(tree1));
138:
139: tree2.add("abcd", tree4);
140: assertTrue(tree1.equals(tree2));
141: assertTrue(tree2.equals(tree1));
142: assertTrue(tree1.hashCode() == tree2.hashCode());
143:
144: tree1.add("a1");
145: tree1.add("a2");
146: // tree1.add("a3");
147: tree2.add("a2");
148: tree2.add("a1");
149:
150: assertFalse(tree1.equals(tree2));
151: assertFalse(tree2.equals(tree1));
152: if (tree1.hashCode() == tree2.hashCode()) {
153: // This is not a requirement
154: System.out
155: .println("WARN: unequal ListedHashTrees should not have equal hashcodes");
156:
157: }
158:
159: tree4.add("abcdef");
160: assertFalse(tree3.equals(tree4));
161: assertFalse(tree4.equals(tree3));
162: }
163:
164: public void testSearch() throws Exception {
165: ListedHashTree tree = new ListedHashTree();
166: SearchByClass searcher = new SearchByClass(Integer.class);
167: String one = "one";
168: String two = "two";
169: Integer o = new Integer(1);
170: tree.add(one, o);
171: tree.getTree(one).add(o, two);
172: tree.traverse(searcher);
173: assertEquals(1, searcher.getSearchResults().size());
174: }
175:
176: }
|