Skip to content

Commit 9b5c719

Browse files
committed
imporve test
1 parent e03d081 commit 9b5c719

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcConnectionUrlParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,29 +324,30 @@ DbInfo.Builder doParse(String jdbcUrl, DbInfo.Builder builder) {
324324
portLoc = clusterSepLoc != -1 && clusterSepLoc < portLoc ? -1 : portLoc;
325325
int dbLoc = jdbcUrl.indexOf("/", Math.max(portLoc, clusterSepLoc));
326326

327-
int paramLoc = jdbcUrl.indexOf("?", dbLoc);
327+
int paramLoc = dbLoc != -1 ? jdbcUrl.indexOf("?", dbLoc) : -1;
328328

329329
if (paramLoc > 0) {
330330
populateStandardProperties(builder, splitQuery(jdbcUrl.substring(paramLoc + 1), "&"));
331331
builder.db(jdbcUrl.substring(dbLoc + 1, paramLoc));
332-
} else {
332+
} else if (dbLoc != -1) {
333333
builder.db(jdbcUrl.substring(dbLoc + 1));
334334
}
335335

336336
if (jdbcUrl.startsWith("address=")) {
337337
return MARIA_ADDRESS.doParse(jdbcUrl, builder);
338338
}
339339

340+
dbLoc = dbLoc != -1 ? dbLoc : jdbcUrl.length();
340341
if (portLoc > 0) {
341342
hostEndLoc = portLoc;
342-
int portEndLoc = clusterSepLoc > 0 ? clusterSepLoc : (dbLoc > 0 ? dbLoc : jdbcUrl.length());
343+
int portEndLoc = clusterSepLoc > 0 ? clusterSepLoc : dbLoc;
343344
try {
344345
builder.port(Integer.parseInt(jdbcUrl.substring(portLoc + 1, portEndLoc)));
345346
} catch (NumberFormatException e) {
346347
logger.log(FINE, e.getMessage(), e);
347348
}
348349
} else {
349-
hostEndLoc = clusterSepLoc > 0 ? clusterSepLoc : (dbLoc > 0 ? dbLoc : jdbcUrl.length());
350+
hostEndLoc = clusterSepLoc > 0 ? clusterSepLoc : dbLoc;
350351
}
351352

352353
if (ipv6End > 0) {

instrumentation/jdbc/library/src/test/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcConnectionUrlParserTest.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
package io.opentelemetry.instrumentation.jdbc.internal;
77

8-
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcConnectionUrlParser.MYSQL;
98
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcConnectionUrlParser.parse;
109
import static io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo.DEFAULT;
1110
import static org.assertj.core.api.Assertions.assertThat;
12-
import static org.assertj.core.api.Assertions.assertThatCode;
1311
import static org.junit.jupiter.params.provider.Arguments.arguments;
1412

1513
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo;
@@ -150,6 +148,27 @@ private static Stream<Arguments> mySqlArguments() {
150148
.setHost("mdb.host")
151149
.setPort(3306)
152150
.setDb("mdbdb")
151+
.build(),
152+
arg("jdbc:mysql:loadbalance://localhost")
153+
.setShortUrl("mysql:loadbalance://localhost:3306")
154+
.setSystem("mysql")
155+
.setSubtype("loadbalance")
156+
.setHost("localhost")
157+
.setPort(3306)
158+
.build(),
159+
arg("jdbc:mysql:loadbalance://host:3306") // with port but no slash
160+
.setShortUrl("mysql:loadbalance://host:3306")
161+
.setSystem("mysql")
162+
.setSubtype("loadbalance")
163+
.setHost("host")
164+
.setPort(3306)
165+
.build(),
166+
arg("jdbc:mysql:failover://[::1]:3306") // IPv6 without slash
167+
.setShortUrl("mysql:failover://::1:3306")
168+
.setSystem("mysql")
169+
.setSubtype("failover")
170+
.setHost("::1")
171+
.setPort(3306)
153172
.build());
154173
}
155174

@@ -332,6 +351,27 @@ private static Stream<Arguments> mariaDbArguments() {
332351
.setHost("localhost")
333352
.setPort(33)
334353
.setDb("mdbdb")
354+
.build(),
355+
arg("jdbc:mariadb:loadbalance://localhost")
356+
.setShortUrl("mariadb:loadbalance://localhost:3306")
357+
.setSystem("mariadb")
358+
.setSubtype("loadbalance")
359+
.setHost("localhost")
360+
.setPort(3306)
361+
.build(),
362+
arg("jdbc:mariadb:loadbalance://host:3306") // with port but no slash
363+
.setShortUrl("mariadb:loadbalance://host:3306")
364+
.setSystem("mariadb")
365+
.setSubtype("loadbalance")
366+
.setHost("host")
367+
.setPort(3306)
368+
.build(),
369+
arg("jdbc:mariadb:failover://[::1]:3306") // IPv6 without slash
370+
.setShortUrl("mariadb:failover://::1:3306")
371+
.setSystem("mariadb")
372+
.setSubtype("failover")
373+
.setHost("::1")
374+
.setPort(3306)
335375
.build());
336376
}
337377

@@ -1377,16 +1417,4 @@ static Stream<Arguments> args(ParseTestArgument... testArguments) {
13771417
}
13781418
return list.stream();
13791419
}
1380-
1381-
@ParameterizedTest
1382-
@ValueSource(
1383-
strings = {
1384-
"mysql:loadbalance://string_without_slash",
1385-
"mysql:loadbalance://host:3306", // with port but no slash
1386-
"mariadb:failover://[::1]:3306" // IPv6 without slash
1387-
})
1388-
void testMysqlUrlsWithoutSlashDoNotThrowException(String url) {
1389-
assertThatCode(() -> MYSQL.doParse(url, DEFAULT.toBuilder().system("mysql")))
1390-
.doesNotThrowAnyException();
1391-
}
13921420
}

0 commit comments

Comments
 (0)