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");
49 public void testWiktionary_DE_DE() throws Exception {
50 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
51 "DE.data", "enwiktionary.german", "German", "it");
54 public void testWiktionary_DE_EN() throws Exception {
55 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
56 "EN.data", "enwiktionary.english", "German", "it");
60 public void testWiktionary_IT_IT() throws Exception {
61 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
62 "IT.data", "enwiktionary.italian", "Italian", "it");
65 public void testWiktionary_IT_EN() throws Exception {
66 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
67 "EN.data", "enwiktionary.english", "Italian", "it");
70 public void wiktionaryTestWithLangToEn(final String name, final String lang1,
71 final String stoplist, final String data, final String dictName,
72 final String langPattern, final String langCode) throws Exception {
73 final File result = new File(TEST_OUTPUTS + name);
74 System.out.println("Writing to: " + result);
75 DictionaryBuilder.main(new String[] {
76 "--dictOut=" + result.getAbsolutePath(),
79 "--lang1Stoplist=" + STOPLISTS + stoplist,
80 "--lang2Stoplist=" + STOPLISTS + "en.txt",
81 "--dictInfo=SomeWikiData",
83 "--input4=" + WIKISPLIT + data,
84 "--input4Name=" + dictName,
85 "--input4Format=enwiktionary",
86 "--input4LangPattern=" + langPattern,
87 "--input4LangCodePattern=" + langCode,
89 "--input4PageLimit=1000",
91 "--print=" + result.getPath() + ".text",
94 checkGolden(name, result);
97 public void testGermanCombined() throws Exception {
98 final String name = "de-en.quickdic";
99 final File result = new File(TEST_OUTPUTS + name);
100 System.out.println("Writing to: " + result);
101 DictionaryBuilder.main(new String[] {
102 "--dictOut=" + result.getAbsolutePath(),
105 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
107 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
108 "--input1Name=chemnitz",
109 "--input1Charset=UTF8",
110 "--input1Format=chemnitz",
112 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
113 "--input2Name=dictcc",
114 "--input2Charset=UTF8",
115 "--input2Format=dictcc",
117 "--print=" + result.getPath() + ".text",
120 checkGolden(name, result);
123 private void checkGolden(final String dictName, final File dictFile)
124 throws IOException, FileNotFoundException {
126 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
129 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
130 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
133 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
137 void assertFilesEqual(final String expected, final String actual) throws IOException {
138 final String expectedString = FileUtil.readToString(new File(expected));
139 final String actualString = FileUtil.readToString(new File(actual));
140 assertEquals(expectedString, actualString);