Skip to content

Commit

Permalink
[#noissue] Add testcase of spring data r2dbc h2 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Dec 9, 2022
1 parent 9131aa7 commit d517e7d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static com.navercorp.pinpoint.plugin.spring.r2dbc.SpringDataR2dbcConstants.UNKNOWN_DATABASE;

public class H2DatabaseInfoParser {
static final String START_URL = "jdbc:h2:";

final ConnectInfo connectInfo;

public H2DatabaseInfoParser() {
Expand All @@ -41,14 +43,15 @@ public List<String> getHostList() {
public List<String> getHostList(String name, boolean remote) {
final List<String> hostList = new ArrayList<>();
if (remote) {
if (name.startsWith("//")) {
name = name.substring("//".length());
String info = name;
if (info.startsWith("//")) {
info = info.substring("//".length());
}
int idx = name.indexOf('/');
int idx = info.indexOf('/');
if (idx < 0) {
return hostList;
}
final String server = name.substring(0, idx);
final String server = info.substring(0, idx);
if (server.indexOf(',') >= 0) {
for (String host : server.split(",")) {
hostList.add(host);
Expand All @@ -71,19 +74,19 @@ public String getDatabase() {

public String getDatabase(String name, boolean remote) {
if (remote) {
if (name.startsWith("//")) {
name = name.substring("//".length());
String info = name;
if (info.startsWith("//")) {
info = info.substring("//".length());
}
int idx = name.indexOf('/');
int idx = info.indexOf('/');
if (idx < 0) {
return UNKNOWN_DATABASE;
}
return name.substring(idx + 1);
return info.substring(idx + 1);
}
return name;
}


static class ConnectInfo {
private String name;
private boolean remote;
Expand All @@ -102,11 +105,16 @@ String parseUrl(String url) {
if (url == null) {
return "";
}
int idx = url.indexOf(';');

String parsed = url;
if (parsed.startsWith(START_URL)) {
parsed = parsed.substring(START_URL.length());
}
int idx = parsed.indexOf(';');
if (idx >= 0) {
return url.substring(0, idx);
return parsed.substring(0, idx);
}
return url;
return parsed;
}

String parseName(String url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.plugin.spring.r2dbc.interceptor.h2;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class H2DatabaseInfoParserTest {

@Test
void getHostListAndDatabase() {
H2DatabaseInfoParser parser = new H2DatabaseInfoParser("jdbc:h2:mem:test_mem");
List<String> list = parser.getHostList();
assertEquals(1, list.size());
assertEquals("local", parser.getHostList().get(0));
assertEquals("mem:test_mem", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:tcp://dbserv:8084/~/sample");
assertEquals("dbserv:8084", parser.getHostList().get(0));
assertEquals("~/sample", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:~/test");
assertEquals("local", parser.getHostList().get(0));
assertEquals("~/test", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:file:/data/sample");
assertEquals("local", parser.getHostList().get(0));
assertEquals("/data/sample", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:file:C:/data/sample");
assertEquals("local", parser.getHostList().get(0));
assertEquals("C:/data/sample", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:mem:");
assertEquals("local", parser.getHostList().get(0));
assertEquals("unnamed-in-memory", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:tcp://localhost/~/test");
assertEquals("localhost", parser.getHostList().get(0));
assertEquals("~/test", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:tcp://localhost/mem:test");
assertEquals("localhost", parser.getHostList().get(0));
assertEquals("mem:test", parser.getDatabase());

parser = new H2DatabaseInfoParser("jdbc:h2:ssl://localhost:8085/~/sample;");
assertEquals("localhost:8085", parser.getHostList().get(0));
assertEquals("~/sample", parser.getDatabase());
}
}

0 comments on commit d517e7d

Please sign in to comment.