1 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 ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.LinkedHashMap ; 17 import java.util.LinkedList ; 18 import java.util.Map ; 19 import java.util.TreeMap ; 20 21 import junit.framework.TestCase; 22 23 public class BasicWalkerTest extends TestCase { 24 25 public void test() throws IOException { 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 output = sink.buffer.toString(); 40 System.err.println(output); 41 validate(output); 42 } 43 44 private void validate(String output) throws IOException { 45 String expected = getExpected(); 46 47 expected = expected.replaceAll("\r", ""); 48 output = output.replaceAll("\r", ""); 49 50 assertEquals(expected, output); 51 } 52 53 private String getExpected() throws IOException { 54 String resource = ClassUtils.getShortClassName(getClass()) + "-output.txt"; 55 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 56 57 InputStream 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 e) { 69 throw new RuntimeException (e); 70 } 71 } 72 } 73 74 baos.flush(); 75 return new String (baos.toByteArray()); 76 } 77 78 private static class MyOutputSink implements PrintVisitor.OutputSink { 79 final StringBuffer buffer = new StringBuffer (); 80 81 public void output(String line) { 82 buffer.append(line + "\n"); 83 } 84 } 85 86 private static class MyValueFormatter implements PrintVisitor.ValueFormatter { 87 88 public String format(Object o) { 89 if (o instanceof String ) { return "\"" + o + "\""; } 90 return String.valueOf(o); 91 } 92 93 public String 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 val = value.getValueObject(); 103 104 if ((val == null) || val.getClass().isPrimitive()) { return false; } 105 106 if (val.getClass() == String .class) return false; 107 if (val.getClass() == Class .class) return false; 108 109 if (val.getClass() == Byte .class) return false; 110 if (val.getClass() == Boolean .class) return false; 111 if (val.getClass() == Character .class) return false; 112 if (val.getClass() == Double .class) return false; 113 if (val.getClass() == Integer .class) return false; 114 if (val.getClass() == Long .class) return false; 115 if (val.getClass() == Short .class) return false; 116 if (val.getClass() == Float .class) return false; 117 118 return true; 119 } 120 } 121 122 private static class Root { 123 int i = 12; 124 Map m = new LinkedHashMap (); 125 String s = "root"; 126 Object b = new B(); 127 int[] intArray = new int[] { 1, 2, 3 }; 128 Object [] objArray = new Object [] { this, new Foo(), new Integer (3) }; 129 Object self = this; 130 Object [][] multi = new Object [][] { { "timmy", new Integer (4) }, { null, null }, { null, this } }; 131 132 double[] emptyArray = new double[] {}; 133 HashMap emptyMap = new HashMap (); 134 LinkedList emptyList = new LinkedList (); 135 136 Class clazz = getClass(); 137 138 Collection c = new ArrayList (); 139 { 140 c.add(new Foo(new Foo())); 141 Map tm = new TreeMap (); 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 (Math.PI)); 148 } 149 } 150 151 private static class B extends A { 152 private int i = 1; 154 } 155 156 private static class A { 157 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 |