Elffuss BraidHub

kikocis/java-driver public

JAVA-1139: ConnectionException.getMessage() throws NPE if address is null

Lets print original InetSocketAddress in getMessage() - it will be
printed even if it is unresolved (getHost() returns null for unresolved
addresses)
Changeset1a48a06341f592495d29b131e4c1783d855e157a15c8b7a3f5c1b1bc5f64ace4
AuthorDmitry Murashenkov <dmitry.murashenkov@mind.com> · 2016-06-08 11:10 +03:00
CommitterDmitry Murashenkov <dmitry.murashenkov@mind.com> · 2016-06-09 11:24 +03:00
Parents47f8bf44bd1e

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -2,6 +2,7 @@
22
33 ### 3.0.3 (in progress)
44
5+- [bug] JAVA-1139: ConnectionException.getMessage() throws NPE if address is null
56
67 ### 3.0.2
78
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/ConnectionException.java +2 −2
@@ -40,7 +40,7 @@
4040
4141 @Override
4242 public InetAddress getHost() {
43- return address.getAddress();
43+ return address == null ? null : address.getAddress();
4444 }
4545
4646 @Override
@@ -50,7 +50,7 @@
5050
5151 @Override
5252 public String getMessage() {
53- return String.format("[%s] %s", getHost(), getRawMessage());
53+ return address == null ? getRawMessage() : String.format("[%s] %s", address, getRawMessage());
5454 }
5555
5656 @Override
added driver-core/src/test/java/com/datastax/driver/core/exceptions/ConnectionExceptionTest.java +58 −0
@@ -0,0 +1,58 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.exceptions;
17+
18+import org.testng.annotations.Test;
19+
20+import java.net.InetSocketAddress;
21+
22+import static org.testng.Assert.assertEquals;
23+import static org.testng.Assert.assertNull;
24+
25+public class ConnectionExceptionTest {
26+
27+ /**
28+ * @jira_ticket JAVA-1139
29+ */
30+ @Test(groups = "unit")
31+ public void getHost_should_return_null_if_address_is_null() {
32+ assertNull(new ConnectionException(null, "Test message").getHost());
33+ }
34+
35+ /**
36+ * @jira_ticket JAVA-1139
37+ */
38+ @Test(groups = "unit")
39+ public void getMessage_should_return_message_if_address_is_null() {
40+ assertEquals(new ConnectionException(null, "Test message").getMessage(), "Test message");
41+ }
42+
43+ /**
44+ * @jira_ticket JAVA-1139
45+ */
46+ @Test(groups = "unit")
47+ public void getMessage_should_return_message_if_address_is_unresolved() {
48+ assertEquals(new ConnectionException(InetSocketAddress.createUnresolved("127.0.0.1", 9042), "Test message").getMessage(), "[127.0.0.1:9042] Test message");
49+ }
50+
51+ /**
52+ * @jira_ticket JAVA-1139
53+ */
54+ @Test(groups = "unit")
55+ public void getMessage_should_return_message_if_address_is_resolved() {
56+ assertEquals(new ConnectionException(new InetSocketAddress("127.0.0.1", 9042), "Test message").getMessage(), "[/127.0.0.1:9042] Test message");
57+ }
58+}