01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.config;
18:
19: import junit.framework.TestCase;
20:
21: import javax.persistence.PersistenceContext;
22: import javax.persistence.PersistenceContextType;
23: import javax.persistence.PersistenceContexts;
24: import javax.persistence.PersistenceProperty;
25: import java.lang.reflect.Field;
26: import java.lang.reflect.Method;
27: import java.util.Map;
28: import java.util.HashMap;
29:
30: public class PersistenceContextAnnFactoryTest extends TestCase {
31: public void test() throws Exception {
32: Field useAsm = PersistenceContextAnnFactory.class
33: .getDeclaredField("useAsm");
34: useAsm.setAccessible(true);
35: useAsm.set(null, true);
36:
37: PersistenceContextAnnFactory factory = new PersistenceContextAnnFactory();
38: factory.addAnnotations(Foo.class);
39:
40: for (PersistenceContext annotation : Foo.class.getAnnotation(
41: PersistenceContexts.class).value()) {
42: assertEq(annotation, factory.create(annotation, null));
43: }
44:
45: PersistenceContext annotation = Foo.class
46: .getAnnotation(PersistenceContext.class);
47: assertEq(annotation, factory.create(annotation, null));
48:
49: for (Field field : Foo.class.getFields()) {
50: annotation = field.getAnnotation(PersistenceContext.class);
51: assertEq(annotation, factory.create(annotation,
52: new AnnotationDeployer.FieldMember(field)));
53: }
54:
55: for (Method method : Foo.class.getMethods()) {
56: annotation = method.getAnnotation(PersistenceContext.class);
57: if (annotation != null) {
58: assertEq(annotation, factory.create(annotation,
59: new AnnotationDeployer.MethodMember(method)));
60: }
61: }
62: }
63:
64: private static void assertEq(PersistenceContext annotation,
65: PersistenceContextAnn wrapper) {
66: if (annotation.name().length() > 0) {
67: assertEquals(annotation.name(), wrapper.name());
68: }
69: assertEquals(annotation.unitName(), wrapper.unitName());
70: assertEquals(annotation.type().toString(), wrapper.type());
71:
72: Map<String, String> properties = new HashMap<String, String>();
73: for (PersistenceProperty property : annotation.properties()) {
74: properties.put(property.name(), property.value());
75: }
76: assertEquals(properties, wrapper.properties());
77: }
78:
79: @PersistenceContexts({@PersistenceContext(name="classPCs1",unitName="CPCs1u",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="classPCs1-1",value="CPCs1"),@PersistenceProperty(name="classPCs1-2",value="CPCs2")}),@PersistenceContext(name="classPCs2",unitName="CPCs2u",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="classPCs2-1",value="CPCs1"),@PersistenceProperty(name="classPCs2-2",value="CPCs2")})})
80: @PersistenceContext(name="class",unitName="cu",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="class1",value="c1"),@PersistenceProperty(name="class2",value="c2")})
81: public static class Foo {
82: @PersistenceContext(name="field",unitName="fu",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="field1",value="f1"),@PersistenceProperty(name="field2",value="f1")})
83: public Object field;
84:
85: @PersistenceContext(name="method",unitName="mu",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="method1",value="m1"),@PersistenceProperty(name="method2",value="m2")})
86: public void method() {
87: }
88:
89: @PersistenceContext(unitName="myfu",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="myField1",value="myf1"),@PersistenceProperty(name="myField2",value="myf1")})
90: public Object myField;
91:
92: @PersistenceContext(unitName="mymu",type=PersistenceContextType.EXTENDED,properties={@PersistenceProperty(name="myMethod1",value="mym1"),@PersistenceProperty(name="myMethod2",value="mym2")})
93: public void setMyMethod() {
94: }
95: }
96: }
|