1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.search.attribute;
18
19 import java.util.Vector;
20
21 import junit.framework.TestCase;
22 import net.sf.ehcache.Element;
23
24 public class JavaBeanAttributeExtractorTest extends TestCase {
25
26 public void testBasic() {
27 JavaBeanAttributeExtractor jbae = new JavaBeanAttributeExtractor("foo");
28
29 assertEquals("foo", jbae.attributeFor(new Element("", new Type1()), ""));
30 assertEquals("foo", jbae.attributeFor(new Element(null, new Type1()), ""));
31 assertEquals("foo", jbae.attributeFor(new Element(new Type1(), ""), ""));
32 assertEquals("foo", jbae.attributeFor(new Element(new Type1(), null), ""));
33
34 assertEquals(true, jbae.attributeFor(new Element(new Type2(), ""), ""));
35 assertEquals(true, jbae.attributeFor(new Element("", new Type2()), ""));
36
37 assertEquals(true, jbae.attributeFor(new Element(new Type3(), ""), ""));
38 assertEquals(true, jbae.attributeFor(new Element("", new Type3()), ""));
39 }
40
41 public void testException() {
42 JavaBeanAttributeExtractor jbae = new JavaBeanAttributeExtractor("foo");
43
44 RuntimeException re = new RuntimeException();
45 try {
46 jbae.attributeFor(new Element(new ExceptionThrowing(re), ""), "");
47 fail();
48 } catch (AttributeExtractorException aee) {
49 assertEquals(re, aee.getCause());
50 }
51
52 Error error = new Error();
53 try {
54 jbae.attributeFor(new Element(new ExceptionThrowing(error), ""), "");
55 fail();
56 } catch (Error e) {
57 assertEquals(error, e);
58 }
59 }
60
61 public void testNonAccessibleMethod() {
62 JavaBeanAttributeExtractor jbae = new JavaBeanAttributeExtractor("foo");
63
64 try {
65 jbae.attributeFor(new Element(new NonAccessible(), ""), "");
66 fail();
67 } catch (AttributeExtractorException aee) {
68
69 }
70
71 try {
72 jbae.attributeFor(new Element("", new NonAccessible()), "");
73 fail();
74 } catch (AttributeExtractorException aee) {
75
76 }
77 }
78
79 public void testMultiThreads() throws InterruptedException {
80 final JavaBeanAttributeExtractor jbae = new JavaBeanAttributeExtractor("foo");
81 final Vector<Throwable> errors = new Vector<Throwable>();
82
83 Thread[] threads = new Thread[4];
84 for (int i = 0; i < threads.length; i++) {
85 threads[i] = new Thread() {
86 @Override
87 public void run() {
88 try {
89 for (int j = 0; j < 50000; j++) {
90 assertEquals("foo", jbae.attributeFor(new Element("", new Type1()), ""));
91 assertEquals("foo", jbae.attributeFor(new Element(null, new Type1()), ""));
92 assertEquals("foo", jbae.attributeFor(new Element(new Type1(), ""), ""));
93 assertEquals("foo", jbae.attributeFor(new Element(new Type1(), null), ""));
94
95 assertEquals(true, jbae.attributeFor(new Element(new Type2(), ""), ""));
96 assertEquals(true, jbae.attributeFor(new Element("", new Type2()), ""));
97
98 assertEquals(true, jbae.attributeFor(new Element(new Type3(), ""), ""));
99 assertEquals(true, jbae.attributeFor(new Element("", new Type3()), ""));
100 }
101 } catch (Throwable t) {
102 t.printStackTrace();
103 errors.add(t);
104 }
105 }
106
107 ;
108 };
109 }
110
111 for (Thread thread : threads) {
112 thread.start();
113 }
114
115 for (Thread thread : threads) {
116 thread.join();
117 }
118
119 assertEquals(0, errors.size());
120 }
121
122 public void testParentType() {
123 JavaBeanAttributeExtractor jbae1 = new JavaBeanAttributeExtractor("bar");
124 JavaBeanAttributeExtractor jbae2 = new JavaBeanAttributeExtractor("wrapper");
125 JavaBeanAttributeExtractor jbae3 = new JavaBeanAttributeExtractor("primitive");
126
127 assertEquals("bar", jbae1.attributeFor(new Element(new Type4(), ""), ""));
128 assertEquals("bar", jbae1.attributeFor(new Element("", new Type4()), ""));
129
130 assertEquals(true, jbae2.attributeFor(new Element("", new Type4()), ""));
131 assertEquals(true, jbae2.attributeFor(new Element(new Type4(), ""), ""));
132
133 assertEquals(true, jbae3.attributeFor(new Element("", new Type4()), ""));
134 assertEquals(true, jbae3.attributeFor(new Element(new Type4(), ""), ""));
135 }
136
137 public void testAmbiguous() {
138 JavaBeanAttributeExtractor jbae = new JavaBeanAttributeExtractor("foo");
139
140 try {
141 jbae.attributeFor(new Element(new Type1(), new Type1()), "");
142 fail();
143 } catch (AttributeExtractorException aee) {
144
145 }
146 }
147
148 public void testNoMethods() {
149 JavaBeanAttributeExtractor jbae = new JavaBeanAttributeExtractor("foo");
150
151 try {
152 jbae.attributeFor(new Element(new Object(), ""), "");
153 fail();
154 } catch (AttributeExtractorException aee) {
155
156 }
157
158 try {
159 jbae.attributeFor(new Element("", new Object()), "");
160 fail();
161 } catch (AttributeExtractorException aee) {
162
163 }
164
165 try {
166 jbae.attributeFor(new Element(null, null), "");
167 fail();
168 } catch (AttributeExtractorException aee) {
169
170 }
171 }
172
173 public void testIllegalArgs() {
174 try {
175 new JavaBeanAttributeExtractor(null);
176 fail();
177 } catch (NullPointerException npe) {
178
179 }
180
181 try {
182 new JavaBeanAttributeExtractor("");
183 fail();
184 } catch (IllegalArgumentException iae) {
185
186 }
187 }
188
189 private static class Type1 {
190 public Object getFoo() {
191 return "foo";
192 }
193 }
194
195 private static class Type2 {
196 public boolean isFoo() {
197 return true;
198 }
199 }
200
201 private static class Type3 {
202 public Boolean isFoo() {
203 return true;
204 }
205 }
206
207 private static abstract class Parent {
208 public boolean isPrimitive() {
209 return true;
210 }
211
212 public Boolean isWrapper() {
213 return true;
214 }
215
216 public Object getBar() {
217 return "bar";
218 }
219 }
220
221 private static class Type4 extends Parent {
222
223 }
224
225 private static class NonAccessible {
226 Object getFoo() {
227 return "foo";
228 }
229 }
230
231 private static class ExceptionThrowing {
232 private final Throwable t;
233
234 public ExceptionThrowing(Throwable t) {
235 this.t = t;
236 }
237
238 public Object getFoo() {
239 HackExceptionThrower.t.set(t);
240
241 try {
242 HackExceptionThrower.class.newInstance();
243 throw new AssertionError();
244 } catch (InstantiationException e) {
245 throw new AssertionError();
246 } catch (IllegalAccessException e) {
247 throw new AssertionError();
248 }
249 }
250 }
251
252 private static class HackExceptionThrower {
253 public static final ThreadLocal<Throwable> t = new ThreadLocal<Throwable>();
254
255 public HackExceptionThrower() throws Throwable {
256 throw t.get();
257 }
258 }
259
260 }