1 package com.sri.emo.dbobj.model_tree;
2
3 import java.util.Iterator;
4 import junit.framework.TestCase;
5
6 /***
7 * Tests the tree model and its functionality.
8 * @author Michael Rimov
9 */
10 public class TestDefaultModel extends TestCase {
11 private Model defaultModel = null;
12 private ModelTreeFactoryTest factory = new ModelTreeFactoryTest();
13
14 protected void setUp() throws Exception {
15 super.setUp();
16 defaultModel = factory.buildTestModel();
17 }
18
19 protected void tearDown() throws Exception {
20 defaultModel = null;
21 super.tearDown();
22 }
23
24 public void testGetRoot() {
25 assertTrue(defaultModel.getRoot() != null);
26 assertTrue(defaultModel.getRoot().getVisitable() == ModelTreeFactoryTest.rootViewable);
27 assertTrue(defaultModel.getRoot().getParent() == ModelNode.NO_PARENT);
28 }
29
30 public void testValidateTreeStructure() {
31 ModelNode root = defaultModel.getRoot();
32 assertTrue(root.getChildren().size() == ModelTreeFactoryTest.NUM_LEVEL_1);
33 for (Iterator i = root.getChildren().iterator(); i.hasNext();) {
34
35 ModelNode level1 = (ModelNode)i.next();
36 assertTrue(level1.getVisitable() == ModelTreeFactoryTest.level1Viewable);
37 assertTrue(level1.getParent() == root);
38 assertTrue(level1.getChildren().size() == ModelTreeFactoryTest.NUM_LEVEL_2);
39
40
41 for (Iterator j = level1.getChildren().iterator(); j.hasNext();) {
42 ModelNode level2 = (ModelNode)j.next();
43 assertTrue(level2.getVisitable() == ModelTreeFactoryTest.level2Viewable);
44 assertTrue(level2.getParent() == level1);
45 assertTrue(level2.getChildren().size() == 0);
46 assertTrue(level2.getChildren() == ModelNode.NO_CHILDREN);
47 }
48 }
49 }
50
51 public void testIterator() {
52 Iterator iterator = defaultModel.iterator();
53 assertTrue(iterator.hasNext());
54
55 ModelNode root = (ModelNode)iterator.next();
56 assertTrue(root.getVisitable() == ModelTreeFactoryTest.rootViewable);
57
58 int numNodesFound = 0;
59 for (int i = 0; i < ModelTreeFactoryTest.NUM_LEVEL_1; i++) {
60 assertTrue(iterator.hasNext());
61 ModelNode level1 = (ModelNode)iterator.next();
62 numNodesFound++;
63 assertTrue(level1 != null);
64 assertTrue(level1.getVisitable() == ModelTreeFactoryTest.level1Viewable);
65 for (int j = 0; j < ModelTreeFactoryTest.NUM_LEVEL_2; j++) {
66 ModelNode level2 = (ModelNode)iterator.next();
67 numNodesFound++;
68 assertTrue(level2 != null);
69 assertTrue(level2.getVisitable() == ModelTreeFactoryTest.level2Viewable);
70 }
71 }
72
73 assertFalse(iterator.hasNext());
74 assertTrue(numNodesFound == (ModelTreeFactoryTest.NUM_LEVEL_1 * ModelTreeFactoryTest.NUM_LEVEL_2)
75 + ModelTreeFactoryTest.NUM_LEVEL_1);
76
77 }
78
79 public void testIteratorThrowsExceptionPastEndOfList() {
80 Iterator iterator = defaultModel.iterator();
81 while(iterator.hasNext()) {
82 iterator.next();
83 }
84
85 try {
86 iterator.next();
87 fail("next() should have thrown an exception");
88 } catch (java.lang.ArrayIndexOutOfBoundsException e) {
89
90 }
91
92 }
93
94 public void testIteratingEmptyTreeStructure() {
95 ModelNode root = factory.buildEmptyModel().getRoot();
96 assertTrue(root.getParent() == ModelNode.NO_PARENT);
97 assertTrue(root.getChildren() == ModelNode.NO_CHILDREN);
98 assertTrue(root.getChildren().size() == 0);
99 Iterator i = factory.buildEmptyModel().iterator();
100 i.next();
101 assertFalse(i.hasNext());
102 }
103
104
105 }