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 = "../DictionaryData/inputs/enWikiSplit/";
31 public static final String STOPLISTS = "../DictionaryData/inputs/stoplists/";
32 public static final String GOLDENS = "testdata/goldens/";
34 public static final String TEST_OUTPUTS = "testdata/outputs/";
37 public void testWiktionary_FR_FR() throws Exception {
38 wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
39 "FR.data", "enwiktionary.french", "French", "fr");
44 public void testWiktionary_AR_AR() throws Exception {
45 wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
46 "AR.data", "enwiktionary.arabic", "Arabic", "ar");
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");
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");
63 public void testWiktionary_DE_DE() throws Exception {
64 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
65 "DE.data", "enwiktionary.german", "German", "it");
68 public void testWiktionary_DE_EN() throws Exception {
69 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
70 "EN.data", "enwiktionary.english", "German", "it");
74 public void testWiktionary_IT_IT() throws Exception {
75 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
76 "IT.data", "enwiktionary.italian", "Italian", "it");
79 public void testWiktionary_IT_EN() throws Exception {
80 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
81 "EN.data", "enwiktionary.english", "Italian", "it");
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(),
93 "--lang1Stoplist=" + STOPLISTS + stoplist,
94 "--lang2Stoplist=" + STOPLISTS + "en.txt",
95 "--dictInfo=SomeWikiData",
97 "--input4=" + WIKISPLIT + data,
98 "--input4Name=" + dictName,
99 "--input4Format=enwiktionary",
100 "--input4LangPattern=" + langPattern,
101 "--input4LangCodePattern=" + langCode,
103 "--input4PageLimit=1000",
105 "--print=" + result.getPath() + ".text",
108 checkGolden(name, result);
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(),
119 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
121 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
122 "--input1Name=chemnitz",
123 "--input1Charset=UTF8",
124 "--input1Format=chemnitz",
126 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
127 "--input2Name=dictcc",
128 "--input2Charset=UTF8",
129 "--input2Format=dictcc",
131 "--print=" + result.getPath() + ".text",
134 checkGolden(name, result);
137 private void checkGolden(final String dictName, final File dictFile)
138 throws IOException, FileNotFoundException {
140 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
143 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
144 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
147 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
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);