• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.net;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.testing.EqualsTester;
23 import com.google.common.testing.NullPointerTester;
24 import java.text.ParseException;
25 import junit.framework.TestCase;
26 
27 /**
28  * {@link TestCase} for {@link HostSpecifier}. This is a relatively cursory test, as HostSpecifier
29  * is a thin wrapper around {@link InetAddresses} and {@link InternetDomainName}; the unit tests for
30  * those classes explore numerous corner cases. The intent here is to confirm that everything is
31  * wired up properly.
32  *
33  * @author Craig Berry
34  */
35 public final class HostSpecifierTest extends TestCase {
36 
37   private static final ImmutableList<String> GOOD_IPS =
38       ImmutableList.of("1.2.3.4", "2001:db8::1", "[2001:db8::1]");
39 
40   private static final ImmutableList<String> BAD_IPS =
41       ImmutableList.of("1.2.3", "2001:db8::1::::::0", "[2001:db8::1", "[::]:80");
42 
43   private static final ImmutableList<String> GOOD_DOMAINS =
44       ImmutableList.of("com", "google.com", "foo.co.uk");
45 
46   private static final ImmutableList<String> BAD_DOMAINS =
47       ImmutableList.of("foo.blah", "", "[google.com]");
48 
testGoodIpAddresses()49   public void testGoodIpAddresses() throws ParseException {
50     for (String spec : GOOD_IPS) {
51       assertGood(spec);
52     }
53   }
54 
testBadIpAddresses()55   public void testBadIpAddresses() {
56     for (String spec : BAD_IPS) {
57       assertBad(spec);
58     }
59   }
60 
testGoodDomains()61   public void testGoodDomains() throws ParseException {
62     for (String spec : GOOD_DOMAINS) {
63       assertGood(spec);
64     }
65   }
66 
testBadDomains()67   public void testBadDomains() {
68     for (String spec : BAD_DOMAINS) {
69       assertBad(spec);
70     }
71   }
72 
testEquality()73   public void testEquality() {
74     new EqualsTester()
75         .addEqualityGroup(spec("1.2.3.4"), spec("1.2.3.4"))
76         .addEqualityGroup(spec("2001:db8::1"), spec("2001:db8::1"), spec("[2001:db8::1]"))
77         .addEqualityGroup(spec("2001:db8::2"))
78         .addEqualityGroup(spec("google.com"), spec("google.com"))
79         .addEqualityGroup(spec("www.google.com"))
80         .testEquals();
81   }
82 
spec(String specifier)83   private static HostSpecifier spec(String specifier) {
84     return HostSpecifier.fromValid(specifier);
85   }
86 
testNulls()87   public void testNulls() {
88     final NullPointerTester tester = new NullPointerTester();
89 
90     tester.testAllPublicStaticMethods(HostSpecifier.class);
91     tester.testAllPublicInstanceMethods(HostSpecifier.fromValid("google.com"));
92   }
93 
assertGood(String spec)94   private void assertGood(String spec) throws ParseException {
95     HostSpecifier.fromValid(spec); // Throws exception if not working correctly
96     HostSpecifier.from(spec);
97     assertTrue(HostSpecifier.isValid(spec));
98   }
99 
assertBad(String spec)100   private void assertBad(String spec) {
101     try {
102       HostSpecifier.fromValid(spec);
103       fail("Should have thrown IllegalArgumentException: " + spec);
104     } catch (IllegalArgumentException expected) {
105     }
106 
107     try {
108       HostSpecifier.from(spec);
109       fail("Should have thrown ParseException: " + spec);
110     } catch (ParseException expected) {
111       assertThat(expected).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
112     }
113 
114     assertFalse(HostSpecifier.isValid(spec));
115   }
116 }
117