KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > TraverserTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object;
6
7 import com.tc.config.schema.setup.ConfigurationSetupException;
8 import com.tc.object.bytecode.MockClassProvider;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.field.TCFieldFactory;
11
12 import java.io.Serializable JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.LinkedList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * @author steve
19  */

20 public class TraverserTest extends BaseDSOTestCase {
21
22   public void testTraverse() throws Exception JavaDoc {
23     System.out.println("Starting");
24     TestA ta1 = new TestA(null, null);
25     TestB tb1 = new TestB(ta1, null, ta1, null);
26     TestA ta2 = new TestA(ta1, tb1);
27     final ArrayList JavaDoc results = new ArrayList JavaDoc();
28     new Traverser(new TraversalAction() {
29       public void visit(List JavaDoc objects) {
30         System.out.println("Adding:" + objects);
31         results.addAll(objects);
32       }
33     }, new TestPortableObjectProvider()).traverse(ta2);
34     assertTrue("Expected 3 but got:" + results.size(), results.size() == 3);
35     assertTrue(results.contains(ta1));
36     assertTrue(results.contains(ta2));
37     assertTrue(results.contains(tb1));
38
39     String JavaDoc[] strings = new String JavaDoc[] { "one", "two", "three" };
40     new Traverser(new TraversalAction() {
41       public void visit(List JavaDoc objects) {
42         results.add(objects);
43       }
44     }, new TestPortableObjectProvider()).traverse(strings);
45
46     // Test stack overflows don't happen
47
final LinkedList JavaDoc list = new LinkedList JavaDoc();
48     System.out.println("Adding");
49     for (int i = 0; i < 100000; i++) {
50       list.add(new Object JavaDoc());
51     }
52     try {
53       new Traverser(new TraversalAction() {
54         public void visit(List JavaDoc objects) {
55           //
56
}
57       }, new TestPortableObjectProvider()).traverse(list);
58       System.out.println("Traversed");
59       assertTrue(true);
60     } catch (StackOverflowError JavaDoc e) {
61       assertTrue(false);
62     }
63   }
64
65   public static void main(String JavaDoc[] args) {
66     //
67
}
68
69   private class TestPortableObjectProvider implements PortableObjectProvider {
70     TCClassFactory cf;
71
72     public TestPortableObjectProvider() throws ConfigurationSetupException {
73       DSOClientConfigHelper config = configHelper();
74       cf = new TCClassFactoryImpl(new TCFieldFactory(config), config, new MockClassProvider());
75     }
76
77     public TraversedReferences getPortableObjects(Class JavaDoc clazz, Object JavaDoc start, TraversedReferences addTo) {
78       Object JavaDoc[] values = new Object JavaDoc[0];
79       if (start instanceof TestB) {
80         TestB tb = (TestB) start;
81         values = new Object JavaDoc[] { tb.ta, tb.tb, tb.tc, tb.td };
82       } else if (start instanceof TestA) {
83         TestA tb = (TestA) start;
84         values = new Object JavaDoc[] { tb.ta, tb.tb };
85       }
86       for (int i = 0; i < values.length; i++) {
87         addTo.addAnonymousReference(values[i]);
88       }
89       return addTo;
90     }
91
92   }
93
94   private static class TestA implements Serializable JavaDoc {
95     public TestA ta;
96
97     public TestB tb;
98
99     public boolean tboolean;
100
101     public String JavaDoc aString = "aString";
102
103     public TestA(TestA ta, TestB tb) {
104       this.ta = ta;
105       this.tb = tb;
106       if (false) {
107         this.ta.equals(null);
108         this.tb.equals(null);
109         if (tboolean) {/**/
110         }
111         this.aString.equals(null);
112       }
113     }
114
115     public void setTA(TestA ta) {
116       this.ta = ta;
117     }
118
119     public void setTB(TestB tb) {
120       this.tb = tb;
121     }
122   }
123
124   private static class TestB extends TestA implements Serializable JavaDoc {
125     private TestA tc;
126
127     private TestB td;
128
129     private String JavaDoc bstring = "bstring";
130
131     public TestB(TestA ta, TestB tb, TestA tc, TestB td) {
132       super(ta, tb);
133       this.tc = tc;
134       this.td = td;
135       if (false) {
136         this.tc.equals(null);
137         this.td.equals(null);
138         this.bstring.equals(null);
139       }
140     }
141
142     public void setTC(TestA tc) {
143       this.tc = tc;
144     }
145
146     public void setTD(TestB td) {
147       this.td = td;
148     }
149   }
150 }
Popular Tags