package jag.tests; import static org.junit.Assert.*; import java.sql.BatchUpdateException; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; import org.junit.After; import org.junit.Before; import org.junit.Test; /* * Unittest uses the following table schema: * MariaDB [database]> show columns from Ent1; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | pk | int(11) | NO | PRI | NULL | | | name | varchar(255) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) */ public class MariaDBTest { private static final String url = "jdbc:mariadb://" + System.getProperty("hostname", "localhost") + ":" + System.getProperty("port", "3306") + "/" + System.getProperty("dbname", "database"); private static final String user = System.getProperty("username", "username"); private static final String passwd = System.getProperty("password", "password"); private Connection con; private static final String pstmtSql = "INSERT INTO Ent1 (pk, name) VALUES (?, ?)"; @Before public void setUp() throws Exception { Class.forName("org.mariadb.jdbc.Driver"); // Load in the DriverManager Class con = DriverManager.getConnection(url, user, passwd); cleanup(); System.out.println("Setup Complete."); System.out.println("**********"); } @After public void tearDown() throws Exception { con.close(); System.out.println("Teardown Complete."); System.out.println("**********"); } private void cleanup() throws Exception { System.out.println("Executing cleanup..."); try { String sql = "DELETE FROM Ent1"; con.setAutoCommit(true); PreparedStatement pstmt = con.prepareStatement(sql); int result = pstmt.executeUpdate(); System.out.println("Cleanup SQL Exec Result = " + result); } finally { System.out.println("Cleanup Complete."); } } @Test public void testDBMeta() throws Exception { DatabaseMetaData dbMeta = con.getMetaData(); System.out.println("DatabaseProductName = " + dbMeta.getDatabaseProductName()); System.out.println("DatabaseProductVersion = " + dbMeta.getDatabaseProductVersion()); System.out.println("DriverName = " + dbMeta.getDriverName()); System.out.println("DriverVersion = " + dbMeta.getDriverVersion()); System.out.println("Driver supports batching = " + dbMeta.supportsBatchUpdates()); } @Test public void testBasicBatching() throws Exception { System.out.println("Running testBasicBatching..."); con.setAutoCommit(false); PreparedStatement pstmt = con.prepareStatement(pstmtSql); pstmt.setInt(1, 200); pstmt.setString(2, "twohundred"); pstmt.addBatch(); pstmt.setInt(1, 201); pstmt.setString(2, "twohundred-one"); pstmt.addBatch(); int[] count = pstmt.executeBatch(); System.out.println("Count (" + ((count == null) ? "null" : count.length) + "): "); if (count != null && count.length > 0) { for (int i : count) { System.out.println(" -- " + i); assertTrue("Assert batch return value > 0 (successful)", i > 0 || i == Statement.SUCCESS_NO_INFO); } } // Explicitly commit statements to apply changes System.out.println("Committing..."); con.commit(); } @Test public void testExceptionSingleBatchedRow() throws Exception { System.out.println("Running testExceptionSingleBatchedRow..."); con.setAutoCommit(false); PreparedStatement pstmt = con.prepareStatement(pstmtSql); pstmt.setInt(1, 200); pstmt.setString(2, "twohundred"); pstmt.addBatch(); try { int[] count = pstmt.executeBatch(); System.out.println("Count (" + ((count == null) ? "null" : count.length) + "): "); if (count != null && count.length > 0) { for (int i : count) { System.out.println(" -- " + i); assertTrue("Assert batch return value > 0 (successful)", i > 0 || i == Statement.SUCCESS_NO_INFO); } } // Explicitly commit statements to apply changes System.out.println("Committing..."); con.commit(); } catch (Throwable t) { t.printStackTrace(); fail("Caught Unexpected Exception."); } // Try to insert the same item again, should fail because of the duplicate key System.out.println("Inserting the row with PK=200 again + PK=201 (for batchsize > 1, should fail)..."); pstmt.clearBatch(); pstmt.setInt(1, 200); pstmt.setString(2, "twohundred"); pstmt.addBatch(); pstmt.setInt(1, 201); pstmt.setString(2, "twohundred-one"); pstmt.addBatch(); try { int[] count = pstmt.executeBatch(); System.out.println("Count (" + ((count == null) ? "null" : count.length) + "): "); if (count != null && count.length > 0) { for (int i : count) { System.out.println(" -- " + i); } } fail("pstmt.executeBatch should have thrown an Exception"); } catch (BatchUpdateException bue) { System.out.println("Caught expected BatchUpdateException!"); bue.printStackTrace(); } finally { con.rollback(); } } }