KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > walker > BasicWalkerTest


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

5 package com.tc.object.walker;
6
7 import org.apache.commons.io.IOUtils;
8 import org.apache.commons.lang.ClassUtils;
9
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.LinkedHashMap JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.TreeMap JavaDoc;
20
21 import junit.framework.TestCase;
22
23 public class BasicWalkerTest extends TestCase {
24
25   public void test() throws IOException JavaDoc {
26     Root r = new Root();
27     r.m.put("timmy", new Foo());
28     r.m.put("yo", null);
29     r.m.put("foo", new Foo());
30     r.m.put("foo foo", new Foo(new Foo()));
31
32     WalkTest test = new MyWalkTestImpl();
33
34     MyOutputSink sink = new MyOutputSink();
35
36     ObjectGraphWalker t = new ObjectGraphWalker(r, test, new PrintVisitor(sink, test, new MyValueFormatter()));
37     t.walk();
38
39     String JavaDoc output = sink.buffer.toString();
40     System.err.println(output);
41     validate(output);
42   }
43
44   private void validate(String JavaDoc output) throws IOException JavaDoc {
45     String JavaDoc expected = getExpected();
46
47     expected = expected.replaceAll("\r", "");
48     output = output.replaceAll("\r", "");
49
50     assertEquals(expected, output);
51   }
52
53   private String JavaDoc getExpected() throws IOException JavaDoc {
54     String JavaDoc resource = ClassUtils.getShortClassName(getClass()) + "-output.txt";
55     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
56
57     InputStream JavaDoc in = null;
58     try {
59       in = getClass().getResourceAsStream(resource);
60       if (in == null) {
61         fail("missing resource: " + resource);
62       }
63       IOUtils.copy(in, baos);
64     } finally {
65       if (in != null) {
66         try {
67           in.close();
68         } catch (Exception JavaDoc e) {
69           throw new RuntimeException JavaDoc(e);
70         }
71       }
72     }
73
74     baos.flush();
75     return new String JavaDoc(baos.toByteArray());
76   }
77
78   private static class MyOutputSink implements PrintVisitor.OutputSink {
79     final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
80
81     public void output(String JavaDoc line) {
82       buffer.append(line + "\n");
83     }
84   }
85
86   private static class MyValueFormatter implements PrintVisitor.ValueFormatter {
87
88     public String JavaDoc format(Object JavaDoc o) {
89       if (o instanceof String JavaDoc) { return "\"" + o + "\""; }
90       return String.valueOf(o);
91     }
92
93     public String JavaDoc valueAdornment(MemberValue value) {
94       return null;
95     }
96
97   }
98
99   private static class MyWalkTestImpl implements WalkTest {
100
101     public boolean shouldTraverse(MemberValue value) {
102       Object JavaDoc val = value.getValueObject();
103
104       if ((val == null) || val.getClass().isPrimitive()) { return false; }
105
106       if (val.getClass() == String JavaDoc.class) return false;
107       if (val.getClass() == Class JavaDoc.class) return false;
108
109       if (val.getClass() == Byte JavaDoc.class) return false;
110       if (val.getClass() == Boolean JavaDoc.class) return false;
111       if (val.getClass() == Character JavaDoc.class) return false;
112       if (val.getClass() == Double JavaDoc.class) return false;
113       if (val.getClass() == Integer JavaDoc.class) return false;
114       if (val.getClass() == Long JavaDoc.class) return false;
115       if (val.getClass() == Short JavaDoc.class) return false;
116       if (val.getClass() == Float JavaDoc.class) return false;
117
118       return true;
119     }
120   }
121
122   private static class Root {
123     int i = 12;
124     Map JavaDoc m = new LinkedHashMap JavaDoc();
125     String JavaDoc s = "root";
126     Object JavaDoc b = new B();
127     int[] intArray = new int[] { 1, 2, 3 };
128     Object JavaDoc[] objArray = new Object JavaDoc[] { this, new Foo(), new Integer JavaDoc(3) };
129     Object JavaDoc self = this;
130     Object JavaDoc[][] multi = new Object JavaDoc[][] { { "timmy", new Integer JavaDoc(4) }, { null, null }, { null, this } };
131
132     double[] emptyArray = new double[] {};
133     HashMap JavaDoc emptyMap = new HashMap JavaDoc();
134     LinkedList JavaDoc emptyList = new LinkedList JavaDoc();
135
136     Class JavaDoc clazz = getClass();
137
138     Collection JavaDoc c = new ArrayList JavaDoc();
139     {
140       c.add(new Foo(new Foo()));
141       Map JavaDoc tm = new TreeMap JavaDoc();
142       tm.put("key", new Foo());
143       tm.put("k", null);
144       c.add(tm);
145       c.add(c);
146       c.add(null);
147       c.add(new Double JavaDoc(Math.PI));
148     }
149   }
150
151   private static class B extends A {
152     // this variable is "shadowed" on purpose, please do not rename them to remove the eclipse warning
153
private int i = 1;
154   }
155
156   private static class A {
157     // this variable is "shadowed" on purpose, please do not rename them to remove the eclipse warning
158
private int i = 0;
159   }
160
161   private static class Foo {
162     private final int i = next();
163
164     private final Foo next;
165
166     public Foo() {
167       this(null);
168     }
169
170     public Foo(Foo foo) {
171       this.next = foo;
172     }
173
174     public int getI() {
175       return i;
176     }
177
178     public Foo getNext() {
179       return next;
180     }
181
182     private static int num = 0;
183
184     private synchronized static int next() {
185       return num++;
186     }
187   }
188
189 }
190
Popular Tags