URLFinderTest.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:14k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.util.html;
  2. import junit.framework.TestCase;
  3. import java.net.URL;
  4. /**
  5.  * $Id: URLFinderTest.java,v 1.6 2003/04/29 17:53:50 vanrogu Exp $
  6.  */
  7. public class URLFinderTest extends TestCase implements URLFinderCallback {
  8.     protected int count;
  9.     protected int malformed;
  10.     protected int malformedContextURL;
  11.     protected URL baseURL;
  12.     protected URL lastURL;
  13.     protected URL contextURL;
  14.     public URLFinderTest ( ) {
  15.         super ( "URLFinderTest" );
  16.     }
  17.     protected void setUp() throws Exception {
  18.         count = 0;
  19.         malformed = 0;
  20.         malformedContextURL = 0;
  21.         baseURL = new URL("http://j-spider.sourceforge.net");
  22.         contextURL = baseURL;
  23.     }
  24.     public void urlFound(URL foundURL) {
  25.         count++;
  26.         lastURL = foundURL;
  27.     }
  28.     public void malformedUrlFound(String malformedURL) {
  29.         malformed++;
  30.     }
  31.     public URL getContextURL() {
  32.         return contextURL;
  33.     }
  34.     public void setContextURL(URL url) {
  35.         this.contextURL = url;
  36.     }
  37.     public void malformedContextURLFound(String malformedURL) {
  38.         malformedContextURL++;
  39.     }
  40.     public void testStringWithNoURL ( ) throws Exception {
  41.         URLFinder.findURLs(this,  "this is a line with no url in it");
  42.         int expected = 0;
  43.         assertEquals("actual nr of urls found differs from expected", expected, count);
  44.         assertEquals("malformed URLs reported", 0, malformed);
  45.     }
  46.     public void testStringWithBaseButNoURL ( ) throws Exception {
  47.         URLFinder.findURLs(this,  "<BaSe HrEf='http://www.somehost.com'> this is a line with no url in it, expect for the base one");
  48.         int expected = 1;
  49.         assertEquals("actual nr of urls found differs from expected", expected, count);
  50.         assertEquals("malformed URLs reported", 0, malformed);
  51.         assertEquals("contextURL error", "http://www.somehost.com", contextURL.toString() );
  52.         assertEquals("lastURL error", "http://www.somehost.com", lastURL.toString() );
  53.     }
  54.     public void testStringWithBaseButNoURLCaseSensitivity ( ) throws Exception {
  55.         URLFinder.findURLs(this,  "<BaSe HrEf='http://www.SomeHost.com'> this is a line with no url in it, expect for the base one");
  56.         int expected = 1;
  57.         assertEquals("actual nr of urls found differs from expected", expected, count);
  58.         assertEquals("malformed URLs reported", 0, malformed);
  59.         assertEquals("contextURL error", "http://www.SomeHost.com", contextURL.toString() );
  60.         assertEquals("lastURL error", "http://www.SomeHost.com", lastURL.toString() );
  61.     }
  62.     public void testStringWithBaseInFolderButNoURL ( ) throws Exception {
  63.         URLFinder.findURLs(this,  "<base href='http://www.somehost.com/folder/subfolder'> this is a line with no url in it, expect for the base one");
  64.         int expected = 1;
  65.         assertEquals("actual nr of urls found differs from expected", expected, count);
  66.         assertEquals("malformed URLs reported", 0, malformed);
  67.         assertEquals("contextURL error", "http://www.somehost.com/folder/subfolder", contextURL.toString() );
  68.         assertEquals("lastURL error", "http://www.somehost.com/folder/subfolder", lastURL.toString() );
  69.     }
  70.     public void testStringWithNoURLNullBaseURL ( ) throws Exception {
  71.         URLFinder.findURLs(this,  "this is a line with no url in it");
  72.         int expected = 0;
  73.         assertEquals("actual nr of urls found differs from expected", expected, count);
  74.         assertEquals("malformed URLs reported", 0, malformed);
  75.     }
  76.     public void testStringWithOneURL ( ) throws Exception {
  77.         URLFinder.findURLs(this,  "this is a line with a <a href='index.html'>url</a> in it");
  78.         int expected = 1;
  79.         assertEquals("actual nr of urls found differs from expected", expected, count);
  80.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  81.         assertEquals("malformed URLs reported", 0, malformed);
  82.     }
  83.     public void testStringWithOneURLNullBaseURL ( ) throws Exception {
  84.         URLFinder.findURLs(this,  "this is a line with a <a href='http://index.html'>url</a> in it");
  85.         int expected = 1;
  86.         assertEquals("actual nr of urls found differs from expected", expected, count);
  87.         assertEquals("malformed URLs reported", 0, malformed);
  88.     }
  89.     public void testStringWithOneURLAndBaseURLWithFile ( ) throws Exception {
  90.         URLFinder.findURLs(this,  "this <base href='http://www.somehost.com/folder/subfolder/test.html'> is a line with a <a href='index.html'>url</a> in it");
  91.         int expected = 2;
  92.         assertEquals("actual nr of urls found differs from expected", expected, count);
  93.         assertEquals("wrong contextURL reported", "http://www.somehost.com/folder/subfolder/test.html", contextURL.toString());
  94.         assertEquals("wrong last URLs reported","http://www.somehost.com/folder/subfolder/index.html", this.lastURL.toString());
  95.     }
  96.     public void testStringWithOneURLAndBaseURLWithFolder ( ) throws Exception {
  97.         URLFinder.findURLs(this,  "this <base href='http://www.somehost.com/folder/subfolder'> is a line with a <a href='index.html'>url</a> in it");
  98.         int expected = 2;
  99.         assertEquals("actual nr of urls found differs from expected", expected, count);
  100.         assertEquals("wrong contextURL reported", "http://www.somehost.com/folder/subfolder", contextURL.toString());
  101.         assertEquals("wrong last URLs reported","http://www.somehost.com/folder/subfolder/index.html", this.lastURL.toString());
  102.     }
  103.     public void testStringWithOneURLAndBaseURLWithFolderAndSlash ( ) throws Exception {
  104.         URLFinder.findURLs(this,  "this <base href='http://www.somehost.com/folder/subfolder/'> is a line with a <a href='index.html'>url</a> in it");
  105.         int expected = 2;
  106.         assertEquals("actual nr of urls found differs from expected", expected, count);
  107.         assertEquals("wrong contextURL reported", "http://www.somehost.com/folder/subfolder/", contextURL.toString());
  108.         assertEquals("wrong last URLs reported","http://www.somehost.com/folder/subfolder/index.html", this.lastURL.toString());
  109.     }
  110.     public void testStringWithTwoURLs ( ) throws Exception {
  111.         URLFinder.findURLs(this,  "this is a line with a <a href='index.html'>url</a> in it, and a <a href='second.html'>second one</a> also");
  112.         int expected = 2;
  113.         assertEquals("actual nr of urls found differs from expected", expected, count);
  114.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/second.html"), lastURL);
  115.         assertEquals("malformed URLs reported", 0, malformed);
  116.     }
  117.     public void testStringWithTwoURLsNullBaseURL ( ) throws Exception {
  118.         URLFinder.findURLs(this,  "this is a line with a <a href='http://index.html'>url</a> in it, and a  <a href='http://second.html'>second one</a> also");
  119.         int expected = 2;
  120.         assertEquals("actual nr of urls found differs from expected", expected, count);
  121.         assertEquals("malformed URLs reported", 0, malformed);
  122.     }
  123.     public void testStringSingleQuote ( ) throws Exception {
  124.         URLFinder.findURLs(this,  "this is a line with a <a href='index.html'>url</a> in it");
  125.         int expected = 1;
  126.         assertEquals("actual nr of urls found differs from expected", expected, count);
  127.         assertEquals("malformed URLs reported", 0, malformed);
  128.     }
  129.     public void testStringDoubleQuote ( ) throws Exception {
  130.         URLFinder.findURLs(this,  "this is a line with a <a href="index.html">url</a> in it");
  131.         int expected = 1;
  132.         assertEquals("actual nr of urls found differs from expected", expected, count);
  133.         assertEquals("malformed URLs reported", 0, malformed);
  134.     }
  135.     public void testStringDoubleNoQuotes ( ) throws Exception {
  136.         URLFinder.findURLs(this,  "this is a line with a <a href=index.html>url</a> in it");
  137.         int expected = 1;
  138.         assertEquals("actual nr of urls found differs from expected", expected, count);
  139.         assertEquals("malformed URLs reported", 0, malformed);
  140.     }
  141.     public void testStringBadlyQuoted1 ( ) throws Exception {
  142.         URLFinder.findURLs(this,  "this is a line with a <a href="index.html>url</a> in it");
  143.         int expected = 1;
  144.         assertEquals("actual nr of urls found differs from expected", expected, count);
  145.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  146.         assertEquals("malformed URLs reported", 0, malformed);
  147.     }
  148.     public void testStringBadlyQuoted2 ( ) throws Exception {
  149.         URLFinder.findURLs(this,  "this is a line with a <a href='index.html>url</a> in it");
  150.         int expected = 1;
  151.         assertEquals("actual nr of urls found differs from expected", expected, count);
  152.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  153.         assertEquals("malformed URLs reported", 0, malformed);
  154.     }
  155.     public void testStringBadlyQuoted3 ( ) throws Exception {
  156.         URLFinder.findURLs(this,  "this is a line with a <a href=index.html'>url</a> in it");
  157.         int expected = 1;
  158.         assertEquals("actual nr of urls found differs from expected", expected, count);
  159.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  160.         assertEquals("malformed URLs reported", 0, malformed);
  161.     }
  162.     public void testStringBadlyQuoted4 ( ) throws Exception {
  163.         URLFinder.findURLs(this,  "this is a line with a <a href=index.html">url</a> in it");
  164.         int expected = 1;
  165.         assertEquals("actual nr of urls found differs from expected", expected, count);
  166.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  167.         assertEquals("malformed URLs reported", 0, malformed);
  168.     }
  169.     public void testStringBadlyQuoted5 ( ) throws Exception {
  170.         URLFinder.findURLs(this,  "this is a line with a <a href='index.html">url</a> in it");
  171.         int expected = 1;
  172.         assertEquals("actual nr of urls found differs from expected", expected, count);
  173.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  174.         assertEquals("malformed URLs reported", 0, malformed);
  175.     }
  176.     public void testStringBadlyQuoted6 ( ) throws Exception {
  177.         URLFinder.findURLs(this,  "this is a line with a <a href="index.html'>url</a> in it");
  178.         int expected = 1;
  179.         assertEquals("actual nr of urls found differs from expected", expected, count);
  180.         assertEquals("url found wasn't the one expected", new URL("http://j-spider.sourceforge.net/index.html"), lastURL);
  181.         assertEquals("malformed URLs reported", 0, malformed);
  182.     }
  183.     public void testMalformed ( ) throws Exception {
  184.         URLFinder.findURLs(this,  "this is a line with a <a href="someprotocol:index.html'>url</a> in it");
  185.         int expected = 0;
  186.         int malformedExpected = 1;
  187.         assertEquals("actual nr of urls found differs from expected", expected, count);
  188.         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
  189.     }
  190.     public void testTwoMalformed ( ) throws Exception {
  191.         URLFinder.findURLs(this,  "this is a line with two malformed <a href="someprotocol:index.html'>urls</a><a href='test:'/> in it");
  192.         int expected = 0;
  193.         int malformedExpected = 2;
  194.         assertEquals("actual nr of urls found differs from expected", expected, count);
  195.         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
  196.     }
  197.     public void testWellformedAndMalformed ( ) throws Exception {
  198.         URLFinder.findURLs(this,  "this is a line with one malformed <a href="someprotocol:index.html'>urls</a><a href='index.html'/> in it, and one wellformed");
  199.         int expected = 1;
  200.         int malformedExpected = 1;
  201.         assertEquals("actual nr of urls found differs from expected", expected, count);
  202.         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
  203.     }
  204.     public void testWellformedAndMalformedWithBaseURL ( ) throws Exception {
  205.         URLFinder.findURLs(this,  "this is a line <base href='http://www.somehost.com/folder/test.html'/>with one malformed <a href="someprotocol:index.html'>urls</a><a href='index.html'/> in it, and one wellformed");
  206.         int expected = 2;
  207.         int malformedExpected = 1;
  208.         assertEquals("actual nr of urls found differs from expected", expected, count);
  209.         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
  210.         assertEquals("baseURL is wrong", "http://www.somehost.com/folder/test.html", contextURL.toString());
  211.         assertEquals("lastURL is wrong", "http://www.somehost.com/folder/index.html", lastURL.toString());
  212.     }
  213.     public void testWithBaseURL ( ) throws Exception {
  214.         URLFinder.findURLs(this,  "this is a line <base href='http://www.somehost.com/folder/subfolder/test.html'/>with one malformed <a href="someprotocol:index.html'>urls</a><a href='../index.html'/> in it, and one wellformed");
  215.         int expected = 2;
  216.         int malformedExpected = 1;
  217.         assertEquals("actual nr of urls found differs from expected", expected, count);
  218.         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
  219.         assertEquals("baseURL is wrong", "http://www.somehost.com/folder/subfolder/test.html", contextURL.toString());
  220.         assertEquals("lastURL is wrong", "http://www.somehost.com/folder/index.html", lastURL.toString());
  221.     }
  222. }