001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object;
006:
007: import com.tc.config.schema.setup.ConfigurationSetupException;
008: import com.tc.object.bytecode.MockClassProvider;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.field.TCFieldFactory;
011:
012: import java.io.Serializable;
013: import java.util.ArrayList;
014: import java.util.LinkedList;
015: import java.util.List;
016:
017: /**
018: * @author steve
019: */
020: public class TraverserTest extends BaseDSOTestCase {
021:
022: public void testTraverse() throws Exception {
023: System.out.println("Starting");
024: TestA ta1 = new TestA(null, null);
025: TestB tb1 = new TestB(ta1, null, ta1, null);
026: TestA ta2 = new TestA(ta1, tb1);
027: final ArrayList results = new ArrayList();
028: new Traverser(new TraversalAction() {
029: public void visit(List objects) {
030: System.out.println("Adding:" + objects);
031: results.addAll(objects);
032: }
033: }, new TestPortableObjectProvider()).traverse(ta2);
034: assertTrue("Expected 3 but got:" + results.size(), results
035: .size() == 3);
036: assertTrue(results.contains(ta1));
037: assertTrue(results.contains(ta2));
038: assertTrue(results.contains(tb1));
039:
040: String[] strings = new String[] { "one", "two", "three" };
041: new Traverser(new TraversalAction() {
042: public void visit(List objects) {
043: results.add(objects);
044: }
045: }, new TestPortableObjectProvider()).traverse(strings);
046:
047: // Test stack overflows don't happen
048: final LinkedList list = new LinkedList();
049: System.out.println("Adding");
050: for (int i = 0; i < 100000; i++) {
051: list.add(new Object());
052: }
053: try {
054: new Traverser(new TraversalAction() {
055: public void visit(List objects) {
056: //
057: }
058: }, new TestPortableObjectProvider()).traverse(list);
059: System.out.println("Traversed");
060: assertTrue(true);
061: } catch (StackOverflowError e) {
062: assertTrue(false);
063: }
064: }
065:
066: public static void main(String[] args) {
067: //
068: }
069:
070: private class TestPortableObjectProvider implements
071: PortableObjectProvider {
072: TCClassFactory cf;
073:
074: public TestPortableObjectProvider()
075: throws ConfigurationSetupException {
076: DSOClientConfigHelper config = configHelper();
077: cf = new TCClassFactoryImpl(new TCFieldFactory(config),
078: config, new MockClassProvider());
079: }
080:
081: public TraversedReferences getPortableObjects(Class clazz,
082: Object start, TraversedReferences addTo) {
083: Object[] values = new Object[0];
084: if (start instanceof TestB) {
085: TestB tb = (TestB) start;
086: values = new Object[] { tb.ta, tb.tb, tb.tc, tb.td };
087: } else if (start instanceof TestA) {
088: TestA tb = (TestA) start;
089: values = new Object[] { tb.ta, tb.tb };
090: }
091: for (int i = 0; i < values.length; i++) {
092: addTo.addAnonymousReference(values[i]);
093: }
094: return addTo;
095: }
096:
097: }
098:
099: private static class TestA implements Serializable {
100: public TestA ta;
101:
102: public TestB tb;
103:
104: public boolean tboolean;
105:
106: public String aString = "aString";
107:
108: public TestA(TestA ta, TestB tb) {
109: this .ta = ta;
110: this .tb = tb;
111: if (false) {
112: this .ta.equals(null);
113: this .tb.equals(null);
114: if (tboolean) {/**/
115: }
116: this .aString.equals(null);
117: }
118: }
119:
120: public void setTA(TestA ta) {
121: this .ta = ta;
122: }
123:
124: public void setTB(TestB tb) {
125: this .tb = tb;
126: }
127: }
128:
129: private static class TestB extends TestA implements Serializable {
130: private TestA tc;
131:
132: private TestB td;
133:
134: private String bstring = "bstring";
135:
136: public TestB(TestA ta, TestB tb, TestA tc, TestB td) {
137: super (ta, tb);
138: this .tc = tc;
139: this .td = td;
140: if (false) {
141: this .tc.equals(null);
142: this .td.equals(null);
143: this .bstring.equals(null);
144: }
145: }
146:
147: public void setTC(TestA tc) {
148: this .tc = tc;
149: }
150:
151: public void setTD(TestB td) {
152: this.td = td;
153: }
154: }
155: }
|