public void testSpecificBinderInitializingCommandProvidingFormController() throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            StaticPortletApplicationContext wac = new StaticPortletApplicationContext();
            wac.registerBeanDefinition(
                "controller",
                new RootBeanDefinition(
                    MySpecificBinderInitializingCommandProvidingFormController.class));
            wac.refresh();
            return wac;
          }

          protected void render(ModelAndView mv, RenderRequest request, RenderResponse response)
              throws Exception {
            new TestView().render(mv.getViewName(), mv.getModel(), request, response);
          }
        };
    portlet.init(new MockPortletConfig());

    MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("defaultName", "myDefaultName");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    MockRenderResponse response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals(
        "myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue",
        response.getContentAsString());
  }
  public void testTypedCommandProvidingFormController() throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            GenericWebApplicationContext wac = new GenericWebApplicationContext();
            wac.registerBeanDefinition(
                "controller", new RootBeanDefinition(MyTypedCommandProvidingFormController.class));
            wac.registerBeanDefinition(
                "controller2",
                new RootBeanDefinition(MyOtherTypedCommandProvidingFormController.class));
            RootBeanDefinition adapterDef =
                new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
            adapterDef
                .getPropertyValues()
                .addPropertyValue("webBindingInitializer", new MyWebBindingInitializer());
            adapterDef
                .getPropertyValues()
                .addPropertyValue("customArgumentResolver", new MySpecialArgumentResolver());
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
            wac.refresh();
            return wac;
          }

          protected void render(ModelAndView mv, RenderRequest request, RenderResponse response)
              throws Exception {
            new TestView().render(mv.getViewName(), mv.getModel(), request, response);
          }
        };
    portlet.init(new MockPortletConfig());

    MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("myParam", "myValue");
    request.addParameter("defaultName", "10");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    MockRenderResponse response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals(
        "myView-Integer:10-typeMismatch-tb1-myOriginalValue", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("myParam", "myOtherValue");
    request.addParameter("defaultName", "10");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals(
        "myOtherView-Integer:10-typeMismatch-tb1-myOriginalValue", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "myValue");
    request.addParameter("defaultName", "10");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myView-myName-typeMismatch-tb1-myOriginalValue", response.getContentAsString());
  }
  public void testModelFormController() throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            GenericWebApplicationContext wac = new GenericWebApplicationContext();
            wac.registerBeanDefinition(
                "controller", new RootBeanDefinition(MyModelFormController.class));
            wac.refresh();
            return wac;
          }

          protected void render(ModelAndView mv, RenderRequest request, RenderResponse response)
              throws Exception {
            new TestView().render(mv.getViewName(), mv.getModel(), request, response);
          }
        };
    portlet.init(new MockPortletConfig());

    MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("name", "name1");
    request.addParameter("age", "value2");
    MockRenderResponse response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myView-name1-typeMismatch-tb1-myValue", response.getContentAsString());
  }
  public void testStandardHandleMethod() throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            GenericWebApplicationContext wac = new GenericWebApplicationContext();
            wac.registerBeanDefinition("controller", new RootBeanDefinition(MyController.class));
            wac.refresh();
            return wac;
          }
        };
    portlet.init(new MockPortletConfig());

    MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
    MockRenderResponse response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("test", response.getContentAsString());
  }
  public void testParameterDispatchingController() throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            StaticPortletApplicationContext wac = new StaticPortletApplicationContext();
            wac.setPortletContext(new MockPortletContext());
            RootBeanDefinition bd = new RootBeanDefinition(MyParameterDispatchingController.class);
            bd.setScope(WebApplicationContext.SCOPE_REQUEST);
            wac.registerBeanDefinition("controller", bd);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.refresh();
            return wac;
          }
        };
    portlet.init(new MockPortletConfig());

    MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
    MockRenderResponse response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("view", "other");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myOtherView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myLangView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("surprise", "!");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
  }
  public void doTestAdaptedHandleMethods(final Class controllerClass) throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            GenericWebApplicationContext wac = new GenericWebApplicationContext();
            wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
            wac.refresh();
            return wac;
          }
        };
    portlet.init(new MockPortletConfig());

    MockActionRequest actionRequest = new MockActionRequest(PortletMode.VIEW);
    MockActionResponse actionResponse = new MockActionResponse();
    portlet.processAction(actionRequest, actionResponse);
    assertEquals("value", actionResponse.getRenderParameter("test"));

    MockRenderRequest request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("param1", "value1");
    request.addParameter("param2", "2");
    MockRenderResponse response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("test-value1-2", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.HELP);
    request.addParameter("name", "name1");
    request.addParameter("age", "2");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("test-name1-2", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.VIEW);
    request.addParameter("name", "name1");
    request.addParameter("age", "value2");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("test-name1-typeMismatch", response.getContentAsString());
  }
  public void testTypeLevelParameterDispatchingController() throws Exception {
    DispatcherPortlet portlet =
        new DispatcherPortlet() {
          protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
              throws BeansException {
            StaticPortletApplicationContext wac = new StaticPortletApplicationContext();
            wac.setPortletContext(new MockPortletContext());
            RootBeanDefinition bd =
                new RootBeanDefinition(MyTypeLevelParameterDispatchingController.class);
            bd.setScope(WebApplicationContext.SCOPE_REQUEST);
            wac.registerBeanDefinition("controller", bd);
            RootBeanDefinition bd2 =
                new RootBeanDefinition(MySpecialParameterDispatchingController.class);
            bd2.setScope(WebApplicationContext.SCOPE_REQUEST);
            wac.registerBeanDefinition("controller2", bd2);
            RootBeanDefinition bd3 =
                new RootBeanDefinition(MyOtherSpecialParameterDispatchingController.class);
            bd3.setScope(WebApplicationContext.SCOPE_REQUEST);
            wac.registerBeanDefinition("controller3", bd3);
            RootBeanDefinition bd4 = new RootBeanDefinition(MyParameterDispatchingController.class);
            bd4.setScope(WebApplicationContext.SCOPE_REQUEST);
            wac.registerBeanDefinition("controller4", bd4);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.refresh();
            return wac;
          }
        };
    portlet.init(new MockPortletConfig());

    MockRenderRequest request = new MockRenderRequest(PortletMode.HELP);
    MockRenderResponse response = new MockRenderResponse();
    try {
      portlet.render(request, response);
      fail("Should have thrown UnavailableException");
    } catch (UnavailableException ex) {
      // expected
    }

    request = new MockRenderRequest(PortletMode.EDIT);
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myDefaultView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "myValue");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "mySpecialValue");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("mySpecialView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "myOtherSpecialValue");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myOtherSpecialView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.VIEW);
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "myValue");
    request.addParameter("view", "other");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myOtherView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "myValue");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("myLangView", response.getContentAsString());

    request = new MockRenderRequest(PortletMode.EDIT);
    request.addParameter("myParam", "myValue");
    request.addParameter("surprise", "!");
    response = new MockRenderResponse();
    portlet.render(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
  }