Permalink
Browse files

METRICS-3198: Postgres sql parser fails to parse EDB connection urls

  • Loading branch information...
1 parent 40f9ddb commit a8980eaff1bea18a1b386ee57d97b23b92e77069 Shachar Anchelovich committed Dec 30, 2012
View
50 .../src/main/java/com/springsource/insight/plugin/jdbc/parser/parsers/PostgresSqlParser.java
@@ -16,7 +16,7 @@
package com.springsource.insight.plugin.jdbc.parser.parsers;
-import com.springsource.insight.plugin.jdbc.parser.SimpleSqlUrlParser;
+import com.springsource.insight.plugin.jdbc.parser.AbstractSqlPatternParser;
/**
* <P>The following formats are supported:</P></BR>
@@ -35,11 +35,55 @@
* jdbc:postgresql://neptune.acme.com:5432/test
* jdbc:postgresql:test - equivalent to jdbc:postgresql://localhost:5432/test
*/
-public class PostgresSqlParser extends SimpleSqlUrlParser {
+public class PostgresSqlParser extends AbstractSqlPatternParser {
+ /**
+ * Defines a non-capturing group for postgres or edb
+ */
+ private static final String VENDOR_LIST = JDBC_PREFIX + ":(?:postgres|edb)";
+
+ /**
+ * Defines a group for IPv6, IPv4 IPs and host names<br/>
+ * Will match the next addresses:
+ * <ol>
+ * <li>IPv6 localhost - [::1]
+ * <li>IPv6 ip address - [2001:0db8:85a3:0042:1000:8a2e:0370:7334]
+ * </ol>
+ */
+ private static final String IPv6_PATTERN = "\\[(::1|[0-9a-f]{4}(?:[:][0-9a-f]{4}){7})\\]";
+
+ /**
+ * Matches jdbc:postgres:database and jdbc:edb:database
+ */
+ private static final String PATTERN1 = VENDOR_LIST+":([^:/?]+)";
+
+ /**
+ * Matches jdbc:postgres://host/database and jdbc:edb://host/database (IPv4 and host names)
+ */
+ private static final String PATTERN2 = VENDOR_LIST+"://([^:/?]+)/([^:/?]+).*";
+
+ /**
+ * Matches jdbc:postgres://host:port/database and jdbc:edb://host:port/database (IPv4 and host names)
+ */
+ private static final String PATTERN3 = VENDOR_LIST+"://([^:/?]+):(\\d+)/([^:/?]+).*";
+
+ /**
+ * Matches jdbc:postgres://host/database and jdbc:edb://host/database (IPv6)
+ */
+ private static final String PATTERN4 = VENDOR_LIST+"://"+IPv6_PATTERN+"/([^:/?]+).*";
+
+ /**
+ * Matches jdbc:postgres://host:port/database and jdbc:edb://host:port/database (IPv6)
+ */
+ private static final String PATTERN5 = VENDOR_LIST+"://"+IPv6_PATTERN+":(\\d+)/([^:/?]+).*";
+
public static final int DEFAULT_CONNECTION_PORT=5432;
public static final String VENDOR="postgresql";
public PostgresSqlParser () {
- super(VENDOR, DEFAULT_CONNECTION_PORT);
+ super(VENDOR, DEFAULT_CONNECTION_PORT, create(PATTERN1, -1, -1, 1),
+ create(PATTERN2, 1, -1, 2),
+ create(PATTERN3, 1, 2, 3),
+ create(PATTERN4, 1, -1, 2),
+ create(PATTERN5, 1, 2, 3));
}
}
View
125 ...dbc/src/test/java/com/springsource/insight/plugin/jdbc/parsers/PostgresSqlParserTest.java
@@ -17,13 +17,134 @@
package com.springsource.insight.plugin.jdbc.parsers;
import com.springsource.insight.plugin.jdbc.parser.DatabaseType;
+import com.springsource.insight.plugin.jdbc.parser.JdbcUrlParser;
import com.springsource.insight.plugin.jdbc.parser.parsers.PostgresSqlParser;
/**
*
*/
-public class PostgresSqlParserTest extends SimpleSqlUrlParserTestSupport<PostgresSqlParser> {
+public class PostgresSqlParserTest extends SqlParserTestImpl<PostgresSqlParser> {
public PostgresSqlParserTest () {
- super(DatabaseType.POSTGRESQL, new PostgresSqlParser());
+ super(DatabaseType.POSTGRESQL, new PostgresSqlParser(),
+ new SqlTestEntry("jdbc:postgres:database",
+ JdbcUrlParser.DEFAULT_HOST,
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb:database",
+ JdbcUrlParser.DEFAULT_HOST,
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres:data123",
+ JdbcUrlParser.DEFAULT_HOST,
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "data123")
+ ,
+ new SqlTestEntry("jdbc:edb:data123",
+ JdbcUrlParser.DEFAULT_HOST,
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "data123")
+ ,
+ new SqlTestEntry("jdbc:postgres://[::1]/database",
+ "::1",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://[::1]/database",
+ "::1",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]/database",
+ "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]/database",
+ "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]/database?someprop=somevalue",
+ "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://myhost/database",
+ "myhost",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://myhost/database",
+ "myhost",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://10.23.197.110/database",
+ "10.23.197.110",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://10.23.197.110/database",
+ "10.23.197.110",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://10.23.197.110/database?someprop=somevalue",
+ "10.23.197.110",
+ PostgresSqlParser.DEFAULT_CONNECTION_PORT,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://[::1]:9090/database",
+ "::1",
+ 9090,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://[::1]:9090/database",
+ "::1",
+ 9090,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:9090/database",
+ "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
+ 9090,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:9090/database",
+ "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
+ 9090,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://[2001:0db8:85a3:0042:1000:8a2e:0370:7334]:9090/database?someprop=somevalue",
+ "2001:0db8:85a3:0042:1000:8a2e:0370:7334",
+ 9090,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://myhost:8080/database",
+ "myhost",
+ 8080,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://myhost:8080/database",
+ "myhost",
+ 8080,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:postgres://10.23.197.110:8080/database",
+ "10.23.197.110",
+ 8080,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://10.23.197.110:8080/database",
+ "10.23.197.110",
+ 8080,
+ "database")
+ ,
+ new SqlTestEntry("jdbc:edb://10.23.197.110:8080/database?someprop=somevalue",
+ "10.23.197.110",
+ 8080,
+ "database")
+ );
}
}

0 comments on commit a8980ea

Please sign in to comment.