alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (InvalidScopeBindingTest.java)

This example Java source code file (InvalidScopeBindingTest.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

dummyfilterimpl, guicefilter, http, httpservlet, invalidscopebindingtest, mynonsingletonfilter, mynonsingletonservlet, mysingletonfilter, override, request, requestscoped, response, servlet, servletexception, servletmodule, sessionscoped, singleton, testcase

The InvalidScopeBindingTest.java Java example source code

package com.google.inject.servlet;

import static org.easymock.EasyMock.createMock;

import com.google.inject.Guice;
import com.google.inject.Scopes;
import com.google.inject.Singleton;

import junit.framework.TestCase;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
 * Ensures that an error is thrown if a Servlet or Filter is bound
 * under any scope other than singleton, explicitly.
 *
 * @author dhanji@gmail.com
 */
public class InvalidScopeBindingTest extends TestCase {

  @Override
  protected void tearDown() throws Exception {
    GuiceFilter.reset();
  }

  public final void testServletInNonSingletonScopeThrowsServletException(){
    GuiceFilter guiceFilter = new GuiceFilter();

    Guice.createInjector(new ServletModule() {
      @Override
      protected void configureServlets() {
        serve("/*").with(MyNonSingletonServlet.class);
      }
    });

    ServletException se = null;
    try {
      guiceFilter.init(createMock(FilterConfig.class));
    } catch (ServletException e) {
      se = e;
    } finally {
      assertNotNull("Servlet exception was not thrown with wrong scope binding", se);
    }
  }

  public final void testFilterInNonSingletonScopeThrowsServletException(){
    GuiceFilter guiceFilter = new GuiceFilter();

    Guice.createInjector(new ServletModule() {
      @Override
      protected void configureServlets() {
        filter("/*").through(MyNonSingletonFilter.class);
      }
    });

    ServletException se = null;
    try {
      guiceFilter.init(createMock(FilterConfig.class));
    } catch (ServletException e) {
      se = e;
    } finally {
      assertNotNull("Servlet exception was not thrown with wrong scope binding", se);
    }
  }

  public final void testHappyCaseFilter(){
    GuiceFilter guiceFilter = new GuiceFilter();

    Guice.createInjector(new ServletModule() {
      @Override
      protected void configureServlets() {
        // Annotated scoping variant.
        filter("/*").through(MySingletonFilter.class);

        // Explicit scoping variant.
        bind(DummyFilterImpl.class).in(Scopes.SINGLETON);
        filter("/*").through(DummyFilterImpl.class);
      }
    });

    ServletException se = null;
    try {
      guiceFilter.init(createMock(FilterConfig.class));
    } catch (ServletException e) {
      se = e;
    } finally {
      assertNull("Servlet exception was thrown with correct scope binding", se);
    }
  }

  @RequestScoped
  public static class MyNonSingletonServlet extends HttpServlet { }

  @SessionScoped
  public static class MyNonSingletonFilter extends DummyFilterImpl { }

  @Singleton
  public static class MySingletonFilter extends DummyFilterImpl { }
}

Other Java examples (source code examples)

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

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

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.