1 /** 2 * Copyright (C) 2010 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 17 package com.google.inject.servlet; 18 19 import com.google.common.collect.Lists; 20 import com.google.inject.Binding; 21 import com.google.inject.Guice; 22 import com.google.inject.Injector; 23 import com.google.inject.spi.DefaultBindingTargetVisitor; 24 import com.google.inject.spi.Elements; 25 26 import junit.framework.TestCase; 27 28 import java.util.List; 29 30 /** 31 * Tests for ServletModule, to ensure it captures bindings correctly. 32 * 33 * @author sameb@google.com (Sam Berlin) 34 */ 35 public class ServletModuleTest extends TestCase { 36 testServletModuleCallOutsideConfigure()37 public void testServletModuleCallOutsideConfigure() { 38 try { 39 new ServletModule() {{ 40 serve("/*").with(DummyServlet.class); 41 }}; 42 fail(); 43 } catch (IllegalStateException e) { 44 // Expected. 45 } 46 } 47 testServletModuleReuse()48 public void testServletModuleReuse() { 49 Module module = new Module(); 50 Elements.getElements(module); // use the module once (to, say, introspect bindings) 51 Injector injector = Guice.createInjector(module); // use it again. 52 53 Visitor visitor = new Visitor(); 54 // Validate only a single servlet binding & a single filter binding exist. 55 for(Binding<?> binding : injector.getAllBindings().values()) { 56 binding.acceptTargetVisitor(visitor); 57 } 58 assertEquals("wrong linked servlets: " + visitor.linkedServlets, 59 0, visitor.linkedServlets.size()); 60 assertEquals("wrong linked filters: " + visitor.linkedFilters, 61 0, visitor.linkedFilters.size()); 62 assertEquals("wrong instance servlets: " + visitor.instanceServlets, 63 1, visitor.instanceServlets.size()); 64 assertEquals("wrong instance filters: " + visitor.instanceFilters, 65 1, visitor.instanceFilters.size()); 66 } 67 68 private static class Module extends ServletModule { 69 @Override configureServlets()70 protected void configureServlets() { 71 serve("/sam/*").with(new DummyServlet()); 72 filter("/tara/*").through(new DummyFilterImpl()); 73 } 74 } 75 76 private static class Visitor extends DefaultBindingTargetVisitor<Object, Void> implements 77 ServletModuleTargetVisitor<Object, Void> { 78 List<LinkedFilterBinding> linkedFilters = Lists.newArrayList(); 79 List<LinkedServletBinding> linkedServlets = Lists.newArrayList(); 80 List<InstanceFilterBinding> instanceFilters = Lists.newArrayList(); 81 List<InstanceServletBinding> instanceServlets = Lists.newArrayList(); 82 visit(LinkedFilterBinding binding)83 public Void visit(LinkedFilterBinding binding) { 84 linkedFilters.add(binding); 85 return null; 86 } 87 visit(InstanceFilterBinding binding)88 public Void visit(InstanceFilterBinding binding) { 89 instanceFilters.add(binding); 90 return null; 91 } 92 visit(LinkedServletBinding binding)93 public Void visit(LinkedServletBinding binding) { 94 linkedServlets.add(binding); 95 return null; 96 } 97 visit(InstanceServletBinding binding)98 public Void visit(InstanceServletBinding binding) { 99 instanceServlets.add(binding); 100 return null; 101 } 102 } 103 104 } 105