[jboss-svn-commits] JBoss Common SVN: r3771 - in arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi: util and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Nov 24 07:55:05 EST 2009
Author: aslak
Date: 2009-11-24 07:55:05 -0500 (Tue, 24 Nov 2009)
New Revision: 3771
Added:
arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/ContainerMethodExecutor.java
arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/TestRunner.java
arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/SecurityActions.java
arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/TestRunners.java
Log:
ARQ-23 Added ContainerMethodExecutor interface for handling multiple communication protocols between client test framework and running test code. TestRunner SPI for loading a Test Framework on the server side.
Added: arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/ContainerMethodExecutor.java
===================================================================
--- arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/ContainerMethodExecutor.java (rev 0)
+++ arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/ContainerMethodExecutor.java 2009-11-24 12:55:05 UTC (rev 3771)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.spi;
+
+/**
+ * RemoteMethodExecutor
+ *
+ * A Generic way to execute a in-container/remotely deployed test case.
+ *
+ * Implementations should handle the protocol used for communication
+ * ie http/servlet, jmx, rmi etc.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public interface ContainerMethodExecutor
+{
+
+ /**
+ * Invoke a test method deployed in the container.
+ *
+ * @param testMethodExecutor
+ * @return
+ */
+ TestResult invoke(TestMethodExecutor testMethodExecutor);
+
+}
Added: arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/TestRunner.java
===================================================================
--- arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/TestRunner.java (rev 0)
+++ arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/TestRunner.java 2009-11-24 12:55:05 UTC (rev 3771)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.spi;
+
+/**
+ * TestRunner
+ *
+ * A Generic way to start the test framwork.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public interface TestRunner
+{
+ public enum ExecutionMode
+ {
+ /**
+ * When running deployed inside the container
+ */
+ CONTAINER,
+
+ /**
+ * When running outside the container
+ */
+ STANDALONE
+ }
+
+ /**
+ * Run a single test method in a test class.
+ *
+ * @param testClass The test case class to execute
+ * @param methodName The method to execute
+ * @return The result of the test
+ */
+ TestResult execute(Class<?> testClass, String methodName);
+
+ /**
+ * Instruct the TestRunner which mode to run in.
+ *
+ * @param executionMode
+ */
+ void setExecutionMode(ExecutionMode executionMode);
+}
Added: arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/SecurityActions.java
===================================================================
--- arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/SecurityActions.java (rev 0)
+++ arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/SecurityActions.java 2009-11-24 12:55:05 UTC (rev 3771)
@@ -0,0 +1,180 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.spi.util;
+
+import java.lang.reflect.Constructor;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+/**
+ * SecurityActions
+ *
+ * A set of privileged actions that are not to leak out
+ * of this package
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+final class SecurityActions
+{
+
+ //-------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * No instantiation
+ */
+ private SecurityActions()
+ {
+ throw new UnsupportedOperationException("No instantiation");
+ }
+
+ //-------------------------------------------------------------------------------||
+ // Utility Methods --------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the Thread Context ClassLoader
+ */
+ static ClassLoader getThreadContextClassLoader()
+ {
+ return AccessController.doPrivileged(GetTcclAction.INSTANCE);
+ }
+
+ /**
+ * Obtains the Constructor specified from the given Class and argument types
+ * @param clazz
+ * @param argumentTypes
+ * @return
+ * @throws NoSuchMethodException
+ */
+ static Constructor<?> getConstructor(final Class<?> clazz, final Class<?>... argumentTypes)
+ throws NoSuchMethodException
+ {
+ try
+ {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>()
+ {
+ @Override
+ public Constructor<?> run() throws NoSuchMethodException
+ {
+ return clazz.getConstructor(argumentTypes);
+ }
+ });
+ }
+ // Unwrap
+ catch (final PrivilegedActionException pae)
+ {
+ final Throwable t = pae.getCause();
+ // Rethrow
+ if (t instanceof NoSuchMethodException)
+ {
+ throw (NoSuchMethodException) t;
+ }
+ else
+ {
+ // No other checked Exception thrown by Class.getConstructor
+ try
+ {
+ throw (RuntimeException) t;
+ }
+ // Just in case we've really messed up
+ catch (final ClassCastException cce)
+ {
+ throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
+ }
+ }
+ }
+ }
+
+ /**
+ * Create a new instance by finding a constructor that matches the argumentTypes signature
+ * using the arguments for instantiation.
+ *
+ * @param className Full classname of class to create
+ * @param argumentTypes The constructor argument types
+ * @param arguments The constructor arguments
+ * @return a new instance
+ * @throws IllegalArgumentException if className, argumentTypes, or arguments are null
+ * @throws RuntimeException if any exceptions during creation
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ */
+ static <T> T newInstance(final String className, final Class<?>[] argumentTypes, final Object[] arguments,
+ final Class<T> expectedType)
+ {
+ if (className == null)
+ {
+ throw new IllegalArgumentException("ClassName must be specified");
+ }
+ if (argumentTypes == null)
+ {
+ throw new IllegalArgumentException("ArgumentTypes must be specified. Use empty array if no arguments");
+ }
+ if (arguments == null)
+ {
+ throw new IllegalArgumentException("Arguments must be specified. Use empty array if no arguments");
+ }
+ final Object obj;
+ try
+ {
+ final ClassLoader tccl = getThreadContextClassLoader();
+ final Class<?> implClass = Class.forName(className, false, tccl);
+ Constructor<?> constructor = getConstructor(implClass, argumentTypes);
+ obj = constructor.newInstance(arguments);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not create new instance of " + className
+ + ", missing package from classpath?", e);
+ }
+
+ // Cast
+ try
+ {
+ return expectedType.cast(obj);
+ }
+ catch (final ClassCastException cce)
+ {
+ // Reconstruct so we get some useful information
+ throw new ClassCastException("Incorrect expected type, " + expectedType.getName() + ", defined for "
+ + obj.getClass().getName());
+ }
+ }
+
+ //-------------------------------------------------------------------------------||
+ // Inner Classes ----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------||
+
+ /**
+ * Single instance to get the TCCL
+ */
+ private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
+ INSTANCE;
+
+ @Override
+ public ClassLoader run()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ }
+
+}
Added: arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/TestRunners.java
===================================================================
--- arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/TestRunners.java (rev 0)
+++ arquillian/trunk/spi/src/main/java/org/jboss/arquillian/spi/util/TestRunners.java 2009-11-24 12:55:05 UTC (rev 3771)
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.spi.util;
+
+import org.jboss.arquillian.spi.TestRunner;
+
+/**
+ * TestRunners
+ *
+ * Helper factory for loading TestRunners in container.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public final class TestRunners
+{
+ private TestRunners() {}
+
+ /**
+ * Dynamically loads / creates a new instance of a test runner.
+ *
+ * @return A Initialized TestRunner
+ * @throws IllegalStateException if multiple TestRunners found in classpath.
+ */
+ public static TestRunner getTestRunner()
+ {
+ DefaultServiceLoader<TestRunner> serviceLoader = DefaultServiceLoader.load(
+ TestRunner.class,
+ SecurityActions.getThreadContextClassLoader());
+
+ if(serviceLoader.getProviders().size() > 1)
+ {
+ throw new IllegalStateException("Multiple TestRunners found, only one allowed. Check your classpath");
+ }
+ return serviceLoader.iterator().next();
+ }
+}
More information about the jboss-svn-commits
mailing list