Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing test cases
Inserting assumes instead of ifs and correcting hardcoded URL
parameters such as host, database, username
  • Loading branch information
Rasmus Johansson committed Apr 17, 2015
1 parent cbb6522 commit 1111466
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 40 deletions.
7 changes: 3 additions & 4 deletions src/test/java/org/mariadb/jdbc/BigQueryTest.java
@@ -1,6 +1,7 @@
package org.mariadb.jdbc;


import org.junit.Assume;
import org.junit.Test;

import java.sql.PreparedStatement;
Expand All @@ -16,8 +17,7 @@ public class BigQueryTest extends BaseTest{
@Test
public void sendBigQuery2() throws SQLException {

if(!checkMaxAllowedPacket("sendBigQuery2"))
return;
Assume.assumeTrue(checkMaxAllowedPacket("sendBigQuery2"));

Statement stmt = connection.createStatement();
stmt.execute("drop table if exists bigblob");
Expand Down Expand Up @@ -46,8 +46,7 @@ public void sendBigQuery2() throws SQLException {
@Test
public void sendBigPreparedQuery() throws SQLException {

if(!checkMaxAllowedPacket("sendBigPreparedQuery"))
return;
Assume.assumeTrue(checkMaxAllowedPacket("sendBigPreparedQuery"));

Statement stmt = connection.createStatement();
stmt.execute("drop table if exists bigblob2");
Expand Down
51 changes: 39 additions & 12 deletions src/test/java/org/mariadb/jdbc/DriverTest.java
@@ -1,6 +1,7 @@
package org.mariadb.jdbc;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.mariadb.jdbc.internal.common.packet.buffer.WriteBuffer;

Expand Down Expand Up @@ -471,7 +472,7 @@ public void bigAutoIncTest() throws SQLException {

@Test
public void bigUpdateCountTest() throws SQLException {
Statement stmt = connection.createStatement();
Statement stmt = connection.createStatement();
stmt.execute("drop table if exists test_big_update");
stmt.execute("create table test_big_update (id int primary key not null, updateme int)");
for(int i=0;i<4000;i++) {
Expand Down Expand Up @@ -778,7 +779,6 @@ public void testBatchLoopWithDupKey() throws SQLException {
connection.createStatement().execute(
"create table rewritetest2 (id int not null primary key, a varchar(10), b int) engine=innodb");

long startTime = System.currentTimeMillis();
PreparedStatement ps = connection.prepareStatement("insert into rewritetest2 values (?,?,?) on duplicate key update a=values(a)");
for(int i = 0;i<2;i++) {
ps.setInt(1,0);
Expand All @@ -787,7 +787,7 @@ public void testBatchLoopWithDupKey() throws SQLException {
ps.addBatch();
}
ps.executeBatch();
assertTrue(System.currentTimeMillis() - startTime < 10);

ResultSet rs = connection.createStatement().executeQuery("select * from rewritetest2");
int i = 0;
while(rs.next()) {
Expand Down Expand Up @@ -1132,8 +1132,9 @@ public void testConnectWithDB() throws SQLException {

@Test
public void testError() throws SQLException {
if(!checkMaxAllowedPacket("testError"))
return;
// check that max_allowed_packet is big enough for the test
Assume.assumeTrue(checkMaxAllowedPacket("testError"));

try {
char arr[] = new char[16*1024*1024-1];
Arrays.fill(arr,'a');
Expand Down Expand Up @@ -1169,6 +1170,10 @@ public void StreamingResult() throws SQLException {
@Test
public void NoBackslashEscapes() throws SQLException {
requireMinimumVersion(5,0);

// super privilege is needed for this test
Assume.assumeTrue(hasSuperPrivilege("NoBackslashEscapes"));

Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select @@global.sql_mode");
rs.next();
Expand Down Expand Up @@ -1203,6 +1208,10 @@ public void NoBackslashEscapes() throws SQLException {
@Test
public void NoBackslashEscapes2() throws SQLException {
requireMinimumVersion(5,0);

// super privilege is needed for this test
Assume.assumeTrue(hasSuperPrivilege("NoBackslashEscapes2"));

Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select @@global.sql_mode");
rs.next();
Expand Down Expand Up @@ -1243,6 +1252,10 @@ public void NoBackslashEscapes2() throws SQLException {
// Test if driver works with sql_mode= ANSI_QUOTES
@Test
public void AnsiQuotes() throws SQLException {

// super privilege is needed for this test
Assume.assumeTrue(hasSuperPrivilege("AnsiQuotes"));

Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select @@global.sql_mode");
rs.next();
Expand Down Expand Up @@ -1316,7 +1329,7 @@ public void autoreconnect() throws Exception {

@Test
public void useSSL() throws Exception {
org.junit.Assume.assumeTrue(haveSSL());
Assume.assumeTrue(haveSSL());
setConnection("&useSSL=1&trustServerCertificate=1");
connection.createStatement().execute("select 1");

Expand All @@ -1325,6 +1338,15 @@ public void useSSL() throws Exception {
@Test
// Bug in URL parser
public void mdev3916() throws Exception {
try {
setConnection("&password=");
}
catch (SQLException ex)
{
//SQLException is ok because we might get for example an access denied exception
if (!(ex.getMessage().indexOf("Could not connect: Access denied") > -1))
throw ex;
}
setConnection("&password=");
}

Expand Down Expand Up @@ -1386,7 +1408,7 @@ public void conj14() throws Exception {
Statement st = connection.createStatement();

/* 1. Test update statement */
st.execute("use test");
st.execute("use " + database);
assertEquals(0,st.getUpdateCount());

/* No more results */
Expand Down Expand Up @@ -1431,7 +1453,7 @@ public void conj14() throws Exception {

/* 4. Batch with a SELECT and non-SELECT */

st.execute("select 1; use test");
st.execute("select 1; use " + database);
/* First result (select)*/
assertEquals(-1,st.getUpdateCount());
assertTrue(st.getResultSet() != null);
Expand Down Expand Up @@ -1476,14 +1498,16 @@ public void namedpipe() throws Exception {
if(rs.getBoolean(1)) {
namedPipeName = rs.getString(2);
} else {
System.out.println("skipping named pipe test");
return;
System.out.println("test 'namedpipe' skipped");
}
} catch(SQLException e) {
//named pipe not found,
System.out.println("skipping named pipe test");
return;
System.out.println("test 'namedpipe' skipped");
}

//skip test if no namedPipeName was obtained because then we do not use a socket connection
Assume.assumeTrue(namedPipeName != null);

setConnection("&pipe=" + namedPipeName);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
Expand Down Expand Up @@ -1517,6 +1541,9 @@ public void batchUpdateException() throws Exception {
@Test
public void localSocket() throws Exception {
requireMinimumVersion(5,1);

Assume.assumeTrue(isLocalConnection("localSocket"));

Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select @@version_compile_os,@@socket");
if (!rs.next())
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/mariadb/jdbc/MySQLDriverTest.java
@@ -1,9 +1,11 @@
package org.mariadb.jdbc;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;

import javax.sql.rowset.CachedRowSet;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand Down Expand Up @@ -216,9 +218,8 @@ public void FiftyMBRow() throws SQLException {
@Test
// Test query with length around max packet length. Requires max_allowed_packet to be >16M
public void largeQueryWrite() throws SQLException {
if (!checkMaxAllowedPacket("largeQueryWrite")) {
return;
}
Assume.assumeTrue(checkMaxAllowedPacket("largeQueryWrite"));

char[] str= new char[16*1024*1024];
Arrays.fill(str, 'a');
String prefix= "select length('";
Expand All @@ -239,8 +240,7 @@ public void largeQueryWrite() throws SQLException {

@Test
public void largePreparedQueryWrite() throws SQLException {
if (!checkMaxAllowedPacket("largePreparedQueryWrite"))
return;
Assume.assumeTrue(checkMaxAllowedPacket("largePreparedQueryWrite"));

char[] str= new char[16*1024*1024];
Arrays.fill(str, 'a');
Expand Down Expand Up @@ -272,8 +272,8 @@ public void smallQueryWriteCompress() throws SQLException {
}
@Test
public void largePreparedQueryWriteCompress() throws SQLException {
if(!checkMaxAllowedPacket("largePreparedQueryCompress"))
return;
Assume.assumeTrue(checkMaxAllowedPacket("largePreparedQueryCompress"));

setConnection("&useCompression=true");
try {
char[] str= new char[16*1024*1024];
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/mariadb/jdbc/PooledConnectionTest.java
Expand Up @@ -42,11 +42,11 @@ public void statementErrorOccurred(StatementEvent event) {
}
}

public class PooledConnectionTest {
public class PooledConnectionTest extends BaseTest {
@Test
public void testPooledConnectionClosed() throws Exception {
ConnectionPoolDataSource ds = new MySQLDataSource("localhost", 3306, "test");
PooledConnection pc = ds.getPooledConnection("root","");
ConnectionPoolDataSource ds = new MySQLDataSource(hostname, port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
Connection c= pc.getConnection();
MyEventListener listener = new MyEventListener();
pc.addConnectionEventListener(listener);
Expand All @@ -68,8 +68,8 @@ public void testPooledConnectionClosed() throws Exception {

@Test
public void testPooledConnectionException() throws Exception {
ConnectionPoolDataSource ds = new MySQLDataSource("localhost", 3306, "test");
PooledConnection pc = ds.getPooledConnection("root","");
ConnectionPoolDataSource ds = new MySQLDataSource(hostname, port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
MyEventListener listener = new MyEventListener();
pc.addConnectionEventListener(listener);
MySQLConnection c = (MySQLConnection)pc.getConnection();
Expand Down Expand Up @@ -100,8 +100,8 @@ public void testPooledConnectionException() throws Exception {

public void testPooledConnectionStatementError() throws Exception
{
ConnectionPoolDataSource ds = new MySQLDataSource("localhost", 3306, "test");
PooledConnection pc = ds.getPooledConnection("root","");
ConnectionPoolDataSource ds = new MySQLDataSource(hostname, port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
MyEventListener listener = new MyEventListener();
pc.addStatementEventListener(listener);
MySQLConnection c = (MySQLConnection)pc.getConnection();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/mariadb/jdbc/PreparedStatementTest.java
Expand Up @@ -30,7 +30,8 @@ public void setUp() throws SQLException {
*/
@Test
public void reexecuteStatementTest() throws SQLException {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&allowMultiQueries=true");
// set the allowMultiQueries parameter
setConnection("&allowMultiQueries=true");
PreparedStatement stmt = connection.prepareStatement("SELECT 1");
stmt.setFetchSize(Integer.MIN_VALUE);
ResultSet rs = stmt.executeQuery();
Expand Down
16 changes: 7 additions & 9 deletions src/test/java/org/mariadb/jdbc/XA.java
Expand Up @@ -27,9 +27,12 @@ public void checkSupported() throws SQLException {
requireMinimumVersion(5,0);
}
public XA() {
dataSource = new MySQLDataSource();
dataSource.setUser(username);
dataSource = new MySQLDataSource();
dataSource.setServerName(hostname);
dataSource.setPortNumber(port);
dataSource.setDatabaseName(database);
dataSource.setUser(username);
dataSource.setPassword(password);
}
Xid newXid() {
return new MySQLXid(1, UUID.randomUUID().toString().getBytes(),UUID.randomUUID().toString().getBytes());
Expand All @@ -44,12 +47,7 @@ Xid newXid(Xid branchFrom) {
* @throws Exception
*/
void test2PC(boolean doCommit) throws Exception{


MySQLDataSource dataSource = new MySQLDataSource();
dataSource.setUser("root");
dataSource.setDatabaseName("test");


connection.createStatement().execute("DROP TABLE IF EXISTS xatable");
connection.createStatement().execute("CREATE TABLE xatable (i int) ENGINE=InnoDB");

Expand Down Expand Up @@ -213,4 +211,4 @@ public void resumeAndJoinTest() throws Exception {
}
}

}
}

0 comments on commit 1111466

Please sign in to comment.