📄 testdbbasics.java
字号:
st.close(); assertEquals(sql, 2, ret); } { // needs CDATA needs_prot(?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "NEEDS_PROT"); st.setString(3, "this is a <a"); st.setString(4, "unused"); st.setLong(5, 1L); // st.registerOutParameter(1, Types.INTEGER); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); // int ret = st.getInt(1); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); String txt = new String(buf); log.fine("The return value of the query '" + sql + "' is '" + txt + "'"); int ret = -1000; try { ret = Integer.parseInt(txt); } catch (Throwable e) { assertTrue("Conversion exception should not occur on '" + sql + "' where ret is '" + txt + "'", false); } rs.close(); st.close(); assertEquals(sql, 1, ret); } { // needs CDATA needs_prot(?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "NEEDS_PROT"); st.setString(3, "<this is a"); st.setString(4, "unused"); st.setLong(5, 1L); // st.registerOutParameter(1, Types.INTEGER); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); // int ret = st.getInt(1); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); String txt = new String(buf); log.fine("The return value of the query '" + sql + "' is '" + txt + "'"); int ret = -1000; try { ret = Integer.parseInt(txt); } catch (Throwable e) { assertTrue("Conversion exception should not occur on '" + sql + "' where ret is '" + txt + "'", false); } rs.close(); st.close(); assertEquals(sql, 1, ret); } { // needs CDATA needs_prot(?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "NEEDS_PROT"); st.setString(3, "a<this is a"); st.setString(4, "unused"); st.setLong(5, 1L); // st.registerOutParameter(1, Types.INTEGER); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); // int ret = st.getInt(1); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); String txt = new String(buf); log.fine("The return value of the query '" + sql + "' is '" + txt + "'"); int ret = -1000; try { ret = Integer.parseInt(txt); } catch (Throwable e) { assertTrue("Conversion exception should not occur on '" + sql + "' where ret is '" + txt + "'", false); } rs.close(); st.close(); assertEquals(sql, 1, ret); } // now testing the " + this.replPrefix + "needs_prot for the three cases ... { // needs no protection // col2xml(?, ?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "COL2XML"); st.setString(3, "colValue"); st.setString(4, "colName"); st.setLong(5, 1L); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); rs.close(); st.close(); log.fine("The return value of the query '" + sql + "' is '" + new String(buf) + "'"); assertEquals(sql, "<col name=\"colName\">colValue</col>", new String(buf)); } { // col2xml(?, ?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "COL2XML"); st.setString(3, "prova"); st.setString(4, "test"); st.setLong(5, 1L); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); rs.close(); st.close(); log.fine("The return value of the query '" + sql + "' is '" + new String(buf) + "'"); assertEquals(sql, "<col name=\"test\">prova</col>", new String(buf)); } { // needs BASE64 col2xml(?, ?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "COL2XML"); st.setString(3, "<![CDATA[colValue]]>"); st.setString(4, "colName"); st.setLong(5, 1L); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); rs.close(); st.close(); log.fine("The return value of the query '" + sql + "' is '" + new String(buf) + "'"); assertEquals(sql, "<col name=\"colName\" encoding=\"base64\">PCFbQ0RBVEFbY29sVmFsdWVdXT4=</col>", new String(buf)); } { // needs CDATA col2xml(?, ?) sql = "{? = call " + this.replPrefix + "test_clob(?,?,?,?)}"; // name text, content text) CallableStatement st = conn.prepareCall(sql); st.setString(2, "COL2XML"); st.setString(3, "c<olValue"); st.setString(4, "colName"); st.setLong(5, 1L); st.registerOutParameter(1, Types.CLOB); ResultSet rs = st.executeQuery(); Clob clob = st.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); rs.close(); st.close(); log.fine("The return value of the query '" + sql + "' is '" + new String(buf) + "'"); assertEquals(sql, "<col name=\"colName\"><![CDATA[c<olValue]]></col>", new String(buf)); } // now test the counter ... (this invocation is used in SpecificDefault.incrementReplKey { long oldVal = 0; for (int i=0; i < 2; i++) { // sql = "{? = call nextval('repl_seq')}"; sql = "{? = call " + this.replPrefix + "increment()}"; CallableStatement st = conn.prepareCall(sql); // st.registerOutParameter(1, Types.BIGINT); st.registerOutParameter(1, Types.INTEGER); ResultSet rs = st.executeQuery(); // long ret = st.getLong(1); long ret = st.getLong(1); rs.close(); st.close(); log.fine("The return value of the query '" + sql + "' is '" + ret + "'. The maximum integer value is '" + Integer.MAX_VALUE + "'"); if (i == 0) oldVal = ret; else assertEquals(sql, oldVal + i , ret); } } { // tests that by broadcast statements they are written in the ITEMS table // make sure there is nothing in the ITEMS table sql = "DELETE FROM " + this.dbHelper.getIdentifier(this.replPrefix + "ITEMS"); this.pool.update(sql); sql = "SELECT * FROM " + this.dbHelper.getIdentifier(this.replPrefix + "ITEMS"); long maxResponseEntries = 10L; boolean isHighPrio = true; boolean isMaster = false; String sqlTopic = null; String statementId = "1"; this.dbSpecific.broadcastStatement(sql, maxResponseEntries, isHighPrio, isMaster, sqlTopic, statementId); Statement st = conn.createStatement(); try { ResultSet rs = st.executeQuery("SELECT content FROM " + this.dbHelper.getIdentifier(this.replPrefix + "ITEMS")); if (rs.next()) { Clob clob = rs.getClob(1); long len = clob.length(); byte[] buf = new byte[(int)len]; clob.getAsciiStream().read(buf); String txt = new String(buf); log.info("The statement to broadcast is '" + txt + "'"); assertFalse("There must not be any results after a SELECT statement to broadcast", rs.next()); } else assertTrue("There must be entries in the ITEMS table. Seems that the broadcastStatement function does not work", false); } finally { st.close(); } } } catch (Exception ex) { ex.printStackTrace(); assertTrue("an exception should not occur " + ex.getMessage(), false); } log.info("SUCCESS"); } /** * Used by testChangesToReplTables * @param conn The jdbc connection to use * @param catalog the catalog (can be null) * @param schema the name of the schema (db specific). Can be null. * @param tableName The table name to add. Can NOT be null. * @param doReplicate the flag indicating it has to be replicated. * @throws Exception */ private void changesToReplTables(Connection conn, String catalog, String schema, String tableName, boolean doReplicate) throws Exception { String flags = ""; if (doReplicate) flags = "IDU"; boolean force = false; String destination = null; boolean forceSend = false; TableToWatchInfo tableToWatch = new TableToWatchInfo(catalog, schema, tableName); tableToWatch.setActions(flags); tableToWatch.setTrigger(null); this.dbSpecific.addTableToWatch(tableToWatch, force, new String[] { destination }, forceSend); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * from " + this.replPrefix + "tables WHERE tablename='" + this.dbHelper.getIdentifier(tableName) + "'"); assertTrue("testing '" + tableName + "' went wrong since no entries found", rs.next()); String name = rs.getString(3); assertEquals("testing if the name of the added table is the same as the retrieved one '" + tableName + "'", this.dbHelper.getIdentifier(tableName), name); String doReplTxt = rs.getString(4); boolean doRepl = false; if (doReplTxt != null && doReplTxt.length() > 0) doRepl = true; assertEquals("testing if the 'replicate' flag for table '" + tableName + "' is the same as used when adding it", doReplicate, doRepl); rs.close(); st.close(); // test removal now this.dbSpecific.removeTableToWatch(tableToWatch, false); st = conn.createStatement(); rs = st.executeQuery("SELECT * from " + this.replPrefix + "tables WHERE tablename='" + this.dbHelper.getIdentifier(tableName) + "'"); assertFalse("testing if removal of entries from the '" + this.replPrefix + "tables' for '" + tableName + "' works (tests if the entry is still there after removal)", rs.next()); rs.close(); st.close(); } /** * This method tests adding and removing of entries to repl_tables. It checks if a table which has been added is correctly * written in the repl_tables table (case sensitivity is checked replicate' flag is checked and null and empty catalog and * schema are checked. * * @throws Exception Any type is possible */ public final void testChangesToReplTables() throws Exception { log.info("Start testChangesToReplTables"); 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 schema = this.specificHelper.getOwnSchema(pool); changesToReplTables(conn, null, schema, "test_lowecase", true); changesToReplTables(conn, "", schema, "test_lowecase", true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -