[jboss-cvs] JBossAS SVN: r112205 - in projects/jboss-jca/trunk/embedded/src: test/java/org/jboss/jca/embedded/unit and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Sep 6 14:23:51 EDT 2011
Author: jesper.pedersen
Date: 2011-09-06 14:23:51 -0400 (Tue, 06 Sep 2011)
New Revision: 112205
Added:
projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAEnricher.java
projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/Inject.java
projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/SecurityActions.java
projects/jboss-jca/trunk/embedded/src/test/java/org/jboss/jca/embedded/unit/InjectTestCase.java
Modified:
projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAContainer.java
projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCALoadableExtension.java
Log:
[JBJCA-667] Injection of custom objects
Modified: projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAContainer.java
===================================================================
--- projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAContainer.java 2011-09-06 14:40:12 UTC (rev 112204)
+++ projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAContainer.java 2011-09-06 18:23:51 UTC (rev 112205)
@@ -52,10 +52,14 @@
/** EmbeddedJCA */
private Embedded embedded;
- /** Instance producer */
+ /** Context */
@Inject @ContainerScoped
- private InstanceProducer<Context> instanceProducer;
+ private InstanceProducer<Context> contextProducer;
+ /** Kernel */
+ @Inject @ContainerScoped
+ private InstanceProducer<Embedded> embeddedProducer;
+
/**
* Constructor
*/
@@ -194,7 +198,8 @@
{
embedded.startup();
- instanceProducer.set(createContext());
+ contextProducer.set(createContext());
+ embeddedProducer.set(embedded);
}
catch (Throwable t)
{
Added: projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAEnricher.java
===================================================================
--- projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAEnricher.java (rev 0)
+++ projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCAEnricher.java 2011-09-06 18:23:51 UTC (rev 112205)
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.embedded.arquillian;
+
+import org.jboss.jca.embedded.Embedded;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.jboss.arquillian.core.api.Instance;
+import org.jboss.arquillian.test.spi.TestEnricher;
+
+/**
+ * Handle @Inject fields for test cases based on kernel based lookups.
+ *
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+public class EmbeddedJCAEnricher implements TestEnricher
+{
+ @org.jboss.arquillian.core.api.annotation.Inject
+ private Instance<Embedded> embeddedInst;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void enrich(Object testCase)
+ {
+ if (embeddedInst.get() != null)
+ {
+ injectClass(testCase);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object[] resolve(Method method)
+ {
+ return new Object[method.getParameterTypes().length];
+ }
+
+ @SuppressWarnings("unchecked")
+ private void injectClass(Object testCase)
+ {
+ try
+ {
+ List<Field> annotatedFields =
+ SecurityActions.getFieldsWithAnnotation(testCase.getClass(),
+ org.jboss.jca.embedded.arquillian.Inject.class);
+
+ for (Field field : annotatedFields)
+ {
+ try
+ {
+ org.jboss.jca.embedded.arquillian.Inject annotation =
+ field.getAnnotation(org.jboss.jca.embedded.arquillian.Inject.class);
+
+ field.set(testCase, resolveResource(field, annotation));
+ }
+ catch (Throwable t)
+ {
+ // Unable to set field
+ }
+ }
+ }
+ catch (Throwable t)
+ {
+ throw new RuntimeException("Could not inject fields", t);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Object resolveResource(Field field, org.jboss.jca.embedded.arquillian.Inject annotation)
+ {
+ try
+ {
+ return embeddedInst.get().lookup(annotation.name(), field.getType());
+ }
+ catch (Throwable t)
+ {
+ // Nothing to do
+ }
+
+ return null;
+ }
+}
Modified: projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCALoadableExtension.java
===================================================================
--- projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCALoadableExtension.java 2011-09-06 14:40:12 UTC (rev 112204)
+++ projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/EmbeddedJCALoadableExtension.java 2011-09-06 18:23:51 UTC (rev 112205)
@@ -23,6 +23,7 @@
import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.core.spi.LoadableExtension;
+import org.jboss.arquillian.test.spi.TestEnricher;
/**
* Arquillian {@link LoadableExtension} adaptor for Embedded JCA
@@ -46,5 +47,6 @@
public void register(ExtensionBuilder builder)
{
builder.service(DeployableContainer.class, EmbeddedJCAContainer.class);
+ builder.service(TestEnricher.class, EmbeddedJCAEnricher.class);
}
}
Added: projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/Inject.java
===================================================================
--- projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/Inject.java (rev 0)
+++ projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/Inject.java 2011-09-06 18:23:51 UTC (rev 112205)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.embedded.arquillian;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Inject
+ *
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+ at Documented
+ at Retention(RUNTIME)
+ at Target(ElementType.FIELD)
+public @interface Inject
+{
+ /** Name of the embedded resource */
+ public String name();
+}
Added: projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/SecurityActions.java
===================================================================
--- projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/SecurityActions.java (rev 0)
+++ projects/jboss-jca/trunk/embedded/src/main/java/org/jboss/jca/embedded/arquillian/SecurityActions.java 2011-09-06 18:23:51 UTC (rev 112205)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.embedded.arquillian;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SecurityActions
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+final class SecurityActions
+{
+ /**
+ * No instantiation
+ */
+ private SecurityActions()
+ {
+ throw new UnsupportedOperationException("No instantiation");
+ }
+
+ /**
+ * Get field which contains a certain annotation
+ * @param clz The class
+ * @param annotationClass The annotation class
+ * @return The list of fields
+ */
+ public static List<Field> getFieldsWithAnnotation(final Class<?> clz,
+ final Class<? extends Annotation> annotationClass)
+ {
+ List<Field> declaredAccessableFields = AccessController.doPrivileged(new PrivilegedAction<List<Field>>()
+ {
+ public List<Field> run()
+ {
+ List<Field> foundFields = new ArrayList<Field>(1);
+ Class<?> nextClz = clz;
+ while (nextClz != Object.class)
+ {
+ for (Field field : nextClz.getDeclaredFields())
+ {
+ if (field.isAnnotationPresent(annotationClass))
+ {
+ if (!field.isAccessible())
+ {
+ field.setAccessible(true);
+ }
+ foundFields.add(field);
+ }
+ }
+ nextClz = nextClz.getSuperclass();
+ }
+ return foundFields;
+ }
+ });
+
+ return declaredAccessableFields;
+ }
+}
Added: projects/jboss-jca/trunk/embedded/src/test/java/org/jboss/jca/embedded/unit/InjectTestCase.java
===================================================================
--- projects/jboss-jca/trunk/embedded/src/test/java/org/jboss/jca/embedded/unit/InjectTestCase.java (rev 0)
+++ projects/jboss-jca/trunk/embedded/src/test/java/org/jboss/jca/embedded/unit/InjectTestCase.java 2011-09-06 18:23:51 UTC (rev 112205)
@@ -0,0 +1,112 @@
+/*/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.jca.embedded.unit;
+
+import org.jboss.jca.core.spi.mdr.MetadataRepository;
+import org.jboss.jca.embedded.arquillian.Inject;
+import org.jboss.jca.embedded.rars.simple.MessageListener;
+import org.jboss.jca.embedded.rars.simple.TestActivationSpec;
+import org.jboss.jca.embedded.rars.simple.TestConnection;
+import org.jboss.jca.embedded.rars.simple.TestConnectionFactory;
+import org.jboss.jca.embedded.rars.simple.TestConnectionInterface;
+import org.jboss.jca.embedded.rars.simple.TestConnectionManager;
+import org.jboss.jca.embedded.rars.simple.TestManagedConnection;
+import org.jboss.jca.embedded.rars.simple.TestManagedConnectionFactory;
+import org.jboss.jca.embedded.rars.simple.TestResourceAdapter;
+
+import java.util.UUID;
+
+import javax.annotation.Resource;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.logging.Logger;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit test for Arquillian integration and injecting
+ *
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+ at RunWith(Arquillian.class)
+public class InjectTestCase
+{
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ private static Logger log = Logger.getLogger(InjectTestCase.class);
+
+ /**
+ * Define the deployment
+ * @return The deployment archive
+ */
+ @Deployment
+ public static ResourceAdapterArchive createDeployment()
+ {
+ ResourceAdapterArchive raa =
+ ShrinkWrap.create(ResourceAdapterArchive.class, "ArquillianTest.rar");
+
+ JavaArchive ja = ShrinkWrap.create(JavaArchive.class, UUID.randomUUID().toString() + ".jar");
+ ja.addClasses(MessageListener.class, TestActivationSpec.class, TestConnection.class,
+ TestConnectionFactory.class, TestConnectionManager.class,
+ TestConnectionInterface.class, TestManagedConnection.class,
+ TestManagedConnectionFactory.class, TestResourceAdapter.class);
+
+ raa.addAsLibrary(ja);
+ raa.addAsManifestResource("simple.rar/META-INF/ra.xml", "ra.xml");
+
+ return raa;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ @Resource(mappedName = "java:/eis/ArquillianTest")
+ private TestConnectionFactory connectionFactory;
+
+ @Inject(name = "MDR")
+ private MetadataRepository mdr;
+
+ /**
+ * Basic
+ * @exception Throwable Thrown if case of an error
+ */
+ @Test
+ public void testBasic() throws Throwable
+ {
+ assertNotNull(connectionFactory);
+ assertNotNull(mdr);
+ assertNotNull(mdr.getResourceAdapters());
+ assertTrue(mdr.getResourceAdapters().size() == 1);
+ }
+}
More information about the jboss-cvs-commits
mailing list