aslak

Arquillian - Reflection - Query

Jan 7th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.90 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.api.Run;
  28. import org.jboss.arquillian.api.RunModeType;
  29. import org.jboss.arquillian.impl.query.executer.QueryExecuter;
  30. import org.jboss.arquillian.spi.core.annotation.Inject;
  31. import org.jboss.shrinkwrap.api.Archive;
  32. import org.jboss.shrinkwrap.descriptor.api.Descriptor;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35.  
  36. /**
  37.  * QueryTestCase
  38.  *
  39.  * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
  40.  * @version $Revision: $
  41.  */
  42. public class QueryTestCase
  43. {
  44.    private QueryService service;
  45.    
  46.    @Before
  47.    public void register()
  48.    {
  49.       service = new QueryService(new LookupService());
  50.       service.register(QueryTestScenario.class, QueryTestScenario2.class);
  51.    }
  52.  
  53.    @Test
  54.    public void shouldFindClassWithType() throws Exception
  55.    {
  56.       Collection<ClassDescriptor> result = service.query(
  57.             ClassDescriptor.class,
  58.             Query.forClass()
  59.                .ofType(QueryTestInterface.class));
  60.      
  61.       Assert.assertNotNull(result);
  62.       Assert.assertEquals(2, result.size());
  63.       Assert.assertTrue(containsClassName("QueryTestScenario", result));
  64.       Assert.assertTrue(containsClassName("QueryTestScenario2", result));
  65.    }
  66.    
  67.    @Test
  68.    public void shouldFindClassWithTypeAndAnnotation() throws Exception
  69.    {
  70.       Collection<ClassDescriptor> result = service.query(
  71.             ClassDescriptor.class,
  72.             Query.forClass()
  73.                .withAnnotation(Run.class)
  74.                .ofType(QueryTestInterface.class));
  75.      
  76.       Assert.assertNotNull(result);
  77.       Assert.assertEquals(1, result.size());
  78.       Assert.assertTrue(containsClassName("QueryTestScenario", result));
  79.    }
  80.  
  81.    @Test
  82.    public void shouldFindClassWithTypeGenericType() throws Exception
  83.    {
  84.       Collection<ClassDescriptor> result = service.query(
  85.             ClassDescriptor.class,
  86.             Query.forClass()
  87.                .withGenericType(String.class)
  88.                .ofType(QueryTestInterface.class));
  89.      
  90.       Assert.assertNotNull(result);
  91.       Assert.assertEquals(1, result.size());
  92.       Assert.assertTrue(containsClassName("QueryTestScenario2", result));
  93.    }
  94.  
  95.    @Test
  96.    public void shouldFindMethodWithTypeAndAnnotation() throws Exception
  97.    {
  98.       Collection<MethodDescriptor> result = service.query(
  99.             MethodDescriptor.class,
  100.             Query.forMethod()
  101.                .withAnnotation(Deployment.class)
  102.                .ofType(Archive.class));
  103.      
  104.       Assert.assertNotNull(result);
  105.       Assert.assertEquals(1, result.size());
  106.       Assert.assertTrue(containsMethodName("archiveDeployment", result));
  107.    }
  108.  
  109.    @Test
  110.    public void shouldFindFieldsWithAnnotation() throws Exception
  111.    {
  112.       Collection<FieldDescriptor> result = service.query(
  113.             FieldDescriptor.class,
  114.             Query.forField()
  115.                .withAnnotation(Inject.class));
  116.      
  117.       Assert.assertNotNull(result);
  118.       Assert.assertEquals(2, result.size());
  119.       Assert.assertTrue(containsFieldName("testField", result));
  120.       Assert.assertTrue(containsFieldName("testField2", result));
  121.    }
  122.  
  123.    @Test
  124.    public void shouldFindFieldWithTypeAndAnnotation() throws Exception
  125.    {
  126.       List<FieldDescriptor> result = service.query(
  127.             FieldDescriptor.class,
  128.             Query.forField()
  129.                .withAnnotation(Inject.class)
  130.                .ofType(String.class));
  131.      
  132.       Assert.assertNotNull(result);
  133.       Assert.assertEquals(1, result.size());
  134.       Assert.assertTrue(containsFieldName("testField", result));
  135.    }
  136.  
  137.    @Test
  138.    public void shouldFindSameWrapperInstance() throws Exception
  139.    {
  140.       QueryExecuter<Method> query = Query.forMethod()
  141.                         .withAnnotation(Deployment.class)
  142.                         .ofType(Archive.class);
  143.      
  144.       Collection<MethodDescriptor> result = service.query(
  145.             MethodDescriptor.class,
  146.             query
  147.        );
  148.      
  149.       Assert.assertNotNull(result);
  150.       Assert.assertEquals(1, result.size());
  151.      
  152.       // set some state on the Descriptor object
  153.       result.iterator().next().setPreviouslyFound(true);
  154.      
  155.       // re query, should return same instance
  156.       result = service.query(
  157.             MethodDescriptor.class,
  158.             query
  159.        );
  160.      
  161.       Assert.assertTrue(result.iterator().next().isPreviouslyFound());
  162.    }
  163.  
  164.    
  165.    private boolean containsFieldName(String name, Collection<? extends Desc<Field>> result)
  166.    {
  167.       for(Desc<Field> desc : result)
  168.       {
  169.          if(desc.getTarget().getName().equals(name))
  170.          {
  171.             return true;
  172.          }
  173.       }
  174.       return false;
  175.    }
  176.  
  177.    private boolean containsMethodName(String name, Collection<? extends Desc<Method>> result)
  178.    {
  179.       for(Desc<Method> desc : result)
  180.       {
  181.          if(desc.getTarget().getName().equals(name))
  182.          {
  183.             return true;
  184.          }
  185.       }
  186.       return false;
  187.    }
  188.  
  189.    private boolean containsClassName(String name, Collection<? extends Desc<Class<?>>> result)
  190.    {
  191.       for(Desc<Class<?>> desc : result)
  192.       {
  193.          if(desc.getTarget().getSimpleName().equals(name))
  194.          {
  195.             return true;
  196.          }
  197.       }
  198.       return false;
  199.    }
  200.  
  201.    private static interface QueryTestInterface<T>
  202.    {
  203.      
  204.    }
  205.    
  206.    @SuppressWarnings("unused")
  207.    @Run(RunModeType.AS_CLIENT)
  208.    private static class QueryTestScenario implements QueryTestInterface<Object>
  209.    {
  210.       @Inject
  211.       private String testField;
  212.      
  213.       @Inject
  214.       private Integer testField2;
  215.      
  216.       @Deployment(name = "archive")
  217.       public static Archive<?> archiveDeployment() { return null; }
  218.  
  219.       @Deployment(name = "descriptor")
  220.       public static Descriptor descriptorDeployment() { return null; }
  221.    }
  222.    
  223.    private static class QueryTestScenario2 implements QueryTestInterface<String>
  224.    {
  225.    }
  226. }
Add Comment
Please, Sign In to add comment