Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARQ-1975 Adds arquillian resource provider for servlet context #92

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions protocols/servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>arquillian-container-test-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-impl-base</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.jboss.arquillian.container.test.spi.client.protocol.Protocol;
import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.arquillian.protocol.servlet.runner.ServletContextResourceProvider;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;

/**
* ServletExtension
Expand All @@ -32,6 +34,7 @@ public class ServletExtension implements LoadableExtension
public void register(ExtensionBuilder builder)
{
builder.service(Protocol.class, org.jboss.arquillian.protocol.servlet.v_2_5.ServletProtocol.class)
.service(Protocol.class, org.jboss.arquillian.protocol.servlet.v_3.ServletProtocol.class);
.service(Protocol.class, org.jboss.arquillian.protocol.servlet.v_3.ServletProtocol.class)
.service(ResourceProvider.class, ServletContextResourceProvider.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you need to add a Validate.classExists("javax.servlet.ServletContext") around the ServletContextResourceProvider service registration to avoid requiring the servlet-api classes to be available on client classpath.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix that btw..:)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.protocol.servlet.runner;

import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.core.api.event.ManagerStarted;

import javax.servlet.ServletContext;

public class ServletContextRegistrar {

@Inject
@ApplicationScoped
private InstanceProducer<ServletContext> servletContextInstanceProducer;

public void registerServletContext(@Observes ManagerStarted managerStarted)
{
servletContextInstanceProducer.set(ServletTestRunner.getCurrentServletContext());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.protocol.servlet.runner;


import org.jboss.arquillian.container.test.api.OperateOnDeployment;
import org.jboss.arquillian.container.test.impl.enricher.resource.OperatesOnDeploymentAwareProvider;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;

import javax.servlet.ServletContext;
import java.lang.annotation.Annotation;

/**
* ServletContextResourceProvider
*
* @author asotobu
*/
public class ServletContextResourceProvider extends OperatesOnDeploymentAwareProvider
{

@Inject
Instance<ServletContext> servletContextInstance;

@Override
public boolean canProvide(Class<?> type)
{
return javax.servlet.ServletContext.class.isAssignableFrom(type);
}

@Override
public Object doLookup(ArquillianResource arquillianResource, Annotation... annotations)
{
return servletContextInstance.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
import org.jboss.arquillian.container.test.spi.command.CommandService;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;

/**
* ServletRemoteExtension
Expand All @@ -32,5 +33,7 @@ public class ServletRemoteExtension implements RemoteLoadableExtension
public void register(ExtensionBuilder builder)
{
builder.service(CommandService.class, ServletCommandService.class);
builder.service(ResourceProvider.class, ServletContextResourceProvider.class);
builder.observer(ServletContextRegistrar.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintWriter;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -60,21 +61,29 @@ public class ServletTestRunner extends HttpServlet
public static final String CMD_NAME_TEST = "test";
public static final String CMD_NAME_EVENT = "event";

private static ThreadLocal<ServletContext> currentServletContext;

static ConcurrentHashMap<String, Command<?>> events;
static ThreadLocal<String> currentCall;

public static ServletContext getCurrentServletContext() {
return currentServletContext.get();
}

@Override
public void init() throws ServletException
{
events = new ConcurrentHashMap<String, Command<?>>();
currentCall = new ThreadLocal<String>();
currentServletContext = new ThreadLocal<ServletContext>();
}

@Override
public void destroy()
{
events.clear();
currentCall.remove();
currentServletContext.remove();
}

@Override
Expand Down Expand Up @@ -117,7 +126,10 @@ protected void execute(HttpServletRequest request, HttpServletResponse response)
{
cmd = request.getParameter(PARA_CMD_NAME);
}


if(currentServletContext.get() == null) {
currentServletContext.set(request.getServletContext());
}
currentCall.set(className + methodName);

if(CMD_NAME_TEST.equals(cmd))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void shouldReturnTestResult() throws Exception
"No Exception should have been thrown",
result.getThrowable());
}

@Test
public void shouldReturnThrownException() throws Exception
{
Expand Down