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.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();
- service.register(QueryTestScenario.class);
- }
- @Test
- public void shouldFindMethodWithTypeAndAnnotation() throws Exception
- {
- Collection<DeploymentDescriptor> result = service.query(
- DeploymentDescriptor.class,
- Query.forType(Method.class)
- .annotatedWith(Deployment.class)
- .isOfType(Archive.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- Assert.assertTrue(containsMethodName("archiveDeployment", result));
- }
- @Test
- public void shouldFindFieldsWithAnnotation() throws Exception
- {
- Collection<InjectionPoint> result = service.query(
- InjectionPoint.class,
- Query.forType(Field.class)
- .annotatedWith(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<InjectionPoint> result = service.query(
- InjectionPoint.class,
- Query.forType(Field.class)
- .annotatedWith(Inject.class)
- .isOfType(String.class));
- Assert.assertNotNull(result);
- Assert.assertEquals(1, result.size());
- Assert.assertTrue(containsFieldName("testField", result));
- }
- 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 static class QueryTestScenario
- {
- @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; }
- }
- }
Add Comment
Please, Sign In to add comment