]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderTest.java
Reworking handling of foreign 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 = "../DictionaryData/testdata/inputs/";
30   public static final String WIKISPLIT = "../DictionaryData/inputs/wikiSplit/";
31   public static final String GOLDENS = "../DictionaryData/testdata/goldens/";
32
33   public static final String TEST_OUTPUTS = "../DictionaryData/testdata/outputs/";
34
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(),
41         "--lang1=IT",
42         "--lang2=EN",
43         "--dictInfo=SomeWikiData",
44
45         "--input4=" + WIKISPLIT + "italian.data",
46         "--input4Name=enwiktionary.italian",
47         "--input4Format=enwiktionary",
48         "--input4LangPattern=Italian",
49         "--input4LangCodePattern=it",
50         "--input4EnIndex=2",
51         "--input4PageLimit=1000",
52
53         "--print=" + result.getPath() + ".text",
54     });
55     
56     checkGolden(name, result); 
57   }
58
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(),
65         "--lang1=IT",
66         "--lang2=EN",
67         "--dictInfo=SomeWikiData",
68
69         "--input3=" + WIKISPLIT + "english.data",
70         "--input3Name=enwiktionary.english",
71         "--input3Format=enwiktionary",
72         "--input3LangPattern=Italian",
73         "--input3LangCodePattern=it",
74         "--input3EnIndex=2",
75         "--input3PageLimit=1000",
76
77         "--print=" + result.getPath() + ".text",
78     });
79     
80     checkGolden(name, result); 
81   }
82
83   
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(),
90         "--lang1=DE",
91         "--lang2=EN",
92         "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
93
94         "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
95         "--input1Name=chemnitz",
96         "--input1Charset=UTF8",
97         "--input1Format=chemnitz",
98
99         "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
100         "--input2Name=dictcc",
101         "--input2Charset=UTF8",
102         "--input2Format=dictcc",
103
104         "--print=" + result.getPath() + ".text",
105     });
106     
107     checkGolden(name, result); 
108   }
109
110   private void checkGolden(final String dictName, final File dictFile)
111       throws IOException, FileNotFoundException {
112     // Check it once:
113     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
114
115     // Check it again.
116     final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
117     final PrintStream out = new PrintStream(new File(dictFile.getName() + ".text"));
118     dict.print(out);
119     out.close();
120     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
121   }
122
123
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);
128   }
129
130   
131 }