From 665d65836cb72045160d42f3190b3b84de344aa9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 28 Mar 2014 14:27:14 +0100 Subject: [PATCH] Cleanup of pingMyServer() API only throw NotConnectedException, otherwise return false. Before as some sort of exception was always thrown in the error case. --- .../jivesoftware/smackx/ping/PingManager.java | 25 +++++++++++++------ .../jivesoftware/smackx/ping/PingTest.java | 13 +++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java b/extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java index 053cf305b..ecb69511e 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java @@ -183,9 +183,10 @@ public class PingManager extends Manager { * * @param jid The id of the entity the ping is being sent to * @return true if a reply was received from the entity, false otherwise. - * @throws SmackException if there was no response from the server. + * @throws NotConnectedException + * @throws NoResponseException */ - public boolean ping(String jid) throws SmackException { + public boolean ping(String jid) throws NoResponseException, NotConnectedException { return ping(jid, connection().getPacketReplyTimeout()); } @@ -210,9 +211,9 @@ public class PingManager extends Manager { * {@link #isPingSupported(String)} is false. * * @return true if a reply was received from the server, false otherwise. - * @throws SmackException if there was no response from the server. + * @throws NotConnectedException */ - public boolean pingMyServer() throws SmackException { + public boolean pingMyServer() throws NotConnectedException { return pingMyServer(true); } @@ -225,13 +226,21 @@ public class PingManager extends Manager { * * @param notifyListeners Notify the PingFailedListener in case of error if true * @return true if the user's server could be pinged. - * @throws SmackException if there was no response from the server. + * @throws NotConnectedException */ - public boolean pingMyServer(boolean notifyListeners) throws SmackException { - boolean res = ping(connection().getServiceName()); + public boolean pingMyServer(boolean notifyListeners) throws NotConnectedException { + boolean res; + try { + res = ping(connection().getServiceName()); + } + catch (NoResponseException e) { + res = false; + } + if (res) { pongReceived(); - } else if (notifyListeners) { + } + else if (notifyListeners) { for (PingFailedListener l : pingFailedListeners) l.pingFailed(); } diff --git a/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java b/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java index d75cf92db..1b98422f2 100644 --- a/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java +++ b/extensions/src/test/java/org/jivesoftware/smackx/ping/PingTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.fail; import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NoResponseException; +import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.ThreadedDummyConnection; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Packet; @@ -180,18 +181,12 @@ public class PingTest extends InitExtensions { } @Test - public void checkPingToServerTimeout() throws SmackException { + public void checkPingToServerTimeout() throws NotConnectedException { DummyConnection con = new DummyConnection(); PingManager pinger = PingManager.getInstanceFor(con); - try { - pinger.pingMyServer(); - } - catch (NoResponseException e) { - return; - } - - fail(); + boolean res = pinger.pingMyServer(); + assertFalse(res); } @Test