⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testtimestamp.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------------Name:      TestTimestamp.javaProject:   org.xmlBlasterProject:   xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.test.contrib.dbwatcher;import java.text.SimpleDateFormat;import java.util.prefs.Preferences;import junit.framework.TestCase;import org.custommonkey.xmlunit.XMLTestCase;import org.xmlBlaster.contrib.I_Info;import org.xmlBlaster.contrib.db.I_DbPool;import org.xmlBlaster.contrib.dbwatcher.DbWatcher;import org.xmlBlaster.contrib.dbwatcher.Info;import org.xmlBlaster.contrib.dbwatcher.detector.I_ChangeDetector;import org.xmlBlaster.contrib.dbwatcher.detector.TimestampChangeDetector;import java.util.logging.Logger;import java.util.Map;import java.util.HashMap;/** * Test basic functionality.  * <p>  * To run most of the tests you need to have a databse (for example Oracle) * and XmlBlaster up and running. * </p> * <p> * The connection configuration (url, password etc.) is configured * as JVM property or in * {@link org.xmlBlaster.test.contrib.dbwatcher.TestResultSetToXmlConverter#createTest(I_Info, Map)} * and  * {@link org.xmlBlaster.test.contrib.dbwatcher.TestResultSetToXmlConverter#setUpDbPool(I_Info)} * </p>  * * @see DbWatcher * @author Marcel Ruff */public class TestTimestamp extends XMLTestCase {    private static Logger log = Logger.getLogger(TestTimestamp.class.getName());    private Preferences prefs;    private I_Info info;    private I_DbPool dbPool;    private Map updateMap = new HashMap(); // collects received update messages    private DbWatcher processor;    /**     * Start the test.      * <pre>     * java -Ddb.password=secret junit.swingui.TestRunner -noloading org.xmlBlaster.test.contrib.dbwatcher.TestTimestamp     * </pre>     * @param args Command line settings     */    public static void main(String[] args) {        // junit.swingui.TestRunner.run(TestResultSetToXmlConverter.class);               TestTimestamp test = new TestTimestamp();       try {          // test.setUp();          test.testReplaceDate();          // test.tearDown();                 }       catch (Exception ex) {          ex.printStackTrace();          fail("An exception occured: '" + ex.getMessage() + "-");       }    }    /**     * Default ctor.      */    public TestTimestamp() {    }   /**    * Constructor for TestResultSetToXmlConverter.    * @param arg0    */    public TestTimestamp(String arg0) {       super(arg0);    }    /**     * Configure database access.      * @see TestCase#setUp()     */   protected void setUp() throws Exception {      super.setUp();      this.prefs = Preferences.userRoot();      this.prefs.clear();      this.info = new Info(this.prefs);      this.dbPool = org.xmlBlaster.test.contrib.dbwatcher.TestResultSetToXmlConverter.setUpDbPool(info);      this.processor = null;      try {         this.dbPool.update("DROP TABLE TEST_TS");      } catch(Exception e) {         log.warning(e.toString());       }   }   /**    * Creates a DbWatcher instance and listens on MoM messages.     * @see org.xmlBlaster.test.contrib.dbwatcher.TestResultSetToXmlConverter#createTest(I_Info, Map)     */   private DbWatcher createTest(I_Info info, final Map updateMap) throws Exception {      return org.xmlBlaster.test.contrib.dbwatcher.TestResultSetToXmlConverter.createTest(info, updateMap);   }   /*    * @see TestCase#tearDown()    */   protected void tearDown() throws Exception {      super.tearDown();             if (this.processor != null) {         this.processor.shutdown();         this.processor = null;      }             if (this.dbPool != null) {         try {            this.dbPool.update("DROP TABLE TEST_TS");         } catch(Exception e) {            log.warning(e.toString());          }         this.dbPool.shutdown();      }   }   private void sleep(long millis) {      try { Thread.sleep(millis); } catch(Exception e) { /* Ignore */ }   }      /**    * Check detection of changes on a table and deliver the change as    * pure event without XML dump.      * @throws Exception Any type is possible    */   public final void testEmptyTableStates() throws Exception {      this.prefs.put("changeDetector.class", "org.xmlBlaster.contrib.dbwatcher.detector.TimestampChangeDetector");      this.prefs.put("converter.class", "");      this.prefs.put("changeDetector.detectStatement", "SELECT MAX(TO_CHAR(ts, 'YYYY-MM-DD HH24:MI:SSXFF')) FROM TEST_TS");      this.prefs.put("changeDetector.timestampColName", "ts");      this.prefs.put("alertScheduler.pollInterval", "0"); // switch off      this.prefs.put("changeDetector.groupColName", "ICAO_ID");      this.prefs.put("converter.rootName", "myRootTag");      this.prefs.put("converter.addMeta", ""+true);      this.prefs.put("db.queryMeatStatement", "SELECT * FROM TEST_TS WHERE TO_CHAR(ts, 'YYYY-MM-DD HH24:MI:SSXFF') > '${oldTimestamp}' ORDER BY ICAO_ID");      this.prefs.put("mom.topicName", "db.change.event.${groupColValue}");            this.processor = createTest(new Info(prefs), this.updateMap);      I_ChangeDetector changeDetector = processor.getChangeDetector();            for (int i=0; i<2; i++) {         log.info("Testing no table ...");         changeDetector.checkAgain(null);         sleep(500);         assertEquals("Number of message is wrong", 0, this.updateMap.size());      }      {      log.info("Now testing an empty table ...");      this.dbPool.update("CREATE TABLE TEST_TS (ts TIMESTAMP(9), colKey NUMBER(10,3), col1 VARCHAR(20), col2 NUMBER(12), ICAO_ID VARCHAR(10))");      changeDetector.checkAgain(null);      sleep(500);      assertEquals("Number of message is wrong", 1, this.updateMap.size());      String xml = (String)this.updateMap.get("db.change.event.${groupColValue}");      assertNotNull("No db.change.event.${groupColValue} message has arrived", xml);      assertEquals("", xml);      this.updateMap.clear();      changeDetector.checkAgain(null);      sleep(500);      assertEquals("Number of message is wrong", 0, this.updateMap.size());      }      {      log.info("Insert one row");      this.dbPool.update("INSERT INTO TEST_TS VALUES (CURRENT_TIMESTAMP, '1.1', '<Bla', '9000', 'EDDI')");      changeDetector.checkAgain(null);      sleep(500);      assertEquals("Number of message is wrong", 1, this.updateMap.size());      String xml = (String)this.updateMap.get("db.change.event.EDDI");      assertEquals("", xml);      this.updateMap.clear();      changeDetector.checkAgain(null);      sleep(500);      assertEquals("Number of message is wrong", 0, this.updateMap.size());      }      {         log.info("Update one row");         this.dbPool.update("UPDATE TEST_TS SET ts=CURRENT_TIMESTAMP, colKey='4.44' WHERE ICAO_ID='EDDI'");         changeDetector.checkAgain(null);         Thread.sleep(500);         assertEquals("Number of message is wrong", 1, this.updateMap.size());         String xml = (String)this.updateMap.get("db.change.event.EDDI");         assertNotNull("xml returned is null", xml);         assertEquals("", xml);         this.updateMap.clear();         changeDetector.checkAgain(null);         sleep(500);         assertEquals("Number of message is wrong", 0, this.updateMap.size());      }      // Is not detected by Timestamp poller!      {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -