Search in sources :

Example 6 with MockServletContext

use of com.mockrunner.mock.web.MockServletContext in project cayenne by apache.

the class WebUtilTest method testGetCayenneRuntime.

@Test
public void testGetCayenneRuntime() {
    MockServletContext context = new MockServletContext();
    assertNull(WebUtil.getCayenneRuntime(context));
    CayenneRuntime runtime = mock(CayenneRuntime.class);
    WebUtil.setCayenneRuntime(context, runtime);
    assertSame(runtime, WebUtil.getCayenneRuntime(context));
    CayenneRuntime runtime1 = mock(CayenneRuntime.class);
    WebUtil.setCayenneRuntime(context, runtime1);
    assertSame(runtime1, WebUtil.getCayenneRuntime(context));
    WebUtil.setCayenneRuntime(context, null);
    assertNull(WebUtil.getCayenneRuntime(context));
}
Also used : CayenneRuntime(org.apache.cayenne.configuration.CayenneRuntime) MockServletContext(com.mockrunner.mock.web.MockServletContext) Test(org.junit.Test)

Example 7 with MockServletContext

use of com.mockrunner.mock.web.MockServletContext in project cayenne by apache.

the class ROPServletTest method testInitWithLocation.

@Test
public void testInitWithLocation() throws Exception {
    String location = "cayenne-org.apache.cayenne.configuration.rop.server.test-config.xml";
    MockServletConfig config = new MockServletConfig();
    config.setServletName("abc");
    config.setInitParameter("configuration-location", location);
    MockServletContext context = new MockServletContext();
    config.setServletContext(context);
    ROPServlet servlet = new ROPServlet();
    servlet.init(config);
    runtime = WebUtil.getCayenneRuntime(context);
    assertNotNull(runtime);
    List<String> locations = runtime.getInjector().getInstance(Key.getListOf(String.class, Constants.SERVER_PROJECT_LOCATIONS_LIST));
    assertEquals(Arrays.asList(location), locations);
}
Also used : MockServletConfig(com.mockrunner.mock.web.MockServletConfig) ROPServlet(org.apache.cayenne.rop.ROPServlet) MockServletContext(com.mockrunner.mock.web.MockServletContext) Test(org.junit.Test)

Example 8 with MockServletContext

use of com.mockrunner.mock.web.MockServletContext in project cayenne by apache.

the class ROPServletTest method testInitWithStandardModules.

@Test
public void testInitWithStandardModules() throws Exception {
    String name = "cayenne-org.apache.cayenne.configuration.rop.server.test-config";
    MockServletConfig config = new MockServletConfig();
    config.setServletName(name);
    MockServletContext context = new MockServletContext();
    config.setServletContext(context);
    ROPServlet servlet = new ROPServlet();
    servlet.init(config);
    runtime = WebUtil.getCayenneRuntime(context);
    assertNotNull(runtime);
    List<String> locations = runtime.getInjector().getInstance(Key.getListOf(String.class, Constants.SERVER_PROJECT_LOCATIONS_LIST));
    assertEquals(Arrays.asList(name + ".xml"), locations);
    Collection<Module> modules = runtime.getModules();
    assertEquals(4, modules.size());
    Object[] marray = modules.toArray();
    if (marray[0] instanceof ServerModule) {
        assertTrue(marray[1] instanceof WebModule);
    } else {
        assertTrue(marray[0] instanceof WebModule);
    }
    assertTrue(marray[2] instanceof ROPServerModule);
}
Also used : MockServletConfig(com.mockrunner.mock.web.MockServletConfig) WebModule(org.apache.cayenne.configuration.web.WebModule) ROPServlet(org.apache.cayenne.rop.ROPServlet) Module(org.apache.cayenne.di.Module) WebModule(org.apache.cayenne.configuration.web.WebModule) ServerModule(org.apache.cayenne.configuration.server.ServerModule) MockServletContext(com.mockrunner.mock.web.MockServletContext) ServerModule(org.apache.cayenne.configuration.server.ServerModule) Test(org.junit.Test)

Example 9 with MockServletContext

use of com.mockrunner.mock.web.MockServletContext in project cayenne by apache.

the class ROPServletTest method testInitHessianService.

@Test
public void testInitHessianService() throws Exception {
    MockServletConfig config = new MockServletConfig();
    config.setServletName("abc");
    MockServletContext context = new MockServletContext();
    config.setServletContext(context);
    config.setInitParameter("extra-modules", ROPHessianServlet_ConfigModule.class.getName());
    ROPServlet servlet = new ROPServlet();
    servlet.init(config);
    runtime = WebUtil.getCayenneRuntime(context);
    Collection<Module> modules = runtime.getModules();
    assertEquals(5, modules.size());
    Object[] marray = modules.toArray();
    assertTrue(marray[3] instanceof ROPHessianServlet_ConfigModule);
// TODO: mock servlet request to check that the right service instance
// is invoked
}
Also used : MockServletConfig(com.mockrunner.mock.web.MockServletConfig) ROPServlet(org.apache.cayenne.rop.ROPServlet) Module(org.apache.cayenne.di.Module) WebModule(org.apache.cayenne.configuration.web.WebModule) ServerModule(org.apache.cayenne.configuration.server.ServerModule) MockServletContext(com.mockrunner.mock.web.MockServletContext) Test(org.junit.Test)

Example 10 with MockServletContext

use of com.mockrunner.mock.web.MockServletContext in project cayenne by apache.

the class CayenneFilterTest method testDoFilter.

@Test
public void testDoFilter() throws Exception {
    MockFilterConfig config = new MockFilterConfig();
    config.setFilterName("abc");
    config.setInitParameter(WebConfiguration.EXTRA_MODULES_PARAMETER, CayenneFilter_DispatchModule.class.getName());
    MockServletContext context = new MockServletContext();
    config.setupServletContext(context);
    CayenneFilter filter = new CayenneFilter();
    filter.init(config);
    CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
    CayenneFilter_DispatchRequestHandler handler = (CayenneFilter_DispatchRequestHandler) runtime.getInjector().getInstance(RequestHandler.class);
    assertEquals(0, handler.getStarted());
    assertEquals(0, handler.getEnded());
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertEquals(1, handler.getStarted());
    assertEquals(1, handler.getEnded());
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertEquals(2, handler.getStarted());
    assertEquals(2, handler.getEnded());
}
Also used : MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) CayenneRuntime(org.apache.cayenne.configuration.CayenneRuntime) MockFilterChain(com.mockrunner.mock.web.MockFilterChain) MockServletContext(com.mockrunner.mock.web.MockServletContext) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockFilterConfig(com.mockrunner.mock.web.MockFilterConfig) Test(org.junit.Test)

Aggregations

MockServletContext (com.mockrunner.mock.web.MockServletContext)11 Test (org.junit.Test)11 CayenneRuntime (org.apache.cayenne.configuration.CayenneRuntime)6 MockFilterConfig (com.mockrunner.mock.web.MockFilterConfig)5 MockServletConfig (com.mockrunner.mock.web.MockServletConfig)5 ServerModule (org.apache.cayenne.configuration.server.ServerModule)5 Module (org.apache.cayenne.di.Module)5 ROPServlet (org.apache.cayenne.rop.ROPServlet)5 WebModule (org.apache.cayenne.configuration.web.WebModule)3 MockFilterChain (com.mockrunner.mock.web.MockFilterChain)1 MockHttpServletRequest (com.mockrunner.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (com.mockrunner.mock.web.MockHttpServletResponse)1 RequestHandler (org.apache.cayenne.configuration.web.RequestHandler)1