KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > parse > ext > TestExtParser


1 /* Copyright (c) 2004 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.parse.ext;
5
6 import net.nutch.protocol.ProtocolFactory;
7 import net.nutch.protocol.Protocol;
8 import net.nutch.protocol.Content;
9 import net.nutch.protocol.ProtocolException;
10
11 import net.nutch.parse.ParserFactory;
12 import net.nutch.parse.Parser;
13 import net.nutch.parse.Parse;
14 import net.nutch.parse.ParseException;
15
16 import junit.framework.TestCase;
17
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 /**
23  * Unit tests for ExtParser.
24  * First creates a temp file with fixed content, then fetch
25  * and parse it using external command 'cat' and 'md5sum' alternately
26  * for 10 times. Doing so also does a light stress test for class
27  * CommandRunner.java (as used in ExtParser.java).
28  *
29  * Warning: currently only do test on linux platform.
30  *
31  * @author John Xing
32  */

33 public class TestExtParser extends TestCase {
34   private File JavaDoc tempFile = null;
35   private String JavaDoc urlString = null;
36   private Content content = null;;
37   private Parser parser = null;;
38   private Parse parse = null;
39
40   private String JavaDoc expectedText = "nutch rocks nutch rocks nutch rocks";
41   // echo -n "nutch rocks nutch rocks nutch rocks" | md5sum
42
private String JavaDoc expectedMD5sum = "df46711a1a48caafc98b1c3b83aa1526";
43
44   public TestExtParser(String JavaDoc name) {
45     super(name);
46   }
47
48   protected void setUp() throws ProtocolException, IOException JavaDoc {
49     // prepare a temp file with expectedText as its content
50
// This system property is defined in ./src/plugin/build-plugin.xml
51
String JavaDoc path = System.getProperty("test.data");
52     if (path != null) {
53       File JavaDoc tempDir = new File JavaDoc(path);
54       if (!tempDir.exists())
55         tempDir.mkdir();
56       tempFile = File.createTempFile("nutch.test.plugin.ExtParser.","",tempDir);
57     } else {
58       // otherwise in java.io.tmpdir
59
tempFile = File.createTempFile("nutch.test.plugin.ExtParser.","");
60     }
61     urlString = tempFile.toURL().toString();
62
63     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tempFile);
64     fos.write(expectedText.getBytes());
65     fos.close();
66
67     // get nutch content
68
Protocol protocol = ProtocolFactory.getProtocol(urlString);
69     content = protocol.getContent(urlString);
70     protocol = null;
71   }
72
73   protected void tearDown() {
74     // clean content
75
content = null;
76
77     // clean temp file
78
//if (tempFile != null && tempFile.exists())
79
// tempFile.delete();
80
}
81
82   public void testIt() throws ParseException {
83     String JavaDoc contentType;
84
85     // now test only on linux platform
86
if (!System.getProperty("os.name").equalsIgnoreCase("linux"))
87       return;
88
89     // loop alternately, total 10*2 times of invoking external command
90
for (int i=0; i<10; i++) {
91       // check external parser that does 'cat'
92
contentType = "application/vnd.nutch.example.cat";
93       content.setContentType(contentType);
94       parser = ParserFactory.getParser(contentType, urlString);
95       parse = parser.getParse(content);
96       assertEquals(expectedText,parse.getText());
97
98       // check external parser that does 'md5sum'
99
contentType = "application/vnd.nutch.example.md5sum";
100       content.setContentType(contentType);
101       parser = ParserFactory.getParser(contentType, urlString);
102       parse = parser.getParse(content);
103       assertTrue(parse.getText().startsWith(expectedMD5sum));
104     }
105   }
106
107 }
108
Popular Tags