1 25 package org.archive.util; 26 27 import java.io.IOException ; 28 import java.io.StringReader ; 29 import java.util.Iterator ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 38 public class SurtPrefixSetTest extends TestCase { 39 private static final String ARCHIVE_ORG_DOMAIN_SURT = "http://(org,archive,"; 40 private static final String WWW_EXAMPLE_ORG_HOST_SURT = "http://(org,example,www,)"; 41 private static final String HOME_EXAMPLE_ORG_PATH_SURT = "http://(org,example,home,)/pages/"; 42 private static final String BOK_IS_REDUNDANT_SURT = "http://(is,bok,"; 43 private static final String IS_DOMAIN_SURT = "http://(is,"; 44 private static final String WWW_BOK_IS_REDUNDANT_SURT = "http://(is,bok,www"; 45 46 private static final String TEST_SURT_LIST = 47 "# a test set of surt prefixes \n" + 48 ARCHIVE_ORG_DOMAIN_SURT + "\n" + 49 WWW_EXAMPLE_ORG_HOST_SURT + "\n" + 50 HOME_EXAMPLE_ORG_PATH_SURT + "\n" + 51 BOK_IS_REDUNDANT_SURT + " # is redundant\n" + 52 IS_DOMAIN_SURT + "\n" + 53 WWW_BOK_IS_REDUNDANT_SURT + " # is redundant\n"; 54 55 61 public SurtPrefixSetTest(final String testName) { 62 super(testName); 63 } 64 65 71 public static void main(String argv[]) { 72 junit.textui.TestRunner.run(suite()); 73 } 74 75 80 public static Test suite() { 81 return new TestSuite(SurtPrefixSetTest.class); 82 } 83 84 85 86 public void testMisc() throws IOException { 87 SurtPrefixSet surts = new SurtPrefixSet(); 88 StringReader sr = new StringReader (TEST_SURT_LIST); 89 surts.importFrom(sr); 90 91 assertContains(surts,ARCHIVE_ORG_DOMAIN_SURT); 92 assertContains(surts,WWW_EXAMPLE_ORG_HOST_SURT); 93 assertContains(surts,HOME_EXAMPLE_ORG_PATH_SURT); 94 assertContains(surts,IS_DOMAIN_SURT); 95 96 assertDoesntContain(surts,BOK_IS_REDUNDANT_SURT); 97 assertDoesntContain(surts,WWW_BOK_IS_REDUNDANT_SURT); 98 99 assertContainsPrefix(surts,SURT.fromURI("http://example.is/foo")); 100 assertDoesntContainPrefix(surts,SURT.fromURI("http://home.example.org/foo")); 101 } 102 103 107 private void assertDoesntContainPrefix(SurtPrefixSet surts, String s) { 108 assertEquals(s+" is prefixed", surts.containsPrefixOf(s), false); 109 } 110 111 115 private void assertContainsPrefix(SurtPrefixSet surts, String s) { 116 assertEquals(s+" isn't prefixed", surts.containsPrefixOf(s), true); 117 } 118 119 123 private void assertDoesntContain(SurtPrefixSet surts, String s) { 124 assertEquals(s+" is present", surts.contains(s), false); 125 } 126 127 130 private void assertContains(SurtPrefixSet surts, String s) { 131 assertEquals(s+" is missing", surts.contains(s), true); 132 } 133 134 public void testImportFromUris() throws IOException { 135 String seed = "http://www.archive.org/index.html"; 136 assertEquals("Convert failed " + seed, 137 "http://(org,archive,www,)/", 138 makeSurtPrefix(seed)); 139 seed = "http://timmknibbs4senate.blogspot.com/"; 140 assertEquals("Convert failed " + seed, 141 "http://(com,blogspot,timmknibbs4senate,)/", 142 makeSurtPrefix(seed)); 143 seed = "https://one.two.three"; 144 assertEquals("Convert failed " + seed, 145 "http://(three,two,one,", 146 makeSurtPrefix(seed)); 147 seed = "https://xone.two.three/a/b/c/"; 148 assertEquals("Convert failed " + seed, 149 "http://(three,two,xone,)/a/b/c/", 150 makeSurtPrefix(seed)); 151 seed = "https://yone.two.three/a/b/c"; 152 assertEquals("Convert failed " + seed, 153 "http://(three,two,yone,)/a/b/", 154 makeSurtPrefix(seed)); 155 } 156 157 private String makeSurtPrefix(String seed) { 158 SurtPrefixSet surts = new SurtPrefixSet(); 159 StringReader sr = new StringReader (seed); 160 surts.importFromUris(sr); 161 String result = null; 162 for (Iterator i = surts.iterator(); i.hasNext();) { 163 result = (String )i.next(); 164 } 165 return result; 166 } 167 } 168 | Popular Tags |