diff --git a/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java b/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java index a34fbb6..fcb1205 100644 --- a/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java +++ b/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java @@ -1,27 +1,47 @@ package com.github.manevolent.ts3j; import com.github.manevolent.ts3j.protocol.TS3DNS; -import com.github.manevolent.ts3j.util.Ts3Debugging; import junit.framework.TestCase; import java.net.InetSocketAddress; -import java.net.SocketAddress; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +/** SRV answers are stubbed: the test must not depend on what the network resolves today. */ public class TS3DNSTest extends TestCase { - public static void main(String[] args) throws Exception { - new TS3DNSTest().testParser(); + @Override + protected void tearDown() { + TS3DNS.setSrvLookup(null); } - public void testParser() throws Exception { - Ts3Debugging.setEnabled(true); + public void testOrdersRecordsByPriorityThenWeight() throws Exception { + TS3DNS.setSrvLookup(name -> { + assertEquals("_ts3._udp.voice.example.com", name); + return Arrays.asList("20 0 9989 backup.example.com.", "10 5 9988 b.example.com.", + "10 1 9987 a.example.com."); + }); - List soaddr = TS3DNS.lookup("voice.teamspeak.com"); + List addresses = TS3DNS.lookup("voice.example.com"); - assertTrue(soaddr.size() > 0); + assertEquals(3, addresses.size()); + assertEquals("a.example.com", addresses.get(0).getHostString()); + assertEquals(9987, addresses.get(0).getPort()); + assertEquals("b.example.com", addresses.get(1).getHostString()); + assertEquals("backup.example.com", addresses.get(2).getHostString()); + } - for (SocketAddress socketAddress : soaddr) - Ts3Debugging.debug(socketAddress); + public void testNoRecordMeansTheHostIsNotFound() { + TS3DNS.setSrvLookup(name -> Collections.emptyList()); + try { + TS3DNS.lookup("voice.example.com"); + fail("expected UnknownHostException"); + } catch (UnknownHostException expected) { + // the caller then connects to the address itself + } catch (Exception e) { + fail("unexpected " + e); + } } }