From b6285679cd7dd62339fcd0f3ae0754b51befb9c0 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 5 Oct 2014 12:17:25 +0200 Subject: [PATCH] Remove trailing comma in ConnectionException message Also add SmackExceptionTest --- .../jivesoftware/smack/SmackException.java | 7 +-- .../smack/SmackExceptionTest.java | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 smack-core/src/test/java/org/jivesoftware/smack/SmackExceptionTest.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java index af2dfa8ce..3a638bdbf 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java @@ -175,13 +175,14 @@ public class SmackException extends Exception { } public static ConnectionException from(List failedAddresses) { + final String DELIMITER = ", "; StringBuilder sb = new StringBuilder("The following addresses failed: "); for (HostAddress hostAddress : failedAddresses) { sb.append(hostAddress.getErrorMessage()); - sb.append(", "); + sb.append(DELIMITER); } - // Remove the last whitespace - sb.deleteCharAt(sb.length() - 1); + // Remove the last delimiter + sb.setLength(sb.length() - DELIMITER.length()); return new ConnectionException(sb.toString(), failedAddresses); } diff --git a/smack-core/src/test/java/org/jivesoftware/smack/SmackExceptionTest.java b/smack-core/src/test/java/org/jivesoftware/smack/SmackExceptionTest.java new file mode 100644 index 000000000..ec6013a8f --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/SmackExceptionTest.java @@ -0,0 +1,48 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * 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 org.jivesoftware.smack; + +import static org.junit.Assert.assertEquals; + +import java.util.LinkedList; +import java.util.List; + +import org.jivesoftware.smack.SmackException.ConnectionException; +import org.jivesoftware.smack.util.dns.HostAddress; +import org.junit.Test; + +public class SmackExceptionTest { + + @Test + public void testConnectionException() { + List failedAddresses = new LinkedList(); + + HostAddress hostAddress = new HostAddress("foo.bar.example", 1234); + hostAddress.setException(new Exception("Failed for some reason")); + failedAddresses.add(hostAddress); + + hostAddress = new HostAddress("barz.example", 5678); + hostAddress.setException(new Exception("Failed for some other reason")); + failedAddresses.add(hostAddress); + + ConnectionException connectionException = ConnectionException.from(failedAddresses); + String message = connectionException.getMessage(); + assertEquals("The following addresses failed: foo.bar.example:1234 Exception: Failed for some reason, barz.example:5678 Exception: Failed for some other reason", + message); + } + +}