Skip to content

Commit

Permalink
[misc] adding test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Mar 18, 2020
1 parent 3e6d64c commit f1d0924
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
8 changes: 0 additions & 8 deletions src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -1544,12 +1544,4 @@ protected void checkClose() throws SQLException {
}
}

/**
* Permit to retrieve current connection thread id, or -1 if unknown.
*
* @return current connection thread id.
*/
public long getServerThreadId() {
return (protocol != null) ? protocol.getServerThreadId() : -1;
}
}
21 changes: 5 additions & 16 deletions src/main/java/org/mariadb/jdbc/UrlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,14 @@ private static void defineUrlParserParameters(
if (additionalParameters != null) {
//noinspection Annotator
Matcher matcher = URL_PARAMETER.matcher(additionalParameters);

if (matcher.find()) {

urlParser.database = matcher.group(2);
urlParser.options =
DefaultOptions.parse(urlParser.haMode, matcher.group(4), properties, urlParser.options);
if (urlParser.database != null && urlParser.database.isEmpty()) {
urlParser.database = null;
}

} else {

matcher.find();
urlParser.database = matcher.group(2);
urlParser.options =
DefaultOptions.parse(urlParser.haMode, matcher.group(4), properties, urlParser.options);
if (urlParser.database != null && urlParser.database.isEmpty()) {
urlParser.database = null;
urlParser.options =
DefaultOptions.parse(urlParser.haMode, "", properties, urlParser.options);
}

} else {

urlParser.database = null;
urlParser.options = DefaultOptions.parse(urlParser.haMode, "", properties, urlParser.options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public HandleErrorResult relaunchOperation(Method method, Object[] args) throws
handleErrorResult.resultObject = method.invoke(currentProtocol, args);
handleErrorResult.mustThrowError = false;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new SQLException(e);
throw new SQLException(e.getCause());
}
}
}
Expand Down
52 changes: 49 additions & 3 deletions src/test/java/org/mariadb/jdbc/JdbcParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@

package org.mariadb.jdbc;

import static org.junit.Assert.*;
import org.junit.Test;
import org.mariadb.jdbc.internal.util.constant.HaMode;
import org.mariadb.jdbc.internal.util.constant.ParameterConstant;
import org.mariadb.jdbc.util.Options;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;
import org.junit.Test;
import org.mariadb.jdbc.internal.util.constant.HaMode;

import static org.junit.Assert.*;

@SuppressWarnings("ConstantConditions")
public class JdbcParserTest {
Expand Down Expand Up @@ -129,6 +133,48 @@ public void testAuroraUseBatchMultiSend() throws Throwable {
assertTrue(datasource.getUrlParser().auroraPipelineQuirks().getOptions().useBatchMultiSend);
}

@Test
public void testWrongFormat() {
try {
UrlParser.parse("jdbc:mariadb:/localhost/test");
fail("Must have throw exception");
} catch (SQLException ie) {
assertTrue(ie.getMessage().contains("url parsing error : '//' is not present in the url"));
}
}

@Test
public void testNoAdditionalPart() throws SQLException {
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost/").getDatabase());
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost/").getUsername());
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost").getDatabase());
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost").getUsername());
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost?").getDatabase());
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost?").getUsername());
}

@Test
public void testDatabaseOnly() throws SQLException {
assertEquals("DB", UrlParser.parse("jdbc:mariadb://localhost/DB").getDatabase());
assertEquals(null, UrlParser.parse("jdbc:mariadb://localhost/DB").getUsername());
}

@Test
public void testUrl() throws SQLException {
UrlParser parser = new UrlParser("DB", Arrays.asList(new HostAddress("local", 3306)), new Options(), HaMode.REPLICATION);
assertEquals("jdbc:mariadb:replication://address=(host=local)(port=3306)(type=master)/DB", parser.getInitialUrl());
assertEquals("jdbc:mariadb:replication://address=(host=local)(port=3306)(type=master)/DB", parser.toString());
assertEquals(parser, parser);
assertNotEquals(this, parser);
assertEquals(UrlParser.parse("jdbc:mariadb:replication://address=(host=local)(port=3306)(type=master)/DB"), parser);

parser = new UrlParser("DB", Arrays.asList(new HostAddress("host1", 3306), new HostAddress("host2", 3307, ParameterConstant.TYPE_SLAVE)), new Options(), HaMode.REPLICATION);
assertEquals("jdbc:mariadb:replication://address=(host=host1)(port=3306)(type=master),address=(host=host2)(port=3307)(type=slave)/DB", parser.getInitialUrl());
parser.setDatabase("DB2");
assertEquals("DB2", parser.getDatabase());
}


@Test
public void testAuroraUsePipelineAuth() throws Throwable {
assertTrue(
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/org/mariadb/jdbc/LocalInfileDisableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public static void initClass() throws SQLException {

@Test
public void testLocalInfileWithoutInputStream() throws SQLException {
Assume.assumeFalse(
(isMariadbServer() && minVersion(10, 4, 0)) || (!isMariadbServer() && minVersion(8, 0, 3)));
try (Connection connection = setConnection("&allowLocalInfile=false")) {
Exception ex = null;
try (Statement stmt = connection.createStatement()) {
Expand All @@ -88,9 +86,8 @@ public void testLocalInfileWithoutInputStream() throws SQLException {

assertNotNull("Expected an exception to be thrown", ex);
String message = ex.getMessage();
String expectedMessage =
"Usage of LOCAL INFILE is disabled. To use it enable it via the connection property allowLocalInfile=true";
assertTrue(message.contains(expectedMessage));
assertTrue(message.contains("Usage of LOCAL INFILE is disabled. To use it enable it via the connection property allowLocalInfile=true") ||
message.contains("Loading local data is disabled"));
}
}
}

0 comments on commit f1d0924

Please sign in to comment.