You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Loritsch, Berin C." <Be...@gd-ais.com> on 2009/11/12 22:12:31 UTC

SpringWicketTester code (tested with Wicket 1.4 and spring-wicket)

import javax.servlet.ServletContext;

import org.apache.wicket.protocol.http.MockServletContext;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.util.tester.WicketTester;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * Spring enabled WicketTester allows a user to test applications that
have
 * been wired using the SpringComponentInjector.  This subclass of the
 * WicketTester sets up the spring web context, which is normally set up
by
 * the org.springframework.web.context.ContextLoaderListener class.
 */
public class SpringWicketTester extends WicketTester {
	/**
	 * The Spring web application context
	 */
	private XmlWebApplicationContext spring;
	
	/**
	 * Instantiate the WicketTester with your application and a set
of URLs
	 * to find the Spring XML configuration files.  Ex:
	 * 
	 * <pre>
	 * new SpringWicketTester(new MyApp(),
"classpath:application.xml",
	 *
"classpath:application-test.xml");
	 * </pre>
	 * 
	 * @param app               Your Wicket web application
	 * @param springConfigURLs  The set of URLs for the
configuration files.
	 */
	public SpringWicketTester(WebApplication app, String...
springConfigURLs) {
		this(app, null, springConfigURLs);
	}
	
	
	/**
	 * Instantiate the WicketTester with your application and a set
of URLs
	 * to find the Spring XML configuration files.  Ex:
	 * 
	 * <pre>
	 * new SpringWicketTester(new MyApp(), "myapp",
	 *
"classpath:application.xml",
	 *
"classpath:application-test.xml");
	 * </pre>
	 * 
	 * @param app               Your Wicket web application
	 * @param context           The base url for the application
	 * @param springConfigURLs  The set of URLs for the
configuration files.
	 */
	public SpringWicketTester(WebApplication app, String context,
String... springConfigURLs) {
		super(app, context);

		reconfigure(springConfigURLs);
	}

	/**
	 * Reconfigure the tester with new spring configuration files.
This method
	 * also calls the Spring <code>refresh</code> method to force
the files to
	 * load.
	 * 
	 * @param springConfigURLs  The set of URLs for the
configuration files.
	 */
	public void reconfigure(String... springConfigURLs) {
		getSpringContext().setConfigLocations(springConfigURLs);
		getSpringContext().refresh();
	}

	/**
	 * Create the new ServletContext that will be used with this
test session.
	 * This method configures the spring web context to be included
in your
	 * Servlet context.  It's the magic that makes everything happy.
	 * 
	 * @param path  the root context path for URLs
	 * 
	 * @return a configured ServletContext
	 */
	@Override
	public ServletContext newServletContext(final String path) {
		ServletContext context = new
MockServletContext(getApplication(), path);
		getSpringContext().setServletContext(context);
	
context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_
ATTRIBUTE, spring);
		return context;
	}
	
	/**
	 * Lazy loader for the creating the SpringContext.  Required to
work around
	 * the initialization in the constructor.
	 * 
	 * @return the single web application context for this tester
	 */
	private XmlWebApplicationContext getSpringContext() {
		if(null == spring) {
			spring = new XmlWebApplicationContext();
		}
		
		return spring;
	}
}

Re: SpringWicketTester code (tested with Wicket 1.4 and spring-wicket)

Posted by Peter Gardfjell <pe...@gmail.com>.
Great post!

I had difficulties getting the wicket tester up and running with my
Spring-enabled app but your class worked like a charm.

Wouldn't it be a good idea to contribute that code to the wicket-spring
subproject?

best regards, Peter
-- 
View this message in context: http://old.nabble.com/SpringWicketTester-code-%28tested-with-Wicket-1.4-and-spring-wicket%29-tp26326536p26765130.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: SpringWicketTester code (tested with Wicket 1.4 and spring-wicket)

Posted by benjamintilford <be...@yahoo.com>.
Does this handle transactions (e.g. rolls transactions back at the end of a
test method) like the test runners provided by Spring?


Loritsch, Berin C. wrote:
> 
> import javax.servlet.ServletContext;
> 
> import org.apache.wicket.protocol.http.MockServletContext;
> import org.apache.wicket.protocol.http.WebApplication;
> import org.apache.wicket.util.tester.WicketTester;
> import org.springframework.web.context.WebApplicationContext;
> import org.springframework.web.context.support.XmlWebApplicationContext;
> 
> /**
>  * Spring enabled WicketTester allows a user to test applications that
> have
>  * been wired using the SpringComponentInjector.  This subclass of the
>  * WicketTester sets up the spring web context, which is normally set up
> by
>  * the org.springframework.web.context.ContextLoaderListener class.
>  */
> public class SpringWicketTester extends WicketTester {
> 	/**
> 	 * The Spring web application context
> 	 */
> 	private XmlWebApplicationContext spring;
> 	
> 	/**
> 	 * Instantiate the WicketTester with your application and a set
> of URLs
> 	 * to find the Spring XML configuration files.  Ex:
> 	 * 
> 	 * <pre>
> 	 * new SpringWicketTester(new MyApp(),
> "classpath:application.xml",
> 	 *
> "classpath:application-test.xml");
> 	 * </pre>
> 	 * 
> 	 * @param app               Your Wicket web application
> 	 * @param springConfigURLs  The set of URLs for the
> configuration files.
> 	 */
> 	public SpringWicketTester(WebApplication app, String...
> springConfigURLs) {
> 		this(app, null, springConfigURLs);
> 	}
> 	
> 	
> 	/**
> 	 * Instantiate the WicketTester with your application and a set
> of URLs
> 	 * to find the Spring XML configuration files.  Ex:
> 	 * 
> 	 * <pre>
> 	 * new SpringWicketTester(new MyApp(), "myapp",
> 	 *
> "classpath:application.xml",
> 	 *
> "classpath:application-test.xml");
> 	 * </pre>
> 	 * 
> 	 * @param app               Your Wicket web application
> 	 * @param context           The base url for the application
> 	 * @param springConfigURLs  The set of URLs for the
> configuration files.
> 	 */
> 	public SpringWicketTester(WebApplication app, String context,
> String... springConfigURLs) {
> 		super(app, context);
> 
> 		reconfigure(springConfigURLs);
> 	}
> 
> 	/**
> 	 * Reconfigure the tester with new spring configuration files.
> This method
> 	 * also calls the Spring <code>refresh</code> method to force
> the files to
> 	 * load.
> 	 * 
> 	 * @param springConfigURLs  The set of URLs for the
> configuration files.
> 	 */
> 	public void reconfigure(String... springConfigURLs) {
> 		getSpringContext().setConfigLocations(springConfigURLs);
> 		getSpringContext().refresh();
> 	}
> 
> 	/**
> 	 * Create the new ServletContext that will be used with this
> test session.
> 	 * This method configures the spring web context to be included
> in your
> 	 * Servlet context.  It's the magic that makes everything happy.
> 	 * 
> 	 * @param path  the root context path for URLs
> 	 * 
> 	 * @return a configured ServletContext
> 	 */
> 	@Override
> 	public ServletContext newServletContext(final String path) {
> 		ServletContext context = new
> MockServletContext(getApplication(), path);
> 		getSpringContext().setServletContext(context);
> 	
> context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_
> ATTRIBUTE, spring);
> 		return context;
> 	}
> 	
> 	/**
> 	 * Lazy loader for the creating the SpringContext.  Required to
> work around
> 	 * the initialization in the constructor.
> 	 * 
> 	 * @return the single web application context for this tester
> 	 */
> 	private XmlWebApplicationContext getSpringContext() {
> 		if(null == spring) {
> 			spring = new XmlWebApplicationContext();
> 		}
> 		
> 		return spring;
> 	}
> }
> 
> 

-- 
View this message in context: http://old.nabble.com/SpringWicketTester-code-%28tested-with-Wicket-1.4-and-spring-wicket%29-tp26326536p26769106.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org