• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.inject.servlet;
17 
18 import com.google.inject.AbstractModule;
19 import com.google.inject.Guice;
20 import com.google.inject.Injector;
21 import com.google.inject.Key;
22 import com.google.inject.Module;
23 import com.google.inject.Singleton;
24 import com.google.inject.Stage;
25 
26 import junit.framework.TestCase;
27 
28 import java.util.HashMap;
29 
30 /**
31  * Sanity checks the EDSL and resultant bound module(s).
32  *
33  * @author Dhanji R. Prasanna (dhanji gmail com)
34  */
35 public class EdslTest extends TestCase {
36 
testExplicitBindingsWorksWithGuiceServlet()37   public final void testExplicitBindingsWorksWithGuiceServlet() {
38     Injector injector = Guice.createInjector(
39         new AbstractModule() {
40           @Override
41           protected void configure() {
42             binder().requireExplicitBindings();
43           }
44         }, new ServletModule() {
45           @Override protected void configureServlets() {
46             bind(DummyServlet.class).in(Singleton.class);
47             serve("/*").with(DummyServlet.class);
48           }
49         });
50 
51     assertNotNull(injector.getInstance(DummyServlet.class));
52   }
53 
testConfigureServlets()54   public final void testConfigureServlets() {
55 
56     //the various possible config calls--
57     Module webModule = new ServletModule() {
58 
59       @Override
60       protected void configureServlets() {
61         filter("/*").through(DummyFilterImpl.class);
62         filter("*.html").through(DummyFilterImpl.class);
63         filter("/*").through(Key.get(DummyFilterImpl.class));
64         filter("/*").through(new DummyFilterImpl());
65 
66         filter("*.html").through(DummyFilterImpl.class,
67             new HashMap<String, String>());
68 
69         filterRegex("/person/[0-9]*").through(DummyFilterImpl.class);
70         filterRegex("/person/[0-9]*").through(DummyFilterImpl.class,
71             new HashMap<String, String>());
72 
73         filterRegex("/person/[0-9]*").through(Key.get(DummyFilterImpl.class));
74         filterRegex("/person/[0-9]*").through(Key.get(DummyFilterImpl.class),
75             new HashMap<String, String>());
76 
77         filterRegex("/person/[0-9]*").through(new DummyFilterImpl());
78         filterRegex("/person/[0-9]*").through(new DummyFilterImpl(),
79             new HashMap<String, String>());
80 
81 
82         serve("/1/*").with(DummyServlet.class);
83         serve("/2/*").with(Key.get(DummyServlet.class));
84         serve("/3/*").with(new DummyServlet());
85         serve("/4/*").with(DummyServlet.class, new HashMap<String, String>());
86 
87         serve("*.htm").with(Key.get(DummyServlet.class));
88         serve("*.html").with(Key.get(DummyServlet.class),
89             new HashMap<String, String>());
90 
91         serveRegex("/person/[0-8]*").with(DummyServlet.class);
92         serveRegex("/person/[0-9]*").with(DummyServlet.class,
93             new HashMap<String, String>());
94 
95         serveRegex("/person/[0-6]*").with(Key.get(DummyServlet.class));
96         serveRegex("/person/[0-9]/2/*").with(Key.get(DummyServlet.class),
97             new HashMap<String, String>());
98 
99         serveRegex("/person/[0-5]*").with(new DummyServlet());
100         serveRegex("/person/[0-9]/3/*").with(new DummyServlet(),
101             new HashMap<String, String>());
102       }
103     };
104 
105     //verify that it doesn't blow up!
106     Guice.createInjector(Stage.TOOL, webModule);
107   }
108 }
109