KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > ontology > TestOntology


1 /* Copyright (c) 2004 michael j pan. All rights reserved. */
2 /* Use subject to the same conditions as Nutch. */
3 /* see http://www.nutch.org/LICENSE.txt. */
4
5 package net.nutch.ontology;
6
7 import net.nutch.protocol.ProtocolFactory;
8 import net.nutch.protocol.Protocol;
9 import net.nutch.protocol.Content;
10 import net.nutch.protocol.ProtocolException;
11
12 import net.nutch.parse.ParserFactory;
13 import net.nutch.parse.Parser;
14 import net.nutch.parse.Parse;
15 import net.nutch.parse.ParseException;
16
17 import junit.framework.TestCase;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.LinkedList JavaDoc;
22
23 import java.lang.Exception JavaDoc;
24
25 /**
26  * Unit tests for Ontology
27  *
28  * @author michael j pan
29  */

30 public class TestOntology extends TestCase {
31
32   private String JavaDoc fileSeparator = System.getProperty("file.separator");
33   // This system property is defined in ./src/plugin/build-plugin.xml
34
private String JavaDoc sampleDir = System.getProperty("test.data",".");
35   // Make sure sample files are copied to "test.data" as specified in
36
// ./src/plugin/ontology/build.xml during plugin compilation.
37
// Check ./src/plugin/ontology/sample/README.txt for what they are.
38
private String JavaDoc[] sampleFiles = {"time.owl"};
39
40   private static Ontology ontology;
41
42   public TestOntology(String JavaDoc name) {
43     super(name);
44   }
45
46   protected void setUp() {}
47
48   protected void tearDown() {}
49
50   public void testIt() throws ProtocolException, ParseException, Exception JavaDoc {
51     String JavaDoc className = "Season";
52     String JavaDoc[] subclassNames =
53       new String JavaDoc[] {"Spring", "Summer", "Fall", "Winter"};
54
55     if (ontology==null) {
56       try {
57         ontology = OntologyFactory.getOntology();
58       } catch (Exception JavaDoc e) {
59         throw new Exception JavaDoc("Failed to instantiate ontology");
60       }
61     }
62
63     //foreach sample file
64
for (int i=0; i<sampleFiles.length; i++) {
65       //construct the url
66
String JavaDoc urlString = "file:" + sampleDir + fileSeparator + sampleFiles[i];
67
68       ontology.load(new String JavaDoc[] {urlString});
69
70       List JavaDoc subclassList = new LinkedList JavaDoc();
71   
72       Iterator JavaDoc iter = ontology.subclasses(className);
73       while (iter.hasNext()) {
74         String JavaDoc subclassLabel = (String JavaDoc) iter.next();
75         System.out.println(subclassLabel);
76         subclassList.add(subclassLabel);
77       }
78   
79       for (int j=0; j<subclassNames.length; j++) {
80         assertTrue(subclassList.contains(subclassNames[j]));
81       }
82     }
83
84   }
85
86 }
87
Popular Tags