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.j2ee.deployment.annotation;
017:
018: import java.lang.annotation.Annotation;
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Method;
021: import java.net.URL;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collections;
025: import java.util.Comparator;
026: import java.util.List;
027:
028: import javax.annotation.security.DeclareRoles;
029: import javax.annotation.security.RunAs;
030: import javax.ejb.EJB;
031: import javax.ejb.EJBs;
032: import javax.jws.HandlerChain;
033: import javax.persistence.PersistenceContext;
034: import javax.persistence.PersistenceContexts;
035: import javax.persistence.PersistenceUnit;
036: import javax.persistence.PersistenceUnits;
037: import javax.xml.ws.WebServiceRef;
038: import javax.xml.ws.WebServiceRefs;
039:
040: import org.apache.geronimo.kernel.config.Configuration;
041: import org.apache.geronimo.testsupport.XmlBeansTestSupport;
042: import org.apache.geronimo.xbeans.javaee.WebAppDocument;
043: import org.apache.geronimo.xbeans.javaee.WebAppType;
044: import org.apache.xbean.finder.ClassFinder;
045: import org.apache.xmlbeans.XmlCursor;
046: import org.apache.xmlbeans.XmlException;
047: import org.apache.xmlbeans.XmlObject;
048: import org.apache.xmlbeans.XmlOptions;
049:
050: /**
051: * Testcases for each of the various AnnotationHelper class
052: */
053: public class AnnotationHelperTest extends XmlBeansTestSupport {
054:
055: private Class[] classes = { EJBAnnotationTest.class,
056: HandlerChainAnnotationTest.class,
057: PersistenceContextAnnotationTest.class,
058: PersistenceUnitAnnotationTest.class,
059: WebServiceRefAnnotationTest.class,
060: SecurityAnnotationTest.class };
061:
062: private ClassFinder classFinder = new TestClassFinder(classes);
063: private ClassLoader classLoader = this .getClass().getClassLoader();
064: private XmlOptions options = new XmlOptions();
065:
066: static class TestClassFinder extends ClassFinder {
067: public TestClassFinder(Class[] classes) {
068: super (classes);
069: }
070:
071: public List<Field> findAnnotatedFields(
072: Class<? extends Annotation> arg) {
073: List<Field> fields = super .findAnnotatedFields(arg);
074: Collections.sort(fields, new FieldComparator());
075: return fields;
076: }
077: }
078:
079: static class FieldComparator implements Comparator {
080: public int compare(Object o1, Object o2) {
081: return compare((Field) o1, (Field) o2);
082: }
083:
084: public int compare(Field o1, Field o2) {
085: String field1 = o1.getDeclaringClass().getName() + "/"
086: + o1.getName();
087: String field2 = o2.getDeclaringClass().getName() + "/"
088: + o2.getName();
089: return field1.compareTo(field2);
090: }
091: }
092:
093: public void testEJBAnnotationHelper() throws Exception {
094:
095: //-------------------------------------------------
096: // Ensure annotations are discovered correctly
097: //-------------------------------------------------
098: List<Class> annotatedClasses = classFinder
099: .findAnnotatedClasses(EJBs.class);
100: assertNotNull(annotatedClasses);
101: assertEquals(1, annotatedClasses.size());
102: assertTrue(annotatedClasses.contains(EJBAnnotationTest.class));
103:
104: List<Method> annotatedMethods = classFinder
105: .findAnnotatedMethods(EJB.class);
106: assertNotNull(annotatedMethods);
107: assertEquals(2, annotatedMethods.size());
108: assertTrue(annotatedMethods.contains(EJBAnnotationTest.class
109: .getDeclaredMethod("setAnnotatedMethod1",
110: new Class[] { int.class })));
111: assertTrue(annotatedMethods.contains(EJBAnnotationTest.class
112: .getDeclaredMethod("setAnnotatedMethod2",
113: new Class[] { String.class })));
114:
115: List<Field> annotatedFields = classFinder
116: .findAnnotatedFields(EJB.class);
117: assertNotNull(annotatedFields);
118: assertEquals(2, annotatedFields.size());
119: assertTrue(annotatedFields.contains(EJBAnnotationTest.class
120: .getDeclaredField("annotatedField1")));
121: assertTrue(annotatedFields.contains(EJBAnnotationTest.class
122: .getDeclaredField("annotatedField2")));
123:
124: //-------------------------------------------------
125: // Ensure annotations are processed correctly
126: //-------------------------------------------------
127: URL srcXML = classLoader
128: .getResource("annotation/empty-web-src.xml");
129: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
130: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
131: .changeType(WebAppDocument.type);
132: WebAppType webApp = webAppDoc.getWebApp();
133: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
134: EJBAnnotationHelper.processAnnotations(annotatedWebApp,
135: classFinder);
136: URL expectedXML = classLoader
137: .getResource("annotation/ejb-expected.xml");
138: XmlObject expected = XmlObject.Factory.parse(expectedXML);
139: log.debug("[@EJB Source XML] " + '\n' + webApp.toString()
140: + '\n');
141: log.debug("[@EJB Expected XML]" + '\n' + expected.toString()
142: + '\n');
143: List problems = new ArrayList();
144: boolean ok = compareXmlObjects(webApp, expected, problems);
145: assertTrue("Differences: " + problems, ok);
146: }
147:
148: public void testHandlerChainAnnotationHelper() throws Exception {
149:
150: //-------------------------------------------------
151: // Ensure annotations are discovered correctly
152: //-------------------------------------------------
153: List<Class> annotatedClasses = classFinder
154: .findAnnotatedClasses(HandlerChain.class);
155: assertNotNull(annotatedClasses);
156: assertEquals(1, annotatedClasses.size());
157: assertTrue(annotatedClasses
158: .contains(HandlerChainAnnotationTest.class));
159:
160: List<Method> annotatedMethods = classFinder
161: .findAnnotatedMethods(HandlerChain.class);
162: assertNotNull(annotatedMethods);
163: assertEquals(3, annotatedMethods.size());
164: assertTrue(annotatedMethods
165: .contains(HandlerChainAnnotationTest.class
166: .getDeclaredMethod("setAnnotatedMethod1",
167: new Class[] { String.class })));
168: assertTrue(annotatedMethods
169: .contains(HandlerChainAnnotationTest.class
170: .getDeclaredMethod("setAnnotatedMethod2",
171: new Class[] { int.class })));
172:
173: List<Field> annotatedFields = classFinder
174: .findAnnotatedFields(HandlerChain.class);
175: assertNotNull(annotatedFields);
176: assertEquals(2, annotatedFields.size());
177: assertTrue(annotatedFields
178: .contains(HandlerChainAnnotationTest.class
179: .getDeclaredField("annotatedField1")));
180: assertTrue(annotatedFields
181: .contains(HandlerChainAnnotationTest.class
182: .getDeclaredField("annotatedField2")));
183:
184: //-------------------------------------------------
185: // Ensure annotations are processed correctly
186: //-------------------------------------------------
187: URL srcXML = classLoader
188: .getResource("annotation/handler-chain-src.xml");
189: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
190: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
191: .changeType(WebAppDocument.type);
192: WebAppType webApp = webAppDoc.getWebApp();
193: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
194: HandlerChainAnnotationHelper.processAnnotations(
195: annotatedWebApp, classFinder);
196: URL expectedXML = classLoader
197: .getResource("annotation/handler-chain-expected.xml");
198: XmlObject expected = XmlObject.Factory.parse(expectedXML);
199: log.debug("[@HandlerChain Source XML] " + '\n'
200: + webApp.toString() + '\n');
201: log.debug("[@HandlerChain Expected XML]" + '\n'
202: + expected.toString() + '\n');
203: List problems = new ArrayList();
204: boolean ok = compareXmlObjects(webApp, expected, problems);
205: assertTrue("Differences: " + problems, ok);
206: }
207:
208: public void testPersistenceContextAnnotationHelper()
209: throws Exception {
210:
211: //-------------------------------------------------
212: // Ensure annotations are discovered correctly
213: //-------------------------------------------------
214: List<Class> annotatedClasses = classFinder
215: .findAnnotatedClasses(PersistenceContexts.class);
216: assertNotNull(annotatedClasses);
217: assertEquals(1, annotatedClasses.size());
218: assertTrue(annotatedClasses
219: .contains(PersistenceContextAnnotationTest.class));
220:
221: List<Method> annotatedMethods = classFinder
222: .findAnnotatedMethods(PersistenceContext.class);
223: assertNotNull(annotatedMethods);
224: assertEquals(2, annotatedMethods.size());
225: assertTrue(annotatedMethods
226: .contains(PersistenceContextAnnotationTest.class
227: .getDeclaredMethod("setAnnotatedMethod1",
228: new Class[] { String.class })));
229: assertTrue(annotatedMethods
230: .contains(PersistenceContextAnnotationTest.class
231: .getDeclaredMethod("setAnnotatedMethod2",
232: new Class[] { String.class })));
233:
234: List<Field> annotatedFields = classFinder
235: .findAnnotatedFields(PersistenceContext.class);
236: assertNotNull(annotatedFields);
237: assertEquals(2, annotatedFields.size());
238: assertTrue(annotatedFields
239: .contains(PersistenceContextAnnotationTest.class
240: .getDeclaredField("annotatedField1")));
241: assertTrue(annotatedFields
242: .contains(PersistenceContextAnnotationTest.class
243: .getDeclaredField("annotatedField2")));
244:
245: //-------------------------------------------------
246: // Ensure annotations are processed correctly
247: //-------------------------------------------------
248: URL srcXML = classLoader
249: .getResource("annotation/empty-web-src.xml");
250: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
251: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
252: .changeType(WebAppDocument.type);
253: WebAppType webApp = webAppDoc.getWebApp();
254: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
255: PersistenceContextAnnotationHelper.processAnnotations(
256: annotatedWebApp, classFinder);
257: URL expectedXML = classLoader
258: .getResource("annotation/persistence-context-expected.xml");
259: XmlObject expected = XmlObject.Factory.parse(expectedXML);
260: log.debug("[@PersistenceContext Source XML] " + '\n'
261: + webApp.toString() + '\n');
262: log.debug("[@PersistenceContext Expected XML]" + '\n'
263: + expected.toString() + '\n');
264: List problems = new ArrayList();
265: boolean ok = compareXmlObjects(webApp, expected, problems);
266: assertTrue("Differences: " + problems, ok);
267: }
268:
269: public void testPersistenceUnitAnnotationHelper() throws Exception {
270:
271: List<Class> annotatedClasses = classFinder
272: .findAnnotatedClasses(PersistenceUnits.class);
273: assertNotNull(annotatedClasses);
274: assertEquals(1, annotatedClasses.size());
275: assertTrue(annotatedClasses
276: .contains(PersistenceUnitAnnotationTest.class));
277:
278: List<Method> annotatedMethods = classFinder
279: .findAnnotatedMethods(PersistenceUnit.class);
280: assertNotNull(annotatedMethods);
281: assertEquals(2, annotatedMethods.size());
282: assertTrue(annotatedMethods
283: .contains(PersistenceUnitAnnotationTest.class
284: .getDeclaredMethod("setAnnotatedMethod1",
285: new Class[] { int.class })));
286: assertTrue(annotatedMethods
287: .contains(PersistenceUnitAnnotationTest.class
288: .getDeclaredMethod("setAnnotatedMethod2",
289: new Class[] { boolean.class })));
290:
291: List<Field> annotatedFields = classFinder
292: .findAnnotatedFields(PersistenceUnit.class);
293: assertNotNull(annotatedFields);
294: assertEquals(2, annotatedFields.size());
295: assertTrue(annotatedFields
296: .contains(PersistenceUnitAnnotationTest.class
297: .getDeclaredField("annotatedField1")));
298: assertTrue(annotatedFields
299: .contains(PersistenceUnitAnnotationTest.class
300: .getDeclaredField("annotatedField2")));
301:
302: //-------------------------------------------------
303: // Ensure annotations are processed correctly
304: //-------------------------------------------------
305: URL srcXML = classLoader
306: .getResource("annotation/empty-web-src.xml");
307: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
308: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
309: .changeType(WebAppDocument.type);
310: WebAppType webApp = webAppDoc.getWebApp();
311: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
312: PersistenceUnitAnnotationHelper.processAnnotations(
313: annotatedWebApp, classFinder);
314: URL expectedXML = classLoader
315: .getResource("annotation/persistence-unit-expected.xml");
316: XmlObject expected = XmlObject.Factory.parse(expectedXML);
317: log.debug("[@PersistenceUnit Source XML] " + '\n'
318: + webApp.toString() + '\n');
319: log.debug("[@PersistenceUnit Expected XML]" + '\n'
320: + expected.toString() + '\n');
321: List problems = new ArrayList();
322: boolean ok = compareXmlObjects(webApp, expected, problems);
323: assertTrue("Differences: " + problems, ok);
324: }
325:
326: public void testWebServiceRefAnnotationHelper() throws Exception {
327:
328: //-------------------------------------------------
329: // Ensure annotations are discovered correctly
330: //-------------------------------------------------
331: List<Class> annotatedClasses = classFinder
332: .findAnnotatedClasses(WebServiceRefs.class);
333: assertNotNull(annotatedClasses);
334: assertEquals(1, annotatedClasses.size());
335: assertTrue(annotatedClasses
336: .contains(WebServiceRefAnnotationTest.class));
337:
338: List<Method> annotatedMethods = classFinder
339: .findAnnotatedMethods(WebServiceRef.class);
340: assertNotNull(annotatedMethods);
341: assertEquals(5, annotatedMethods.size());
342: assertTrue(annotatedMethods
343: .contains(WebServiceRefAnnotationTest.class
344: .getDeclaredMethod("setAnnotatedMethod1",
345: new Class[] { boolean.class })));
346: assertTrue(annotatedMethods
347: .contains(WebServiceRefAnnotationTest.class
348: .getDeclaredMethod("setAnnotatedMethod2",
349: new Class[] { String.class })));
350: assertTrue(annotatedMethods
351: .contains(HandlerChainAnnotationTest.class
352: .getDeclaredMethod("setAnnotatedMethod1",
353: new Class[] { String.class })));
354: assertTrue(annotatedMethods
355: .contains(HandlerChainAnnotationTest.class
356: .getDeclaredMethod("setAnnotatedMethod2",
357: new Class[] { int.class })));
358:
359: List<Field> annotatedFields = classFinder
360: .findAnnotatedFields(WebServiceRef.class);
361: assertNotNull(annotatedFields);
362: assertEquals(4, annotatedFields.size());
363: assertTrue(annotatedFields
364: .contains(WebServiceRefAnnotationTest.class
365: .getDeclaredField("annotatedField1")));
366: assertTrue(annotatedFields
367: .contains(WebServiceRefAnnotationTest.class
368: .getDeclaredField("annotatedField2")));
369: assertTrue(annotatedFields
370: .contains(HandlerChainAnnotationTest.class
371: .getDeclaredField("annotatedField1")));
372: assertTrue(annotatedFields
373: .contains(HandlerChainAnnotationTest.class
374: .getDeclaredField("annotatedField2")));
375:
376: //-------------------------------------------------
377: // Ensure annotations are processed correctly
378: //-------------------------------------------------
379: URL srcXML = classLoader
380: .getResource("annotation/empty-web-src.xml");
381: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
382: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
383: .changeType(WebAppDocument.type);
384: WebAppType webApp = webAppDoc.getWebApp();
385: AnnotatedWebApp annotatedWebApp = new AnnotatedWebApp(webApp);
386: WebServiceRefAnnotationHelper.processAnnotations(
387: annotatedWebApp, classFinder);
388: URL expectedXML = classLoader
389: .getResource("annotation/webservice-ref-expected.xml");
390: XmlObject expected = XmlObject.Factory.parse(expectedXML);
391: log.debug("[@WebServiceRef Source XML] " + '\n'
392: + webApp.toString() + '\n');
393: log.debug("[@WebServiceRef Expected XML]" + '\n'
394: + expected.toString() + '\n');
395: List problems = new ArrayList();
396: boolean ok = compareXmlObjects(webApp, expected, problems);
397: assertTrue("Differences: " + problems, ok);
398: }
399:
400: public void testSecurityAnnotationHelper() throws Exception {
401:
402: //-------------------------------------------------
403: // Ensure annotations are discovered correctly
404: //-------------------------------------------------
405: List<Class> annotatedClasses = classFinder
406: .findAnnotatedClasses(DeclareRoles.class);
407: assertNotNull(annotatedClasses);
408: assertEquals(1, annotatedClasses.size());
409: assertTrue(annotatedClasses
410: .contains(SecurityAnnotationTest.class));
411:
412: annotatedClasses.clear();
413: annotatedClasses = classFinder
414: .findAnnotatedClasses(RunAs.class);
415: assertNotNull(annotatedClasses);
416: assertEquals(1, annotatedClasses.size());
417: assertTrue(annotatedClasses
418: .contains(SecurityAnnotationTest.class));
419:
420: //-------------------------------------------------
421: // Ensure annotations are processed correctly
422: //-------------------------------------------------
423: URL srcXML = classLoader
424: .getResource("annotation/empty-web-src.xml");
425: XmlObject xmlObject = XmlObject.Factory.parse(srcXML, options);
426: WebAppDocument webAppDoc = (WebAppDocument) xmlObject
427: .changeType(WebAppDocument.type);
428: WebAppType webApp = webAppDoc.getWebApp();
429: SecurityAnnotationHelper
430: .processAnnotations(webApp, classFinder);
431: URL expectedXML = classLoader
432: .getResource("annotation/security-expected.xml");
433: XmlObject expected = XmlObject.Factory.parse(expectedXML);
434: log.debug("[Security Source XML] " + '\n' + webApp.toString()
435: + '\n');
436: log.debug("[Security Expected XML]" + '\n'
437: + expected.toString() + '\n');
438: List problems = new ArrayList();
439: boolean ok = compareXmlObjects(webApp, expected, problems);
440: assertTrue("Differences: " + problems, ok);
441:
442: srcXML = classLoader.getResource("annotation/security-src.xml");
443: xmlObject = XmlObject.Factory.parse(srcXML, options);
444: webAppDoc = (WebAppDocument) xmlObject
445: .changeType(WebAppDocument.type);
446: webApp = webAppDoc.getWebApp();
447: SecurityAnnotationHelper
448: .processAnnotations(webApp, classFinder);
449: expectedXML = classLoader
450: .getResource("annotation/security-expected-1.xml");
451: expected = XmlObject.Factory.parse(expectedXML);
452: log.debug("[Security Source XML] " + '\n' + webApp.toString()
453: + '\n');
454: log.debug("[Security Expected XML]" + '\n'
455: + expected.toString() + '\n');
456: problems = new ArrayList();
457: ok = compareXmlObjects(webApp, expected, problems);
458: assertTrue("Differences: " + problems, ok);
459: }
460: }
|