]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderTest.java
a73b1b11a8c741c3fe0ea8f38b01934947252571
[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 = "testdata/inputs/";
30   public static final String WIKISPLIT = "data/inputs/wikiSplit/en/";
31   public static final String STOPLISTS = "data/inputs/stoplists/";
32   public static final String GOLDENS = "testdata/goldens/";
33
34   public static final String TEST_OUTPUTS = "testdata/outputs/";
35   
36   public void testWiktionary_IT_EN() throws Exception {
37     wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
38         "EN.data", "enwiktionary.english", "Italian", "it");
39   }
40
41   public void testWiktionary_ZH_EN() throws Exception {
42     wiktionaryTestWithLangToEn("wiktionary.zh_en.quickdic", "ZH", "empty.txt",
43         // These missing "e" prevents a complete match, forcing the name to be printed
44         "EN.data", "enwiktionary.english", "Chinese|Mandarin|Cantones", "zh");
45   }
46
47   public void testWiktionary_DE_EN() throws Exception {
48     wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
49         "EN.data", "enwiktionary.english", "German", "it");
50   }
51
52   public void testWiktionary_IT_IT() throws Exception {
53     wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
54         "IT.data", "enwiktionary.italian", "Italian", "it");
55   }
56
57   // French
58   public void testWiktionary_FR_FR() throws Exception {
59     wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
60         "FR.data", "enwiktionary.french", "French", "fr");
61   }
62
63   
64   // Arabic
65   public void testWiktionary_AR_AR() throws Exception {
66     wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
67         "AR.data", "enwiktionary.arabic", "Arabic", "ar");
68   }
69
70   // Chinese
71   public void testWiktionary_ZH_ZH() throws Exception {
72     wiktionaryTestWithLangToEn("wiktionary.zh_zh.quickdic", "ZH", "empty.txt",
73         // These missing "e" prevents a complete match, forcing the name to be printed.
74         "ZH.data", "enwiktionary.chinese", "Chinese|Mandarin|Cantones", "zh");
75   }
76
77   // German
78   public void testWiktionary_DE_DE() throws Exception {
79     wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
80         "DE.data", "enwiktionary.german", "German", "it");
81   }
82
83   public void wiktionaryTestWithLangToEn(final String name, final String lang1,
84       final String stoplist, final String data, final String dictName,
85       final String langPattern, final String langCode) throws Exception {
86     final File result = new File(TEST_OUTPUTS + name);
87     System.out.println("Writing to: " + result);
88     final String type = data.equals("EN.data") ? "EnToTranslation" : "EnForeign";
89     DictionaryBuilder.main(new String[] {
90         "--dictOut=" + result.getAbsolutePath(),
91         "--lang1=" + lang1,
92         "--lang2=EN",
93         "--lang1Stoplist=" + STOPLISTS + stoplist,
94         "--lang2Stoplist=" + STOPLISTS + "en.txt",
95         "--dictInfo=SomeWikiData",
96
97         "--input4=" + WIKISPLIT + data,
98         "--input4Name=" + dictName,
99         "--input4Format=enwiktionary",
100         "--input4WiktionaryType=" + type,
101         "--input4LangPattern=" + langPattern,
102         "--input4LangCodePattern=" + langCode,
103         "--input4EnIndex=2",
104         "--input4PageLimit=1000",
105
106         "--print=" + result.getPath() + ".text",
107     });
108     
109     checkGolden(name, result); 
110   }
111
112   public void testGermanCombined() throws Exception {
113     final String name = "de-en.quickdic";
114     final File result = new File(TEST_OUTPUTS + name);
115     System.out.println("Writing to: " + result);
116     DictionaryBuilder.main(new String[] {
117         "--dictOut=" + result.getAbsolutePath(),
118         "--lang1=DE",
119         "--lang2=EN",
120         "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
121
122         "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
123         "--input1Name=chemnitz",
124         "--input1Charset=UTF8",
125         "--input1Format=chemnitz",
126
127         "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
128         "--input2Name=dictcc",
129         "--input2Charset=UTF8",
130         "--input2Format=tab_separated",
131
132         "--print=" + result.getPath() + ".text",
133     });
134     
135     checkGolden(name, result); 
136   }
137
138   private void checkGolden(final String dictName, final File dictFile)
139       throws IOException, FileNotFoundException {
140     // Check it once:
141     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
142
143     // Check it again.
144     final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
145     final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
146     dict.print(out);
147     out.close();
148     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
149   }
150
151
152   void assertFilesEqual(final String expected, final String actual) throws IOException {
153     final String expectedString = FileUtil.readToString(new File(expected));
154     final String actualString = FileUtil.readToString(new File(actual));
155     assertEquals(expectedString, actualString);
156   }
157
158   
159 }