FileDocCategorySizeDatePackage
FilterDispatcherTest.javaAPI DocExample10711Mon Jul 23 13:26:12 BST 2007org.apache.struts2.dispatcher

FilterDispatcherTest

public class FilterDispatcherTest extends org.apache.struts2.StrutsTestCase
FilterDispatcher TestCase.
version
$Date: 2006-12-08 15:22:37 -0500 (Fri, 08 Dec 2006) $ $Id: FilterDispatcherTest.java 484741 2006-12-08 20:22:37Z mrdon $

Fields Summary
Constructors Summary
Methods Summary
public voidtestCharacterEncodingSetBeforeRequestWrappingAndActionService()

        MockServletContext servletContext = new MockServletContext();
        MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
        MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
        MockHttpServletResponse res = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        final InnerDispatcher _dispatcher = new InnerDispatcher(servletContext);
        Dispatcher.setInstance(null);

        DefaultConfiguration conf = new DefaultConfiguration() {
        	@Override
        	public Container getContainer() {
        		return new ContainerBuilder().create(false);
        	}
        };
       
        ConfigurationManager confManager = new ConfigurationManager();
        confManager.setConfiguration(conf);
        _dispatcher.setConfigurationManager(confManager);


        ObjectFactory.setObjectFactory(new InnerObjectFactory());

        _dispatcher.setDefaultEncoding("UTF-16_DUMMY");

        FilterDispatcher filter = new FilterDispatcher() {
            protected Dispatcher createDispatcher(FilterConfig filterConfig) {
                return _dispatcher;
            }
        };
        filter.setActionMapper(new InnerActionMapper());
        filter.init(filterConfig);
        filter.doFilter(req, res, chain);

        assertTrue(_dispatcher.wrappedRequest);
        assertTrue(_dispatcher.serviceRequest);
    
public voidtestIfActionMapperIsNullDontServiceAction()

        MockServletContext servletContext = new MockServletContext();
        MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
        MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
        MockHttpServletResponse res = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        final NoOpDispatcher _dispatcher = new NoOpDispatcher(servletContext);
        ConfigurationManager confManager = new ConfigurationManager();
        confManager.setConfiguration(new DefaultConfiguration());
        _dispatcher.setConfigurationManager(confManager);
        Dispatcher.setInstance(_dispatcher);

        


        ObjectFactory.setObjectFactory(new InnerObjectFactory());

        FilterDispatcher filter = new FilterDispatcher() {
            protected Dispatcher createDispatcher() {
                return _dispatcher;
            }
        };
        filter.setActionMapper(null);
        filter.init(filterConfig);
        filter.doFilter(req, res, chain);

        assertFalse(_dispatcher.serviceRequest);
    
public voidtestObjectFactoryDestroy()


        final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
        FilterDispatcher filterDispatcher = new FilterDispatcher() {
            @Override
            protected Dispatcher createDispatcher(FilterConfig cfg) {
                return new Dispatcher(cfg.getServletContext(), new HashMap()) {
                    Container cont = new ContainerBuilder()
                        .factory(ObjectFactory.class, new Factory() {
                            public Object create(Context context) throws Exception { return destroyedObjectFactory; }
                        })
                        .create(false);
                    
                    @Override
                    public Container getContainer() {
                        return cont;
                    }
                };
            }
        };
        filterDispatcher.init(new MockFilterConfig((ServletContext) null));
        
        assertFalse(destroyedObjectFactory.destroyed);
        filterDispatcher.destroy();
        assertTrue(destroyedObjectFactory.destroyed);
    
public voidtestParsePackages()


        FilterDispatcher filterDispatcher = new FilterDispatcher();
        String[] result1 = filterDispatcher.parse("foo.bar.package1 foo.bar.package2 foo.bar.package3");
        String[] result2 = filterDispatcher.parse("foo.bar.package1\tfoo.bar.package2\tfoo.bar.package3");
        String[] result3 = filterDispatcher.parse("foo.bar.package1,foo.bar.package2,foo.bar.package3");
        String[] result4 = filterDispatcher.parse("foo.bar.package1    foo.bar.package2  \t foo.bar.package3   , foo.bar.package4");

        assertEquals(result1[0], "foo/bar/package1/");
        assertEquals(result1[1], "foo/bar/package2/");
        assertEquals(result1[2], "foo/bar/package3/");

        assertEquals(result2[0], "foo/bar/package1/");
        assertEquals(result2[1], "foo/bar/package2/");
        assertEquals(result2[2], "foo/bar/package3/");

        assertEquals(result3[0], "foo/bar/package1/");
        assertEquals(result3[1], "foo/bar/package2/");
        assertEquals(result3[2], "foo/bar/package3/");

        assertEquals(result4[0], "foo/bar/package1/");
        assertEquals(result4[1], "foo/bar/package2/");
        assertEquals(result4[2], "foo/bar/package3/");
        assertEquals(result4[3], "foo/bar/package4/");