📄 testhelperclasses.java
字号:
tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val10); assertEquals("replicate for val10", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val10", (String)null, tableToWatchInfo.getTrigger()); assertEquals("replKey for val10", 10L, tableToWatchInfo.getReplKey()); String val11 = "actions=IDU,trigger=trigger"; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val11); assertEquals("replicate for val11", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val11", "trigger", tableToWatchInfo.getTrigger()); assertEquals("replKey for val11", -1L, tableToWatchInfo.getReplKey()); String val12 = " actions = IDU , trigger = trigger"; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val12); assertEquals("replicate for val12", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val12", "trigger", tableToWatchInfo.getTrigger()); assertEquals("replKey for val12", -1L, tableToWatchInfo.getReplKey()); String val13 = "sequence=10"; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val13); assertEquals("replicate for val13", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val13", (String)null, tableToWatchInfo.getTrigger()); assertEquals("replKey for val13", 10L, tableToWatchInfo.getReplKey()); String val14 = " sequence = 10 "; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val14); assertEquals("replicate for val14", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val14", (String)null, tableToWatchInfo.getTrigger()); assertEquals("replKey for val14", 10L, tableToWatchInfo.getReplKey()); String val15 = "trigger=trigger"; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val15); assertEquals("replicate for val15", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val15", "trigger", tableToWatchInfo.getTrigger()); assertEquals("replKey for val15", -1L, tableToWatchInfo.getReplKey()); String val16 = " trigger = trigger "; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val16); assertEquals("replicate for val16", true, tableToWatchInfo.isReplicate()); assertEquals("trigger for val16", "trigger", tableToWatchInfo.getTrigger()); assertEquals("replKey for val16", -1L, tableToWatchInfo.getReplKey()); String val17 = "actions=IDU"; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val17); assertEquals("replicate for val17", "IDU", tableToWatchInfo.getActions()); String val18 = "actions=U"; tableToWatchInfo = new TableToWatchInfo(); tableToWatchInfo.assignFromInfoPair(key, val18); assertEquals("replicate for val17", "U", tableToWatchInfo.getActions()); } catch (Exception ex) { ex.printStackTrace(); } log.info("SUCCESS"); } /** * Test the replacement of variables in the info object. */ public final void testInfo() { log.info("Start testInfo"); Properties props = new Properties(); props.put("one", "value1"); props.put("two", "value2"); props.put("three", "${one}"); props.put("value1", "value43"); PropertiesInfo propInfo = new PropertiesInfo(props); String key = "one"; String val = propInfo.get(key, null); assertEquals("testing key '" + key + "'", "value1", val); key = "three"; val = propInfo.get(key, null); assertEquals("testing key '" + key + "'", "value1", val); key = "four"; val = propInfo.get(key, "default"); assertEquals("testing key '" + key + "'", "default", val); key = "four"; val = propInfo.get(key, "${one}"); assertEquals("testing key '" + key + "'", "value1", val); key = "${one}"; val = propInfo.get(key, null); assertEquals("testing key '" + key + "'", "value43", val); key = "four"; val = propInfo.get(key, "${five}"); assertEquals("testing key '" + key + "'", "${five}", val); props.put("test.${one}", "testOne"); props.put("test.${two}", "test_${two}"); InfoHelper helper = new InfoHelper(propInfo); helper.replaceAllEntries(propInfo, null); Map testProps = InfoHelper.getPropertiesStartingWith("test.", propInfo, null); assertEquals("wrong number of properties starting with *" + testProps + "'", 2, testProps.size()); log.info("SUCCESS"); } /** * */ public final void testTableToWatchInfoStatic() { log.info("Start testTableToWatchInfoStatic"); I_Info info = new PropertiesInfo(new Properties()); // info.put("table.schema1.table1", null); does not work since info will no add this info.put("table.schema1.table1", "actions=IDU"); info.put("table.schema1.table2", "actions=IDU,trigger=trigger2,sequence=10"); info.put("table.schema1.table3", "actions=IDU,trigger=trigger3,sequence=155"); info.put("table.schema1.table4", "actions=IDU,trigger=trigger4,sequence=6"); info.put("table.schema1.table5", "actions=IDU,trigger=trigger5,sequence=13"); info.put("tablesomethingother", "should be ignored"); info.put("somethingother", "should be ignored"); try { Connection conn = null; TableToWatchInfo[] tables = TableToWatchInfo.getTablesToWatch(conn, info); assertEquals("number of tables", 5, tables.length); assertEquals("table sequence #1", "table1", tables[0].getTable()); assertEquals("table sequence #2", "table4", tables[1].getTable()); assertEquals("table sequence #3", "table2", tables[2].getTable()); assertEquals("table sequence #4", "table5", tables[3].getTable()); assertEquals("table sequence #5", "table3", tables[4].getTable()); } catch (Exception ex) { ex.printStackTrace(); assertTrue("An exception should not occur here" + ex.getMessage(), false); } log.info("SUCCESS"); } /** * 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; } /** * */ public final void testTableToWatchInfoForAllToken() { String txt = "testTableToWatchInfoForAllToken"; log.info("Start " + txt); try { SpecificHelper specificHelper = new SpecificHelper(System.getProperties()); I_Info info = new PropertiesInfo(specificHelper.getProperties()); I_DbPool dbPool = setUpDbPool(info); Connection conn = dbPool.reserve(); info.put("table.XMLBLASTER.*", ""); TableToWatchInfo[] tables = TableToWatchInfo.getTablesToWatch(conn, info); assertTrue("wrong number of tables", tables.length > 0); for (int i=0; i < tables.length; i++) { log.info(tables[i].toXml()); } dbPool.release(conn); } catch (Exception ex) { ex.printStackTrace(); fail("An exception should not occur in " + txt); } log.info("SUCCESS"); } private SqlInfo createMsg(I_Info info, String oldFirst, String oldSecond, String first, String second, String fifth, int replKey) { SqlInfo ret = new SqlInfo(info); SqlDescription description = new SqlDescription(info); // description.setCommand("UPDATE"); description.setCommand(ReplicationConstants.REPLICATION_CMD); description.setIdentity("TABLE1"); ret.setDescription(description); SqlRow row = new SqlRow(info, 0); row.setColumn(new ClientProperty("FIRST", null, null, first)); row.setColumn(new ClientProperty("SECOND", null, null, second)); row.setColumn(new ClientProperty("FIFTH", null, null, fifth)); row.setAttribute(new ClientProperty("transaction", null, null, "" + replKey)); row.setAttribute(new ClientProperty("guid", null, null, "" + replKey)); row.setAttribute(new ClientProperty("dbId", null, null, "NULL")); row.setAttribute(new ClientProperty("tableName", null, null, "TABLE1")); row.setAttribute(new ClientProperty("version", null, null, "0.5")); row.setAttribute(new ClientProperty("action", null, null, "UPDATE")); StringBuffer buf = new StringBuffer(1024); buf.append("<col name='FIRST'>").append(oldFirst).append("</col>\n"); buf.append("<col name='SECOND'>").append(oldSecond).append("</col>\n"); row.setAttribute(new ClientProperty("oldContent", null, "forcePlain", buf.toString())); row.setAttribute(new ClientProperty("schema", null, null, "XMLBLASTER")); row.setAttribute(new ClientProperty("replKey", null, null, "" + replKey)); ret.getRows().add(row); return ret; } /** * */ public final void testSearchableConfig() { log.info("Start testSearchableConfig"); try { SpecificHelper specificHelper = new SpecificHelper(System.getProperties()); I_Info info = new PropertiesInfo((Properties)specificHelper.getProperties().clone()); info.put("replication.searchable.xmlBlaster.table1", "first, second, third, fourth"); info.put("replication.searchable.XMLBLASTER.TABLE2", "FIRST,SECOND"); DbPool pool = new DbPool(); pool.init(info); info.putObject(DbWriter.DB_POOL_KEY, pool); SearchableConfig searchableConfig = new SearchableConfig(); searchableConfig.init(info); info.putObject(SearchableConfig.NAME, searchableConfig); pool.update("drop table table1"); String txt = "create table table1 (first VARCHAR(100), second VARCHAR(100), third blob, fifth VARCHAR(29), primary key(first))"; pool.update(txt); pool.update("INSERT INTO table1 (first, second, fifth) values ('oldOne', 'oldTwo', 'oldFive')"); Set vals = searchableConfig.getSearchableColumnNames(null, "XMLBLASTER", "TABLE1"); String[] ret = (String[])vals.toArray(new String[vals.size()]); assertEquals("Wrong number of colums found", 4, ret.length); SqlInfo sqlInfo = new SqlInfo(info); Connection conn = pool.reserve(); sqlInfo.fillMetadata(conn, null, "XMLBLASTER", "TABLE1", null, null); pool.release(conn); SqlDescription description = sqlInfo.getDescription(); boolean isConfigured = description.isColumnSearchConfigured(null); assertTrue("shall be configured", isConfigured); isConfigured = description.isColumnSearchConfigured("FIRST"); assertTrue("shall be configured", isConfigured); isConfigured = description.isColumnSearchConfigured("SECOND"); assertTrue("shall be configured", isConfigured); isConfigured = description.isColumnSearchConfigured("THIRD"); assertFalse("shall not be configured", isConfigured); isConfigured = description.isColumnSearchConfigured("FOURTH"); assertTrue("shall be configured (since it could be added later)", isConfigured); String oldFirst = "oldOne"; String oldSecond = "oldTwo"; String first = "one"; String second = "two"; String fifth = "five"; int replKey = 1; SqlInfo sql = createMsg(info, oldFirst, oldSecond, first, second, fifth, replKey); String xml = sql.toXml(""); log.info(xml); SqlInfoParser parser = new SqlInfoParser(); parser.init(info); sql = parser.parse(xml); ReplicationWriter writer = new ReplicationWriter(); writer.init(info); writer.store(sql); // verify now, conn = pool.reserve(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT count(*) FROM TABLE1"); rs.next(); int count = rs.getInt(1); assertEquals("there must be only one entry in the table TABLE1", 1, count); st.close(); st = conn.createStatement(); rs = st.executeQuery("SELECT FIRST,SECOND,FIFTH FROM TABLE1"); rs.next(); first = rs.getString(1); second = rs.getString(2); fifth = rs.getString(3); st.close(); pool.release(conn); assertEquals("first", "one", first); assertEquals("second", "two", second); assertEquals("fifth", "five", fifth); pool.update("drop table table1"); } catch (Exception ex) { ex.printStackTrace(); assertTrue("An exception should not occur here" + ex.getMessage(), false); } log.info("SUCCESS"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -