From 7f6c01a9435fdaf35c804b97faca01242b439e70 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Jul 2016 14:03:10 +0300 Subject: refactor --- .../java/com/juick/xmpp/s2s/ConnectionOut.java | 9 ++---- src/main/java/com/juick/xmpp/s2s/DNSQueries.java | 5 ++-- src/main/java/com/juick/xmpp/s2s/HostnamePort.java | 16 ----------- src/test/java/com/juick/JsonTests.java | 32 ---------------------- src/test/java/com/juick/tests/JsonTests.java | 29 ++++++++++++++++++++ 5 files changed, 35 insertions(+), 56 deletions(-) delete mode 100644 src/main/java/com/juick/xmpp/s2s/HostnamePort.java delete mode 100644 src/test/java/com/juick/JsonTests.java create mode 100644 src/test/java/com/juick/tests/JsonTests.java (limited to 'src') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index fede701e..cf54997f 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -10,6 +10,7 @@ import javax.net.ssl.SSLSocket; import java.io.EOFException; import java.io.IOException; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.security.KeyManagementException; @@ -61,12 +62,8 @@ public class ConnectionOut extends Connection implements Runnable { public void run() { LOGGER.info("STREAM TO " + to + " START"); try { - HostnamePort addr = DNSQueries.getServerAddress(to); - try { - socket = new Socket(InetAddress.getByName(addr.hostname), addr.port); - } catch (UnknownHostException e) { - socket = new Socket(InetAddress.getByName("talk.google.com"), 5269); - } + socket = new Socket(); + socket.connect(DNSQueries.getServerAddress(to)); restartParser(); sendOpenStream(); diff --git a/src/main/java/com/juick/xmpp/s2s/DNSQueries.java b/src/main/java/com/juick/xmpp/s2s/DNSQueries.java index 2b2d60e0..e4c5f085 100644 --- a/src/main/java/com/juick/xmpp/s2s/DNSQueries.java +++ b/src/main/java/com/juick/xmpp/s2s/DNSQueries.java @@ -1,5 +1,6 @@ package com.juick.xmpp.s2s; +import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.Hashtable; import java.util.Random; @@ -16,7 +17,7 @@ public class DNSQueries { private static Random rand = new Random(); - public static HostnamePort getServerAddress(String hostname) throws UnknownHostException { + public static InetSocketAddress getServerAddress(String hostname) throws UnknownHostException { String host = hostname; int port = 5269; @@ -41,6 +42,6 @@ public class DNSQueries { } catch (NamingException e) { } - return new HostnamePort(host, port); + return new InetSocketAddress(host, port); } } diff --git a/src/main/java/com/juick/xmpp/s2s/HostnamePort.java b/src/main/java/com/juick/xmpp/s2s/HostnamePort.java deleted file mode 100644 index ce020f8d..00000000 --- a/src/main/java/com/juick/xmpp/s2s/HostnamePort.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.juick.xmpp.s2s; - -/** - * - * @author ugnich - */ -public class HostnamePort { - - public String hostname; - public int port; - - public HostnamePort(String hostname, int port) { - this.hostname = hostname; - this.port = port; - } -} diff --git a/src/test/java/com/juick/JsonTests.java b/src/test/java/com/juick/JsonTests.java deleted file mode 100644 index ba01e80c..00000000 --- a/src/test/java/com/juick/JsonTests.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.juick; - -import static org.junit.Assert.assertEquals; - -import com.juick.json.MessageSerializer; -import org.junit.Assert; -import org.junit.Test; - -import com.juick.Message; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.TimeZone; - -public class JsonTests { - @Test - public void DateTest() { - Message msg = new Message(); - SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - msg.setDate(df.parse("2015-01-01 05:02:10")); - MessageSerializer serializer = new MessageSerializer(); - String json = serializer.serialize(msg).toString(); - assertEquals("{\"timestamp\":\"2015-01-01 05:02:10\"}", json); - } catch (ParseException e) { - e.printStackTrace(); - } - - } -} \ No newline at end of file diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java new file mode 100644 index 00000000..babcc6c7 --- /dev/null +++ b/src/test/java/com/juick/tests/JsonTests.java @@ -0,0 +1,29 @@ +package com.juick.tests; + +import com.juick.Message; +import com.juick.json.MessageSerializer; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +public class JsonTests { + @Test + public void DateTest() { + Message msg = new Message(); + SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + msg.setDate(df.parse("2015-01-01 05:02:10")); + MessageSerializer serializer = new MessageSerializer(); + String json = serializer.serialize(msg).toString(); + assertEquals("{\"timestamp\":\"2015-01-01 05:02:10\"}", json); + } catch (ParseException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file -- cgit v1.2.3