Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.jboss.arquillian.impl.query;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.Collection;
- import java.util.List;
- import junit.framework.Assert;
- import org.jboss.arquillian.api.Deployment;
- import org.jboss.arquillian.api.Run;
- import org.jboss.arquillian.api.RunModeType;
- import org.jboss.arquillian.impl.query.executer.QueryExecuter;
- import org.jboss.arquillian.spi.core.annotation.Inject;
- import org.jboss.shrinkwrap.api.Archive;
- import org.jboss.shrinkwrap.descriptor.api.Descriptor;
- import org.junit.Before;
- import org.junit.Test;
- /**
- * QueryTestCase
- *
- * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
- * @version $Revision: $
- */
- public class QueryTestCase
- {
- private QueryService service;
- @Before
- public void register()
- {
- service = new QueryService(new LookupService());
- service.register(QueryTestScenario.class, QueryTestScenario2.class);
- }
- @Test
- public void shouldFindClassWithType() throws Exception
- {
- Collection<ClassDescriptor> result = service.query(
- ClassDescriptor.class,
- Query.forClass()
- .ofType(QueryTestInterface.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(2, result.size());
- Assert.assertTrue(containsClassName("QueryTestScenario", result));
- Assert.assertTrue(containsClassName("QueryTestScenario2", result));
- }
- @Test
- public void shouldFindClassWithTypeAndAnnotation() throws Exception
- {
- Collection<ClassDescriptor> result = service.query(
- ClassDescriptor.class,
- Query.forClass()
- .withAnnotation(Run.class)
- .ofType(QueryTestInterface.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- Assert.assertTrue(containsClassName("QueryTestScenario", result));
- }
- @Test
- public void shouldFindClassWithTypeGenericType() throws Exception
- {
- Collection<ClassDescriptor> result = service.query(
- ClassDescriptor.class,
- Query.forClass()
- .withGenericType(String.class)
- .ofType(QueryTestInterface.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- Assert.assertTrue(containsClassName("QueryTestScenario2", result));
- }
- @Test
- public void shouldFindMethodWithTypeAndAnnotation() throws Exception
- {
- Collection<MethodDescriptor> result = service.query(
- MethodDescriptor.class,
- Query.forMethod()
- .withAnnotation(Deployment.class)
- .ofType(Archive.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- Assert.assertTrue(containsMethodName("archiveDeployment", result));
- }
- @Test
- public void shouldFindFieldsWithAnnotation() throws Exception
- {
- Collection<FieldDescriptor> result = service.query(
- FieldDescriptor.class,
- Query.forField()
- .withAnnotation(Inject.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(2, result.size());
- Assert.assertTrue(containsFieldName("testField", result));
- Assert.assertTrue(containsFieldName("testField2", result));
- }
- @Test
- public void shouldFindFieldWithTypeAndAnnotation() throws Exception
- {
- List<FieldDescriptor> result = service.query(
- FieldDescriptor.class,
- Query.forField()
- .withAnnotation(Inject.class)
- .ofType(String.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- Assert.assertTrue(containsFieldName("testField", result));
- }
- @Test
- public void shouldFindSameWrapperInstance() throws Exception
- {
- QueryExecuter<Method> query = Query.forMethod()
- .withAnnotation(Deployment.class)
- .ofType(Archive.class);
- Collection<MethodDescriptor> result = service.query(
- MethodDescriptor.class,
- query
- );
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- // set some state on the Descriptor object
- result.iterator().next().setPreviouslyFound(true);
- // re query, should return same instance
- result = service.query(
- MethodDescriptor.class,
- query
- );
- Assert.assertTrue(result.iterator().next().isPreviouslyFound());
- }
- private boolean containsFieldName(String name, Collection<? extends Desc<Field>> result)
- {
- for(Desc<Field> desc : result)
- {
- if(desc.getTarget().getName().equals(name))
- {
- return true;
- }
- }
- return false;
- }
- private boolean containsMethodName(String name, Collection<? extends Desc<Method>> result)
- {
- for(Desc<Method> desc : result)
- {
- if(desc.getTarget().getName().equals(name))
- {
- return true;
- }
- }
- return false;
- }
- private boolean containsClassName(String name, Collection<? extends Desc<Class<?>>> result)
- {
- for(Desc<Class<?>> desc : result)
- {
- if(desc.getTarget().getSimpleName().equals(name))
- {
- return true;
- }
- }
- return false;
- }
- private static interface QueryTestInterface<T>
- {
- }
- @SuppressWarnings("unused")
- @Run(RunModeType.AS_CLIENT)
- private static class QueryTestScenario implements QueryTestInterface<Object>
- {
- @Inject
- private String testField;
- @Inject
- private Integer testField2;
- @Deployment(name = "archive")
- public static Archive<?> archiveDeployment() { return null; }
- @Deployment(name = "descriptor")
- public static Descriptor descriptorDeployment() { return null; }
- }
- private static class QueryTestScenario2 implements QueryTestInterface<String>
- {
- }
- }
Add Comment
Please, Sign In to add comment