• 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 com.google.common.collect.ImmutableList;
20 import com.google.common.testing.EqualsTester;
21 import com.google.common.testing.NullPointerTester;
22 
23 import junit.framework.TestCase;
24 
25 import java.text.ParseException;
26 import java.util.List;
27 
28 /**
29  * {@link TestCase} for {@link HostSpecifier}.  This is a relatively
30  * cursory test, as HostSpecifier is a thin wrapper around
31  * {@link InetAddresses} and {@link InternetDomainName}; the unit tests for
32  * those classes explore numerous corner cases.  The intent here is to
33  * confirm that everything is wired up properly.
34  *
35  * @author Craig Berry
36  */
37 public final class HostSpecifierTest extends TestCase {
38 
39   private static final List<String> GOOD_IPS = ImmutableList.of(
40       "1.2.3.4", "2001:db8::1", "[2001:db8::1]");
41 
42   private static final List<String> BAD_IPS = ImmutableList.of(
43       "1.2.3", "2001:db8::1::::::0", "[2001:db8::1", "[::]:80");
44 
45   private static final List<String> GOOD_DOMAINS = ImmutableList.of(
46       "com", "google.com", "foo.co.uk");
47 
48   private static final List<String> BAD_DOMAINS = ImmutableList.of(
49       "foo.blah", "", "[google.com]");
50 
testGoodIpAddresses()51   public void testGoodIpAddresses() throws ParseException {
52     for (String spec : GOOD_IPS) {
53       assertGood(spec);
54     }
55   }
56 
testBadIpAddresses()57   public void testBadIpAddresses() {
58     for (String spec : BAD_IPS) {
59       assertBad(spec);
60     }
61   }
62 
testGoodDomains()63   public void testGoodDomains() throws ParseException {
64     for (String spec : GOOD_DOMAINS) {
65       assertGood(spec);
66     }
67   }
68 
testBadDomains()69   public void testBadDomains() {
70     for (String spec : BAD_DOMAINS) {
71       assertBad(spec);
72     }
73   }
74 
testEquality()75   public void testEquality() {
76     new EqualsTester()
77         .addEqualityGroup(spec("1.2.3.4"), spec("1.2.3.4"))
78         .addEqualityGroup(
79             spec("2001:db8::1"), spec("2001:db8::1"), spec("[2001:db8::1]"))
80         .addEqualityGroup(spec("2001:db8::2"))
81         .addEqualityGroup(spec("google.com"), spec("google.com"))
82         .addEqualityGroup(spec("www.google.com"))
83         .testEquals();
84   }
85 
spec(String specifier)86   private static HostSpecifier spec(String specifier) {
87     return HostSpecifier.fromValid(specifier);
88   }
89 
testNulls()90   public void testNulls() {
91     final NullPointerTester tester = new NullPointerTester();
92 
93     tester.testAllPublicStaticMethods(HostSpecifier.class);
94     tester.testAllPublicInstanceMethods(HostSpecifier.fromValid("google.com"));
95   }
96 
assertGood(String spec)97   private void assertGood(String spec) throws ParseException {
98     HostSpecifier.fromValid(spec);  // Throws exception if not working correctly
99     HostSpecifier.from(spec);
100     assertTrue(HostSpecifier.isValid(spec));
101   }
102 
assertBad(String spec)103   private void assertBad(String spec) {
104     try {
105       HostSpecifier.fromValid(spec);
106       fail("Should have thrown IllegalArgumentException: " + spec);
107     } catch (IllegalArgumentException expected) {
108       // Expected outcome
109     }
110 
111     try {
112       HostSpecifier.from(spec);
113       fail("Should have thrown ParseException: " + spec);
114     } catch (ParseException expected) {
115       assertTrue(expected.getCause() instanceof IllegalArgumentException);
116     }
117 
118     assertFalse(HostSpecifier.isValid(spec));
119   }
120 
121 }
122