HostSpecifierTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
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
17package com.google.common.net;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.testing.EqualsTester;
21import com.google.common.testing.NullPointerTester;
22
23import junit.framework.TestCase;
24
25import java.text.ParseException;
26import 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 */
37public 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
51  public void testGoodIpAddresses() throws ParseException {
52    for (String spec : GOOD_IPS) {
53      assertGood(spec);
54    }
55  }
56
57  public void testBadIpAddresses() {
58    for (String spec : BAD_IPS) {
59      assertBad(spec);
60    }
61  }
62
63  public void testGoodDomains() throws ParseException {
64    for (String spec : GOOD_DOMAINS) {
65      assertGood(spec);
66    }
67  }
68
69  public void testBadDomains() {
70    for (String spec : BAD_DOMAINS) {
71      assertBad(spec);
72    }
73  }
74
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
86  private static HostSpecifier spec(String specifier) {
87    return HostSpecifier.fromValid(specifier);
88  }
89
90  public void testNulls() throws Exception {
91    final NullPointerTester tester = new NullPointerTester();
92
93    tester.testAllPublicStaticMethods(HostSpecifier.class);
94    tester.testAllPublicInstanceMethods(HostSpecifier.fromValid("google.com"));
95  }
96
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
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