]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderTest.java
Fixed handling of non top level languages inside Translations section.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryBuilderTest.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.io.File;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.io.RandomAccessFile;
22
23 import com.hughes.util.FileUtil;
24
25 import junit.framework.TestCase;
26
27 public class DictionaryBuilderTest extends TestCase {
28   
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/";
33
34   public static final String TEST_OUTPUTS = "testdata/outputs/";
35
36   // Chinese
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");
40   }
41
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");
45   }
46
47   
48   // German
49   public void testWiktionary_DE_DE() throws Exception {
50     wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
51         "DE.data", "enwiktionary.german", "German", "it");
52   }
53
54   public void testWiktionary_DE_EN() throws Exception {
55     wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
56         "EN.data", "enwiktionary.english", "German", "it");
57   }
58
59   // Italian
60   public void testWiktionary_IT_IT() throws Exception {
61     wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
62         "IT.data", "enwiktionary.italian", "Italian", "it");
63   }
64
65   public void testWiktionary_IT_EN() throws Exception {
66     wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
67         "EN.data", "enwiktionary.english", "Italian", "it");
68   }
69
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(),
77         "--lang1=" + lang1,
78         "--lang2=EN",
79         "--lang1Stoplist=" + STOPLISTS + stoplist,
80         "--lang2Stoplist=" + STOPLISTS + "en.txt",
81         "--dictInfo=SomeWikiData",
82
83         "--input4=" + WIKISPLIT + data,
84         "--input4Name=" + dictName,
85         "--input4Format=enwiktionary",
86         "--input4LangPattern=" + langPattern,
87         "--input4LangCodePattern=" + langCode,
88         "--input4EnIndex=2",
89         "--input4PageLimit=1000",
90
91         "--print=" + result.getPath() + ".text",
92     });
93     
94     checkGolden(name, result); 
95   }
96
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(),
103         "--lang1=DE",
104         "--lang2=EN",
105         "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
106
107         "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
108         "--input1Name=chemnitz",
109         "--input1Charset=UTF8",
110         "--input1Format=chemnitz",
111
112         "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
113         "--input2Name=dictcc",
114         "--input2Charset=UTF8",
115         "--input2Format=dictcc",
116
117         "--print=" + result.getPath() + ".text",
118     });
119     
120     checkGolden(name, result); 
121   }
122
123   private void checkGolden(final String dictName, final File dictFile)
124       throws IOException, FileNotFoundException {
125     // Check it once:
126     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
127
128     // Check it again.
129     final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
130     final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
131     dict.print(out);
132     out.close();
133     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
134   }
135
136
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);
141   }
142
143   
144 }