[jboss-cvs] jbossretro/src/test/org/jboss/test/annotations ...

Kabir Khan kkhan at jboss.com
Tue Jul 18 04:57:48 EDT 2006


  User: kkhan   
  Date: 06/07/18 04:57:48

  Modified:    src/test/org/jboss/test/annotations 
                        AnnotationsTestCase.java
  Log:
  Got a ClassCastException when reading annotations from strongly typed PrivilegedActions. Fix and test for this
  
  Revision  Changes    Path
  1.7       +136 -1    jbossretro/src/test/org/jboss/test/annotations/AnnotationsTestCase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AnnotationsTestCase.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbossretro/src/test/org/jboss/test/annotations/AnnotationsTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -b -r1.6 -r1.7
  --- AnnotationsTestCase.java	17 Jul 2006 08:38:12 -0000	1.6
  +++ AnnotationsTestCase.java	18 Jul 2006 08:57:48 -0000	1.7
  @@ -22,9 +22,12 @@
   package org.jboss.test.annotations;
   
   import java.lang.annotation.Annotation;
  +import java.lang.reflect.AccessibleObject;
   import java.lang.reflect.Constructor;
   import java.lang.reflect.Field;
   import java.lang.reflect.Method;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
   
   import junit.framework.TestCase;
   
  @@ -58,28 +61,34 @@
         assertFalse(annotatedClass.isAnnotationPresent(HandlerChain.class));
         
         // Process an @WebMethod annotations
  +      boolean hadWebMethod = false;
         for (Method method : annotatedClass.getMethods())
         {
            if (method.isAnnotationPresent(WebMethod.class))
            {
  +            hadWebMethod = true;
               WebMethod anWebMethod = method.getAnnotation(WebMethod.class);
               String operationName = anWebMethod.operationName();
               assertEquals("", operationName);
   
  +            boolean hadTestAnnotation = false;
               Annotation[] annotations = method.getAnnotations();
               for(Annotation a : annotations)
               {
                  if( a instanceof TestAnnotation )
                  {
  +                  hadTestAnnotation = true;
                     TestAnnotation ta = (TestAnnotation) a;
                     assertTrue("TestAnnotation.isValue == true", ta.isValid());
                     assertTrue("TestAnnotation.interations == 10", ta.interations() == 10);
                  }
               }
  +            assertTrue("@WebMethod method should have had @TestAnnotation", hadTestAnnotation);
            }
            
            method.getParameterAnnotations();
         }
  +      assertTrue("Should have picked up @WebMethod", hadWebMethod);
         
         Method method = annotatedClass.getMethod("doSomething", new Class[] { Integer.TYPE, Integer.TYPE });
         Annotation[][] annotations = method.getParameterAnnotations();
  @@ -95,12 +104,14 @@
         assertNotNull(annotation);
         assertTrue(annotation.isValid());
         
  +      boolean hadTestAnnotation = false;
         for (Constructor constructor : annotatedClass.getConstructors())
         {
            constructor.getParameterAnnotations();
   
            if (constructor.isAnnotationPresent(TestAnnotation.class))
            {
  +            hadTestAnnotation = true;
               annotation = (TestAnnotation) constructor.getAnnotation(TestAnnotation.class);
   
               assertTrue(annotation.isValid());
  @@ -114,5 +125,129 @@
               assertEquals(2, annotation.interations());
            }
         }
  +      assertTrue("Should have picked up @TestAnnotation", hadTestAnnotation);
  +      
  +   }
  +
  +   public void testWebServicePrivilegedActions()
  +      throws Exception
  +   {
  +      ClassLoader loader = Thread.currentThread().getContextClassLoader();
  +      Class annotatedClass = loader.loadClass("org.jboss.test.annotations.TestWebService");
  +      
  +      boolean hadWebService = false;
  +      Annotation[] classAnns = readAnnotations(annotatedClass);
  +      for (Annotation ann : classAnns)
  +      {
  +         if (ann instanceof WebService)
  +         {
  +            hadWebService = true;
  +         }
  +      }
  +      assertTrue("Class should have had @WebService", hadWebService);
  +      
  +      // Process an @WebMethod annotations
  +      boolean hadWebMethod = false;
  +      for (Method method : annotatedClass.getMethods())
  +      {
  +         if (method.isAnnotationPresent(WebMethod.class))
  +         {
  +            hadWebMethod = true;
  +            WebMethod anWebMethod = method.getAnnotation(WebMethod.class);
  +            String operationName = anWebMethod.operationName();
  +            assertEquals("", operationName);
  +   
  +            Annotation[] annotations = readAnnotations(method);
  +            for(Annotation a : annotations)
  +            {
  +               if( a instanceof TestAnnotation )
  +               {
  +                  TestAnnotation ta = (TestAnnotation) a;
  +                  assertTrue("TestAnnotation.isValue == true", ta.isValid());
  +                  assertTrue("TestAnnotation.interations == 10", ta.interations() == 10);
  +               }
  +            }
      }
  +      }
  +      assertTrue("Should have picked up @WebMethod", hadWebMethod);
  +      
  +      Method method = annotatedClass.getMethod("doSomething", new Class[] { Integer.TYPE, Integer.TYPE });
  +      Annotation[][] annotations = method.getParameterAnnotations();
  +      assertNotNull(annotations);
  +      assertEquals(2, annotations.length);
  +      TestAnnotation annotation = (TestAnnotation) annotations[0][0];
  +      assertEquals(1, annotation.interations());
  +      annotation = (TestAnnotation) annotations[1][0];
  +      assertEquals(2, annotation.interations());
  +      
  +      boolean hadFieldAnnotation = false;
  +      Field field = annotatedClass.getField("testField");
  +      Annotation[] fieldAnnotations = readAnnotations(field);
  +      for (Annotation fldAnn : fieldAnnotations)
  +      {
  +         if (fldAnn instanceof TestAnnotation)
  +         {
  +            hadFieldAnnotation = true;
  +            assertTrue(((TestAnnotation)fldAnn).isValid());
  +         }
  +      }
  +      assertTrue("Field should have had @TestAnnotation", hadFieldAnnotation);
  +     
  +      boolean hadTestAnnotation = false;
  +      for (Constructor constructor : annotatedClass.getConstructors())
  +      {
  +         constructor.getParameterAnnotations();
  +   
  +         for (Annotation conAnn : readAnnotations(constructor))
  +         {
  +            if (conAnn instanceof TestAnnotation)
  +            {
  +               hadTestAnnotation = true;
  +            }
  +         }
  +         if (constructor.isAnnotationPresent(TestAnnotation.class))
  +         {
  +            annotation = (TestAnnotation) constructor.getAnnotation(TestAnnotation.class);
  +   
  +            assertTrue(annotation.isValid());
  +   
  +            annotations = constructor.getParameterAnnotations();
  +            assertNotNull(annotations);
  +            assertEquals(2, annotations.length);
  +            annotation = (TestAnnotation) annotations[0][0];
  +            assertEquals(1, annotation.interations());
  +            annotation = (TestAnnotation) annotations[1][0];
  +            assertEquals(2, annotation.interations());
  +         }
  +      }
  +      assertTrue("Should have picked up @TestAnnotation", hadTestAnnotation);
  +      
  +   }
  +   
  +   private Annotation[] readAnnotations(final AccessibleObject ao)
  +   {
  +      PrivilegedAction<Annotation[]> action = new PrivilegedAction<Annotation[]>()
  +      {
  +         public Annotation[] run()
  +         {
  +            return ao.getAnnotations();
  +         }
  +      };
  +      return AccessController.doPrivileged(action);
  +   }
  +   
  +   private Annotation[] readAnnotations(final Class clazz)
  +   {
  +         PrivilegedAction<Annotation[]> action = new PrivilegedAction<Annotation[]>()
  +         {
  +            public Annotation[] run()
  +            {
  +               Annotation[] annotations =  clazz.getAnnotations();
  +               return annotations;
  +            }
  +         };
  +      return AccessController.doPrivileged(action);
  +   }
  +   
  +   
   }
  
  
  



More information about the jboss-cvs-commits mailing list