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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.functional.specific.model;
  2. import junit.framework.TestCase;
  3. import net.javacoding.jspider.JSpider;
  4. import net.javacoding.jspider.api.model.Folder;
  5. import net.javacoding.jspider.mockobjects.plugin.JUnitEventSink;
  6. import net.javacoding.jspider.core.util.config.ConfigurationFactory;
  7. import net.javacoding.jspider.core.util.config.JSpiderConfiguration;
  8. import net.javacoding.jspider.core.util.URLUtil;
  9. import net.javacoding.jspider.core.storage.Storage;
  10. import net.javacoding.jspider.functional.TestingConstants;
  11. import java.net.URL;
  12. /**
  13.  * $Id: ModelTest.java,v 1.1 2003/04/10 16:19:23 vanrogu Exp $
  14.  * @todo elaborate to check better
  15.  */
  16. public class ModelTest extends TestCase {
  17.     public static final String
  18.       tree[][] = {
  19.           {"testcases", "specific", "model"},
  20.           {"testcases", "specific", "model", "test1"},
  21.           {"testcases", "specific", "model", "test2"}
  22.       };
  23.     public static final int
  24.       resourceCount[][] = {
  25.           {0, 0, 1 },
  26.           {0, 0, 1, 1},
  27.           {0, 0, 1, 2}
  28.       };
  29.     protected JUnitEventSink sink;
  30.     protected JSpiderConfiguration config;
  31.     /**
  32.      * Public constructor giving a name to the test.
  33.      */
  34.     public ModelTest ( ) {
  35.         super ( "ParseTest ");
  36.     }
  37.     /**
  38.      * JUnit's overridden setUp method
  39.      * @throws java.lang.Exception in case something fails during setup
  40.      */
  41.     protected void setUp() throws Exception {
  42.         System.err.println("setUp");
  43.         config = ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
  44.         sink = JUnitEventSink.getInstance();
  45.     }
  46.     /**
  47.      * JUnit's overridden tearDown method
  48.      * @throws java.lang.Exception in case something fails during tearDown
  49.      */
  50.     protected void tearDown() throws Exception {
  51.         System.err.println("tearDown");
  52.         ConfigurationFactory.cleanConfiguration();
  53.         sink.reset();
  54.     }
  55.     /**
  56.      * Test a simple parse.
  57.      */
  58.     public void testSimpleParse ( ) throws Exception {
  59.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/model/index.html" );
  60.         JSpider jspider = new JSpider ( url );
  61.         jspider.start ( );
  62.         Storage storage = jspider.getContext().getStorage();
  63.         testFolders ( storage );
  64.     }
  65.     public void testFolders ( Storage storage ) throws Exception {
  66.         for (int i = 0; i < tree.length; i++) {
  67.             String[] folders = tree[i];
  68.             Folder[] rootFolders = storage.getSiteDAO().find(URLUtil.normalize(new URL("http", TestingConstants.HOST, ""))).getRootFolders();
  69.             ensureFolders(i, rootFolders, folders, 0);
  70.             //email
  71.             //refs
  72.             //resources
  73.         }
  74.     }
  75.     public void ensureFolders ( int treeIndex, Folder[] currentLevel, String[] folderNames, int index ) {
  76.         String name = folderNames[index];
  77.         Folder foundFolder = null;
  78.         for (int i = 0; i < currentLevel.length; i++) {
  79.             Folder folder = currentLevel[i];
  80.             if ( folder.getName().equals(name)){
  81.                 foundFolder = folder;
  82.                 assertEquals("folder " + name + " reported wrong number of resources", resourceCount[treeIndex][index], folder.getResources().length);
  83.             }
  84.         }
  85.         assertNotNull("folder " + name + " not found", foundFolder);
  86.         if ( (index+1) < folderNames.length ) {
  87.             ensureFolders(treeIndex, foundFolder.getFolders(), folderNames, index+1);
  88.         }
  89.     }
  90. }