aboutsummaryrefslogtreecommitdiff
path: root/test/com/google/inject/servlet/ServletTest.java
blob: a36a2d26aece63c0841ce01d05335a13231baaf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * Copyright (C) 2006 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,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.inject.servlet;

import com.google.inject.Container;
import com.google.inject.ContainerBuilder;
import com.google.inject.ContainerCreationException;
import com.google.inject.Key;

import junit.framework.TestCase;

import static org.easymock.EasyMock.*;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @author crazybob@google.com (Bob Lee)
 */
public class ServletTest extends TestCase {

  public void testNewRequestObject()
      throws ContainerCreationException, IOException, ServletException {
    ContainerBuilder builder = new ContainerBuilder();
    builder.install(new ServletModule());
    builder.bind(InSession.class);
    builder.bind(InRequest.class);
    final Container container = builder.create(false);

    GuiceFilter filter = new GuiceFilter();

    final HttpServletRequest request = createMock(HttpServletRequest.class);

    String requestName = Key.get(InRequest.class).toString();
    expect(request.getAttribute(requestName)).andReturn(null);
    request.setAttribute(eq(requestName), isA(InRequest.class));

    final boolean[] invoked = new boolean[1];
    FilterChain filterChain = new FilterChain() {
      public void doFilter(ServletRequest servletRequest,
          ServletResponse servletResponse) {
        invoked[0] = true;
        assertSame(request, servletRequest);
        assertTrue(container.getInstance(InRequest.class) instanceof InRequest);
      }
    };

    replay(request);

    filter.doFilter(request, null, filterChain);

    verify(request);
    assertTrue(invoked[0]);
  }

  @SessionScoped
  static class InSession {}

  @RequestScoped
  static class InRequest {}
}