KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > ReflectiveVisitorHelperTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core;
18
19 import java.util.AbstractList JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import junit.framework.TestCase;
26
27 /**
28  * @author Keith Donald
29  */

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 JavaDoc());
40     }
41
42     public void testDirectDefaultLookupCache() {
43         DirectMockVisitor visitor = new DirectMockVisitor();
44         ensureVisit(visitor, new ArrayList JavaDoc());
45         visitor.visited = false;
46         ensureVisit(visitor, new ArrayList JavaDoc());
47     }
48
49     public void testSuperClassDefaultLookup() {
50         ensureVisit(new SuperClassMockVisitor(), new ArrayList JavaDoc());
51     }
52
53     public void testPackagePrivateDefaultLookup() {
54         ensureVisit(new PackagePrivateMockVisitor(), new ArrayList JavaDoc());
55     }
56
57     public void testSuperClassSuperInterfaceDefaultLookup() {
58         ensureVisit(new SuperClassSuperInterfaceMockVisitor(), new ArrayList JavaDoc());
59     }
60
61     public void testInterfaceDefaultLookup() {
62         ensureVisit(new InterfaceMockVisitor(), new ArrayList JavaDoc());
63     }
64
65     public void testInnerClassArgument() {
66         Map.Entry JavaDoc entry = new Map.Entry JavaDoc() {
67             public Object JavaDoc getKey() {
68                 return null;
69             }
70
71             public Object JavaDoc getValue() {
72                 return null;
73             }
74
75             public Object JavaDoc setValue(Object JavaDoc value) {
76                 return null;
77             }
78         };
79         ensureVisit(new DirectMockVisitor(), entry);
80     }
81
82     public void testInheritedValueLookup() {
83         ensureVisit(new InheritedArgMockVisitor(), new ArrayList JavaDoc());
84     }
85
86     public void testNullArgument() {
87         ensureVisit(new NullVisitor(), null);
88     }
89
90     public void testInvisibleClassVisitor() {
91         ensureVisit(new InvisibleClassVisitor(), new ArrayList JavaDoc());
92     }
93
94     public void testNullVisitor() {
95         try {
96             support.invokeVisit(null, new Object JavaDoc());
97             fail("null visitor was accepted");
98         }
99         catch (IllegalArgumentException JavaDoc e) {
100             // correct behavior.
101
}
102         catch (Exception JavaDoc e) {
103             fail("null visitor was not handled properly");
104         }
105     }
106
107     private void ensureVisit(AbstractMockVisitor visitor, Object JavaDoc 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 JavaDoc value) {
118             visited = true;
119         }
120
121         public void visit(Map.Entry JavaDoc entry) {
122             visited = true;
123         }
124     }
125
126     public class InheritedArgMockVisitor extends AbstractMockVisitor {
127         public void visit(Object JavaDoc value) {
128             visited = true;
129         }
130     }
131
132     public class SuperClassMockVisitor extends AbstractMockVisitor {
133         public void visit(AbstractList JavaDoc value) {
134             visited = true;
135         }
136     }
137
138     public class SuperClassSuperInterfaceMockVisitor extends AbstractMockVisitor {
139         public void visit(Collection JavaDoc value) {
140             visited = true;
141         }
142     }
143
144     public class InterfaceMockVisitor extends AbstractMockVisitor {
145         public void visit(List JavaDoc value) {
146             visited = true;
147         }
148     }
149
150     public class PackagePrivateMockVisitor extends AbstractMockVisitor {
151         void visit(ArrayList JavaDoc value) {
152             visited = true;
153         }
154     }
155
156     private class InvisibleClassVisitor extends AbstractMockVisitor {
157         public void visit(ArrayList JavaDoc 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