[jboss-svn-commits] JBoss Common SVN: r3773 - in arquillian/trunk: protocol-servlet and 18 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Nov 24 08:03:10 EST 2009
Author: aslak
Date: 2009-11-24 08:03:10 -0500 (Tue, 24 Nov 2009)
New Revision: 3773
Added:
arquillian/trunk/protocol-servlet/
arquillian/trunk/protocol-servlet/pom.xml
arquillian/trunk/protocol-servlet/src/
arquillian/trunk/protocol-servlet/src/main/
arquillian/trunk/protocol-servlet/src/main/java/
arquillian/trunk/protocol-servlet/src/main/java/org/
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/SecurityActions.java
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletMethodExecutor.java
arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletTestRunner.java
arquillian/trunk/protocol-servlet/src/main/resources/
arquillian/trunk/protocol-servlet/src/test/
arquillian/trunk/protocol-servlet/src/test/java/
arquillian/trunk/protocol-servlet/src/test/java/org/
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/MockTestRunner.java
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/ProtocolTestCase.java
arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/TestUtil.java
arquillian/trunk/protocol-servlet/src/test/resources/
arquillian/trunk/protocol-servlet/src/test/resources/META-INF/
arquillian/trunk/protocol-servlet/src/test/resources/META-INF/services/
arquillian/trunk/protocol-servlet/src/test/resources/META-INF/services/org.jboss.arquillian.spi.TestRunner
Log:
ARQ-29 Client/Server impl of Container communication protocol
Added: arquillian/trunk/protocol-servlet/pom.xml
===================================================================
--- arquillian/trunk/protocol-servlet/pom.xml (rev 0)
+++ arquillian/trunk/protocol-servlet/pom.xml 2009-11-24 13:03:10 UTC (rev 3773)
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <!-- Parent -->
+ <parent>
+ <groupId>org.jboss.arquillian</groupId>
+ <artifactId>arquillian-build</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <relativePath>../build/pom.xml</relativePath>
+ </parent>
+
+ <!-- Model Version -->
+ <modelVersion>4.0.0</modelVersion>
+
+ <!-- Artifact Configuration -->
+ <artifactId>arquillian-protocol-servlet</artifactId>
+ <name>Arquillian Protocol Servlet</name>
+ <description>Protocol handler for communicating using a servlet / http</description>
+
+
+ <!-- Properties -->
+ <properties>
+
+ <!-- Versioning -->
+
+ </properties>
+
+ <!-- Dependencies -->
+ <dependencies>
+
+ <!--
+ org.jboss.arquillian
+ -->
+ <dependency>
+ <groupId>org.jboss.arquillian</groupId>
+ <artifactId>arquillian-spi</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <!--
+ servlet api
+ -->
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty-embedded</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+</project>
+
Added: arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/SecurityActions.java
===================================================================
--- arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/SecurityActions.java (rev 0)
+++ arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/SecurityActions.java 2009-11-24 13:03:10 UTC (rev 3773)
@@ -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.protocol.servlet;
+
+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/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletMethodExecutor.java
===================================================================
--- arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletMethodExecutor.java (rev 0)
+++ arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletMethodExecutor.java 2009-11-24 13:03:10 UTC (rev 3773)
@@ -0,0 +1,130 @@
+/*
+ * 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.protocol.servlet;
+
+import java.io.ObjectInputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.jboss.arquillian.spi.ContainerMethodExecutor;
+import org.jboss.arquillian.spi.TestMethodExecutor;
+import org.jboss.arquillian.spi.TestResult;
+
+/**
+ * ServletMethodExecutor
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ServletMethodExecutor implements ContainerMethodExecutor
+{
+ private URL baseURL;
+
+ public ServletMethodExecutor(URL baseURL)
+ {
+ this.baseURL = baseURL;
+ }
+
+ @Override
+ public TestResult invoke(TestMethodExecutor testMethodExecutor)
+ {
+ if(testMethodExecutor == null)
+ {
+ throw new IllegalArgumentException("TestMethodExecutor must be specified");
+ }
+
+ Class<?> testClass = testMethodExecutor.getInstance().getClass();
+ String url = baseURL.toExternalForm() +
+ "?outputMode=serializedObject&className=" + testClass.getName() +
+ "&methodName=" + testMethodExecutor.getMethod().getName();
+
+ try
+ {
+ TestResult result = execute(url);
+ return result;
+ }
+ catch (Exception e)
+ {
+ throw new IllegalStateException("Error launching test " + testClass.getName() + " " + testMethodExecutor.getMethod(), e);
+ }
+ }
+
+ private TestResult execute(String url) throws Exception
+ {
+ long timeoutTime = System.currentTimeMillis() + 1000;
+ boolean interrupted = false;
+ while (timeoutTime > System.currentTimeMillis())
+ {
+ URLConnection connection = new URL(url).openConnection();
+ if (!(connection instanceof HttpURLConnection))
+ {
+ throw new IllegalStateException("Not an http connection! " + connection);
+ }
+ HttpURLConnection httpConnection = (HttpURLConnection) connection;
+ httpConnection.setUseCaches(false);
+ httpConnection.setDefaultUseCaches(false);
+ try
+ {
+ httpConnection.connect();
+ if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
+ {
+ ObjectInputStream ois = new ObjectInputStream(httpConnection.getInputStream());
+ Object o;
+ try
+ {
+ o = ois.readObject();
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw e;
+ }
+ ois.close();
+ if (!(o instanceof TestResult))
+ {
+ throw new IllegalStateException("Error reading test results - expected a TestResult but got " + o);
+ }
+ TestResult result = (TestResult) o;
+ return result;
+ }
+ else if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND)
+ {
+ throw new IllegalStateException(
+ "Error launching test at " + url + ". " +
+ "Got " + httpConnection.getResponseCode() + " ("+ httpConnection.getResponseMessage() + ")");
+ }
+ try
+ {
+ Thread.sleep(200);
+ }
+ catch (InterruptedException e)
+ {
+ interrupted = true;
+ }
+ }
+ finally
+ {
+ httpConnection.disconnect();
+ }
+ }
+ if (interrupted)
+ {
+ Thread.currentThread().interrupt();
+ }
+ throw new IllegalStateException("Error launching test at " + url + ". Kept on getting 404s.");
+ }
+}
Added: arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletTestRunner.java
===================================================================
--- arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletTestRunner.java (rev 0)
+++ arquillian/trunk/protocol-servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletTestRunner.java 2009-11-24 13:03:10 UTC (rev 3773)
@@ -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.protocol.servlet;
+
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.jboss.arquillian.spi.TestResult;
+import org.jboss.arquillian.spi.TestRunner;
+import org.jboss.arquillian.spi.TestRunner.ExecutionMode;
+import org.jboss.arquillian.spi.util.TestRunners;
+
+/**
+ * ServletTestRunner
+ *
+ * The server side executor for the Servlet protocol impl.
+ *
+ * Supports multiple output modes ("outputmode"):
+ * - html
+ * - serializedObject
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ServletTestRunner extends HttpServlet
+{
+ private static final long serialVersionUID = 1L;
+
+ public static final String PARA_METHOD_NAME = "methodName";
+ public static final String PARA_CLASS_NAME = "className";
+ public static final String PARA_OUTPUT_MODE = "outputMode";
+
+ public static final String OUTPUT_MODE_SERIALIZED = "serializedObject";
+ public static final String OUTPUT_MODE_HTML = "html";
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ String outputMode = OUTPUT_MODE_HTML;
+ try
+ {
+ String className = null;
+ String methodName = null;
+
+ if (request.getParameter(PARA_OUTPUT_MODE) != null)
+ {
+ outputMode = request.getParameter(PARA_OUTPUT_MODE);
+ }
+ if ( ( className = request.getParameter(PARA_CLASS_NAME)) == null)
+ {
+ throw new IllegalArgumentException(PARA_CLASS_NAME + " must be specified");
+ }
+ if ( (methodName = request.getParameter(PARA_METHOD_NAME) ) == null)
+ {
+ throw new IllegalArgumentException(PARA_METHOD_NAME + " must be specified");
+ }
+
+ Class<?> testClass = SecurityActions.getThreadContextClassLoader().loadClass(className);
+
+ TestRunner runner = TestRunners.getTestRunner();
+ runner.setExecutionMode(ExecutionMode.CONTAINER);
+
+ TestResult testResult = runner.execute(testClass, methodName);
+
+ if(OUTPUT_MODE_SERIALIZED.equalsIgnoreCase(outputMode))
+ {
+ writeObject(testResult, response);
+ }
+ else
+ {
+ // TODO: implement a html view of the result
+ response.setContentType("text/html");
+ response.setStatus(HttpServletResponse.SC_OK);
+ PrintWriter writer = response.getWriter();
+ writer.write("<html>\n");
+ writer.write("<head><title>TCK Report</title></head>\n");
+ writer.write("<body>\n");
+ writer.write("<h2>Configuration</h2>\n");
+ writer.write("<table>\n");
+ writer.write("<tr>\n");
+ writer.write("<td><b>Method</b></td><td><b>Status</b></td>\n");
+ writer.write("</tr>\n");
+
+ writer.write("</table>\n");
+ writer.write("<h2>Tests</h2>\n");
+ writer.write("<table>\n");
+ writer.write("<tr>\n");
+ writer.write("<td><b>Method</b></td><td><b>Status</b></td>\n");
+ writer.write("</tr>\n");
+
+ writer.write("</table>\n");
+ writer.write("</body>\n");
+ }
+
+ }
+ catch(Exception e)
+ {
+ if(OUTPUT_MODE_SERIALIZED.equalsIgnoreCase(outputMode))
+ {
+ writeObject(createFailedResult(e), response);
+ }
+ else
+ {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
+ }
+ }
+ }
+
+ private void writeObject(Object object, HttpServletResponse response)
+ {
+ try
+ {
+ ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
+ oos.writeObject(object);
+ response.setStatus(HttpServletResponse.SC_OK);
+ oos.flush();
+ oos.close();
+ }
+ catch (Exception e)
+ {
+ try
+ {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
+ }
+ catch (Exception e2)
+ {
+ throw new RuntimeException("Could not write to output", e2);
+ }
+ }
+ }
+
+ private TestResult createFailedResult(Throwable throwable)
+ {
+ return new FailedResult(throwable);
+ }
+
+ public static class FailedResult implements TestResult {
+
+ private static final long serialVersionUID = 1L;
+
+ private Throwable throwable;
+
+ public FailedResult(Throwable throwable)
+ {
+ this.throwable = throwable;
+ }
+
+ @Override
+ public Status getStatus()
+ {
+ return Status.FAILED;
+ }
+
+ @Override
+ public Throwable getThrowable()
+ {
+ return throwable;
+ }
+ }
+}
Added: arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/MockTestRunner.java
===================================================================
--- arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/MockTestRunner.java (rev 0)
+++ arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/MockTestRunner.java 2009-11-24 13:03:10 UTC (rev 3773)
@@ -0,0 +1,45 @@
+/*
+ * 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.protocol.servlet;
+
+import org.jboss.arquillian.spi.TestResult;
+import org.jboss.arquillian.spi.TestRunner;
+
+/**
+ * MockTestRunner
+ *
+ * TestRunner that will return what you want for testing
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class MockTestRunner implements TestRunner
+{
+ public static TestResult wantedResult;
+
+ @Override
+ public TestResult execute(Class<?> testClass, String methodName)
+ {
+ return wantedResult;
+ }
+
+ @Override
+ public void setExecutionMode(ExecutionMode executionMode)
+ {
+ }
+
+}
Added: arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/ProtocolTestCase.java
===================================================================
--- arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/ProtocolTestCase.java (rev 0)
+++ arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/ProtocolTestCase.java 2009-11-24 13:03:10 UTC (rev 3773)
@@ -0,0 +1,241 @@
+/*
+ * 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.protocol.servlet;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.net.URL;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.spi.TestMethodExecutor;
+import org.jboss.arquillian.spi.TestResult;
+import org.jboss.arquillian.spi.TestResult.Status;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.servlet.Context;
+
+
+/**
+ * ProtocolTestCase
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ProtocolTestCase
+{
+
+ private Server server;
+
+ @Before
+ public void setup() throws Exception
+ {
+ server = new Server(8181);
+ Context root = new Context(server, "/", Context.SESSIONS);
+ root.addServlet(ServletTestRunner.class, "/test");
+ server.start();
+ }
+
+ @After
+ public void cleanup() throws Exception
+ {
+ server.stop();
+ }
+
+ @Test
+ public void shouldReturnTestResult() throws Exception
+ {
+ MockTestRunner.wantedResult = new TestResultImpl(Status.PASSED, null);
+
+ ServletMethodExecutor executor = new ServletMethodExecutor(createBaseURL());
+ TestResult result = executor.invoke(new MockTestExecutor());
+
+ Assert.assertEquals(
+ "Should have returned a passed test",
+ MockTestRunner.wantedResult.getStatus(),
+ result.getStatus());
+
+ Assert.assertNull(
+ "No Exception should have been thrown",
+ result.getThrowable());
+ }
+
+ @Test
+ public void shouldReturnThrownException() throws Exception
+ {
+ MockTestRunner.wantedResult = new TestResultImpl(Status.FAILED, new Exception().fillInStackTrace());
+
+ ServletMethodExecutor executor = new ServletMethodExecutor(createBaseURL());
+ TestResult result = executor.invoke(new MockTestExecutor());
+
+ Assert.assertEquals(
+ "Should have returned a passed test",
+ MockTestRunner.wantedResult.getStatus(),
+ result.getStatus());
+
+ Assert.assertNotNull(
+ "No Exception should have been thrown",
+ result.getThrowable());
+
+ }
+
+ @Test
+ public void shouldReturnExceptionWhenMissingTestClassParameter() throws Exception
+ {
+ URL url = createURL(ServletTestRunner.OUTPUT_MODE_SERIALIZED, null, null);
+ TestResult result = (TestResult)TestUtil.execute(url);
+
+ Assert.assertEquals(
+ "Should have returned a passed test",
+ Status.FAILED,
+ result.getStatus());
+
+ Assert.assertTrue(
+ "No Exception should have been thrown",
+ result.getThrowable() instanceof IllegalArgumentException);
+ }
+
+ @Test
+ public void shouldReturnExceptionWhenMissingMethodParameter() throws Exception
+ {
+ URL url = createURL(ServletTestRunner.OUTPUT_MODE_SERIALIZED, "org.my.test", null);
+ TestResult result = (TestResult)TestUtil.execute(url);
+
+ Assert.assertEquals(
+ "Should have returned a passed test",
+ Status.FAILED,
+ result.getStatus());
+
+ Assert.assertTrue(
+ "No Exception should have been thrown",
+ result.getThrowable() instanceof IllegalArgumentException);
+ }
+
+ @Test
+ public void shouldReturnExceptionWhenErrorLoadingClass() throws Exception
+ {
+ URL url = createURL(ServletTestRunner.OUTPUT_MODE_SERIALIZED, "org.my.test", "test");
+ TestResult result = (TestResult)TestUtil.execute(url);
+
+ Assert.assertEquals(
+ "Should have returned a passed test",
+ Status.FAILED,
+ result.getStatus());
+
+ Assert.assertTrue(
+ "No Exception should have been thrown",
+ result.getThrowable() instanceof ClassNotFoundException);
+ }
+
+ private URL createBaseURL() {
+ try
+ {
+ return new URL("http", "localhost", server.getConnectors()[0].getPort(), "/test");
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not create URL", e);
+ }
+ }
+
+ private URL createURL(String outputMode, String testClass, String methodName)
+ {
+ StringBuilder url = new StringBuilder(createBaseURL().toExternalForm());
+ boolean first = true;
+ if(outputMode != null)
+ {
+ if(first) {first = false; url.append("?"); } else { url.append("&"); }
+ url.append(ServletTestRunner.PARA_OUTPUT_MODE).append("=").append(outputMode);
+ }
+ if(testClass != null)
+ {
+ if(first) {first = false; url.append("?"); } else { url.append("&"); }
+ url.append(ServletTestRunner.PARA_CLASS_NAME).append("=").append(testClass);
+ }
+ if(methodName != null)
+ {
+ if(first) {first = false; url.append("?"); } else { url.append("&"); }
+ url.append(ServletTestRunner.PARA_METHOD_NAME).append("=").append(methodName);
+ }
+
+ try
+ {
+ return new URL(url.toString());
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not create url", e);
+ }
+ }
+
+ public static class TestResultImpl implements TestResult {
+
+ private static final long serialVersionUID = 1L;
+
+ private Status status;
+ private Throwable throwable;
+
+ public TestResultImpl(Status status, Throwable throwable)
+ {
+ this.status = status;
+ this.throwable = throwable;
+ }
+
+ @Override
+ public Status getStatus()
+ {
+ return status;
+ }
+
+ @Override
+ public Throwable getThrowable()
+ {
+ return throwable;
+ }
+ }
+
+ public static class MockTestExecutor implements TestMethodExecutor, Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void invoke() throws Throwable
+ {
+ }
+
+ @Override
+ public Method getMethod()
+ {
+ try
+ {
+ return this.getClass().getMethod("getMethod");
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not find my own method ?? ");
+ }
+ }
+
+ @Override
+ public Object getInstance()
+ {
+ return this;
+ }
+ }
+}
Added: arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/TestUtil.java
===================================================================
--- arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/TestUtil.java (rev 0)
+++ arquillian/trunk/protocol-servlet/src/test/java/org/jboss/arquillian/protocol/servlet/TestUtil.java 2009-11-24 13:03:10 UTC (rev 3773)
@@ -0,0 +1,68 @@
+/*
+ * 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.protocol.servlet;
+
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.net.URL;
+
+/**
+ * TestUtil
+ *
+ * Internal helper for testcase to do http request
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+class TestUtil
+{
+ private TestUtil() {}
+
+ public static Object execute(URL url)
+ {
+ ObjectInputStream input = null;
+ try
+ {
+ input = new ObjectInputStream(url.openStream());
+ return input.readObject();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not fetch url " + url);
+ }
+ finally
+ {
+ close(input);
+ }
+ }
+
+ private static void close(InputStream input)
+ {
+ if(input == null)
+ {
+ return;
+ }
+ try
+ {
+ input.close();
+ }
+ catch (Exception e)
+ {
+ // ignore
+ }
+ }
+}
Added: arquillian/trunk/protocol-servlet/src/test/resources/META-INF/services/org.jboss.arquillian.spi.TestRunner
===================================================================
--- arquillian/trunk/protocol-servlet/src/test/resources/META-INF/services/org.jboss.arquillian.spi.TestRunner (rev 0)
+++ arquillian/trunk/protocol-servlet/src/test/resources/META-INF/services/org.jboss.arquillian.spi.TestRunner 2009-11-24 13:03:10 UTC (rev 3773)
@@ -0,0 +1 @@
+org.jboss.arquillian.protocol.servlet.MockTestRunner
\ No newline at end of file
More information about the jboss-svn-commits
mailing list