Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andreas Wurzer http://andeee.wordpress.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

testing new bean scopes with spring 2.0 - example

// scoped bean definition
   1  
   2  	<bean id="scopedBean"
   3  		class="org.bla.ScopedBeanImpl"
   4  		scope="session">
   5  		<aop:scoped-proxy />
   6  	</bean>

// an example test case
   1  
   2  public class ScopedBeanTest extends AbstractRequestContextFilterTestBase {
   3  
   4  	protected String[] getConfigLocations() {
   5  		return new String[] { "classpath:applicationContext-with-scopedBean.xml" };
   6  	}
   7  
   8  
   9  	@Test
  10  	public void testScopedBean() throws Exception {
  11  
  12  		new FilterTest(new FilterChain() {
  13  
  14  			public void doFilter(ServletRequest arg0, ServletResponse arg1)
  15  					throws IOException, ServletException {
  16  				ScopedBean scopedBean = (ScopedBean) applicationContext
  17  						.getBean("scopedBean");
  18  				...
  19  				/* 
  20  				 * let the classses under test do something with your scopedBean
  21  				 * and check if modifications are done correctly 
  22  				 */
  23  				...
  24  			}
  25  
  26  		}).run();
  27  
  28  	}

testing new bean scopes with spring 2.0

   1  
   2  import javax.servlet.FilterChain;
   3  
   4  import org.springframework.context.ConfigurableApplicationContext;
   5  import org.springframework.mock.web.MockFilterConfig;
   6  import org.springframework.mock.web.MockHttpServletRequest;
   7  import org.springframework.mock.web.MockHttpServletResponse;
   8  import org.springframework.mock.web.MockHttpSession;
   9  import org.springframework.mock.web.MockServletContext;
  10  import org.springframework.test.AbstractSingleSpringContextTests;
  11  import org.springframework.util.StringUtils;
  12  import org.springframework.web.context.support.XmlWebApplicationContext;
  13  import org.springframework.web.filter.RequestContextFilter;
  14  
  15  abstract public class AbstractRequestContextFilterTestBase extends
  16  		AbstractSingleSpringContextTests {
  17  
  18  	private static MockServletContext context;
  19  
  20  	private MockHttpServletResponse response;
  21  
  22  	private MockHttpServletRequest request;
  23  
  24  	private static MockHttpSession session;
  25  
  26  	private static RequestContextFilter contextFilter;
  27  
  28  	static {
  29  
  30  		context = new MockServletContext();
  31  
  32  		MockFilterConfig filterConfig = new MockFilterConfig(context,
  33  				"filterConfig");
  34  
  35  		contextFilter = new RequestContextFilter();
  36  		contextFilter.setFilterConfig(filterConfig);
  37  
  38  		session = new MockHttpSession(context);
  39  	}
  40  
  41  	@Override
  42  	protected void onSetUp() throws Exception {
  43  		super.onSetUp();
  44  
  45  		// new request/response for every test method
  46  		request = new MockHttpServletRequest(context);
  47  		request.setSession(session);
  48  		response = new MockHttpServletResponse();
  49  	}
  50  
  51  	@Override
  52  	protected ConfigurableApplicationContext loadContextLocations(
  53  			String[] locations) throws Exception {
  54  
  55  		if (logger.isInfoEnabled()) {
  56  			logger.info("Loading web application context for: "
  57  					+ StringUtils.arrayToCommaDelimitedString(locations));
  58  		}
  59  
  60  		XmlWebApplicationContext ctx = new XmlWebApplicationContext();
  61  		ctx.setConfigLocations(locations);
  62  		ctx.setServletContext(context);
  63  		ctx.refresh();
  64  		return ctx;
  65  	}
  66  
  67  	protected final class FilterTest {
  68  
  69  		private FilterChain chain;
  70  
  71  		public FilterTest(FilterChain chain) {
  72  			this.chain = chain;
  73  		}
  74  
  75  		// execute the filter
  76  		public final void run() throws Exception {
  77  			contextFilter.doFilter(request, response, chain);
  78  		}
  79  	}
  80  
  81  }
  82  
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS