package com.tacitknowledge.util.migration.jdbc;
import java.util.Iterator;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import junit.framework.TestCase;
import org.mockejb.jndi.MockContextFactory;
import com.mockrunner.mock.jdbc.MockDataSource;
import com.mockrunner.mock.web.MockServletContext;
import com.tacitknowledge.util.migration.MigrationException;
{
public void setUp()
throws Exception
{
super.setUp();
MockContextFactory.setAsInitial();
InitialContext context = new InitialContext();
context.createSubcontext("java");
}
{
super.tearDown();
InitialContext context = new InitialContext();
context.destroySubcontext("java");
}
{
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", "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());
}
{
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", "jdbc/testsource1");
sc.setInitParameter("migration.system2.databasetype", dbType2);
sc.setInitParameter("migration.system2.datasource", "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());
}
}