Skip to content

Commit

Permalink
Merge branch 'BaseArquillianTest' of https://github.com/smigielski/wi…
Browse files Browse the repository at this point in the history
…cket into smigielski-BaseArquillianTest
  • Loading branch information
sbryzak committed Mar 24, 2011
2 parents 7cdff3c + 3496626 commit da80688
Show file tree
Hide file tree
Showing 23 changed files with 1,197 additions and 1 deletion.
51 changes: 51 additions & 0 deletions mock/pom.xml
Expand Up @@ -65,6 +65,17 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-junit</artifactId>
</dependency>


<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
Expand Down Expand Up @@ -112,6 +123,46 @@
</plugins>
</build>
</profile>
<profile>
<!-- An optional Arquillian testing profile that executes tests in a remote JBoss AS instance -->
<!-- Run with 'mvn clean test -Pjbossas-remote-6' -->
<id>jbossas-remote-6</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-jbossas-remote-6</artifactId>
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>
<!-- Java EE 6 API dependency -->
<!-- This one dependency imports all APIs available for a Java EE 6.0 application -->
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<type>pom</type>
<scope>provided</scope>
</dependency>
<!-- needed for org.jnp.interfaces.NamingContextFactory -->
<dependency>
<groupId>org.jboss.jbossas</groupId>
<artifactId>jboss-as-client</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<!-- Overrides default configuration to use alternate persistence.xml with Hibernate settings and declare a JBoss AS Datasource -->
<!-- Used by Arquillian -->
<testResource>
<directory>src/test/resources-jbossas</directory>
</testResource>
</testResources>
</build>
</profile>
</profiles>

</project>
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., 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.seam.wicket.mock;

import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.jboss.seam.wicket.SeamRequestCycle;

/**
* Simple request cycle with manual control of detaching conversation context and rest of wicket stuff.
* @author <a href="http://community.jboss.org/people/smigielski">Marek
* Smigielski</a>
*
*/
public class SeamTestRequestCycle extends SeamRequestCycle
{
private boolean detach;


public SeamTestRequestCycle(WebApplication application, WebRequest request, Response response,boolean detach)
{
super(application, request, response);
this.detach=detach;
}

/** Detach only when you know that conversation scope isn't need any more.
* @see org.jboss.seam.wicket.SeamRequestCycle#detach()
*/
@Override
public void detach()
{
if(isDetach()){
super.detach();
}
}

public void superDetach()
{
super.detach();
}


public boolean isDetach()
{
return detach;
}

public void setDetach(boolean detach)
{
this.detach = detach;
}
}
128 changes: 127 additions & 1 deletion mock/src/main/java/org/jboss/seam/wicket/mock/SeamWicketTester.java
@@ -1,16 +1,25 @@
package org.jboss.seam.wicket.mock;

import javax.inject.Inject;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore;
import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.util.tester.DummyHomePage;
import org.apache.wicket.util.tester.WicketTester;
import org.jboss.seam.wicket.SeamApplication;
import org.jboss.seam.wicket.SeamRequestCycle;
import org.jboss.weld.context.http.HttpRequestContext;

/**
* A helper class for testing Seam Pages containing CDI beans. The
Expand All @@ -27,12 +36,82 @@
public class SeamWicketTester extends WicketTester
{

@Inject
HttpRequestContext requestContext;

/*
* Override because there is need for manualy restart request context in
* wicket tester.
*
* @see
* org.apache.wicket.protocol.http.MockWebApplication#setupRequestAndResponse
* (boolean)
*/
@Override
public WebRequestCycle setupRequestAndResponse(boolean isAjax)
{
if (requestContext.isActive())
{
endRequest(getServletRequest());
}
WebRequestCycle webRequestCycle = super.setupRequestAndResponse(isAjax);
startRequest(getServletRequest());
return webRequestCycle;
}

/*
* Start the request, providing a data store which will last the lifetime of
* the request
*/

public void startRequest(ServletRequest servletRequest)
{
// Associate the store with the context and acticate the context
requestContext.associate(servletRequest);
requestContext.activate();
}

/*
* End the request, providing the same data store as was used to start the
* request
*/
public void endRequest(ServletRequest servletRequest)
{
try
{
/*
* Invalidate the request (all bean instances will be scheduled for
* destruction)
*/
requestContext.invalidate();
/*
* Deactivate the request, causing all bean instances to be destroyed
* (as the context is invalid)
*/
requestContext.deactivate();
}
finally
{

/*
* Ensure that whatever happens we dissociate to prevent any memory
* leaks
*/
requestContext.dissociate(servletRequest);
}
}

/**
* Default dummy seam web application for testing. Uses
* {@link HttpSessionStore} to store pages and the <code>Session</code>.
*/
public static class DummySeamApplication extends SeamApplication
{
// For manually detaching wicket request object and conversation scope
private SeamTestRequestCycle seamTestRequestCycle;

private boolean manuallyDetach = false;

/**
* @see org.apache.wicket.Application#getHomePage()
*/
Expand Down Expand Up @@ -64,6 +143,53 @@ protected void outputDevelopmentModeWarning()
{
// do nothing
}

/**
* Override to return Seam-specific request cycle with manually detach
* control.
*
* @see SeamRequestCycle
*/
@Override
public RequestCycle newRequestCycle(final Request request, final Response response)
{
seamTestRequestCycle = new SeamTestRequestCycle(this, (WebRequest) request, (WebResponse) response, !manuallyDetach);
return seamTestRequestCycle;
}

/**
* Detach wicket request object and conversation scope. Should be call
* after all tests on current page.
*/
public void detach()
{
if (seamTestRequestCycle != null)
{
seamTestRequestCycle.superDetach();
}
}

/**
* Whether wicket request and conversation scope objects should be
* manually detach or will be detached immediate after rendering page.
*
* @return
*/
public boolean isManuallyDetach()
{
return !seamTestRequestCycle.isDetach();
}

/**
*
* @param manuallyDetach wicket request and conversation scope objects
* instead of immediate after rendering page.
*/
public void setManuallyDetach(boolean manuallyDetach)
{
this.manuallyDetach = manuallyDetach;
this.seamTestRequestCycle.setDetach(!manuallyDetach);
}
}

/**
Expand Down
@@ -0,0 +1,71 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., 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.seam.wicket.test.application;

import java.util.LinkedList;
import java.util.List;

/**
* Abstract object producer which can be injected and control from test class.
*
* @author <a href="http://community.jboss.org/people/smigielski">Marek Smigielski</a>
*
*/
public class AbstractObjectProducer
{

protected List<String> localMessages = new LinkedList<String>();

public AbstractObjectProducer()
{
super();
}

/**
* Returning next message from the list
* @return StringObject wrapping text message
*/
public StringObject getMessage(){
if (localMessages.size() > 0)
{
String value = localMessages.remove(0);
return new StringObject().setValue(value);
}
return new StringObject();
}

/**
* Get local message list for manipulation. List implements LinkedList.
*
* @return local message list
*/
public List<String> getLocalMessages()
{
return localMessages;
}

/**
* Add single text message to the end of the list.
*
* @param text
*/
public void add(String text)
{
localMessages.add(text);
}

}

0 comments on commit da80688

Please sign in to comment.