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: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.tree;
021:
022: import javax.swing.SwingTestCase;
023:
024: public class TreePathTest extends SwingTestCase {
025: protected TreePath treePath;
026:
027: @Override
028: protected void setUp() throws Exception {
029: super .setUp();
030: }
031:
032: @Override
033: protected void tearDown() throws Exception {
034: super .tearDown();
035: }
036:
037: /*
038: * Test method for 'javax.swing.tree.TreePath.TreePath(Object)'
039: */
040: public void testTreePathObject() {
041: Object path1 = new Object();
042: Object path2 = "new Object()";
043: try {
044: treePath = new TreePath((Object) null);
045: fail("exception hasn't been thrown");
046: } catch (IllegalArgumentException e) {
047: }
048: treePath = new TreePath(path1);
049: assertEquals(1, treePath.getPathCount());
050: assertEquals(path1, treePath.getPath()[0]);
051: treePath = new TreePath(path2);
052: assertEquals(1, treePath.getPathCount());
053: assertEquals(path2, treePath.getPath()[0]);
054: }
055:
056: /*
057: * Test method for 'javax.swing.tree.TreePath.TreePath()'
058: */
059: public void testTreePath() {
060: treePath = new TreePath();
061: assertEquals(1, treePath.getPathCount());
062: assertNull(treePath.getPath()[0]);
063: }
064:
065: /*
066: * Test method for 'javax.swing.tree.TreePath.TreePath(Object[], int)'
067: */
068: public void testTreePathObjectArrayInt() {
069: Object[] path1 = new Object[] { new Object(), "1", "2" };
070: treePath = new TreePath(path1, 3);
071: assertEquals(3, treePath.getPathCount());
072: assertNotSame(path1, treePath.getPath());
073: assertEquals(path1[0], treePath.getPath()[0]);
074: assertEquals(path1[1], treePath.getPath()[1]);
075: assertEquals(path1[2], treePath.getPath()[2]);
076: treePath = new TreePath(path1, 1);
077: assertEquals(1, treePath.getPathCount());
078: assertNotSame(path1, treePath.getPath());
079: assertEquals(path1[0], treePath.getPath()[0]);
080: boolean thrown = false;
081: try {
082: treePath = new TreePath(path1, 4);
083: } catch (ArrayIndexOutOfBoundsException e) {
084: thrown = true;
085: }
086: assertTrue(thrown);
087: }
088:
089: /*
090: * Test method for 'javax.swing.tree.TreePath.TreePath(Object[])'
091: */
092: public void testTreePathObjectArray() {
093: Object[] path1 = new Object[] { new Object(), "1", "2" };
094: Object[] path2 = new Object[] { new Object(), "11", "22", "33",
095: "2222" };
096: try {
097: treePath = new TreePath((Object[]) null);
098: fail("exception hasn't been thrown");
099: } catch (IllegalArgumentException e) {
100: }
101: try {
102: treePath = new TreePath(new Object[0]);
103: fail("exception hasn't been thrown");
104: } catch (IllegalArgumentException e) {
105: }
106: treePath = new TreePath(path1);
107: assertEquals(3, treePath.getPathCount());
108: assertNotSame(path1, treePath.getPath());
109: assertEquals(path1[0], treePath.getPath()[0]);
110: assertEquals(path1[1], treePath.getPath()[1]);
111: assertEquals(path1[2], treePath.getPath()[2]);
112: treePath = new TreePath(path2);
113: assertEquals(5, treePath.getPathCount());
114: assertNotSame(path2, treePath.getPath());
115: assertEquals(path2[0], treePath.getPath()[0]);
116: assertEquals(path2[1], treePath.getPath()[1]);
117: assertEquals(path2[2], treePath.getPath()[2]);
118: assertEquals(path2[3], treePath.getPath()[3]);
119: assertEquals(path2[4], treePath.getPath()[4]);
120: }
121:
122: /*
123: * Test method for 'javax.swing.tree.TreePath.TreePath(TreePath, Object)'
124: */
125: public void testTreePathTreePathObject() {
126: Object last1 = "3";
127: Object last2 = "345";
128: Object[] path1 = new Object[] { new Object(), "1", "2" };
129: Object[] path2 = new Object[] { new Object(), "11", "22", "33",
130: "2222" };
131: TreePath parent = new TreePath(path1);
132: treePath = new TreePath(parent, last1);
133: assertEquals(4, treePath.getPathCount());
134: assertNotSame(parent.getPath(), treePath.getPath());
135: assertEquals(path1[0], treePath.getPath()[0]);
136: assertEquals(path1[1], treePath.getPath()[1]);
137: assertEquals(path1[2], treePath.getPath()[2]);
138: assertEquals(last1, treePath.getPath()[3]);
139: parent = new TreePath(path2);
140: treePath = new TreePath(parent, last2);
141: assertEquals(6, treePath.getPathCount());
142: assertNotSame(parent.getPath(), treePath.getPath());
143: assertEquals(path2[0], treePath.getPath()[0]);
144: assertEquals(path2[1], treePath.getPath()[1]);
145: assertEquals(path2[2], treePath.getPath()[2]);
146: assertEquals(path2[3], treePath.getPath()[3]);
147: assertEquals(path2[4], treePath.getPath()[4]);
148: assertEquals(last2, treePath.getPath()[5]);
149: treePath = new TreePath(null, last2);
150: assertEquals(1, treePath.getPathCount());
151: assertEquals(last2, treePath.getPath()[0]);
152: boolean thrown = false;
153: try {
154: treePath = new TreePath(parent, null);
155: } catch (IllegalArgumentException e) {
156: thrown = true;
157: }
158: assertTrue(thrown);
159: }
160:
161: /*
162: * Test method for 'javax.swing.tree.TreePath.equals(Object)'
163: */
164: public void testEqualsObject() {
165: Object o1 = "1";
166: Object o2 = "2";
167: Object o3 = "3";
168: Object o4 = "4";
169: TreePath path1 = new TreePath(new Object[] { o1, o2, o3, o4 });
170: TreePath path2 = new TreePath(new Object[] { o1, o2, o3, o4 });
171: TreePath path3 = new TreePath(new Object[] { o4, o3, o1, o2 });
172: TreePath path4 = new TreePath(new Object[] { o1, o2, o3 });
173: TreePath path5 = new TreePath(new Object[] { o2 });
174: assertTrue(path1.equals(path2));
175: assertFalse(path1.equals(path3));
176: assertFalse(path4.equals(path3));
177: assertFalse(path4.equals(null));
178: assertFalse(path5.equals(o2));
179: TreePath path51 = new TreePath(new Object[] { new String("2") });
180: assertTrue(path5.equals(path51));
181: }
182:
183: /*
184: * Test method for 'javax.swing.tree.TreePath.getLastPathComponent()'
185: */
186: public void testGetLastPathComponent() {
187: Object[] path1 = new Object[] { new Object(), "1", "2" };
188: Object[] path2 = new Object[] { new Object(), "11", "22", "33",
189: "2222" };
190: treePath = new TreePath(path1);
191: assertEquals(path1[2], treePath.getLastPathComponent());
192: treePath = new TreePath(path2);
193: assertEquals(path2[4], treePath.getLastPathComponent());
194: }
195:
196: /*
197: * Test method for 'javax.swing.tree.TreePath.getParentPath()'
198: */
199: public void testGetParentPath() {
200: Object[] path1 = new Object[] { new Object(), "1", "2" };
201: Object[] path2 = new Object[] { new Object(), "11", "22", "33",
202: "2222" };
203: treePath = new TreePath(path1);
204: TreePath parent = treePath.getParentPath();
205: assertEquals(2, parent.getPathCount());
206: assertNotSame(parent.getPath(), treePath.getPath());
207: assertEquals(path1[0], parent.getPath()[0]);
208: assertEquals(path1[1], parent.getPath()[1]);
209: treePath = new TreePath(path2);
210: parent = treePath.getParentPath();
211: assertEquals(4, parent.getPathCount());
212: assertNotSame(parent.getPath(), treePath.getPath());
213: assertEquals(path2[0], parent.getPath()[0]);
214: assertEquals(path2[1], parent.getPath()[1]);
215: assertEquals(path2[2], parent.getPath()[2]);
216: assertEquals(path2[3], parent.getPath()[3]);
217: treePath = new TreePath(path1[0]);
218: parent = treePath.getParentPath();
219: assertNull(parent);
220: }
221:
222: /*
223: * is being tested by constructor's tests
224: */
225: public void testGetPath() {
226: }
227:
228: /*
229: * Test method for 'javax.swing.tree.TreePath.getPathComponent(int)'
230: */
231: public void testGetPathComponent() {
232: Object[] path1 = new Object[] { new Object(), "1", "2" };
233: Object[] path2 = new Object[] { new Object(), "11", "22", "33",
234: "2222" };
235: treePath = new TreePath(path1);
236: assertEquals(path1[0], treePath.getPathComponent(0));
237: assertEquals(path1[1], treePath.getPathComponent(1));
238: assertEquals(path1[2], treePath.getPathComponent(2));
239: treePath = new TreePath(path2);
240: assertEquals(path2[0], treePath.getPathComponent(0));
241: assertEquals(path2[1], treePath.getPathComponent(1));
242: assertEquals(path2[2], treePath.getPathComponent(2));
243: assertEquals(path2[3], treePath.getPathComponent(3));
244: assertEquals(path2[4], treePath.getPathComponent(4));
245: boolean thrown = false;
246: try {
247: treePath.getPathComponent(-1);
248: } catch (IllegalArgumentException e) {
249: thrown = true;
250: }
251: assertTrue(thrown);
252: thrown = false;
253: try {
254: treePath.getPathComponent(5);
255: } catch (IllegalArgumentException e) {
256: thrown = true;
257: }
258: assertTrue(thrown);
259: }
260:
261: /*
262: * is being tested by constructor's tests
263: */
264: public void testGetPathCount() {
265: }
266:
267: /*
268: * Test method for 'javax.swing.tree.TreePath.isDescendant(TreePath)'
269: */
270: public void testIsDescendant() {
271: Object o1 = "1";
272: Object o2 = "2";
273: Object o3 = "3";
274: Object o4 = "4";
275: TreePath path1 = new TreePath(new Object[] { o1, o2, o3, o4 });
276: TreePath path2 = new TreePath(new Object[] { o1, o2, o3, o4 });
277: TreePath path3 = new TreePath(new Object[] { o4, o3, o1, o2 });
278: TreePath path4 = new TreePath(new Object[] { o1, o2, o3 });
279: TreePath path5 = new TreePath(new Object[] { o2 });
280: TreePath path6 = new TreePath(new Object[] { o4 });
281: assertTrue(path1.isDescendant(path2));
282: assertTrue(path2.isDescendant(path1));
283: assertFalse(path2.isDescendant(path3));
284: assertFalse(path3.isDescendant(path1));
285: assertFalse(path2.isDescendant(path4));
286: assertTrue(path4.isDescendant(path2));
287: assertFalse(path5.isDescendant(path1));
288: assertTrue(path6.isDescendant(path3));
289: assertFalse(path5.isDescendant(null));
290: }
291:
292: /*
293: * Test method for 'javax.swing.tree.TreePath.pathByAddingChild(Object)'
294: */
295: public void testPathByAddingChild() {
296: Object last1 = "3";
297: Object last2 = "345";
298: Object[] path1 = new Object[] { new Object(), "1", "2" };
299: Object[] path2 = new Object[] { new Object(), "11", "22", "33",
300: "2222" };
301: TreePath parent = new TreePath(path1);
302: treePath = parent.pathByAddingChild(last1);
303: assertEquals(4, treePath.getPathCount());
304: assertNotSame(parent.getPath(), treePath.getPath());
305: assertEquals(path1[0], treePath.getPath()[0]);
306: assertEquals(path1[1], treePath.getPath()[1]);
307: assertEquals(path1[2], treePath.getPath()[2]);
308: assertEquals(last1, treePath.getPath()[3]);
309: parent = new TreePath(path2);
310: treePath = parent.pathByAddingChild(last2);
311: assertEquals(6, treePath.getPathCount());
312: assertNotSame(parent.getPath(), treePath.getPath());
313: assertEquals(path2[0], treePath.getPath()[0]);
314: assertEquals(path2[1], treePath.getPath()[1]);
315: assertEquals(path2[2], treePath.getPath()[2]);
316: assertEquals(path2[3], treePath.getPath()[3]);
317: assertEquals(path2[4], treePath.getPath()[4]);
318: assertEquals(last2, treePath.getPath()[5]);
319: parent = new TreePath(last1);
320: treePath = parent.pathByAddingChild(last2);
321: assertEquals(2, treePath.getPathCount());
322: boolean thrown = false;
323: try {
324: treePath = parent.pathByAddingChild(null);
325: } catch (NullPointerException e) {
326: thrown = true;
327: }
328: assertTrue(thrown);
329: }
330:
331: /*
332: * Test method for 'javax.swing.tree.TreePath.hashCode()'
333: */
334: public void testHashCode() {
335: Object last = "o4";
336: TreePath path1 = new TreePath(new Object[] { "o1", "o2", "o3",
337: last });
338: assertEquals(path1.hashCode(), last.hashCode());
339: }
340:
341: /*
342: * Test method for 'javax.swing.tree.TreePath.toString()'
343: */
344: public void testToString() {
345: TreePath path1 = new TreePath(new Object[] { "o1", "o2", "o3",
346: "o4" });
347: TreePath path2 = new TreePath(new Object[] { "o1", "o2", null });
348: assertNotNull(path1.toString());
349: assertNotNull(path2.toString());
350: }
351: }
|