View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * Tests {@link DnsStringLookup}.
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          // does not blow up and gives some kind of string.
84          assertFalse(DnsStringLookup.INSTANCE.toString().isEmpty());
85      }
86  
87  }