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.connector.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.connector.deployment.ResourceRefBuilder;
030: import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedWebApp;
031: import org.apache.geronimo.testsupport.XmlBeansTestSupport;
032: import org.apache.geronimo.xbeans.javaee.WebAppDocument;
033: import org.apache.geronimo.xbeans.javaee.WebAppType;
034: import org.apache.xmlbeans.XmlObject;
035: import org.apache.xbean.finder.ClassFinder;
036: import org.apache.xmlbeans.XmlOptions;
037:
038: /**
039: * Testcases for each of the various AnnotationHelper class
040: */
041: public class AnnotationHelperTest extends XmlBeansTestSupport {
042:
043: private Class[] classes = { ResourceAnnotationTest.class };
044:
045: private ClassFinder classFinder = new ClassFinder(classes);
046: private ClassLoader classLoader = this .getClass().getClassLoader();
047: private XmlOptions options = new XmlOptions();
048:
049: public void testResourceAnnotationHelper() throws Exception {
050:
051: //-------------------------------------------------
052: // Ensure annotations are discovered correctly
053: //-------------------------------------------------
054: List<Class> annotatedClasses = classFinder
055: .findAnnotatedClasses(Resources.class);
056: assertNotNull(annotatedClasses);
057: assertEquals(1, annotatedClasses.size());
058: assertTrue(annotatedClasses
059: .contains(ResourceAnnotationTest.class));
060:
061: List<Method> annotatedMethods = classFinder
062: .findAnnotatedMethods(Resource.class);
063: assertNotNull(annotatedMethods);
064: assertEquals(2, annotatedMethods.size());
065: assertTrue(annotatedMethods
066: .contains(ResourceAnnotationTest.class
067: .getDeclaredMethod("setAnnotatedMethod1",
068: new Class[] { String.class })));
069: assertTrue(annotatedMethods
070: .contains(ResourceAnnotationTest.class
071: .getDeclaredMethod("setAnnotatedMethod2",
072: new Class[] { String.class })));
073:
074: List<Field> annotatedFields = classFinder
075: .findAnnotatedFields(Resource.class);
076: assertNotNull(annotatedFields);
077: assertEquals(2, annotatedFields.size());
078: assertTrue(annotatedFields
079: .contains(ResourceAnnotationTest.class
080: .getDeclaredField("annotatedField1")));
081: assertTrue(annotatedFields
082: .contains(ResourceAnnotationTest.class
083: .getDeclaredField("annotatedField2")));
084:
085: //-------------------------------------------------
086: // Ensure annotations are processed correctly
087: //-------------------------------------------------
088: //
089: // 1. resource-ref
090: //
091: URL srcXML = classLoader
092: .getResource("annotation/empty-web-src.xml");
093: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
094: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
095: .changeType(WebAppDocument.type);
096: WebAppType webApp = webAppDoc.getWebApp();
097: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
098: ResourceAnnotationHelper.processAnnotations(annotatedWebApp,
099: classFinder,
100: ResourceRefBuilder.ResourceRefProcessor.INSTANCE);
101: URL expectedXML = classLoader
102: .getResource("annotation/resource-ref-expected.xml");
103: XmlObject expected = XmlObject.Factory.parse(expectedXML);
104: log.debug("[@Resource <resource-ref> Source XML] " + '\n'
105: + webApp.toString() + '\n');
106: log.debug("[@Resource <resource-ref> Expected XML]" + '\n'
107: + expected.toString() + '\n');
108: List problems = new ArrayList();
109: boolean ok = compareXmlObjects(webApp, expected, problems);
110: assertTrue("Differences: " + problems, ok);
111: }
112: }
|