aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/net/HostSpecifierTest.java
blob: 12c7631fd52c0386d800758cbb397cc226a85062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2009 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.net;

import com.google.common.collect.ImmutableList;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;

import junit.framework.TestCase;

import java.text.ParseException;
import java.util.List;

/**
 * {@link TestCase} for {@link HostSpecifier}.  This is a relatively
 * cursory test, as HostSpecifier is a thin wrapper around
 * {@link InetAddresses} and {@link InternetDomainName}; the unit tests for
 * those classes explore numerous corner cases.  The intent here is to
 * confirm that everything is wired up properly.
 *
 * @author Craig Berry
 */
public final class HostSpecifierTest extends TestCase {

  private static final List<String> GOOD_IPS = ImmutableList.of(
      "1.2.3.4", "2001:db8::1", "[2001:db8::1]");

  private static final List<String> BAD_IPS = ImmutableList.of(
      "1.2.3", "2001:db8::1::::::0", "[2001:db8::1", "[::]:80");

  private static final List<String> GOOD_DOMAINS = ImmutableList.of(
      "com", "google.com", "foo.co.uk");

  private static final List<String> BAD_DOMAINS = ImmutableList.of(
      "foo.blah", "", "[google.com]");

  public void testGoodIpAddresses() throws ParseException {
    for (String spec : GOOD_IPS) {
      assertGood(spec);
    }
  }

  public void testBadIpAddresses() {
    for (String spec : BAD_IPS) {
      assertBad(spec);
    }
  }

  public void testGoodDomains() throws ParseException {
    for (String spec : GOOD_DOMAINS) {
      assertGood(spec);
    }
  }

  public void testBadDomains() {
    for (String spec : BAD_DOMAINS) {
      assertBad(spec);
    }
  }

  public void testEquality() {
    new EqualsTester()
        .addEqualityGroup(spec("1.2.3.4"), spec("1.2.3.4"))
        .addEqualityGroup(
            spec("2001:db8::1"), spec("2001:db8::1"), spec("[2001:db8::1]"))
        .addEqualityGroup(spec("2001:db8::2"))
        .addEqualityGroup(spec("google.com"), spec("google.com"))
        .addEqualityGroup(spec("www.google.com"))
        .testEquals();
  }

  private static HostSpecifier spec(String specifier) {
    return HostSpecifier.fromValid(specifier);
  }

  public void testNulls() {
    final NullPointerTester tester = new NullPointerTester();

    tester.testAllPublicStaticMethods(HostSpecifier.class);
    tester.testAllPublicInstanceMethods(HostSpecifier.fromValid("google.com"));
  }

  private void assertGood(String spec) throws ParseException {
    HostSpecifier.fromValid(spec);  // Throws exception if not working correctly
    HostSpecifier.from(spec);
    assertTrue(HostSpecifier.isValid(spec));
  }

  private void assertBad(String spec) {
    try {
      HostSpecifier.fromValid(spec);
      fail("Should have thrown IllegalArgumentException: " + spec);
    } catch (IllegalArgumentException expected) {
      // Expected outcome
    }

    try {
      HostSpecifier.from(spec);
      fail("Should have thrown ParseException: " + spec);
    } catch (ParseException expected) {
      assertTrue(expected.getCause() instanceof IllegalArgumentException);
    }

    assertFalse(HostSpecifier.isValid(spec));
  }

}