1 16 17 package org.springframework.core; 18 19 import java.util.AbstractList ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import junit.framework.TestCase; 26 27 30 public class ReflectiveVisitorHelperTests extends TestCase { 31 32 private ReflectiveVisitorHelper support; 33 34 public void setUp() { 35 support = new ReflectiveVisitorHelper(); 36 } 37 38 public void testDirectDefaultLookup() { 39 ensureVisit(new DirectMockVisitor(), new ArrayList ()); 40 } 41 42 public void testDirectDefaultLookupCache() { 43 DirectMockVisitor visitor = new DirectMockVisitor(); 44 ensureVisit(visitor, new ArrayList ()); 45 visitor.visited = false; 46 ensureVisit(visitor, new ArrayList ()); 47 } 48 49 public void testSuperClassDefaultLookup() { 50 ensureVisit(new SuperClassMockVisitor(), new ArrayList ()); 51 } 52 53 public void testPackagePrivateDefaultLookup() { 54 ensureVisit(new PackagePrivateMockVisitor(), new ArrayList ()); 55 } 56 57 public void testSuperClassSuperInterfaceDefaultLookup() { 58 ensureVisit(new SuperClassSuperInterfaceMockVisitor(), new ArrayList ()); 59 } 60 61 public void testInterfaceDefaultLookup() { 62 ensureVisit(new InterfaceMockVisitor(), new ArrayList ()); 63 } 64 65 public void testInnerClassArgument() { 66 Map.Entry entry = new Map.Entry () { 67 public Object getKey() { 68 return null; 69 } 70 71 public Object getValue() { 72 return null; 73 } 74 75 public Object setValue(Object value) { 76 return null; 77 } 78 }; 79 ensureVisit(new DirectMockVisitor(), entry); 80 } 81 82 public void testInheritedValueLookup() { 83 ensureVisit(new InheritedArgMockVisitor(), new ArrayList ()); 84 } 85 86 public void testNullArgument() { 87 ensureVisit(new NullVisitor(), null); 88 } 89 90 public void testInvisibleClassVisitor() { 91 ensureVisit(new InvisibleClassVisitor(), new ArrayList ()); 92 } 93 94 public void testNullVisitor() { 95 try { 96 support.invokeVisit(null, new Object ()); 97 fail("null visitor was accepted"); 98 } 99 catch (IllegalArgumentException e) { 100 } 102 catch (Exception e) { 103 fail("null visitor was not handled properly"); 104 } 105 } 106 107 private void ensureVisit(AbstractMockVisitor visitor, Object argument) { 108 support.invokeVisit(visitor, argument); 109 assertTrue("Visitor was not visited!", visitor.visited); 110 } 111 112 public abstract class AbstractMockVisitor { 113 boolean visited; 114 } 115 116 public class DirectMockVisitor extends AbstractMockVisitor { 117 public void visit(ArrayList value) { 118 visited = true; 119 } 120 121 public void visit(Map.Entry entry) { 122 visited = true; 123 } 124 } 125 126 public class InheritedArgMockVisitor extends AbstractMockVisitor { 127 public void visit(Object value) { 128 visited = true; 129 } 130 } 131 132 public class SuperClassMockVisitor extends AbstractMockVisitor { 133 public void visit(AbstractList value) { 134 visited = true; 135 } 136 } 137 138 public class SuperClassSuperInterfaceMockVisitor extends AbstractMockVisitor { 139 public void visit(Collection value) { 140 visited = true; 141 } 142 } 143 144 public class InterfaceMockVisitor extends AbstractMockVisitor { 145 public void visit(List value) { 146 visited = true; 147 } 148 } 149 150 public class PackagePrivateMockVisitor extends AbstractMockVisitor { 151 void visit(ArrayList value) { 152 visited = true; 153 } 154 } 155 156 private class InvisibleClassVisitor extends AbstractMockVisitor { 157 public void visit(ArrayList list) { 158 visited = true; 159 } 160 } 161 162 public class NullVisitor extends AbstractMockVisitor { 163 public void visitNull() { 164 visited = true; 165 } 166 } 167 168 } 169 | Popular Tags |