aslak

Arquillian - Reflection - Query

Jan 7th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2010, Red Hat Middleware LLC, and individual contributors
  4.  * by the @authors tag. See the copyright.txt in the distribution for a
  5.  * full listing of individual contributors.
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jboss.arquillian.impl.query;
  18.  
  19. import java.lang.reflect.Field;
  20. import java.lang.reflect.Method;
  21. import java.util.Collection;
  22. import java.util.List;
  23.  
  24. import junit.framework.Assert;
  25.  
  26. import org.jboss.arquillian.api.Deployment;
  27. import org.jboss.arquillian.spi.core.annotation.Inject;
  28. import org.jboss.shrinkwrap.api.Archive;
  29. import org.jboss.shrinkwrap.descriptor.api.Descriptor;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32.  
  33. /**
  34.  * QueryTestCase
  35.  *
  36.  * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
  37.  * @version $Revision: $
  38.  */
  39. public class QueryTestCase
  40. {
  41.    private QueryService service;
  42.    
  43.    @Before
  44.    public void register()
  45.    {
  46.       service = new QueryService();
  47.       service.register(QueryTestScenario.class);
  48.    }
  49.    
  50.    @Test
  51.    public void shouldFindMethodWithTypeAndAnnotation() throws Exception
  52.    {
  53.       Collection<DeploymentDescriptor> result = service.query(
  54.             DeploymentDescriptor.class,
  55.             Query.forType(Method.class)
  56.                .annotatedWith(Deployment.class)
  57.                .isOfType(Archive.class));
  58.      
  59.       Assert.assertNotNull(result);
  60.       Assert.assertEquals(1, result.size());
  61.       Assert.assertTrue(containsMethodName("archiveDeployment", result));
  62.    }
  63.  
  64.    @Test
  65.    public void shouldFindFieldsWithAnnotation() throws Exception
  66.    {
  67.       Collection<InjectionPoint> result = service.query(
  68.             InjectionPoint.class,
  69.             Query.forType(Field.class)
  70.                .annotatedWith(Inject.class));
  71.      
  72.       Assert.assertNotNull(result);
  73.       Assert.assertEquals(2, result.size());
  74.       Assert.assertTrue(containsFieldName("testField", result));
  75.       Assert.assertTrue(containsFieldName("testField2", result));
  76.    }
  77.  
  78.    @Test
  79.    public void shouldFindFieldWithTypeAndAnnotation() throws Exception
  80.    {
  81.       List<InjectionPoint> result = service.query(
  82.             InjectionPoint.class,
  83.             Query.forType(Field.class)
  84.                .annotatedWith(Inject.class)
  85.                .isOfType(String.class));
  86.      
  87.       Assert.assertNotNull(result);
  88.       Assert.assertEquals(1, result.size());
  89.       Assert.assertTrue(containsFieldName("testField", result));
  90.    }
  91.    
  92.    private boolean containsFieldName(String name, Collection<? extends Desc<Field>> result)
  93.    {
  94.       for(Desc<Field> desc : result)
  95.       {
  96.          if(desc.getTarget().getName().equals(name))
  97.          {
  98.             return true;
  99.          }
  100.       }
  101.       return false;
  102.    }
  103.  
  104.    private boolean containsMethodName(String name, Collection<? extends Desc<Method>> result)
  105.    {
  106.       for(Desc<Method> desc : result)
  107.       {
  108.          if(desc.getTarget().getName().equals(name))
  109.          {
  110.             return true;
  111.          }
  112.       }
  113.       return false;
  114.    }
  115.  
  116.    private static class QueryTestScenario
  117.    {
  118.       @Inject
  119.       private String testField;
  120.      
  121.       @Inject
  122.       private Integer testField2;
  123.      
  124.       @Deployment(name = "archive")
  125.       public static Archive<?> archiveDeployment() { return null; }
  126.  
  127.       @Deployment(name = "descriptor")
  128.       public static Descriptor descriptorDeployment() { return null; }
  129.    }
  130. }
Add Comment
Please, Sign In to add comment