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/enWikiSplit/";
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");
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 DictionaryBuilder.main(new String[] {
89 "--dictOut=" + result.getAbsolutePath(),
92 "--lang1Stoplist=" + STOPLISTS + stoplist,
93 "--lang2Stoplist=" + STOPLISTS + "en.txt",
94 "--dictInfo=SomeWikiData",
96 "--input4=" + WIKISPLIT + data,
97 "--input4Name=" + dictName,
98 "--input4Format=enwiktionary",
99 "--input4LangPattern=" + langPattern,
100 "--input4LangCodePattern=" + langCode,
102 "--input4PageLimit=1000",
104 "--print=" + result.getPath() + ".text",
107 checkGolden(name, result);
110 public void testGermanCombined() throws Exception {
111 final String name = "de-en.quickdic";
112 final File result = new File(TEST_OUTPUTS + name);
113 System.out.println("Writing to: " + result);
114 DictionaryBuilder.main(new String[] {
115 "--dictOut=" + result.getAbsolutePath(),
118 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
120 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
121 "--input1Name=chemnitz",
122 "--input1Charset=UTF8",
123 "--input1Format=chemnitz",
125 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
126 "--input2Name=dictcc",
127 "--input2Charset=UTF8",
128 "--input2Format=tab_separated",
130 "--print=" + result.getPath() + ".text",
133 checkGolden(name, result);
136 private void checkGolden(final String dictName, final File dictFile)
137 throws IOException, FileNotFoundException {
139 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
142 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
143 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
146 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
150 void assertFilesEqual(final String expected, final String actual) throws IOException {
151 final String expectedString = FileUtil.readToString(new File(expected));
152 final String actualString = FileUtil.readToString(new File(actual));
153 assertEquals(expectedString, actualString);