📄 testreplicationwriter.java
字号:
/*------------------------------------------------------------------------------Name: TestReplicationWriter.javaProject: org.xmlBlasterProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.test.contrib.replication;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.logging.Logger;import org.custommonkey.xmlunit.XMLTestCase;import org.custommonkey.xmlunit.XMLUnit;import org.xmlBlaster.contrib.I_Info;import org.xmlBlaster.contrib.PropertiesInfo;import org.xmlBlaster.contrib.db.DbMetaHelper;import org.xmlBlaster.contrib.db.DbPool;import org.xmlBlaster.contrib.db.I_DbPool;import org.xmlBlaster.contrib.dbwriter.I_Parser;import org.xmlBlaster.contrib.dbwriter.SqlInfoParser;import org.xmlBlaster.contrib.dbwriter.I_Writer;import org.xmlBlaster.contrib.dbwriter.info.SqlDescription;import org.xmlBlaster.contrib.dbwriter.info.SqlInfo;import org.xmlBlaster.contrib.dbwriter.info.SqlRow;import org.xmlBlaster.contrib.replication.I_DbSpecific;import org.xmlBlaster.contrib.replication.ReplicationConstants;import org.xmlBlaster.contrib.replication.ReplicationConverter;import org.xmlBlaster.contrib.replication.ReplicationWriter;import org.xmlBlaster.contrib.replication.TableToWatchInfo;import org.xmlBlaster.util.qos.ClientProperty;/** * Tests the functionality of the ReplicationWriter. * * <pre> * java -Ddb=oracle ..... * or if you want to use postgres: * java -Ddb=postgres * </pre> * <p> * <h2>What does this test ?</h2><br/> * <ul> * <li>This test runs without the need of an xmlBlaster server, everything is checked internally.</li> * <li>From an xml statement it creates a SqlInfo object which is then executed on the database. * This way the 'store' operation of this class is tested. * </li> * </ul> * * @author Michele Laghi */public class TestReplicationWriter extends XMLTestCase { private static Logger log = Logger.getLogger(TestReplicationWriter.class.getName()); private I_Info info; private I_DbPool dbPool; private SpecificHelper specificHelper; // this is static since the implementation of I_ChangePublisher is another instance DbMetaHelper dbHelper; private I_DbSpecific dbSpecific; private I_Writer replicationWriter; private String tableName; private long sleepDelay; /** * Start the test. * <pre> * java -Ddb=oracle junit.swingui.TestRunner -noloading org.xmlBlaster.test.contrib.replication.TestReplicationWriter * </pre> * @param args Command line settings */ public static void main(String[] args) { // junit.swingui.TestRunner.run(TestReplicationWriter.class); TestReplicationWriter test = new TestReplicationWriter(); String path = System.getProperty("java.class.path"); if (path == null) path = ""; System.out.println("THE PATH IS: " + path); try { test.setUp(); test.testReadAllTables(); test.tearDown();/* test.setUp(); test.testCreateSeq1(); test.tearDown(); test.setUp(); test.testCreateSeq2(); test.tearDown(); test.setUp(); test.testCreateSeq3(); test.tearDown(); test.setUp(); test.testCreateSeq4(); test.tearDown(); test.setUp(); test.testCreateSeq5(); test.tearDown();*/ /* test.setUp(); test.testCreateSeq6(); test.tearDown(); *//* test.setUp(); test.testCreateSeq7(); test.tearDown();*/ } catch (Exception ex) { ex.printStackTrace(); fail(); } } /** * Default ctor. */ public TestReplicationWriter() { super(); XMLUnit.setIgnoreWhitespace(true); } /** * Constructor for TestReplicationWriter. * @param arg0 */ public TestReplicationWriter(String arg0) { super(arg0); XMLUnit.setIgnoreWhitespace(true); } /** * Configure database access. * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); this.specificHelper = new SpecificHelper(System.getProperties()); this.info = new PropertiesInfo(specificHelper.getProperties()); this.dbPool = setUpDbPool(this.info); boolean forceCreationAndInit = true; this.dbSpecific = ReplicationConverter.getDbSpecific(this.info, forceCreationAndInit); Connection conn = null; try { conn = dbPool.reserve(); this.dbHelper = new DbMetaHelper(this.dbPool); this.tableName = this.dbHelper.getIdentifier("TEST_WRITER"); log.info("setUp: going to cleanup now ..."); this.dbSpecific.cleanup(conn, false); log.info("setUp: cleanup done, going to bootstrap now ..."); boolean doWarn = false; boolean force = true; this.dbSpecific.bootstrap(conn, doWarn, force); this.replicationWriter = new ReplicationWriter(); this.replicationWriter.init(this.info); } catch (Exception ex) { if (conn != null) dbPool.release(conn); } } public void init(I_Info info) throws Exception { this.sleepDelay = info.getLong("test.sleepDelay", 0L); } /** * Used to test the feature. * @param method The invoking method name. * @param message The xml message to parse and process. * @param tableName The name of the table to create. */ private final void createSeq(String method, String message, String tableName) { try { Map map = new HashMap(); map.put("tableName", this.tableName); map.put("schemaName", this.specificHelper.getOwnSchema(this.dbPool)); message = this.specificHelper.replace(message, map); log.info(method + " START"); // first clean up the table try { this.dbPool.update("DROP TABLE " + tableName); } catch (Exception e) { } // check if really empty Connection conn = null; try { conn = this.dbPool.reserve(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * from " + tableName); rs.close(); st.close(); assertTrue("Testing if the table '" + tableName + "' really has been deleted before starting the tests", false); } catch (Exception e) { } finally { if (conn != null) this.dbPool.release(conn); conn = null; } // first check parsing (if an assert occurs here it means there is a discrepancy between toXml and parse SqlInfoParser parser = new SqlInfoParser(); parser.init(info); SqlInfo dbUpdateInfo = parser.parse(message); String sql = this.dbSpecific.getCreateTableStatement(dbUpdateInfo.getDescription(), null); try { this.replicationWriter.store(dbUpdateInfo); } catch (Exception e) { e.printStackTrace(); assertTrue("when testing '" + sql + "': " + e.getMessage(), false); } // verify that it really has been stored try { conn = this.dbPool.reserve(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * from " + tableName); assertEquals("The table '" + tableName + "' exists but was not empty when testing '" + sql + "'", false, rs.next()); rs.close(); st.close(); } catch (Exception e) { assertTrue("Testing if the table '" + tableName + "' for '" + sql + "' failed because the table was not created", false); } finally { if (conn != null) this.dbPool.release(conn); conn = null; } } catch (Exception ex) { ex.printStackTrace(); fail(); } } public void shutdown() { } /** * Creates a database pooling instance and puts it to info.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -