1   package de.fu_berlin.ties.preprocess;
2   
3   import junit.framework.TestCase;
4   // JUnitDoclet begin import
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.InputStreamReader;
8   import java.io.Reader;
9   import java.io.StringWriter;
10  import java.io.Writer;
11  
12  import de.fu_berlin.ties.ContextMap;
13  import de.fu_berlin.ties.TiesConfiguration;
14  import de.fu_berlin.ties.io.IOUtils;
15  import de.fu_berlin.ties.util.TaskRunner;
16  // JUnitDoclet end import
17  
18  /***
19  * Generated by JUnitDoclet, a tool provided by
20  * ObjectFab GmbH under LGPL.
21  * Please see www.junitdoclet.org, www.gnu.org
22  * and www.objectfab.de for informations about
23  * the tool, the licence and the authors.
24  */
25  
26  
27  public class PreProcessorTest
28  // JUnitDoclet begin extends_implements
29  extends TestCase
30  // JUnitDoclet end extends_implements
31  {
32    // JUnitDoclet begin class
33    PreProcessor preprocessor = null;
34    // JUnitDoclet end class
35    
36    public PreProcessorTest(String name) {
37      // JUnitDoclet begin method PreProcessorTest
38      super(name);
39      // JUnitDoclet end method PreProcessorTest
40    }
41    
42    public de.fu_berlin.ties.preprocess.PreProcessor createInstance() throws Exception {
43      // JUnitDoclet begin method testcase.createInstance
44      // component test in cleanHTML method
45      return null;
46      // JUnitDoclet end method testcase.createInstance
47    }
48    
49    protected void setUp() throws Exception {
50      // JUnitDoclet begin method testcase.setUp
51      super.setUp();
52      preprocessor = createInstance();
53      // JUnitDoclet end method testcase.setUp
54    }
55    
56    protected void tearDown() throws Exception {
57      // JUnitDoclet begin method testcase.tearDown
58      preprocessor = null;
59      super.tearDown();
60      // JUnitDoclet end method testcase.tearDown
61    }
62    
63    public void testCleanHTML() throws Exception {
64      // JUnitDoclet begin method cleanHTML
65        // Component test:
66        // test that HTML + text files are converted correctly (not only cleaning)
67  
68        // relative to main dir which must be current working dir
69        final String baseDir = "test/samples";
70  
71        final String[] baseNames = new String[] {
72                "digitalesRadio",
73                "kugelmensch",
74                "cmu.andrew.academic.bio-242",
75        };
76        final String[] inExtensions = new String[] {
77                "html",
78                "html",
79                "txt",
80        };
81        final String[] lang = new String[] {
82                "de",
83                "de",
84                "en",
85        };
86        Writer writer;
87        Reader outputReader = null;
88        String expectedOutput;
89        PreProcessor processor = null;
90        final ContextMap context = new ContextMap();
91        // test files use ISO-8859-1 charset
92        context.put(IOUtils.KEY_LOCAL_CHARSET, IOUtils.STANDARD_HTTP_CHARSET);
93  
94        TaskRunner.registerInterest();
95  
96        try {
97           // skip test on Windows where TreeTagger doesn't run
98           if (File.separatorChar != '//') {
99             for (int i = 0; i < baseNames.length; i++) {
100               try {
101                   // adjust configured language in standard config
102                   TiesConfiguration.CONF.setProperty(
103                           TiesConfiguration.CONFIG_LANG, lang[i]);
104                   processor = new de.fu_berlin.ties.preprocess.PreProcessor(
105                           "aug");
106                   writer = new StringWriter();
107                   processor.process(new File(baseDir, baseNames[i] + '.'
108                           + inExtensions[i]), writer, context);
109                   outputReader = new InputStreamReader(
110                           new FileInputStream(new File(baseDir, baseNames[i]
111                                   + '.'  + "aug")),
112                                   IOUtils.STANDARD_HTTP_CHARSET);
113                   expectedOutput = IOUtils.readToString(outputReader);
114 
115                   /*// debug
116                   final int diffIndex = StringUtils.indexOfDifference(
117                           expectedOutput, writer.toString());
118                   if (diffIndex >= 0) {
119                     System.out.println("Unexpected results of preprocessing "
120                             + baseNames[i] + " at pos " + diffIndex + ": ..."
121                             + StringUtils.substring(
122                                     writer.toString(), diffIndex, diffIndex+10)
123                             + "... instead of ..." + StringUtils.substring(
124                                     expectedOutput, diffIndex, diffIndex+10)
125                             + "... (" 
126                             + (int) writer.toString().charAt(diffIndex)
127                             + " instead of "
128                             + (int) expectedOutput.charAt(diffIndex) + ")");
129                   }*/
130 
131                   assertEquals(expectedOutput.trim(), writer.toString().trim());
132               } finally {
133                   IOUtils.tryToClose(outputReader);
134               } //try-finally
135             } // for
136           } // if
137       } finally {
138           TaskRunner.deregisterInterest();        
139       }
140     // JUnitDoclet end method cleanHTML
141   }
142   
143   public void testToString() throws Exception {
144     // JUnitDoclet begin method toString
145     // JUnitDoclet end method toString
146   }
147   
148   
149   
150   /***
151   * JUnitDoclet moves marker to this method, if there is not match
152   * for them in the regenerated code and if the marker is not empty.
153   * This way, no test gets lost when regenerating after renaming.
154   * Method testVault is supposed to be empty.
155   */
156   public void testVault() throws Exception {
157     // JUnitDoclet begin method testcase.testVault
158     // JUnitDoclet end method testcase.testVault
159   }
160   
161   public static void main(String[] args) {
162     // JUnitDoclet begin method testcase.main
163     junit.textui.TestRunner.run(PreProcessorTest.class);
164     // JUnitDoclet end method testcase.main
165   }
166 }