• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.google.inject.servlet;
2 
3 import static org.easymock.EasyMock.createMock;
4 
5 import com.google.inject.Guice;
6 import com.google.inject.Scopes;
7 import com.google.inject.Singleton;
8 
9 import junit.framework.TestCase;
10 
11 import javax.servlet.FilterConfig;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 
15 /**
16  * Ensures that an error is thrown if a Servlet or Filter is bound
17  * under any scope other than singleton, explicitly.
18  *
19  * @author dhanji@gmail.com
20  */
21 public class InvalidScopeBindingTest extends TestCase {
22 
23   @Override
tearDown()24   protected void tearDown() throws Exception {
25     GuiceFilter.reset();
26   }
27 
testServletInNonSingletonScopeThrowsServletException()28   public final void testServletInNonSingletonScopeThrowsServletException(){
29     GuiceFilter guiceFilter = new GuiceFilter();
30 
31     Guice.createInjector(new ServletModule() {
32       @Override
33       protected void configureServlets() {
34         serve("/*").with(MyNonSingletonServlet.class);
35       }
36     });
37 
38     ServletException se = null;
39     try {
40       guiceFilter.init(createMock(FilterConfig.class));
41     } catch (ServletException e) {
42       se = e;
43     } finally {
44       assertNotNull("Servlet exception was not thrown with wrong scope binding", se);
45     }
46   }
47 
testFilterInNonSingletonScopeThrowsServletException()48   public final void testFilterInNonSingletonScopeThrowsServletException(){
49     GuiceFilter guiceFilter = new GuiceFilter();
50 
51     Guice.createInjector(new ServletModule() {
52       @Override
53       protected void configureServlets() {
54         filter("/*").through(MyNonSingletonFilter.class);
55       }
56     });
57 
58     ServletException se = null;
59     try {
60       guiceFilter.init(createMock(FilterConfig.class));
61     } catch (ServletException e) {
62       se = e;
63     } finally {
64       assertNotNull("Servlet exception was not thrown with wrong scope binding", se);
65     }
66   }
67 
testHappyCaseFilter()68   public final void testHappyCaseFilter(){
69     GuiceFilter guiceFilter = new GuiceFilter();
70 
71     Guice.createInjector(new ServletModule() {
72       @Override
73       protected void configureServlets() {
74         // Annotated scoping variant.
75         filter("/*").through(MySingletonFilter.class);
76 
77         // Explicit scoping variant.
78         bind(DummyFilterImpl.class).in(Scopes.SINGLETON);
79         filter("/*").through(DummyFilterImpl.class);
80       }
81     });
82 
83     ServletException se = null;
84     try {
85       guiceFilter.init(createMock(FilterConfig.class));
86     } catch (ServletException e) {
87       se = e;
88     } finally {
89       assertNull("Servlet exception was thrown with correct scope binding", se);
90     }
91   }
92 
93   @RequestScoped
94   public static class MyNonSingletonServlet extends HttpServlet { }
95 
96   @SessionScoped
97   public static class MyNonSingletonFilter extends DummyFilterImpl { }
98 
99   @Singleton
100   public static class MySingletonFilter extends DummyFilterImpl { }
101 }
102