Java源码示例:com.mockrunner.mock.web.MockServletContext

示例1
/**
 * Tests that the new mechanism for configuring a launcher from a 
 * servlet context works.
 * 
 * @throws NamingException a problem with the test
 */
public void testConfiguredContext() throws NamingException
{
    JdbcMigrationLauncherFactory launcherFactory = new JdbcMigrationLauncherFactory();
    MockServletContext sc = new MockServletContext();
    
    String dbType = "mysql";
    String sysName = "testSystem";
    
    sc.setInitParameter("migration.systemname", sysName);
    sc.setInitParameter("migration.readonly", "true");
    sc.setInitParameter("migration.databasetype", dbType);
    sc.setInitParameter("migration.patchpath", "patches");
    sc.setInitParameter("migration.datasource", "java:comp/env/jdbc/testsource");
    
    MockDataSource ds = new MockDataSource();
    InitialContext context = new InitialContext();
    context.bind("java:comp/env/jdbc/testsource", ds);
    ServletContextEvent sce = new ServletContextEvent(sc);
    JdbcMigrationLauncher launcher = null;
    try
    {
        launcher = launcherFactory.createMigrationLauncher(sce);
    } 
    catch (MigrationException e)
    {
        e.printStackTrace();
        fail("There should not have been an exception");
    }

    JdbcMigrationContext jdbcContext = 
        (JdbcMigrationContext) launcher.getContexts().keySet().iterator().next();
    assertEquals(dbType, jdbcContext.getDatabaseType().getDatabaseType());
    assertEquals(sysName, jdbcContext.getSystemName());
    assertEquals(true, launcher.isReadOnly());
}
 
示例2
/**
 * Tests that the new mechanism for configuring a launcher from a 
 * servlet context works, with multi-node configurations
 * 
 * @throws NamingException a problem with the test
 */
public void testConfiguredMultiNodeContext() throws NamingException
{
    JdbcMigrationLauncherFactory launcherFactory = new JdbcMigrationLauncherFactory();
    MockServletContext sc = new MockServletContext();
    
    String dbType1 = "mysql";
    String dbType2 = "sybase";
    String sysName = "testSystem";
    
    sc.setInitParameter("migration.systemname", sysName);
    sc.setInitParameter("migration.readonly", "true");
    sc.setInitParameter("migration.jdbc.systems", "system1,system2");
    sc.setInitParameter("migration.system1.databasetype", dbType1);
    sc.setInitParameter("migration.system1.datasource", "java:comp/env/jdbc/testsource1");
    sc.setInitParameter("migration.system2.databasetype", dbType2);
    sc.setInitParameter("migration.system2.datasource", "java:comp/env/jdbc/testsource2");
    sc.setInitParameter("migration.patchpath", "patches");
    
    MockDataSource ds = new MockDataSource();
    MockContextFactory.setAsInitial();
    InitialContext context = new InitialContext();
    context.bind("java:comp/env/jdbc/testsource1", ds);
    context.bind("java:comp/env/jdbc/testsource2", ds);
    ServletContextEvent sce = new ServletContextEvent(sc);
    JdbcMigrationLauncher launcher = null;
    try
    {
        launcher = launcherFactory.createMigrationLauncher(sce);
    } 
    catch (MigrationException e)
    {
        e.printStackTrace();
        fail("There should not have been an exception");
    }

    Iterator contextIter = launcher.getContexts().keySet().iterator();
    JdbcMigrationContext jdbcContext1 = (JdbcMigrationContext) contextIter.next();
    JdbcMigrationContext jdbcContext2 = (JdbcMigrationContext) contextIter.next();
    assertEquals(dbType1, jdbcContext1.getDatabaseType().getDatabaseType());
    assertEquals(sysName, jdbcContext1.getSystemName());
    assertEquals(dbType2, jdbcContext2.getDatabaseType().getDatabaseType());
    assertEquals(sysName, jdbcContext2.getSystemName());
    
    assertEquals(true, launcher.isReadOnly());
}
 
示例3
private ServletContext getServletContext() {
    MockServletContext context = new MockServletContext();
    context.setAttribute("app", "OK");

    return context;
}
 
示例4
public void testPageContext() {
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("app", "app");

    MockServletConfig servletConfig = new MockServletConfig();
    servletConfig.setServletContext(servletContext);

    MockHttpSession session = new MockHttpSession();
    session.setupServletContext(servletContext);
    session.setAttribute("session", "session");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("request", "request");
    request.setSession(session);

    MockPageContext pageContext = new MockPageContext();
    pageContext.setServletConfig(servletConfig);
    pageContext.setServletRequest(request);
    pageContext.setAttribute("page", "page");

    assertSame("Request session", session, request.getSession());


    JXPathContext context = JXPathServletContexts.getPageContext(pageContext);
    context.setLenient(true);
    
    checkPointerIterator(context);

    assertEquals("Page Scope", "page", context.getValue("page"));
    assertEquals("Request Scope", "request", context.getValue("request"));
    assertEquals("Session Scope", "session", context.getValue("session"));
    assertEquals("Application Scope", "app", context.getValue("app"));

    assertEquals("Explicit Page Scope", "page", context.getValue("$page/page"));
    assertEquals("Explicit Request Scope", "request", context.getValue("$request/request"));
    assertEquals("Explicit Session Scope", "session", context.getValue("$session/session"));
    assertEquals("Explicit Application Scope", "app", context.getValue("$application/app"));

    // iterate through the elements of page context only (two elements expected, 'page' and the context)
    Iterator it = context.iteratePointers("$page/*");
    assertTrue("element not found", it.hasNext());
    it.next();
    it.next();
    assertFalse("too many elements", it.hasNext());

    // test setting a value in the context
    context.setValue("/foo1", "bar1");
    assertEquals("Context property", "bar1", context.getValue("/foo1"));

    context.setValue("$page/foo2", "bar2");
    assertEquals("Context property", "bar2", context.getValue("$page/foo2"));
}