001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.naming.deployment.annotation;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Method;
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import javax.annotation.Resource;
026: import javax.annotation.Resources;
027:
028: import org.apache.geronimo.j2ee.deployment.annotation.ResourceAnnotationHelper;
029: import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedWebApp;
030: import org.apache.geronimo.naming.deployment.EnvironmentEntryBuilder;
031: import org.apache.geronimo.naming.deployment.SwitchingServiceRefBuilder;
032: import org.apache.geronimo.testsupport.XmlBeansTestSupport;
033: import org.apache.geronimo.xbeans.javaee.WebAppDocument;
034: import org.apache.geronimo.xbeans.javaee.WebAppType;
035: import org.apache.xmlbeans.XmlObject;
036: import org.apache.xbean.finder.ClassFinder;
037: import org.apache.xmlbeans.XmlOptions;
038:
039: /**
040: * Testcases for each of the various AnnotationHelper class
041: */
042: public class AnnotationHelperTest extends XmlBeansTestSupport {
043:
044: private Class[] classes = { ResourceAnnotationTest.class };
045:
046: private ClassFinder classFinder = new ClassFinder(classes);
047: private ClassLoader classLoader = this .getClass().getClassLoader();
048: private XmlOptions options = new XmlOptions();
049:
050: public void testResourceAnnotationHelper() throws Exception {
051:
052: //-------------------------------------------------
053: // Ensure annotations are discovered correctly
054: //-------------------------------------------------
055: List<Class> annotatedClasses = classFinder
056: .findAnnotatedClasses(Resources.class);
057: assertNotNull(annotatedClasses);
058: assertEquals(1, annotatedClasses.size());
059: assertTrue(annotatedClasses
060: .contains(ResourceAnnotationTest.class));
061:
062: List<Method> annotatedMethods = classFinder
063: .findAnnotatedMethods(Resource.class);
064: assertNotNull(annotatedMethods);
065: assertEquals(2, annotatedMethods.size());
066: assertTrue(annotatedMethods
067: .contains(ResourceAnnotationTest.class
068: .getDeclaredMethod("setAnnotatedMethod1",
069: new Class[] { String.class })));
070: assertTrue(annotatedMethods
071: .contains(ResourceAnnotationTest.class
072: .getDeclaredMethod("setAnnotatedMethod2",
073: new Class[] { String.class })));
074:
075: List<Field> annotatedFields = classFinder
076: .findAnnotatedFields(Resource.class);
077: assertNotNull(annotatedFields);
078: assertEquals(2, annotatedFields.size());
079: assertTrue(annotatedFields
080: .contains(ResourceAnnotationTest.class
081: .getDeclaredField("annotatedField1")));
082: assertTrue(annotatedFields
083: .contains(ResourceAnnotationTest.class
084: .getDeclaredField("annotatedField2")));
085:
086: //-------------------------------------------------
087: // Ensure annotations are processed correctly
088: //-------------------------------------------------
089: //
090: // 2. env-entry
091: //
092: URL srcXML = classLoader
093: .getResource("annotation/empty-web-src.xml");
094: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
095: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
096: .changeType(WebAppDocument.type);
097: WebAppType webApp = webAppDoc.getWebApp();
098: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
099: ResourceAnnotationHelper.processAnnotations(annotatedWebApp,
100: classFinder,
101: EnvironmentEntryBuilder.EnvEntryRefProcessor.INSTANCE);
102: URL expectedXML = classLoader
103: .getResource("annotation/env-entry-expected.xml");
104: XmlObject expected = XmlObject.Factory.parse(expectedXML);
105: log.debug("[@Resource <env-entry> Source XML] " + '\n'
106: + webApp.toString() + '\n');
107: log.debug("[@Resource <env-entry> Expected XML]" + '\n'
108: + expected.toString() + '\n');
109: List problems = new ArrayList();
110: boolean ok = compareXmlObjects(webApp, expected, problems);
111: assertTrue("Differences: " + problems, ok);
112: //
113: // 3. service-ref
114: //
115: srcXML = classLoader
116: .getResource("annotation/empty-web-src.xml");
117: xmlObject = XmlObject.Factory.parse(srcXML, options);
118: webAppDoc = (WebAppDocument) xmlObject
119: .changeType(WebAppDocument.type);
120: webApp = webAppDoc.getWebApp();
121: annotatedWebApp = new AnnotatedWebApp(webApp);
122: ResourceAnnotationHelper
123: .processAnnotations(
124: annotatedWebApp,
125: classFinder,
126: SwitchingServiceRefBuilder.ServiceRefProcessor.INSTANCE);
127: expectedXML = classLoader
128: .getResource("annotation/service-ref-expected.xml");
129: expected = XmlObject.Factory.parse(expectedXML);
130: log.debug("[@Resource <service-ref> Source XML] " + '\n'
131: + webApp.toString() + '\n');
132: log.debug("[@Resource <service-ref> Expected XML]" + '\n'
133: + expected.toString() + '\n');
134: problems = new ArrayList();
135: ok = compareXmlObjects(webApp, expected, problems);
136: assertTrue("Differences: " + problems, ok);
137: }
138:
139: }
|