]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderTest.java
Stoplists, fix location of wikisplits.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryBuilderTest.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.io.File;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.io.RandomAccessFile;
22
23 import com.hughes.util.FileUtil;
24
25 import junit.framework.TestCase;
26
27 public class DictionaryBuilderTest extends TestCase {
28   
29   public static final String TEST_INPUTS = "../DictionaryData/testdata/inputs/";
30   public static final String WIKISPLIT = "../DictionaryData/inputs/enWikiSplit/";
31   public static final String STOPLISTS = "../DictionaryData/inputs/stoplists/";
32   public static final String GOLDENS = "../DictionaryData/testdata/goldens/";
33
34   public static final String TEST_OUTPUTS = "../DictionaryData/testdata/outputs/";
35
36   public void testWiktionaryItalianFromItalian() throws Exception {
37     final String name = "wiktionary.it_it.quickdic";
38     final File result = new File(TEST_OUTPUTS + name);
39     System.out.println("Writing to: " + result);
40     DictionaryBuilder.main(new String[] {
41         "--dictOut=" + result.getAbsolutePath(),
42         "--lang1=IT",
43         "--lang2=EN",
44         "--lang1Stoplist=" + STOPLISTS + "it.txt",
45         "--lang2Stoplist=" + STOPLISTS + "en.txt",
46         "--dictInfo=SomeWikiData",
47
48         "--input4=" + WIKISPLIT + "italian.data",
49         "--input4Name=enwiktionary.italian",
50         "--input4Format=enwiktionary",
51         "--input4LangPattern=Italian",
52         "--input4LangCodePattern=it",
53         "--input4EnIndex=2",
54         "--input4PageLimit=1000",
55
56         "--print=" + result.getPath() + ".text",
57     });
58     
59     checkGolden(name, result); 
60   }
61
62   public void testWiktionaryItalianFromEnglish() throws Exception {
63     final String name = "wiktionary.it_en.quickdic";
64     final File result = new File(TEST_OUTPUTS + name);
65     System.out.println("Writing to: " + result);
66     DictionaryBuilder.main(new String[] {
67         "--dictOut=" + result.getAbsolutePath(),
68         "--lang1=IT",
69         "--lang2=EN",
70         "--lang1Stoplist=" + STOPLISTS + "it.txt",
71         "--lang2Stoplist=" + STOPLISTS + "en.txt",
72         "--dictInfo=SomeWikiData",
73
74         "--input3=" + WIKISPLIT + "english.data",
75         "--input3Name=enwiktionary.english",
76         "--input3Format=enwiktionary",
77         "--input3LangPattern=Italian",
78         "--input3LangCodePattern=it",
79         "--input3EnIndex=2",
80         "--input3PageLimit=1000",
81
82         "--print=" + result.getPath() + ".text",
83     });
84     
85     checkGolden(name, result); 
86   }
87
88   
89   public void testGermanCombined() throws Exception {
90     final String name = "de-en.quickdic";
91     final File result = new File(TEST_OUTPUTS + name);
92     System.out.println("Writing to: " + result);
93     DictionaryBuilder.main(new String[] {
94         "--dictOut=" + result.getAbsolutePath(),
95         "--lang1=DE",
96         "--lang2=EN",
97         "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
98
99         "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
100         "--input1Name=chemnitz",
101         "--input1Charset=UTF8",
102         "--input1Format=chemnitz",
103
104         "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
105         "--input2Name=dictcc",
106         "--input2Charset=UTF8",
107         "--input2Format=dictcc",
108
109         "--print=" + result.getPath() + ".text",
110     });
111     
112     checkGolden(name, result); 
113   }
114
115   private void checkGolden(final String dictName, final File dictFile)
116       throws IOException, FileNotFoundException {
117     // Check it once:
118     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
119
120     // Check it again.
121     final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
122     final PrintStream out = new PrintStream(new File(dictFile.getName() + ".text"));
123     dict.print(out);
124     out.close();
125     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
126   }
127
128
129   void assertFilesEqual(final String expected, final String actual) throws IOException {
130     final String expectedString = FileUtil.readToString(new File(expected));
131     final String actualString = FileUtil.readToString(new File(actual));
132     assertEquals(expectedString, actualString);
133   }
134
135   
136 }