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 package org.apache.hc.client5.http.psl;
29
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39
40 class TestPublicSuffixListParser {
41
42 private static final String SUFFIXLIST_TXT = "suffixlist.txt";
43 private static final String SUFFIXLIST2_TXT = "suffixlist2.txt";
44
45 @Test
46 void testParse() throws Exception {
47 final ClassLoader classLoader = getClass().getClassLoader();
48 final PublicSuffixList suffixList;
49 try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST_TXT)) {
50 Assertions.assertNotNull(in, SUFFIXLIST_TXT);
51 final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
52 suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
53 }
54 Assertions.assertNotNull(suffixList);
55 Assertions.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules());
56 Assertions.assertEquals(Collections.singletonList("metro.tokyo.jp"), suffixList.getExceptions());
57 }
58
59 @Test
60 void testParseByType() throws Exception {
61 final ClassLoader classLoader = getClass().getClassLoader();
62 final List<PublicSuffixList> suffixLists;
63 try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST2_TXT)) {
64 Assertions.assertNotNull(in, SUFFIXLIST2_TXT);
65 final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
66 suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
67 }
68 Assertions.assertNotNull(suffixLists);
69 Assertions.assertEquals(2, suffixLists.size());
70 final PublicSuffixList publicSuffixList1 = suffixLists.get(0);
71 Assertions.assertNotNull(publicSuffixList1);
72 Assertions.assertEquals(DomainType.ICANN, publicSuffixList1.getType());
73 Assertions.assertEquals(Arrays.asList("jp", "ac.jp", "*.tokyo.jp"), publicSuffixList1.getRules());
74 Assertions.assertEquals(Collections.singletonList("metro.tokyo.jp"), publicSuffixList1.getExceptions());
75
76 final PublicSuffixList publicSuffixList2 = suffixLists.get(1);
77 Assertions.assertNotNull(publicSuffixList2);
78 Assertions.assertEquals(DomainType.PRIVATE, publicSuffixList2.getType());
79 Assertions.assertEquals(Arrays.asList("googleapis.com", "googlecode.com"), publicSuffixList2.getRules());
80 Assertions.assertEquals(Collections.<String>emptyList(), publicSuffixList2.getExceptions());
81
82 }
83 }