Skip to content

Commit c55bc39

Browse files
committed
improve error msg obscure when connect url port miss
1 parent 920aa06 commit c55bc39

File tree

2 files changed

+146
-123
lines changed

2 files changed

+146
-123
lines changed

src/main/java/org/influxdb/InfluxDBFactory.java

+95-85
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,113 @@
66
import okhttp3.OkHttpClient;
77
import org.influxdb.impl.Preconditions;
88

9+
import java.net.*;
910
import java.util.Objects;
1011

1112

1213
/**
1314
* A Factory to create a instance of a InfluxDB Database adapter.
1415
*
1516
* @author stefan.majer [at] gmail.com
16-
*
1717
*/
1818
public enum InfluxDBFactory {
19-
INSTANCE;
19+
INSTANCE;
20+
21+
/**
22+
* Create a connection to a InfluxDB.
23+
*
24+
* @param url the url to connect to.
25+
* @return a InfluxDB adapter suitable to access a InfluxDB.
26+
*/
27+
public static InfluxDB connect(final String url) {
28+
Preconditions.checkNonEmptyString(url, "url");
29+
checkUrl(url);
30+
return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder());
31+
}
32+
33+
/**
34+
* Create a connection to a InfluxDB.
35+
*
36+
* @param url the url to connect to.
37+
* @param username the username which is used to authorize against the influxDB instance.
38+
* @param password the password for the username which is used to authorize against the influxDB
39+
* instance.
40+
* @return a InfluxDB adapter suitable to access a InfluxDB.
41+
*/
42+
public static InfluxDB connect(final String url, final String username, final String password) {
43+
Preconditions.checkNonEmptyString(url, "url");
44+
Preconditions.checkNonEmptyString(username, "username");
45+
checkUrl(url);
46+
return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder());
47+
}
48+
49+
/**
50+
* Create a connection to a InfluxDB.
51+
*
52+
* @param url the url to connect to.
53+
* @param client the HTTP client to use
54+
* @return a InfluxDB adapter suitable to access a InfluxDB.
55+
*/
56+
public static InfluxDB connect(final String url, final OkHttpClient.Builder client) {
57+
Preconditions.checkNonEmptyString(url, "url");
58+
Objects.requireNonNull(client, "client");
59+
checkUrl(url);
60+
return new InfluxDBImpl(url, null, null, client);
61+
}
2062

21-
/**
22-
* Create a connection to a InfluxDB.
23-
*
24-
* @param url
25-
* the url to connect to.
26-
* @return a InfluxDB adapter suitable to access a InfluxDB.
27-
*/
28-
public static InfluxDB connect(final String url) {
29-
Preconditions.checkNonEmptyString(url, "url");
30-
return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder());
31-
}
63+
/**
64+
* Create a connection to a InfluxDB.
65+
*
66+
* @param url the url to connect to.
67+
* @param username the username which is used to authorize against the influxDB instance.
68+
* @param password the password for the username which is used to authorize against the influxDB
69+
* instance.
70+
* @param client the HTTP client to use
71+
* @return a InfluxDB adapter suitable to access a InfluxDB.
72+
*/
73+
public static InfluxDB connect(final String url, final String username, final String password,
74+
final OkHttpClient.Builder client) {
75+
checkUrl(url);
76+
return connect(url, username, password, client, ResponseFormat.JSON);
77+
}
3278

33-
/**
34-
* Create a connection to a InfluxDB.
35-
*
36-
* @param url
37-
* the url to connect to.
38-
* @param username
39-
* the username which is used to authorize against the influxDB instance.
40-
* @param password
41-
* the password for the username which is used to authorize against the influxDB
42-
* instance.
43-
* @return a InfluxDB adapter suitable to access a InfluxDB.
44-
*/
45-
public static InfluxDB connect(final String url, final String username, final String password) {
46-
Preconditions.checkNonEmptyString(url, "url");
47-
Preconditions.checkNonEmptyString(username, "username");
48-
return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder());
49-
}
79+
/**
80+
* Create a connection to a InfluxDB.
81+
*
82+
* @param url the url to connect to.
83+
* @param username the username which is used to authorize against the influxDB instance.
84+
* @param password the password for the username which is used to authorize against the influxDB
85+
* instance.
86+
* @param client the HTTP client to use
87+
* @param responseFormat The {@code ResponseFormat} to use for response from InfluxDB server
88+
* @return a InfluxDB adapter suitable to access a InfluxDB.
89+
*/
90+
public static InfluxDB connect(final String url, final String username, final String password,
91+
final OkHttpClient.Builder client, final ResponseFormat responseFormat) {
92+
Preconditions.checkNonEmptyString(url, "url");
93+
Preconditions.checkNonEmptyString(username, "username");
94+
Objects.requireNonNull(client, "client");
95+
checkUrl(url);
96+
return new InfluxDBImpl(url, username, password, client, responseFormat);
97+
}
5098

51-
/**
52-
* Create a connection to a InfluxDB.
53-
*
54-
* @param url
55-
* the url to connect to.
56-
* @param client
57-
* the HTTP client to use
58-
* @return a InfluxDB adapter suitable to access a InfluxDB.
59-
*/
60-
public static InfluxDB connect(final String url, final OkHttpClient.Builder client) {
61-
Preconditions.checkNonEmptyString(url, "url");
62-
Objects.requireNonNull(client, "client");
63-
return new InfluxDBImpl(url, null, null, client);
64-
}
99+
/**
100+
* Check url is legal
101+
*/
102+
private static void checkUrl(final String url) {
103+
String colon = ":";
104+
if (!url.contains(colon) || url.endsWith(colon)) {
105+
throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url));
106+
}
65107

66-
/**
67-
* Create a connection to a InfluxDB.
68-
*
69-
* @param url
70-
* the url to connect to.
71-
* @param username
72-
* the username which is used to authorize against the influxDB instance.
73-
* @param password
74-
* the password for the username which is used to authorize against the influxDB
75-
* instance.
76-
* @param client
77-
* the HTTP client to use
78-
* @return a InfluxDB adapter suitable to access a InfluxDB.
79-
*/
80-
public static InfluxDB connect(final String url, final String username, final String password,
81-
final OkHttpClient.Builder client) {
82-
return connect(url, username, password, client, ResponseFormat.JSON);
83-
}
108+
try {
109+
URL urlObj = new URL(url);
110+
if (-1 == urlObj.getPort()) {
111+
throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url));
112+
}
113+
} catch (MalformedURLException e) {
114+
throw new RuntimeException(e);
115+
}
84116

85-
/**
86-
* Create a connection to a InfluxDB.
87-
*
88-
* @param url
89-
* the url to connect to.
90-
* @param username
91-
* the username which is used to authorize against the influxDB instance.
92-
* @param password
93-
* the password for the username which is used to authorize against the influxDB
94-
* instance.
95-
* @param client
96-
* the HTTP client to use
97-
* @param responseFormat
98-
* The {@code ResponseFormat} to use for response from InfluxDB server
99-
* @return a InfluxDB adapter suitable to access a InfluxDB.
100-
*/
101-
public static InfluxDB connect(final String url, final String username, final String password,
102-
final OkHttpClient.Builder client, final ResponseFormat responseFormat) {
103-
Preconditions.checkNonEmptyString(url, "url");
104-
Preconditions.checkNonEmptyString(username, "username");
105-
Objects.requireNonNull(client, "client");
106-
return new InfluxDBImpl(url, username, password, client, responseFormat);
107-
}
117+
}
108118
}

src/test/java/org/influxdb/InfluxDBFactoryTest.java

+51-38
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,59 @@
1212
* Test the InfluxDB Factory API.
1313
*
1414
* @author fujian1115 [at] gmail.com
15-
*
1615
*/
1716
@RunWith(JUnitPlatform.class)
1817
public class InfluxDBFactoryTest {
1918

20-
/**
21-
* Test for a {@link InfluxDBFactory #connect(String)}.
22-
*/
23-
@Test
24-
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithoutUserNameAndPassword() {
25-
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true));
26-
verifyInfluxDBInstance(influxDB);
27-
}
28-
29-
@Test
30-
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithUserNameAndWithoutPassword() {
31-
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", null);
32-
verifyInfluxDBInstance(influxDB);
33-
}
34-
35-
private void verifyInfluxDBInstance(InfluxDB influxDB) {
36-
Assertions.assertNotNull(influxDB);
37-
Pong pong = influxDB.ping();
38-
Assertions.assertNotNull(pong);
39-
Assertions.assertNotEquals(pong.getVersion(), "unknown");
40-
}
41-
42-
/**
43-
* Test for a {@link InfluxDBFactory #connect(String, okhttp3.OkHttpClient.Builder)}.
44-
*/
45-
@Test
46-
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
47-
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), new OkHttpClient.Builder());
48-
verifyInfluxDBInstance(influxDB);
49-
}
50-
51-
@Test
52-
public void testShouldThrowIllegalArgumentWithInvalidUrl() {
53-
Assertions.assertThrows(IllegalArgumentException.class,() -> {
54-
InfluxDBFactory.connect("invalidUrl");
55-
});
56-
}
19+
@Test
20+
public void testUrlNotContainsColon() {
21+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
22+
InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP());
23+
});
24+
}
25+
26+
@Test
27+
public void testUrlEndWithColon() {
28+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
29+
InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":");
30+
});
31+
}
32+
33+
/**
34+
* Test for a {@link InfluxDBFactory #connect(String)}.
35+
*/
36+
@Test
37+
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithoutUserNameAndPassword() {
38+
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true));
39+
verifyInfluxDBInstance(influxDB);
40+
}
41+
42+
@Test
43+
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithUserNameAndWithoutPassword() {
44+
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", null);
45+
verifyInfluxDBInstance(influxDB);
46+
}
47+
48+
private void verifyInfluxDBInstance(InfluxDB influxDB) {
49+
Assertions.assertNotNull(influxDB);
50+
Pong pong = influxDB.ping();
51+
Assertions.assertNotNull(pong);
52+
Assertions.assertNotEquals(pong.getVersion(), "unknown");
53+
}
54+
55+
/**
56+
* Test for a {@link InfluxDBFactory #connect(String, okhttp3.OkHttpClient.Builder)}.
57+
*/
58+
@Test
59+
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
60+
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), new OkHttpClient.Builder());
61+
verifyInfluxDBInstance(influxDB);
62+
}
63+
64+
@Test
65+
public void testShouldThrowIllegalArgumentWithInvalidUrl() {
66+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
67+
InfluxDBFactory.connect("invalidUrl");
68+
});
69+
}
5770
}

0 commit comments

Comments
 (0)