1 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 ; 13 import java.util.ArrayList ; 14 import java.util.LinkedList ; 15 import java.util.List ; 16 17 20 public class TraverserTest extends BaseDSOTestCase { 21 22 public void testTraverse() throws Exception { 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 results = new ArrayList (); 28 new Traverser(new TraversalAction() { 29 public void visit(List 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 [] strings = new String [] { "one", "two", "three" }; 40 new Traverser(new TraversalAction() { 41 public void visit(List objects) { 42 results.add(objects); 43 } 44 }, new TestPortableObjectProvider()).traverse(strings); 45 46 final LinkedList list = new LinkedList (); 48 System.out.println("Adding"); 49 for (int i = 0; i < 100000; i++) { 50 list.add(new Object ()); 51 } 52 try { 53 new Traverser(new TraversalAction() { 54 public void visit(List objects) { 55 } 57 }, new TestPortableObjectProvider()).traverse(list); 58 System.out.println("Traversed"); 59 assertTrue(true); 60 } catch (StackOverflowError e) { 61 assertTrue(false); 62 } 63 } 64 65 public static void main(String [] args) { 66 } 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 clazz, Object start, TraversedReferences addTo) { 78 Object [] values = new Object [0]; 79 if (start instanceof TestB) { 80 TestB tb = (TestB) start; 81 values = new Object [] { tb.ta, tb.tb, tb.tc, tb.td }; 82 } else if (start instanceof TestA) { 83 TestA tb = (TestA) start; 84 values = new Object [] { 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 { 95 public TestA ta; 96 97 public TestB tb; 98 99 public boolean tboolean; 100 101 public String 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 { 125 private TestA tc; 126 127 private TestB td; 128 129 private String 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 |