Skip to content

Commit

Permalink
[CONJ-167] part 2 -Connection send SQLException replacing IllegalArgu…
Browse files Browse the repository at this point in the history
…mentException when URL parsing error
  • Loading branch information
rusher committed Jul 21, 2015
1 parent 58b62cc commit 4419ac0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 68 deletions.
79 changes: 41 additions & 38 deletions src/main/java/org/mariadb/jdbc/JDBCUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.mariadb.jdbc.internal.common.UrlHAMode;
import org.mariadb.jdbc.internal.common.query.IllegalParameterException;

import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -96,7 +97,7 @@ public class JDBCUrl {

private JDBCUrl(){};

protected JDBCUrl(String database, List<HostAddress> addresses, Options options, UrlHAMode haMode) {
protected JDBCUrl(String database, List<HostAddress> addresses, Options options, UrlHAMode haMode) throws SQLException {
this.options = options;
this.database = database;
this.addresses = addresses;
Expand All @@ -120,11 +121,11 @@ static boolean acceptsURL(String url) {

}

public static JDBCUrl parse(final String url) {
public static JDBCUrl parse(final String url) throws SQLException {
return parse(url, new Properties());
}

public static JDBCUrl parse(final String url, Properties prop) {
public static JDBCUrl parse(final String url, Properties prop) throws SQLException {
if (url != null) {
if (prop == null) prop = new Properties();
if (url.startsWith("jdbc:mysql:")) {
Expand All @@ -144,7 +145,7 @@ public static JDBCUrl parse(final String url, Properties prop) {
return null;
}

public void parseUrl(String url) {
public void parseUrl(String url) throws SQLException {
if (!url.startsWith("jdbc:mysql:")) throw new IllegalArgumentException("Url must start with \"jdbc:mysql:\"");
parseInternal(this, url, new Properties());
}
Expand All @@ -154,49 +155,51 @@ public void parseUrl(String url) {
jdbc:mysql://host:port/database
Example: jdbc:mysql://localhost:3306/test?user=root&password=passwd
*/
private static void parseInternal(JDBCUrl jdbcUrl, String url, Properties properties) {

String[] baseTokens = url.substring(0,url.indexOf("//")).split(":");

//parse HA mode
jdbcUrl.haMode = UrlHAMode.NONE;
if (baseTokens.length > 2) {
try {
jdbcUrl.haMode = UrlHAMode.valueOf(baseTokens[2].toUpperCase());
}catch (IllegalArgumentException i) {
throw new IllegalArgumentException("url parameter error '" + baseTokens[2] +"' is a unknown parameter in the url "+url);
private static void parseInternal(JDBCUrl jdbcUrl, String url, Properties properties) throws SQLException {
try {
String[] baseTokens = url.substring(0,url.indexOf("//")).split(":");

//parse HA mode
jdbcUrl.haMode = UrlHAMode.NONE;
if (baseTokens.length > 2) {
try {
jdbcUrl.haMode = UrlHAMode.valueOf(baseTokens[2].toUpperCase());
}catch (IllegalArgumentException i) {
throw new IllegalArgumentException("url parameter error '" + baseTokens[2] +"' is a unknown parameter in the url "+url);
}
}
}

url = url.substring(url.indexOf("//") + 2);
String[] tokens = url.split("/");
String hostAddressesString= tokens[0];
String additionalParameters = (tokens.length > 1) ? url.substring(tokens[0].length() + 1) : null;
url = url.substring(url.indexOf("//") + 2);
String[] tokens = url.split("/");
String hostAddressesString= tokens[0];
String additionalParameters = (tokens.length > 1) ? url.substring(tokens[0].length() + 1) : null;

jdbcUrl.addresses = HostAddress.parse(hostAddressesString, jdbcUrl.haMode);
jdbcUrl.addresses = HostAddress.parse(hostAddressesString, jdbcUrl.haMode);

if (additionalParameters == null) {
jdbcUrl.database = null;
jdbcUrl.options = DefaultOptions.parse(jdbcUrl.haMode, "",properties);
} else {
int ind = additionalParameters.indexOf('?');
if (ind > -1) {
jdbcUrl.database = additionalParameters.substring(0, ind);
jdbcUrl.options = DefaultOptions.parse(jdbcUrl.haMode, additionalParameters.substring(ind + 1),properties);
} else {
jdbcUrl.database = additionalParameters;
if (additionalParameters == null) {
jdbcUrl.database = null;
jdbcUrl.options = DefaultOptions.parse(jdbcUrl.haMode, "",properties);
} else {
int ind = additionalParameters.indexOf('?');
if (ind > -1) {
jdbcUrl.database = additionalParameters.substring(0, ind);
jdbcUrl.options = DefaultOptions.parse(jdbcUrl.haMode, additionalParameters.substring(ind + 1),properties);
} else {
jdbcUrl.database = additionalParameters;
jdbcUrl.options = DefaultOptions.parse(jdbcUrl.haMode, "",properties);
}
}
}

if (jdbcUrl.haMode == UrlHAMode.AURORA) {
for (HostAddress hostAddress : jdbcUrl.addresses) hostAddress.type = null;
} else {
for (HostAddress hostAddress : jdbcUrl.addresses) {
if (hostAddress.type == null) hostAddress.type = ParameterConstant.TYPE_MASTER;
if (jdbcUrl.haMode == UrlHAMode.AURORA) {
for (HostAddress hostAddress : jdbcUrl.addresses) hostAddress.type = null;
} else {
for (HostAddress hostAddress : jdbcUrl.addresses) {
if (hostAddress.type == null) hostAddress.type = ParameterConstant.TYPE_MASTER;
}
}
} catch (IllegalArgumentException i) {
throw new SQLException(i.getMessage());
}

}

public String getUsername() {
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/mariadb/jdbc/MySQLDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,26 @@ public class MySQLDataSource implements DataSource, ConnectionPoolDataSource, XA

private final JDBCUrl jdbcUrl;

public MySQLDataSource(String hostname, int port, String database) {
public MySQLDataSource(String hostname, int port, String database) throws SQLException {
ArrayList<HostAddress> hostAddresses = new ArrayList<HostAddress>();
hostAddresses.add(new HostAddress(hostname, port));
jdbcUrl = new JDBCUrl(database, hostAddresses, DefaultOptions.defaultValues(UrlHAMode.NONE), UrlHAMode.NONE);
}

public MySQLDataSource(String url) {
public MySQLDataSource(String url) throws SQLException {
this.jdbcUrl = JDBCUrl.parse(url);
}

public MySQLDataSource() {
ArrayList<HostAddress> hostAddresses = new ArrayList<HostAddress>();
ArrayList<HostAddress> hostAddresses = new ArrayList<>();
hostAddresses.add(new HostAddress("localhost", 3306));
jdbcUrl = new JDBCUrl("", hostAddresses, DefaultOptions.defaultValues(UrlHAMode.NONE), UrlHAMode.NONE);
JDBCUrl tmpJDBCUrl = null;
try {
tmpJDBCUrl = new JDBCUrl("", hostAddresses, DefaultOptions.defaultValues(UrlHAMode.NONE), UrlHAMode.NONE);
} catch (SQLException e) {
//cannot happen, but eating exception to avoid throw SQLException in class descriptor
}
jdbcUrl = tmpJDBCUrl;
}

/**
Expand Down Expand Up @@ -217,20 +223,19 @@ public void setProperties(String properties) {
/**
* Sets the connection string URL.
*
* @param url
* the connection string
* @param url the connection string
* @throws SQLException
*/
public void setURL(String url) {
public void setURL(String url) throws SQLException {
setUrl(url);
}

/**
* Sets the connection string URL.
*
* @param s
* the connection string
* @param s the connection string
* @throws SQLException
*/
public void setUrl(String s) {
public void setUrl(String s) throws SQLException {
this.jdbcUrl.parseUrl(s);
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/mariadb/jdbc/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class BaseTest {
protected static boolean testSingleHost;

@BeforeClass
public static void beforeClassBaseTest() {
public static void beforeClassBaseTest() throws SQLException {
String url = System.getProperty("dbUrl", mDefUrl);
testSingleHost = Boolean.parseBoolean(System.getProperty("testSingleHost", "true"));
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/mariadb/jdbc/DriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void testConnectNoDB() throws Exception{
}

@Test
public void testConnectorJURL() {
public void testConnectorJURL() throws SQLException {
JDBCUrl url = JDBCUrl.parse("jdbc:mysql://localhost/test");
assertEquals("localhost", url.getHostAddresses().get(0).host);
assertEquals("test", url.getDatabase());
Expand Down
33 changes: 17 additions & 16 deletions src/test/java/org/mariadb/jdbc/JdbcParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;
import org.mariadb.jdbc.internal.common.UrlHAMode;

import java.sql.SQLException;
import java.util.Properties;

public class JdbcParserTest {
Expand Down Expand Up @@ -66,24 +67,24 @@ public void testOptionParseIntegerMinimum() throws Throwable {
Assert.assertTrue("root".equals(jdbc.getOptions().user));
}

@Test(expected = IllegalArgumentException.class )
@Test(expected = SQLException.class )
public void testOptionParseIntegerNotPossible() throws Throwable {
JDBCUrl.parse("jdbc:mysql://localhost/test?user=root&autoReconnect=true&validConnectionTimeout=-2&connectTimeout=5");
Assert.fail();
}

@Test()
public void testJDBCParserSimpleIPv4basic() {
public void testJDBCParserSimpleIPv4basic() throws SQLException {
String url = "jdbc:mysql://master:3306,slave1:3307,slave2:3308/database";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
}
@Test
public void testJDBCParserSimpleIPv4basicError() {
public void testJDBCParserSimpleIPv4basicError() throws SQLException {
JDBCUrl jdbcUrl = JDBCUrl.parse(null);
Assert.assertTrue(jdbcUrl == null);
}
@Test
public void testJDBCParserSimpleIPv4basicwithoutDatabase() {
public void testJDBCParserSimpleIPv4basicwithoutDatabase() throws SQLException {
String url = "jdbc:mysql://master:3306,slave1:3307,slave2:3308/";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Assert.assertNull(jdbcUrl.getDatabase());
Expand All @@ -96,7 +97,7 @@ public void testJDBCParserSimpleIPv4basicwithoutDatabase() {
}

@Test
public void testJDBCParserSimpleIPv4Properties() {
public void testJDBCParserSimpleIPv4Properties() throws SQLException {
String url = "jdbc:mysql://master:3306,slave1:3307,slave2:3308/database?autoReconnect=true";
Properties prop = new Properties();
prop.setProperty("user","greg");
Expand All @@ -114,7 +115,7 @@ public void testJDBCParserSimpleIPv4Properties() {
}

@Test
public void testJDBCParserSimpleIPv4() {
public void testJDBCParserSimpleIPv4() throws SQLException {
String url = "jdbc:mysql://master:3306,slave1:3307,slave2:3308/database?user=greg&password=pass";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Assert.assertTrue("database".equals(jdbcUrl.getDatabase()));
Expand All @@ -128,7 +129,7 @@ public void testJDBCParserSimpleIPv4() {


@Test
public void testJDBCParserSimpleIPv6() {
public void testJDBCParserSimpleIPv6() throws SQLException {
String url = "jdbc:mysql://[2001:0660:7401:0200:0000:0000:0edf:bdd7]:3306,[2001:660:7401:200::edf:bdd7]:3307/database?user=greg&password=pass";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Assert.assertTrue("database".equals(jdbcUrl.getDatabase()));
Expand All @@ -141,7 +142,7 @@ public void testJDBCParserSimpleIPv6() {


@Test
public void testJDBCParserParameter() {
public void testJDBCParserParameter() throws SQLException {
String url = "jdbc:mysql://address=(type=master)(port=3306)(host=master1),address=(port=3307)(type=master)(host=master2),address=(type=slave)(host=slave1)(port=3308)/database?user=greg&password=pass";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Assert.assertTrue("database".equals(jdbcUrl.getDatabase()));
Expand All @@ -153,33 +154,33 @@ public void testJDBCParserParameter() {
Assert.assertTrue(new HostAddress("slave1", 3308, "slave").equals(jdbcUrl.getHostAddresses().get(2)));
}

@Test
@Test()
public void testJDBCParserParameterErrorEqual() {
String url = "jdbc:mysql://address=(type=)(port=3306)(host=master1),address=(port=3307)(type=master)(host=master2),address=(type=slave)(host=slave1)(port=3308)/database?user=greg&password=pass";
try {
JDBCUrl.parse(url);
Assert.fail();
}catch (IllegalArgumentException e) {
}catch (SQLException e) {
Assert.assertTrue(true);
}
}

@Test
public void testJDBCParserHAModeNone() {
public void testJDBCParserHAModeNone() throws SQLException {
String url = "jdbc:mysql://localhost/database";
JDBCUrl jdbc = JDBCUrl.parse(url);
Assert.assertTrue(jdbc.getHaMode().equals(UrlHAMode.NONE));
}

@Test
public void testJDBCParserHAModeLoadReplication() {
public void testJDBCParserHAModeLoadReplication() throws SQLException {
String url = "jdbc:mysql:replication://localhost/database";
JDBCUrl jdbc = JDBCUrl.parse(url);
Assert.assertTrue(jdbc.getHaMode().equals(UrlHAMode.REPLICATION));
}

@Test
public void testJDBCParserReplicationParameter() {
public void testJDBCParserReplicationParameter() throws SQLException {
String url = "jdbc:mysql:replication://address=(type=master)(port=3306)(host=master1),address=(port=3307)(type=master)(host=master2),address=(type=slave)(host=slave1)(port=3308)/database?user=greg&password=pass";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Assert.assertTrue("database".equals(jdbcUrl.getDatabase()));
Expand All @@ -192,7 +193,7 @@ public void testJDBCParserReplicationParameter() {
}

@Test
public void testJDBCParserReplicationParameterWithoutType() {
public void testJDBCParserReplicationParameterWithoutType() throws SQLException {
String url = "jdbc:mysql:replication://master1,slave1,slave2/database";
JDBCUrl jdbcUrl = JDBCUrl.parse(url);
Assert.assertTrue("database".equals(jdbcUrl.getDatabase()));
Expand All @@ -203,7 +204,7 @@ public void testJDBCParserReplicationParameterWithoutType() {
}

@Test
public void testJDBCParserHAModeLoadAurora() {
public void testJDBCParserHAModeLoadAurora() throws SQLException {
String url = "jdbc:mysql:aurora://localhost/database";
JDBCUrl jdbc = JDBCUrl.parse(url);
Assert.assertTrue(jdbc.getHaMode().equals(UrlHAMode.AURORA));
Expand All @@ -213,7 +214,7 @@ public void testJDBCParserHAModeLoadAurora() {
* CONJ-167 : Driver is throwing IllegalArgumentException instead of returning null
*/
@Test
public void checkOtherDriverCompatibility() {
public void checkOtherDriverCompatibility() throws SQLException {
String url = "jdbc:h2:mem:RZM;DB_CLOSE_DELAY=-1";
JDBCUrl jdbc = JDBCUrl.parse(url);
Assert.assertTrue(jdbc == null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static boolean requireMinimumVersion(Connection connection, int major, in
(dbMajor == major && dbMinor >= minor));
}

private static String createProxies(String tmpUrl, TestType proxyType) {
private static String createProxies(String tmpUrl, TestType proxyType) throws SQLException {
JDBCUrl tmpJdbcUrl = JDBCUrl.parse(tmpUrl);
TcpProxy[] tcpProxies = new TcpProxy[tmpJdbcUrl.getHostAddresses().size()];
username = tmpJdbcUrl.getUsername();
Expand Down

0 comments on commit 4419ac0

Please sign in to comment.