1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.io.RandomAccessFile;
23 import com.hughes.util.FileUtil;
25 import junit.framework.TestCase;
27 public class DictionaryBuilderTest extends TestCase {
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/";
34 public static final String TEST_OUTPUTS = "testdata/outputs/";
36 public void testWiktionary_IT_EN() throws Exception {
37 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
38 "EN.data", "enwiktionary.english", "Italian", "it");
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");
47 public void testWiktionary_DE_EN() throws Exception {
48 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
49 "EN.data", "enwiktionary.english", "German", "it");
52 public void testWiktionary_IT_IT() throws Exception {
53 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
54 "IT.data", "enwiktionary.italian", "Italian", "it");
58 public void testWiktionary_FR_FR() throws Exception {
59 wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
60 "FR.data", "enwiktionary.french", "French", "fr");
65 public void testWiktionary_AR_AR() throws Exception {
66 wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
67 "AR.data", "enwiktionary.arabic", "Arabic", "ar");
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");
78 public void testWiktionary_DE_DE() throws Exception {
79 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
80 "DE.data", "enwiktionary.german", "German", "it");
84 public void testWiktionary_TH_TH() throws Exception {
85 wiktionaryTestWithLangToEn("wiktionary.th_th.quickdic", "TH", "empty.txt",
86 // These missing "e" prevents a complete match, forcing the name to be printed.
87 "TH.data", "enwiktionary.thai", "Thai", "th");
90 public void wiktionaryTestWithLangToEn(final String name, final String lang1,
91 final String stoplist, final String data, final String dictName,
92 final String langPattern, final String langCode) throws Exception {
93 final File result = new File(TEST_OUTPUTS + name);
94 System.out.println("Writing to: " + result);
95 final String type = data.equals("EN.data") ? "EnToTranslation" : "EnForeign";
96 DictionaryBuilder.main(new String[] {
97 "--dictOut=" + result.getAbsolutePath(),
100 "--lang1Stoplist=" + STOPLISTS + stoplist,
101 "--lang2Stoplist=" + STOPLISTS + "en.txt",
102 "--dictInfo=SomeWikiData",
104 "--input4=" + WIKISPLIT + data,
105 "--input4Name=" + dictName,
106 "--input4Format=enwiktionary",
107 "--input4WiktionaryType=" + type,
108 "--input4LangPattern=" + langPattern,
109 "--input4LangCodePattern=" + langCode,
111 "--input4PageLimit=1000",
113 "--print=" + result.getPath() + ".text",
116 checkGolden(name, result);
119 public void testGermanCombined() throws Exception {
120 final String name = "de-en.quickdic";
121 final File result = new File(TEST_OUTPUTS + name);
122 System.out.println("Writing to: " + result);
123 DictionaryBuilder.main(new String[] {
124 "--dictOut=" + result.getAbsolutePath(),
127 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
129 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
130 "--input1Name=chemnitz",
131 "--input1Charset=UTF8",
132 "--input1Format=chemnitz",
134 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
135 "--input2Name=dictcc",
136 "--input2Charset=UTF8",
137 "--input2Format=tab_separated",
139 "--print=" + result.getPath() + ".text",
142 checkGolden(name, result);
145 private void checkGolden(final String dictName, final File dictFile)
146 throws IOException, FileNotFoundException {
148 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
151 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
152 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
155 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
159 void assertFilesEqual(final String expected, final String actual) throws IOException {
160 final String expectedString = FileUtil.readToString(new File(expected));
161 final String actualString = FileUtil.readToString(new File(actual));
162 assertEquals(expectedString, actualString);