📄 testdbspecific.java
字号:
/*------------------------------------------------------------------------------Name: TestDbSpecific.javaProject: org.xmlBlasterProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.test.contrib.replication;import java.sql.Connection;import java.util.logging.Logger;import org.custommonkey.xmlunit.XMLTestCase;import org.custommonkey.xmlunit.XMLUnit;import org.xmlBlaster.contrib.I_ChangePublisher;import org.xmlBlaster.contrib.I_Info;import org.xmlBlaster.contrib.I_Update;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.dbwatcher.DbWatcher;import org.xmlBlaster.contrib.dbwriter.SqlInfoParser;import org.xmlBlaster.contrib.dbwriter.info.SqlInfo;import org.xmlBlaster.contrib.replication.I_DbSpecific;import org.xmlBlaster.contrib.replication.TableToWatchInfo;import org.xmlBlaster.contrib.replication.impl.SpecificDefault;import org.xmlBlaster.jms.XBSession;import java.util.HashSet;import java.util.Map;import java.util.Set;/** * Test basic functionality which is specific for each database implementation. * There is a predefined set of default properties which are specific to the database you use. For instance * if you want to use the predefined settings for oracle use: * <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>Creates a user table (which is not in the repl_tables, so it will not fire any trigger)</li> * <li>Explicitly reads the table (readNewTable). This shall fill the metadata of the table and publish the message</li> * <li>The message is catched by our publish Method. In it all asserts will be done * <ul> * <li>Number of columns must be at least one (to detect that metadata is retrieved)</li> * <li>Parsing of message is working correctly</li> * <li>Creation of the table is successful</li> * </ul> * </li> * <li>tests the method getObjectName(String op, String req)</li> * <li>tests the method checkTableForCreation(String creationRequest)</li> * <li>tests the method checkSequenceForCreation(String creationRequest)</li> * </ul> * To run most of the tests you need to have a database (for example Postgres or Oracle). * </p> * <p> * The connection configuration (url, password etc.) is configured * as JVM property or in {@link #createTest(I_Info, Map)} and * {@link #setUpDbPool(I_Info)} * </p> * * @author Michele Laghi */public class TestDbSpecific extends XMLTestCase implements I_ChangePublisher { private static Logger log = Logger.getLogger(TestDbSpecific.class.getName()); private static I_Info info; private static I_DbPool dbPool; private static I_DbSpecific dbSpecific; private static String currentMethod; // since there are two instances running (I_ChangePublisher also) private static boolean doCheck; private static boolean checked; private DbMetaHelper dbHelper; private static SpecificHelper specificHelper; // this is static since the implementation of I_ChangePublisher is another instance private String replPrefix = "repl_"; private String tableName; /** * Start the test. * <pre> * java -Ddb=oracle junit.swingui.TestRunner -noloading org.xmlBlaster.test.contrib.replication.TestDbSpecific * </pre> * @param args Command line settings */ public static void main(String[] args) { // junit.swingui.TestRunner.run(TestDbSpecific.class); TestDbSpecific test = new TestDbSpecific(); try { test.setUp(); test.testSchemaWipeout(); test.tearDown(); test.setUp(); test.testGetObjectName(); test.tearDown(); test.setUp(); test.testCheckTableForCreation(); test.tearDown(); test.setUp(); test.testCheckSequenceForCreation(); test.tearDown(); test.setUp(); test.testCreateTablesWithDifferentTypes(); test.tearDown(); } catch (Exception ex) { ex.printStackTrace(); fail(); } } /** * Default ctor. */ public TestDbSpecific() { super(); XMLUnit.setIgnoreWhitespace(true); } /** * Constructor for TestDbSpecific. * @param arg0 */ public TestDbSpecific(String arg0) { super(arg0); XMLUnit.setIgnoreWhitespace(true); } /** * Configure database access. * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); specificHelper = new SpecificHelper(System.getProperties()); this.tableName = "TEST_DBSPECIFIC"; info = new PropertiesInfo(specificHelper.getProperties()); this.replPrefix = info.get("replication.prefix", "repl_"); dbPool = setUpDbPool(info); String dbSpecificName = specificHelper.getProperties().getProperty("replication.dbSpecific.class"); // , "org.xmlBlaster.contrib.replication.impl.SpecificOracle";" + dbSpecific = setUpDbSpecific(info, dbSpecificName); Connection conn = null; try { conn = dbPool.reserve(); this.dbHelper = new DbMetaHelper(dbPool); this.tableName = this.dbHelper.getIdentifier(this.tableName); log.info("setUp: going to cleanup now ..."); dbSpecific.cleanup(conn, false); try { dbPool.update("DROP TABLE test_dbspecific"); } catch (Exception e) { } for (int i=1; i < 5; i++) { // make sure we have deleted all triggers try { dbPool.update("DROP TRIGGER " + this.replPrefix + i); } catch (Exception ex) { } } log.info("setUp: cleanup done, going to bootstrap now ..."); boolean doWarn = false; boolean force = true; dbSpecific.bootstrap(conn, doWarn, force); String destination = null; boolean forceSend = false; TableToWatchInfo tableToWatch = new TableToWatchInfo(" ", specificHelper.getOwnSchema(dbPool), this.tableName); tableToWatch.setActions(""); tableToWatch.setTrigger("DUMMY"); dbSpecific.addTableToWatch(tableToWatch, false, new String[] { destination }, forceSend); } catch (Exception ex) { if (conn != null) dbPool.release(conn); } } /** * @see org.xmlBlaster.contrib.I_ContribPlugin#getUsedPropertyKeys() */ public Set getUsedPropertyKeys() { return new HashSet(); } public void init(I_Info info) throws Exception { } /** * This method gets invoked when a change is detected. It will check that the message is parsed correctly. * It does not check that the create statement is mapped correctly to xml. */ public String publish(String changeKey, byte[] message, Map attrMap) throws Exception { String msg = new String(message); try { log.info("publish invoked in method '" + currentMethod + "'"); log.fine("message '" + msg + "'"); if (doCheck) { checked = true; // 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(msg); String createStatement = dbSpecific.getCreateTableStatement(dbUpdateInfo.getDescription(), null); log.fine("============================================="); log.fine(createStatement); log.fine("============================================="); String msg1 = dbUpdateInfo.toXml(""); log.fine("original message: " + message); log.fine("parsed message: " + msg1); int numOfCols = dbUpdateInfo.getDescription().getColumns().length; assertTrue("Number of columns must be at least one (to detect that metadata is retrieved)", numOfCols > 0); assertXMLEqual("Parsing of message is working correctly: output xml is not the same as input xml", msg, msg1); TableToWatchInfo tableToWatch = new TableToWatchInfo(); tableToWatch.setActions("IDU"); String functionAndTrigger = dbSpecific.createTableTrigger(dbUpdateInfo.getDescription(), tableToWatch); log.fine("-- ---------------------------------------------------------------------------"); log.fine(functionAndTrigger); log.fine("-- ---------------------------------------------------------------------------"); // check now the creation of the table String sql = "DROP TABLE TEST_CREATION"; try { dbPool.update(sql); } catch (Exception e) { } dbUpdateInfo.getDescription().setIdentity("TEST_CREATION"); sql = dbSpecific.getCreateTableStatement(dbUpdateInfo.getDescription(), null); try { dbPool.update(sql); } catch (Exception ex) { ex.printStackTrace(); assertTrue("Testing creation of table with statement '" + sql + "' failed", false); } } } catch (Exception ex) { ex.printStackTrace(); fail(); } return null; } public boolean registerAlertListener(I_Update momCb, Map attrs) throws Exception { return false; } public void shutdown() { } /** * Creates a database pooling instance and puts it to info. * @param info The configuration * @return The created pool */ private DbPool setUpDbPool(I_Info info) { DbPool dbPool = new DbPool(); dbPool.init(info); info.putObject("db.pool", dbPool); return dbPool; } /** * Creates an I_DbSpecific object. * @param info The configuration.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -