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 = "../DictionaryData/testdata/inputs/";
30 public static final String WIKISPLIT = "../DictionaryData/inputs/wikiSplit/";
31 public static final String GOLDENS = "../DictionaryData/testdata/goldens/";
33 public static final String TEST_OUTPUTS = "../DictionaryData/testdata/outputs/";
35 public void testWiktionaryItalianFromItalian() throws Exception {
36 final String name = "wiktionary.it_it.quickdic";
37 final File result = new File(TEST_OUTPUTS + name);
38 System.out.println("Writing to: " + result);
39 DictionaryBuilder.main(new String[] {
40 "--dictOut=" + result.getAbsolutePath(),
43 "--dictInfo=SomeWikiData",
45 "--input4=" + WIKISPLIT + "italian.data",
46 "--input4Name=enwiktionary.italian",
47 "--input4Format=enwiktionary",
48 "--input4LangPattern=Italian",
49 "--input4LangCodePattern=it",
51 "--input4PageLimit=1000",
53 "--print=" + result.getPath() + ".text",
56 checkGolden(name, result);
59 public void testWiktionaryItalianFromEnglish() throws Exception {
60 final String name = "wiktionary.it_en.quickdic";
61 final File result = new File(TEST_OUTPUTS + name);
62 System.out.println("Writing to: " + result);
63 DictionaryBuilder.main(new String[] {
64 "--dictOut=" + result.getAbsolutePath(),
67 "--dictInfo=SomeWikiData",
69 "--input3=" + WIKISPLIT + "english.data",
70 "--input3Name=enwiktionary.english",
71 "--input3Format=enwiktionary",
72 "--input3LangPattern=Italian",
73 "--input3LangCodePattern=it",
75 "--input3PageLimit=1000",
77 "--print=" + result.getPath() + ".text",
80 checkGolden(name, result);
84 public void testGermanCombined() throws Exception {
85 final String name = "de-en.quickdic";
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 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
94 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
95 "--input1Name=chemnitz",
96 "--input1Charset=UTF8",
97 "--input1Format=chemnitz",
99 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
100 "--input2Name=dictcc",
101 "--input2Charset=UTF8",
102 "--input2Format=dictcc",
104 "--print=" + result.getPath() + ".text",
107 checkGolden(name, result);
110 private void checkGolden(final String dictName, final File dictFile)
111 throws IOException, FileNotFoundException {
113 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
116 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
117 final PrintStream out = new PrintStream(new File(dictFile.getName() + ".text"));
120 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
124 void assertFilesEqual(final String expected, final String actual) throws IOException {
125 final String expectedString = FileUtil.readToString(new File(expected));
126 final String actualString = FileUtil.readToString(new File(actual));
127 assertEquals(expectedString, actualString);