Skip to content

Commit

Permalink
Merge branch 'rasmus-test-case-fixes' into rasmus-failover
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
  • Loading branch information
Rasmus Johansson committed Apr 14, 2015
2 parents 869cbdf + 830e29c commit edef9c0
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 81 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -4,4 +4,8 @@

# Keep empty directories:
# Keep empty directories: >> .gitignore/.git*
<<<<<<< HEAD
=======
/bin/
>>>>>>> rasmus-test-case-fixes
/target/
53 changes: 23 additions & 30 deletions src/main/java/org/mariadb/jdbc/JDBCUrl.java
Expand Up @@ -68,51 +68,44 @@ private JDBCUrl( String username, String password, String database, HostAddress
/*
Parse ConnectorJ compatible urls
jdbc:mysql://host:port/database
Example: jdbc:mysql://localhost:3306/test?user=root&password=passwd
*/
private static JDBCUrl parseConnectorJUrl(String url) {
if (!url.startsWith("jdbc:mysql://")) {
return null;
}

url = url.substring(13);

String hostname;
String database;
String user;
String password;
String user = "";
String password = "";
String[] tokens = url.split("/");
hostname=tokens[0];
database=(tokens.length > 1)?tokens[1]:null;

hostname = tokens[0];
database = (tokens.length > 1) ? tokens[1] : null;

if (database == null) {
return new JDBCUrl("", "", database, HostAddress.parse(hostname));
}
int start = tokens[1].indexOf("user=");
int end = tokens[1].indexOf("&", start);
if (start == -1) {
user = "";
} else {
if (end == -1) {
user = tokens[1].substring(start + 5);
} else {
user = tokens[1].substring(start + 5, end);
}
database = database.replaceFirst("user=" + user, "");
}
start = tokens[1].indexOf("password=");
end = tokens[1].indexOf("&", start);
if (start == -1) {
password = "";
} else {
if (end == -1) {
password = tokens[1].substring(start + 9);
} else {
password = tokens[1].substring(start + 9, end);

//check if there are parameters
if (database.indexOf('?') > -1)
{
String[] credentials = database.substring(database.indexOf('?'), database.length()).split("&");

database = database.substring(0, database.indexOf('?'));

for (int i = 0; i < credentials.length; i++)
{
if (credentials[i].indexOf("user=") > -1)
user = credentials[i].split("=")[1];
else if (credentials[i].indexOf("password=") > -1)
password = credentials[i].split("=")[1];
}
database = database.replaceFirst("password=" + password, "");
}
if (database.lastIndexOf("?") == database.length()-1) {
database = database.substring(0, database.length()-1);
}

return new JDBCUrl(user, password, database, HostAddress.parse(hostname));
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mariadb/jdbc/MySQLConnection.java
Expand Up @@ -60,7 +60,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.util.concurrent.Executor;


public final class MySQLConnection implements Connection {
public final class MySQLConnection implements Connection {
/**
* the protocol to communicate with.
*/
Expand Down Expand Up @@ -88,7 +88,7 @@ public final class MySQLConnection implements Connection {
*
* @param protocol the protocol to use.
*/
private MySQLConnection( MySQLProtocol protocol) {
private MySQLConnection(MySQLProtocol protocol) {
this.protocol = protocol;
clientInfoProperties = protocol.getInfo();
}
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/org/mariadb/jdbc/MySQLStatement.java
Expand Up @@ -1134,13 +1134,28 @@ private void isInsertRewriteable(String sql) {
*/
protected int getInsertIncipit(String sql) {
String sqlUpper = sql.toUpperCase();
if (! sqlUpper.startsWith("INSERT")
|| sqlUpper.indexOf(";") != -1) {

if (! sqlUpper.startsWith("INSERT"))
return -1;
}

int idx = sqlUpper.indexOf(" VALUE");
int index = sqlUpper.indexOf("(", idx);
return index;
int startBracket = sqlUpper.indexOf("(", idx);
int endBracket = sqlUpper.indexOf(")", startBracket);

// Check for semicolons. Allow them inside the VALUES() brackets, otherwise return -1
// there can be multiple, so let's loop through them

int semicolonPos = sqlUpper.indexOf(';');

while (semicolonPos > -1)
{
if (semicolonPos < startBracket || semicolonPos > endBracket)
return -1;

semicolonPos = sqlUpper.indexOf(';', semicolonPos + 1);
}

return startBracket;
}

/**
Expand Down
59 changes: 58 additions & 1 deletion src/test/java/org/mariadb/jdbc/BaseTest.java
Expand Up @@ -6,6 +6,8 @@
import org.junit.BeforeClass;
import org.junit.Ignore;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
Expand All @@ -27,11 +29,15 @@ public class BaseTest {
public static void beforeClassBaseTest() {
String url = System.getProperty("dbUrl", mDefUrl);
JDBCUrl jdbcUrl = JDBCUrl.parse(url);

hostname = jdbcUrl.getHostname();
port = jdbcUrl.getPort();
database = jdbcUrl.getDatabase();
username = jdbcUrl.getUsername();
password = jdbcUrl.getPassword();

logInfo("Properties parsed from JDBC URL - hostname: " + hostname + ", port: " + port + ", database: " + database + ", username: " + username + ", password: " + password);

if (database != null && "".equals(username)) {
String[] tokens = database.contains("?") ? database.split("\\?") : null;
if (tokens != null) {
Expand Down Expand Up @@ -107,7 +113,7 @@ protected void setConnection(Map<String, String> props) throws SQLException {
openConnection(connU, info);
}
protected void setConnection(Properties info) throws SQLException {
openConnection(connU, info);
openConnection(connURI, info);
}
protected void setConnection(String parameters) throws SQLException {
openConnection(connURI + parameters, null);
Expand Down Expand Up @@ -149,6 +155,51 @@ boolean checkMaxAllowedPacket(String testName) throws SQLException
}
return true;
}

//does the user have super privileges or not?
boolean hasSuperPrivilege(String testName) throws SQLException
{
boolean superPrivilege = false;
Statement st = connection.createStatement();

// first test for specific user and host combination
ResultSet rs = st.executeQuery("SELECT Super_Priv FROM mysql.user WHERE user = '" + username + "' AND host = '" + hostname + "'");
if (rs.next())
superPrivilege = (rs.getString(1) == "Y" ? true : false);
else
{
// then check for user on whatever (%) host
rs = st.executeQuery("SELECT Super_Priv FROM mysql.user WHERE user = '" + username + "' AND host = '%'");
if (rs.next())
superPrivilege = (rs.getString(1) == "Y" ? true : false);
}

rs.close();

if (!superPrivilege)
System.out.println("test '" + testName + "' skipped because user '" + username + "' doesn't have SUPER privileges");

return superPrivilege;
}

//is the connection local?
boolean isLocalConnection(String testName)
{
boolean isLocal = false;

try {
if (InetAddress.getByName(hostname).isAnyLocalAddress() || InetAddress.getByName(hostname).isLoopbackAddress())
isLocal = true;
} catch (UnknownHostException e) {
// for some reason it wasn't possible to parse the hostname
// do nothing
}

if (isLocal == false)
System.out.println("test '" + testName + "' skipped because connection is not local");

return isLocal;
}

boolean haveSSL(){
try {
Expand All @@ -169,4 +220,10 @@ void requireMinimumVersion(int major, int minor) throws SQLException {
(dbMajor == major && dbMinor >= minor));

}

// common function for logging information
static void logInfo(String message)
{
System.out.println(message);
}
}
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

0 comments on commit edef9c0

Please sign in to comment.