    1   /* SurtPrefixSetTest
    2   *
    3   * $Id: SurtPrefixSetTest.java 3712 2005-07-19 00:27:50Z gojomo $
    4   *
    5   * Created on Jul 23, 2004
    6   *
    7   * Copyright (C) 2004 Internet Archive.
    8   *
    9   * This file is part of the Heritrix web crawler (crawler.archive.org).
    10  *
    11  * Heritrix is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU Lesser Public License as published by
    13  * the Free Software Foundation; either version 2.1 of the License, or
    14  * any later version.
    15  *
    16  * Heritrix is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU Lesser Public License for more details.
    20  *
    21  * You should have received a copy of the GNU Lesser Public License
    22  * along with Heritrix; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24  */
    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  /***
    36   * @author gojomo
    37   */
    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      /***
    56       * Create a new SurtPrefixSetTest object
    57       *
    58       * @param testName
    59       *            the name of the test
    60       */
    61      public SurtPrefixSetTest(final String testName) {
    62          super(testName);
    63      }
    64
    65      /***
    66       * run all the tests for SurtPrefixSetTest
    67       *
    68       * @param argv
    69       *            the command line arguments
    70       */
    71      public static void main(String argv[]) {
    72          junit.textui.TestRunner.run(suite());
    73      }
    74
    75      /***
    76       * return the suite of tests for SurtPrefixSetTest
    77       *
    78       * @return the suite of test
    79       */
    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     /***
    104      * @param surts
    105      * @param string
    106      */
    107     private void assertDoesntContainPrefix(SurtPrefixSet surts, String s) {
    108         assertEquals(s+" is prefixed", surts.containsPrefixOf(s), false);
    109     }
    110
    111     /***
    112      * @param surts
    113      * @param string
    114      */
    115     private void assertContainsPrefix(SurtPrefixSet surts, String s) {
    116         assertEquals(s+" isn't prefixed", surts.containsPrefixOf(s), true);
    117     }
    118
    119     /***
    120      * @param surts
    121      * @param www_bok_is_redundant_surt2
    122      */
    123     private void assertDoesntContain(SurtPrefixSet surts, String s) {
    124         assertEquals(s+" is present", surts.contains(s), false);
    125     }
    126
    127     /***
    128      * @param archive_org_domain_surt2
    129      */
    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 }