1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.text.lookup;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class DnsStringLookupTest {
34
35 @Test
36 void testAddressFromHostAddress() throws UnknownHostException {
37 final InetAddress localHost = InetAddress.getLocalHost();
38 assertEquals(localHost.getHostAddress(),
39 DnsStringLookup.INSTANCE.apply("address|" + localHost.getHostAddress()));
40 }
41
42 @Test
43 void testAddressFromHostName() throws UnknownHostException {
44 final InetAddress localHost = InetAddress.getLocalHost();
45 assertEquals(localHost.getHostAddress(),
46 DnsStringLookup.INSTANCE.apply("address|" + localHost.getHostName()));
47 }
48
49 @Test
50 void testCanonicalNameFromHostAddress() throws UnknownHostException {
51 final InetAddress localHost = InetAddress.getLocalHost();
52 assertEquals(localHost.getCanonicalHostName(),
53 DnsStringLookup.INSTANCE.apply("canonical-name|" + localHost.getHostAddress()));
54 }
55
56 @Test
57 void testCanonicalNameFromHostName() throws UnknownHostException {
58 final InetAddress localHost = InetAddress.getLocalHost();
59 assertEquals(localHost.getCanonicalHostName(),
60 DnsStringLookup.INSTANCE.apply("canonical-name|" + localHost.getHostName()));
61 }
62
63 @Test
64 void testName() throws UnknownHostException {
65 final String address = InetAddress.getLocalHost().getHostAddress();
66 final InetAddress[] localHostAll = InetAddress.getAllByName(address);
67 boolean matched = false;
68 for (final InetAddress localHost : localHostAll) {
69 if (localHost.getHostName().equals(DnsStringLookup.INSTANCE.apply("name|" + address + ""))) {
70 matched = true;
71 }
72 }
73 assertTrue(matched);
74 }
75
76 @Test
77 void testNull() {
78 assertNull(DnsStringLookup.INSTANCE.apply(null));
79 }
80
81 @Test
82 void testToString() {
83
84 assertFalse(DnsStringLookup.INSTANCE.toString().isEmpty());
85 }
86
87 }