1   
2   /*
3    * SmartCrawler
4    *
5    * $Id: HttpCallRetrieverTest.java,v 1.4 2005/08/05 13:39:43 vincool Exp $
6    * Copyright 2005 Davide Pozza
7    *
8    * This program is free software; you can redistribute it
9    * and/or modify it under the terms of the GNU General Public
10   * License as published by the Free Software Foundation;
11   * either version 2 of the License, or (at your option) any
12   * later version.
13   *
14   * This program is distributed in the hope that it will be
15   * useful, but WITHOUT ANY WARRANTY; without even the implied
16   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17   * PURPOSE. See the GNU General Public License for more
18   * details.
19   *
20   * You should have received a copy of the GNU General Public
21   * License along with this program; if not, write to the Free
22   * Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307 USA
24   *
25   */
26  
27  package org.smartcrawler.retriever;
28  import junit.framework.*;
29  import org.apache.commons.httpclient.HostConfiguration;
30  import org.smartcrawler.common.Context;
31  import org.smartcrawler.common.Link;
32  import org.smartcrawler.common.SCLogger;
33  
34  
35  /***
36   *
37   *
38   * @author <a href="mailto:pozzad@alice.it">Davide Pozza</a>
39   * @version <tt>$Revision: 1.4 $</tt>
40   */
41  public class HttpCallRetrieverTest extends TestCase {
42  
43      static {
44          SCLogger.initialize();
45      }
46  
47      /***
48       * 
49       * @param testName 
50       */
51      public HttpCallRetrieverTest(String testName) {
52          super(testName);
53      }
54  
55      /***
56       * 
57       * @throws java.lang.Exception 
58       */
59      protected void setUp() throws Exception {
60      }
61  
62      /***
63       * 
64       * @throws java.lang.Exception 
65       */
66      protected void tearDown() throws Exception {
67      }
68  
69      /***
70       * 
71       * @return 
72       */
73      public static Test suite() {
74          TestSuite suite = new TestSuite(HttpCallRetrieverTest.class);
75  
76          return suite;
77      }
78  
79      /***
80       * Test of execute method, of class org.smartcrawler.retriever.HttpCallRetriever.
81       */
82      public void testExecute() {
83          System.out.println("testExecute");
84          Link link = null;
85          try {
86              link = new Link("smartcrawler.sourceforge.net/test/testpage.html");
87          } catch (Exception e) {}
88          Context config = new Context();
89          config.setInitialLink(link);
90          Retriever r = new HttpCallRetriever();
91          Call call = new HttpCall(link);
92          Response res = r.execute(call);
93          assertTrue(res.isFound());
94          // TODO add your test code below by replacing the default call to fail.
95          //fail("The test case is empty.");
96      }
97  
98      /***
99       * Test of getRedirLink method, of class org.smartcrawler.retriever.HttpCallRetriever.
100      */
101     public void testGetRedirLink() {
102         System.out.println("testGetRedirLink");
103         Link link = null;
104         try {
105             link = new Link("www.alice.it");
106         } catch (Exception e) {}
107         Context config = new Context();
108         config.setInitialLink(link);
109         Retriever r = new HttpCallRetriever();
110         
111         Call call = new HttpCall(link);
112         Response res = r.execute(call);
113         assertTrue(res.isRedirected());
114     }
115 
116     /***
117      * Test of getContent method, of class org.smartcrawler.retriever.HttpCallRetriever.
118      */
119     public void testGetContent() {
120         System.out.println("testGetContent");
121         Link link = null;
122         try {
123             link = new Link("smartcrawler.sourceforge.net/test/testpage.html");
124         } catch (Exception e) {}
125         Context config = new Context();
126         config.setInitialLink(link);
127         Retriever r = new HttpCallRetriever();
128         Call call = new HttpCall(link);
129         Response res = r.execute(call);
130         Content c = res.getContent();
131         assertTrue(
132                 c != null &&
133                 c.getBuffer().length > 0 &&
134                 c.getLink().equals(link) &&
135                 c.getContentType() != null
136                 );
137     }
138     
139     /***
140      * Test of createHostConfiguration method, of class org.smartcrawler.retriever.HttpCallRetriever.
141      */
142     public void testCreateHostConfiguration() {
143         System.out.println("testCreateHostConfiguration");
144         Link link = null;
145         try {
146             link = new Link("smartcrawler.sourceforge.net");
147         } catch (Exception e) {}
148         Call call = new HttpCall(link);
149         Context config = new Context();
150         config.setInitialLink(link);
151         HttpCallRetriever r = new HttpCallRetriever();
152         
153         HostConfiguration actual = r.createHostConfiguration(call);
154         
155         HostConfiguration expected = new HostConfiguration();
156         expected.setHost("smartcrawler.sourceforge.net", 80, "http");
157         
158         assertEquals("HttpCallRetriever.createHostConfiguration(call)", 
159                 expected, actual);
160     }
161     
162 }