📄 testdbbasics.java
字号:
changesToReplTables(conn, null, schema, "test_lowecase", false); changesToReplTables(conn, null, schema, "TEST_UPPERCASE", true); changesToReplTables(conn, null, schema, "testMixedCase", true); } catch (Exception ex) { ex.printStackTrace(); assertTrue("an exception should not occur " + ex.getMessage(), false); } log.info("SUCCESS"); } /** * Testing if the metadata contains the correct information on how to write table names (if upper- * lower or mixedcase), A new table is created. A check is made to see if this generates an entry in * the repl_items table (it should not). It is then added to the repl_tables table. Checks are * made to detect if the CREATE operation as been detected (first create and then add to repl_tables), * if it is correctly stored in the repl_items table. Then Business Function and Trigger is added to that * table manually (this is normally done by the replication mechanism). Again it is checked if the * operation has been detected by the PL/SQL code (trigger and function) added to the business table. * * If the table does not exist we expect a null ResultSet * @throws Exception Any type is possible */ public final void testCreateThenAddToReplTables() throws Exception { log.info("Start testCreateThenAddToReplTables"); Connection conn = this.pool.reserve(); conn.setAutoCommit(true); // this.dbSpecific.bootstrap(conn); try { conn.setAutoCommit(true); String tableName = "test_replication"; // clean up ... String cascade = this.specificHelper.getCascade(); try { pool.update("DROP TABLE " + tableName + cascade); } catch (Exception ex) { } String sql = "CREATE TABLE " + tableName + "(name VARCHAR(20), city VARCHAR(20), PRIMARY KEY (name))"; pool.update(sql); String storedTableName = this.dbHelper.getIdentifier(tableName); assertNotNull("Testing if the metadata contains the correct information about the way identifiers such table names are stored mixed/upper or lower case. If an exception occurs, this information could not be retrieved. Here testing '" + tableName + "'", storedTableName); ResultSet rs = conn.getMetaData().getTables(null, null, storedTableName, null); boolean tableExists = rs.next(); rs.close(); rs = null; assertTrue("Testing if the creation of the table according to '" + sql + "' has been detected by the metadata. (with getTables). Here table '" + tableName + "' was not found", tableExists); // check that nothing has been written in repl_items Statement st = conn.createStatement(); rs = st.executeQuery("SELECT * from " + this.replPrefix + "items"); assertEquals("Testing if creation of a new table which is not in the " + this.replPrefix + "tables table generates an entry in " + this.replPrefix + "items (it should not)", false, rs.next()); rs.close(); st.close(); // add the tables to be detected to the repl_tables table boolean force = false; String destination = null; boolean forceSend = false; TableToWatchInfo tableToWatch = new TableToWatchInfo(null, this.specificHelper.getOwnSchema(pool), tableName); tableToWatch.setActions("IDU"); tableToWatch.setTrigger(null); this.dbSpecific.addTableToWatch(tableToWatch, force, new String[] { destination }, forceSend); // force a call to the function which detects CREATE / DROP / ALTER operations: writes on repl_items this.dbSpecific.forceTableChangeCheck(); st = conn.createStatement(); rs = st.executeQuery("SELECT * from " + this.replPrefix + "items"); assertTrue("Testing if the trigger/function associated to the business logic table '" + tableName + "' has been working. Here no entry was found in '" + this.replPrefix + "items' so it did not work property. Some DB (example postgres) detect this in forceTableChangeDetect (which was invoked here) and others (like Oracle) have a trigger on the SCHEMA", rs.next()); String name = rs.getString(4); String action = rs.getString(6); assertEquals("Testing if the name of the table has been correctly stored in " + this.replPrefix + "items", this.dbHelper.getIdentifier(tableName), name); assertEquals("Testing if the name of the action performec is correctly stored in " + this.replPrefix + "items for action CREATE", "CREATE", action); assertFalse("Testing the number of entries in " + this.replPrefix + "items table. It is too big, there should be only one entry", rs.next()); st.close(); rs.close(); // now we add the function and trigger which are associated to our business table ... this.dbSpecific.readNewTable(null, this.specificHelper.getOwnSchema(pool), tableName, null, true); // this will invoke the publish method // from now on on an operation on that table should be detected and should start fill the repl_items table st = conn.createStatement(); st.executeUpdate("DELETE FROM " + this.replPrefix + "items"); st.close(); st = conn.createStatement(); st.executeUpdate("INSERT INTO " + tableName + " VALUES ('somebody', 'Paris')"); st.close(); // now it should be in " + this.replPrefix + "items ... st = conn.createStatement(); rs = st.executeQuery("SELECT * FROM " + this.replPrefix + "items"); assertTrue("Testing if the INSERT operation on business table '" + tableName + "' worked. No entry was detected in " + this.replPrefix + "items. Probably the trigger or function on the business table was not working.", rs.next()); name = rs.getString(4); action = rs.getString(6); assertEquals("Testing if the name of the table in " + this.replPrefix + "items for action INSERT is wrong.", this.dbHelper.getIdentifier(tableName), name); assertEquals("Testing if the action for the operation INSERT was correct in " + this.replPrefix + "items.", "INSERT", action); rs.close(); st.close(); // and now cleanup st = conn.createStatement(); st.executeUpdate("DELETE FROM " + this.replPrefix + "items"); st.close(); } catch (Exception ex) { ex.printStackTrace(); assertTrue("an exception should not occur " + ex.getMessage(), false); } finally { if (conn != null) this.pool.release(conn); } log.info("SUCCESS"); } /** * Testing if the metadata contains the correct information on how to write table names (if upper- * lower or mixedcase), A table which does not exist yet is added to the repl_tables. A check is made * to see if the action generated an entry in repl_items (it should not). It is then * created. Checks are made to detect if the CREATE operation as been detected (first create and * then add to repl_tables), if it is correctly stored in the repl_items table. Then Business Function * and Trigger is added to that table manually (this is normally done by the replication mechanism). * Again it is checked if the operation has been detected by the PL/SQL code (trigger and function) * added to the business table. * * If the table does not exist we expect a null ResultSet * @throws Exception Any type is possible */ public final void testAddToReplTablesThenCreate() throws Exception { log.info("Start testAddToReplTablesThenCreate"); try { // make sure you have deleted all entries this.pool.update("DELETE FROM " + this.replPrefix + "ITEMS"); } catch (Exception ex) { ex.printStackTrace(); } Connection conn = this.pool.reserve(); // this.dbSpecific.bootstrap(conn); try { conn.setAutoCommit(true); String tableName = "test_replication"; // clean up ... String cascade = this.specificHelper.getCascade(); // String cascade = " CASCADE"; // just to make sure it is really gone. try { pool.update("DROP TABLE " + tableName + cascade); } catch (Exception ex) { } // add the tables to be detected to the repl_tables table boolean force = false; String destination = null; boolean forceSend = false; TableToWatchInfo tableToWatch = new TableToWatchInfo(null, this.specificHelper.getOwnSchema(pool), tableName); tableToWatch.setActions("IDU"); tableToWatch.setTrigger(null); this.dbSpecific.addTableToWatch(tableToWatch, force, new String[] { destination }, forceSend); // check that nothing has been written in repl_items Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * from " + this.replPrefix + "items"); assertEquals("Testing if the addition of a (non created yet) table to " + this.replPrefix + "tables generates an entry in " + this.replPrefix + "items (it should not)", false, rs.next()); rs.close(); st.close(); String sql = "CREATE TABLE " + tableName + "(name VARCHAR(20), city VARCHAR(20), PRIMARY KEY (name))"; pool.update(sql); String storedTableName = this.dbHelper.getIdentifier(tableName); assertNotNull("Testing if the metadata contains the correct information about the way identifiers such table names are stored mixed/upper or lower case. If an exception occurs, this information could not be retrieved. Here testing '" + tableName + "'", storedTableName); rs = conn.getMetaData().getTables(null, null, storedTableName, null); boolean tableExists = rs.next(); rs.close(); rs = null; assertTrue("Testing if the creation of the table according to '" + sql + "' has been detected by the metadata. (with getTables). Here table '" + tableName + "' was not found", tableExists); // force a call to the function which detects CREATE / DROP / ALTER operations: writes on repl_items this.dbSpecific.forceTableChangeCheck(); st = conn.createStatement(); rs = st.executeQuery("SELECT * from " + this.replPrefix + "items"); assertTrue("Testing if the trigger/function associated to the business logic table '" + tableName + "' has been working. Here no entry was found in '" + this.replPrefix + "items' so it did not work property. Some DB (example postgres) detect this in forceTableChangeDetect (which was invoked here) and others (like Oracle) have a trigger on the SCHEMA", rs.next()); String name = rs.getString(4); String action = rs.getString(6); assertEquals("Testing if the name of the table has been correctly stored in " + this.replPrefix + "items", this.dbHelper.getIdentifier(tableName), name); assertEquals("Testing if the name of the action performec is correctly stored in " + this.replPrefix + "items for action CREATE", "CREATE", action); assertFalse("Testing the number of entries in " + this.replPrefix + "items table. It is too big, there should be only one entry", rs.next()); st.close(); rs.close(); // now we add the function and trigger which are associated to our business table ... this.dbSpecific.readNewTable(null, this.specificHelper.getOwnSchema(pool), tableName, null, true); // this will invoke the publish method // from now on on an operation on that table should be detected and should start fill the repl_items table st = conn.createStatement(); st.executeUpdate("DELETE FROM " + this.replPrefix + "items"); st.close(); st = conn.createStatement(); st.executeUpdate("INSERT INTO " + tableName + " VALUES ('somebody', 'Paris')"); st.close(); // now it should be in " + this.replPrefix + "items ... st = conn.createStatement(); rs = st.executeQuery("SELECT * FROM " + this.replPrefix + "items"); assertTrue("Testing if the INSERT operation on business table '" + tableName + "' worked. No entry was detected in " + this.replPrefix + "items. Probably the trigger or function on the business table was not working.", rs.next()); name = rs.getString(4); action = rs.getString(6); assertEquals("Testing if the name of the table in " + this.replPrefix + "items for action INSERT is wrong.", this.dbHelper.getIdentifier(tableName), name); assertEquals("Testing if the action for the operation INSERT was correct in " + this.replPrefix + "items.", "INSERT", action); rs.close(); st.close(); // and now cleanup st = conn.createStatement(); st.executeUpdate("DELETE FROM " + this.replPrefix + "items"); st.close(); } catch (Exception ex) { ex.printStackTrace(); assertTrue("an exception should not occur " + ex.getMessage(), false); } finally { if (conn != null) this.pool.release(conn); } log.info("SUCCESS"); } /** * @see org.xmlBlaster.contrib.I_ContribPlugin#getUsedPropertyKeys() */ public Set getUsedPropertyKeys() { return new HashSet(); } public void init(I_Info info) throws Exception { } public String publish(String changeKey, byte[] message, Map attrMap) throws Exception { log.info(new String(message)); return null; } public boolean registerAlertListener(I_Update momCb, Map attrs) throws Exception { return false; } public void shutdown() { } public final void testAdhoc() throws Exception { log.info("Start testInternalFunctions"); if (!this.specificHelper.isOracle()) { log.info("Stop testInternalFunctions (nothing to be done since not oracle)"); return; } I_DbPool pool = (I_DbPool)info.getObject("db.pool"); assertNotNull("pool must be instantiated", pool); Connection conn = null; try { conn = pool.reserve(); conn.setAutoCommit(true);// String sql = null; } catch (Exception ex) { ex.printStackTrace(); assertTrue("an exception should not occur " + ex.getMessage(), false); } log.info("SUCCESS"); } /** * @see org.xmlBlaster.contrib.I_ChangePublisher#getJmsSession() */ public XBSession getJmsSession() { return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -