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/";
36 public void testWiktionaryItalianFromItalian() throws Exception {
37 final String name = "wiktionary.it_it.quickdic";
38 final File result = new File(TEST_OUTPUTS + name);
39 System.out.println("Writing to: " + result);
40 DictionaryBuilder.main(new String[] {
41 "--dictOut=" + result.getAbsolutePath(),
44 "--lang1Stoplist=" + STOPLISTS + "it.txt",
45 "--lang2Stoplist=" + STOPLISTS + "en.txt",
46 "--dictInfo=SomeWikiData",
48 "--input4=" + WIKISPLIT + "IT.data",
49 "--input4Name=enwiktionary.italian",
50 "--input4Format=enwiktionary",
51 "--input4LangPattern=Italian",
52 "--input4LangCodePattern=it",
54 "--input4PageLimit=1000",
56 "--print=" + result.getPath() + ".text",
59 checkGolden(name, result);
62 public void testWiktionaryItalianFromEnglish() throws Exception {
63 final String name = "wiktionary.it_en.quickdic";
64 final File result = new File(TEST_OUTPUTS + name);
65 System.out.println("Writing to: " + result);
66 DictionaryBuilder.main(new String[] {
67 "--dictOut=" + result.getAbsolutePath(),
70 "--lang1Stoplist=" + STOPLISTS + "it.txt",
71 "--lang2Stoplist=" + STOPLISTS + "en.txt",
72 "--dictInfo=SomeWikiData",
74 "--input3=" + WIKISPLIT + "EN.data",
75 "--input3Name=enwiktionary.english",
76 "--input3Format=enwiktionary",
77 "--input3LangPattern=Italian",
78 "--input3LangCodePattern=it",
80 "--input3PageLimit=1000",
82 "--print=" + result.getPath() + ".text",
85 checkGolden(name, result);
89 public void testGermanCombined() throws Exception {
90 final String name = "de-en.quickdic";
91 final File result = new File(TEST_OUTPUTS + name);
92 System.out.println("Writing to: " + result);
93 DictionaryBuilder.main(new String[] {
94 "--dictOut=" + result.getAbsolutePath(),
97 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
99 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
100 "--input1Name=chemnitz",
101 "--input1Charset=UTF8",
102 "--input1Format=chemnitz",
104 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
105 "--input2Name=dictcc",
106 "--input2Charset=UTF8",
107 "--input2Format=dictcc",
109 "--print=" + result.getPath() + ".text",
112 checkGolden(name, result);
115 private void checkGolden(final String dictName, final File dictFile)
116 throws IOException, FileNotFoundException {
118 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
121 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
122 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
125 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
129 void assertFilesEqual(final String expected, final String actual) throws IOException {
130 final String expectedString = FileUtil.readToString(new File(expected));
131 final String actualString = FileUtil.readToString(new File(actual));
132 assertEquals(expectedString, actualString);