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_ZH_ZH() throws Exception {
38 wiktionaryTestWithLangToEn("wiktionary.zh_zh.quickdic", "ZH", "empty.txt",
39 "ZH.data", "enwiktionary.chinese", "Chinese|Mandarin|Cantonese", "zh");
42 public void testWiktionary_ZH_EN() throws Exception {
43 wiktionaryTestWithLangToEn("wiktionary.zh_en.quickdic", "ZH", "empty.txt",
44 "EN.data", "enwiktionary.english", "Chinese|Mandarin|Cantonese", "zh");
48 public void testWiktionary_DE_DE() throws Exception {
49 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
50 "DE.data", "enwiktionary.german", "German", "it");
53 public void testWiktionary_DE_EN() throws Exception {
54 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
55 "EN.data", "enwiktionary.english", "German", "it");
59 public void testWiktionary_IT_IT() throws Exception {
60 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
61 "IT.data", "enwiktionary.italian", "Italian", "it");
64 public void testWiktionary_IT_EN() throws Exception {
65 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
66 "EN.data", "enwiktionary.english", "Italian", "it");
69 public void wiktionaryTestWithLangToEn(final String name, final String lang1,
70 final String stoplist, final String data, final String dictName,
71 final String langPattern, final String langCode) throws Exception {
72 final File result = new File(TEST_OUTPUTS + name);
73 System.out.println("Writing to: " + result);
74 DictionaryBuilder.main(new String[] {
75 "--dictOut=" + result.getAbsolutePath(),
78 "--lang1Stoplist=" + STOPLISTS + stoplist,
79 "--lang2Stoplist=" + STOPLISTS + "en.txt",
80 "--dictInfo=SomeWikiData",
82 "--input4=" + WIKISPLIT + data,
83 "--input4Name=" + dictName,
84 "--input4Format=enwiktionary",
85 "--input4LangPattern=" + langPattern,
86 "--input4LangCodePattern=" + langCode,
88 "--input4PageLimit=1000",
90 "--print=" + result.getPath() + ".text",
93 checkGolden(name, result);
96 public void testGermanCombined() throws Exception {
97 final String name = "de-en.quickdic";
98 final File result = new File(TEST_OUTPUTS + name);
99 System.out.println("Writing to: " + result);
100 DictionaryBuilder.main(new String[] {
101 "--dictOut=" + result.getAbsolutePath(),
104 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
106 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
107 "--input1Name=chemnitz",
108 "--input1Charset=UTF8",
109 "--input1Format=chemnitz",
111 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
112 "--input2Name=dictcc",
113 "--input2Charset=UTF8",
114 "--input2Format=dictcc",
116 "--print=" + result.getPath() + ".text",
119 checkGolden(name, result);
122 private void checkGolden(final String dictName, final File dictFile)
123 throws IOException, FileNotFoundException {
125 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
128 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
129 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
132 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
136 void assertFilesEqual(final String expected, final String actual) throws IOException {
137 final String expectedString = FileUtil.readToString(new File(expected));
138 final String actualString = FileUtil.readToString(new File(actual));
139 assertEquals(expectedString, actualString);