]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderTest.java
Initialism, changes in regex matching.
[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   // German
48   public void testWiktionary_DE_DE() throws Exception {
49     wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
50         "DE.data", "enwiktionary.german", "German", "it");
51   }
52
53   public void testWiktionary_DE_EN() throws Exception {
54     wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
55         "EN.data", "enwiktionary.english", "German", "it");
56   }
57
58   // Italian
59   public void testWiktionary_IT_IT() throws Exception {
60     wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
61         "IT.data", "enwiktionary.italian", "Italian", "it");
62   }
63
64   public void testWiktionary_IT_EN() throws Exception {
65     wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
66         "EN.data", "enwiktionary.english", "Italian", "it");
67   }
68
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(),
76         "--lang1=" + lang1,
77         "--lang2=EN",
78         "--lang1Stoplist=" + STOPLISTS + stoplist,
79         "--lang2Stoplist=" + STOPLISTS + "en.txt",
80         "--dictInfo=SomeWikiData",
81
82         "--input4=" + WIKISPLIT + data,
83         "--input4Name=" + dictName,
84         "--input4Format=enwiktionary",
85         "--input4LangPattern=" + langPattern,
86         "--input4LangCodePattern=" + langCode,
87         "--input4EnIndex=2",
88         "--input4PageLimit=1000",
89
90         "--print=" + result.getPath() + ".text",
91     });
92     
93     checkGolden(name, result); 
94   }
95
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(),
102         "--lang1=DE",
103         "--lang2=EN",
104         "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
105
106         "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
107         "--input1Name=chemnitz",
108         "--input1Charset=UTF8",
109         "--input1Format=chemnitz",
110
111         "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
112         "--input2Name=dictcc",
113         "--input2Charset=UTF8",
114         "--input2Format=dictcc",
115
116         "--print=" + result.getPath() + ".text",
117     });
118     
119     checkGolden(name, result); 
120   }
121
122   private void checkGolden(final String dictName, final File dictFile)
123       throws IOException, FileNotFoundException {
124     // Check it once:
125     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
126
127     // Check it again.
128     final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
129     final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
130     dict.print(out);
131     out.close();
132     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
133   }
134
135
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);
140   }
141
142   
143 }