📄 testdbbasics.java
字号:
/*------------------------------------------------------------------------------ Name: TestDbBasics.java Project: org.xmlBlasterProject: xmlBlaster.org Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file ------------------------------------------------------------------------------*/package org.xmlBlaster.test.contrib.replication;import java.io.ByteArrayInputStream;import java.sql.CallableStatement;import java.sql.Clob;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Types;import java.util.logging.Logger;import org.apache.commons.codec.binary.Base64;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.I_DbPool;import org.xmlBlaster.contrib.dbwatcher.DbWatcher;import org.xmlBlaster.contrib.replication.I_DbSpecific;import org.xmlBlaster.contrib.replication.ReplicationConverter;import org.xmlBlaster.contrib.replication.TableToWatchInfo;import org.xmlBlaster.jms.XBSession;import java.util.HashSet;import java.util.Map;import java.util.Set;/** * Test basic functionality of the database. It does need a database conntected * but does not need any xmlBlaster running. * <p> * To run most of the tests you need to have a database (for example Postgres). * </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 <a href="mailto:michele@laghi.eu">Michele Laghi</a> */public class TestDbBasics extends XMLTestCase implements I_ChangePublisher { private static Logger log = Logger.getLogger(TestDbBasics.class.getName()); private I_Info info; private I_DbPool pool; private I_DbSpecific dbSpecific; private DbMetaHelper dbHelper; private SpecificHelper specificHelper; private String replPrefix = "repl_"; /** * Start the test. * <pre> * java -Ddb=oracle junit.swingui.TestRunner -noloading org.xmlBlaster.test.contrib.replication.TestDbBasics * </pre> * @param args Command line settings */ public static void main(String[] args) { // junit.swingui.TestRunner.run(TestDbBasics.class); TestDbBasics test = new TestDbBasics(); try { test.setUp(); test.testBasicPerformance(); test.tearDown(); test.setUp(); test.testInternalFunctions(); test.tearDown(); test.setUp(); test.testFunctions(); test.tearDown(); test.setUp(); test.testChangesToReplTables(); test.tearDown(); test.setUp(); test.testCreateThenAddToReplTables(); test.tearDown(); test.setUp(); test.testAddToReplTablesThenCreate(); test.tearDown(); } catch (Exception ex) { ex.printStackTrace(); fail(); } } /** * Default ctor. */ public TestDbBasics() { super(); XMLUnit.setIgnoreWhitespace(true); } /** * Constructor for TestDbBasics. * * @param arg0 */ public TestDbBasics(String arg0) { super(arg0); XMLUnit.setIgnoreWhitespace(true); } /** * Configure database access. * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); this.specificHelper = new SpecificHelper(System.getProperties()); this.info = new PropertiesInfo(specificHelper.getProperties()); this.replPrefix = this.info.get("replication.prefix", "repl_"); this.pool = DbWatcher.getDbPool(this.info); assertNotNull("pool must be instantiated", this.pool); boolean forceCreationAndInit = true; this.dbSpecific = ReplicationConverter.getDbSpecific(this.info, forceCreationAndInit); assertNotNull("the dbSpecific shall not be null", dbSpecific); Connection conn = this.pool.reserve(); try { this.dbHelper = new DbMetaHelper(this.pool); log.info("setUp: going to cleanup now ..."); conn.setAutoCommit(true); this.dbSpecific.cleanup(conn, false); for (int i=1; i < 5; i++) { // make sure we have deleted all triggers try { this.pool.update("DROP TRIGGER " + this.replPrefix + i); } catch (Exception ex) { } } log.info("setUp: cleanup done, going to bootstrap now ..."); boolean doWarn = false; boolean force = true; this.dbSpecific.bootstrap(conn, doWarn, force); } catch (Exception ex) { log.warning(ex.getMessage()); if (conn != null) this.pool.release(conn); } } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); Connection conn = this.pool.reserve(); try { this.dbSpecific.cleanup(conn, false); } catch (Exception ex) { log.warning(ex.getMessage()); if (conn != null) this.pool.release(conn); } if (this.dbSpecific != null) { this.dbSpecific.shutdown(); this.dbSpecific = null; } if (this.pool != null) { this.pool.shutdown(); this.pool = null; } } /** */ public final void testBasicPerformance() throws Exception { String txt = "testBasicPerformance"; log.info("Start " + txt); I_DbPool pool = (I_DbPool)info.getObject("db.pool"); assertNotNull("pool must be instantiated", pool); try { pool.update("DROP TABLE PERFORM"); } catch (SQLException ex) { log.info("An Exception here is allowed"); } pool.update("CREATE TABLE PERFORM (name1 VARCHAR(20), name2 VARCHAR(128), name3 BLOB, primary key (name1))"); Connection conn = pool.reserve(); conn.setAutoCommit(false); String sql = "INSERT INTO PERFORM VALUES (?, ?, ?)"; byte[] blob = new byte[1024]; for (int i=0; i < blob.length; i++) blob[i] = (byte)i; int nmax = 50; { long t0 = System.currentTimeMillis(); PreparedStatement st = conn.prepareStatement(sql); ByteArrayInputStream bais = new ByteArrayInputStream(blob); for (int i=0; i < nmax; i++) { st.setString(1, "name01_" + i); st.setString(2, "name02_" + i); st.setBinaryStream(3, bais, blob.length); st.addBatch(); } st.executeBatch(); conn.commit(); long t1 = System.currentTimeMillis(); long dt = t1-t0; log.info("batch statements='" + nmax + "' took '" + dt + "' ms (per statement: " + dt/nmax + ")"); pool.update("delete from PERFORM"); conn.commit(); } { long t0 = System.currentTimeMillis(); for (int i=0; i < nmax; i++) { ByteArrayInputStream bais = new ByteArrayInputStream(blob); PreparedStatement st = conn.prepareStatement(sql); st.setString(1, "name01_" + i); st.setString(2, "name02_" + i + "_hhjdhsdsdjsdkljsdjsdljljsdljsdkljsljsdsdsdsd"); st.setBinaryStream(3, bais, blob.length); st.execute(); } conn.commit(); long t1 = System.currentTimeMillis(); long dt = t1-t0; log.info("non-batch (single commit) statements='" + nmax + "' took '" + dt + "' ms (per statement: " + dt/nmax + ")"); pool.update("delete from PERFORM"); conn.commit(); } { long t0 = System.currentTimeMillis(); for (int i=0; i < nmax; i++) { ByteArrayInputStream bais = new ByteArrayInputStream(blob); PreparedStatement st = conn.prepareStatement(sql); st.setString(1, "name01_" + i); st.setString(2, "name02_" + i + "_hhjdhsdsdjsdkljsdjsdljljsdljsdkljsljsdsdsdsd"); st.setBinaryStream(3, bais, blob.length); st.execute(); conn.commit(); } long t1 = System.currentTimeMillis(); long dt = t1-t0; log.info("non-batch (all commit) statements='" + nmax + "' took '" + dt + "' ms (per statement: " + dt/nmax + ")"); pool.update("delete from PERFORM"); conn.commit(); } pool.update("DROP TABLE PERFORM"); pool.release(conn); log.info("SUCCESS"); } /** * This method makes some calls to system functions which are specific to oracle. * * The tested functions are: * CHAR(8) repl_base64_helper(val INTEGER) (this is only tested for no exception thrown) * CLOB repl_base64_enc_raw(msg RAW) * CLOB repl_base64_enc_blob(msg BLOB) * CLOB repl_base64_enc_clob(msg CLOB) * * @throws Exception Any type is possible */ public final void testInternalFunctions() throws Exception { log.info("Start testInternalFunctions");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -