1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35
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
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() {
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);
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
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 }