home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ServletModuleTest.java)

This example Java source code file (ServletModuleTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

creationexception, defaultbindingtargetvisitor, dummyfilterimpl, dummyservlet, illegalstateexception, injector, list, override, servletmodule, servletmoduletargetvisitor, servletmoduletest, testcase, util, visitor, void

The ServletModuleTest.java Java example source code

 * Copyright (C) 2010 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package com.google.inject.servlet;

import com.google.common.collect.Lists;
import com.google.inject.Binding;
import com.google.inject.CreationException;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.spi.DefaultBindingTargetVisitor;
import com.google.inject.spi.Elements;

import junit.framework.TestCase;

import java.util.List;

 * Tests for ServletModule, to ensure it captures bindings correctly.
 * @author sameb@google.com (Sam Berlin)
public class ServletModuleTest extends TestCase {
  public void testServletModuleCallOutsideConfigure() {
    try {
      new ServletModule() {{
    } catch (IllegalStateException e) {
      // Expected.

  public void testServletModuleReuse() {
    Module module = new Module();
    Elements.getElements(module); // use the module once (to, say, introspect bindings)
    Injector injector = Guice.createInjector(module);  // use it again.
    Visitor visitor = new Visitor();    
    // Validate only a single servlet binding & a single filter binding exist.
    for(Binding<?> binding : injector.getAllBindings().values()) {
    assertEquals("wrong linked servlets: " + visitor.linkedServlets,
        0, visitor.linkedServlets.size());
    assertEquals("wrong linked filters: " + visitor.linkedFilters,
        0, visitor.linkedFilters.size());
    assertEquals("wrong instance servlets: " + visitor.instanceServlets,
        1, visitor.instanceServlets.size());
    assertEquals("wrong instance filters: " + visitor.instanceFilters,
        1, visitor.instanceFilters.size());

  public void testServletModule_badPattern() {
    try {
          new ServletModule() {
            protected void configureServlets() {
              serve("/%2E/*").with(new DummyServlet());
              serveRegex("/(foo|bar/").with(new DummyServlet());
              filter("/%2E/*").through(new DummyFilterImpl());
              filterRegex("/(foo|bar/").through(new DummyFilterImpl());
    } catch (CreationException e) {
      assertEquals(4, e.getErrorMessages().size());

  private static class Module extends ServletModule {
    protected void configureServlets() {
      serve("/sam/*").with(new DummyServlet());
      filter("/tara/*").through(new DummyFilterImpl());

  private static class Visitor extends DefaultBindingTargetVisitor<Object, Void> implements
      ServletModuleTargetVisitor<Object, Void> {
    List<LinkedFilterBinding> linkedFilters = Lists.newArrayList();
    List<LinkedServletBinding> linkedServlets = Lists.newArrayList();
    List<InstanceFilterBinding> instanceFilters = Lists.newArrayList();
    List<InstanceServletBinding> instanceServlets = Lists.newArrayList();
    public Void visit(LinkedFilterBinding binding) {
      return null;

    public Void visit(InstanceFilterBinding binding) {
      return null;

    public Void visit(LinkedServletBinding binding) {
      return null;

    public Void visit(InstanceServletBinding binding) {
      return null;

Other Java examples (source code examples)

Here is a short list of links related to this Java ServletModuleTest.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.