1   
2   /*
3    * SmartCrawler
4    *
5    * $Id: FileSystemPersisterTest.java,v 1.6 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.persistence;
28  
29  import java.io.File;
30  import java.util.Hashtable;
31  import junit.framework.*;
32  import org.smartcrawler.common.Context;
33  import org.smartcrawler.common.Link;
34  
35  
36  /***
37   *
38   *
39   * @author <a href="mailto:pozzad@alice.it">Davide Pozza</a>
40   * @version <tt>$Revision: 1.6 $</tt>
41   */
42  public class FileSystemPersisterTest extends TestCase {
43      
44      /***
45       *
46       * @param testName
47       */
48      public FileSystemPersisterTest(String testName) {
49          super(testName);
50      }
51      
52      /***
53       *
54       * @throws java.lang.Exception
55       */
56      protected void setUp() throws Exception {
57      }
58      
59      /***
60       *
61       * @throws java.lang.Exception
62       */
63      protected void tearDown() throws Exception {
64      }
65      
66      /***
67       *
68       * @return
69       */
70      public static Test suite() {
71          TestSuite suite = new TestSuite(FileSystemPersisterTest.class);
72          
73          return suite;
74      }
75      
76      /***
77       * Test of persist method, of class org.smartcrawler.persistence.FileSystemPersister.
78       */
79      public void testPersist() {
80          System.out.println("testPersist");
81          
82          // TODO add your test code below by replacing the default call to fail.
83          //fail("The test case is empty.");
84      }
85      
86      /***
87       * Test of linkToFilePath method, of class org.smartcrawler.persistence.FileSystemPersister.
88       */
89      public void testLinkToFilePathWithInvalidChars() {
90          System.out.println("testLinkToFilePathWithInvalidChars");
91          Link link = null;
92          try {
93              link = new Link("http://smartcrawler.sourceforge.net/index.htm?param=:/<>|*//hi");
94          } catch (Exception e) {}
95          Context config = new Context();
96          config.setInitialLink(link);
97          FileSystemPersister fsp = new FileSystemPersister();
98          Hashtable parameters=new Hashtable();
99          parameters.put("preservePath", "true");
100         fsp.setParameters(parameters);
101         File rootDir = new File(".");
102         String cType = "text/html; charset=ISO-8859-1";
103         String sep = FileSystemPersister.FILE_SEPARATOR;
104         String actual = fsp.linkToFilePath(link, rootDir, cType).getAbsolutePath();
105         String expected = new File(".").getAbsolutePath()
106         + sep + "smartcrawler.sourceforge.net" + sep
107                 + "index.htm_param=_______hi.html";
108         System.out.println("exp=" + expected);
109         System.out.println("act=" + actual);
110         assertEquals("FileSystemPersister.linkToFilePath()", expected, actual);
111     }
112     
113     /***
114      * Test of linkToFilePath method, of class org.smartcrawler.persistence.FileSystemPersister.
115      */
116     public void testLinkToFilePathWithInvalidCharsAndSubDir() {
117         System.out.println("testLinkToFilePathWithInvalidCharsAndSubDir");
118         Link link = null;
119         try {
120             link = new Link("http://smartcrawler.sourceforge.net/test/deep/index.htm?param=:/<>|*//hi");
121         } catch (Exception e) {}
122         
123         Context config = new Context();
124         config.setInitialLink(link);
125         FileSystemPersister fsp = new FileSystemPersister();
126         Hashtable parameters=new Hashtable();
127         parameters.put("preservePath", "true");
128         fsp.setParameters(parameters);
129         File rootDir = new File(".");
130         String cType = "text/html; charset=ISO-8859-1";
131         String sep = FileSystemPersister.FILE_SEPARATOR;
132         String actual = fsp.linkToFilePath(link, rootDir, cType).getAbsolutePath();
133         String expected = new File(".").getAbsolutePath()
134         + sep + "smartcrawler.sourceforge.net" + sep + "test" + sep + "deep"
135                 + sep + "index.htm_param=_______hi.html";
136         System.out.println("exp=" + expected);
137         System.out.println("act=" + actual);
138         assertEquals("FileSystemPersister.linkToFilePath()", expected, actual);
139     }
140     /***
141      * Test of linkToFilePath method, of class org.smartcrawler.persistence.FileSystemPersister.
142      */
143     public void testLinkToFilePathWithExtension() {
144         System.out.println("testLinkToFilePathWithExtension");
145         Link link = null;
146         try {
147             link = new Link("http://smartcrawler.sourceforge.net/test/deep/index.do");
148         } catch (Exception e) {}
149         
150         Context config = new Context();
151         config.setInitialLink(link);
152         FileSystemPersister fsp = new FileSystemPersister();
153         Hashtable parameters=new Hashtable();
154         parameters.put("preservePath", "true");
155         fsp.setParameters(parameters);
156         File rootDir = new File(".");
157         String cType = "text/html; charset=ISO-8859-1";
158         String sep = FileSystemPersister.FILE_SEPARATOR;
159         String actual = fsp.linkToFilePath(link, rootDir, cType).getAbsolutePath();
160         String expected = new File(".").getAbsolutePath()
161         + sep + "smartcrawler.sourceforge.net" + sep + "test" + sep + "deep"
162                 + sep + "index.do.html";
163         System.out.println("exp=" + expected);
164         System.out.println("act=" + actual);
165         assertEquals("FileSystemPersister.linkToFilePath()", expected, actual);
166     }
167 }