public void testGetActionWhenActionsContainerAlreadyExists() {
    MutablePicoContainer requestContainer = new DefaultPicoContainer();
    requestContainer.registerComponentInstance(TestService.class, service);
    MutablePicoContainer actionsContainer = new DefaultPicoContainer(requestContainer);

    requestMock
        .stubs()
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));

    StrutsTestAction action1 =
        (StrutsTestAction) actionFactory.getAction(request, mapping1, servlet);
    StrutsTestAction action2 =
        (StrutsTestAction) actionFactory.getAction(request, mapping2, servlet);
    TestAction action3 = (TestAction) actionFactory.getAction(request, mapping1, servlet);
    TestAction action4 = (TestAction) actionFactory.getAction(request, mapping2, servlet);

    assertNotNull(action1);
    assertNotNull(action2);
    assertNotSame(action1, action2);
    assertSame(action1, action3);
    assertSame(action2, action4);

    assertSame(action1, actionsContainer.getComponentInstance("/myPath1"));
    assertSame(action2, actionsContainer.getComponentInstance("/myPath2"));

    assertSame(service, action1.getService());
    assertSame(service, action2.getService());

    assertNotNull(action1.getServlet());
    assertNotNull(action2.getServlet());
    assertSame(servlet, action1.getServlet());
    assertSame(servlet, action2.getServlet());
  }
  public void testActionContainerCreatedOnlyOncePerRequest() {
    MutablePicoContainer requestContainer = new DefaultPicoContainer();
    requestContainer.registerComponentImplementation(TestService.class);
    MutablePicoContainer actionsContainer = new DefaultPicoContainer(requestContainer);

    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));
    requestMock
        .expects(once())
        .method("setAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER), isA(MutablePicoContainer.class));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.REQUEST_CONTAINER))
        .will(returnValue(requestContainer));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(null));

    actionFactory.getAction(request, mapping1, servlet);
    actionFactory.getAction(request, mapping1, servlet);
    actionFactory.getAction(request, mapping1, servlet);
  }
  public void testNoContainerExists() {
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(null));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.REQUEST_CONTAINER))
        .will(returnValue(null));
    sessionMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.SESSION_CONTAINER))
        .will(returnValue(null));
    servletContextMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.APPLICATION_CONTAINER))
        .will(returnValue(null));

    try {
      actionFactory.getAction(request, mapping1, servlet);
      fail("PicoInitializationException should have been raised");
    } catch (PicoInitializationException e) {
      // expected
    }
  }
  public void testApplicationContainerExists() {
    MutablePicoContainer appContainer = new DefaultPicoContainer();
    appContainer.registerComponentInstance(TestService.class, service);

    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(null));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.REQUEST_CONTAINER))
        .will(returnValue(null));
    sessionMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.SESSION_CONTAINER))
        .will(returnValue(null));
    servletContextMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.APPLICATION_CONTAINER))
        .will(returnValue(appContainer));
    requestMock
        .expects(once())
        .method("setAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER), isA(MutablePicoContainer.class));

    TestAction action = (TestAction) actionFactory.getAction(request, mapping1, servlet);
    assertNotNull(action);
    assertSame(service, action.getService());
  }
  public void testBadActionType() {
    MutablePicoContainer actionsContainer = new DefaultPicoContainer();
    requestMock
        .stubs()
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));

    mapping1.setType("/i/made/a/typo");
    try {
      actionFactory.getAction(request, mapping1, servlet);
      fail("PicoIntrospectionException should have been raised");
    } catch (PicoIntrospectionException e) {
      // expected
    }
  }