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