xref: /AOO41X/main/linguistic/qa/complex/linguistic/HangulHanjaConversion.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.linguistic;
28 
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.container.ElementExistException;
31 import com.sun.star.container.NoSuchElementException;
32 import com.sun.star.container.XNameContainer;
33 import com.sun.star.i18n.TextConversionOption;
34 import com.sun.star.lang.IllegalArgumentException;
35 import com.sun.star.lang.Locale;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XEventListener;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.linguistic2.ConversionDictionaryType;
40 import com.sun.star.linguistic2.ConversionDirection;
41 import com.sun.star.linguistic2.XConversionDictionary;
42 import com.sun.star.linguistic2.XConversionDictionaryList;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.table.XCell;
46 
47 import com.sun.star.uno.UnoRuntime;
48 
49 
50 
51 import util.DesktopTools;
52 
53 // import org.junit.After;
54 import org.junit.AfterClass;
55 import org.junit.Before;
56 import org.junit.BeforeClass;
57 import org.junit.Test;
58 import org.openoffice.test.OfficeConnection;
59 import static org.junit.Assert.*;
60 
61 public class HangulHanjaConversion {
62     XMultiServiceFactory xMSF = null;
63     boolean disposed = false;
64     Locale aLocale = new Locale("ko", "KR", "");
65     short dictType = ConversionDictionaryType.HANGUL_HANJA;
66 
67 //    public String[] getTestMethodNames() {
68 //        return new String[] { "ConversionDictionaryList" };
69 //    }
70 
71     @Before public void before() {
72         xMSF = getMSF();
73     }
74 
75     @Test public void ConversionDictionaryList() {
76         Object ConversionDictionaryList = null;
77 
78         try {
79             ConversionDictionaryList = xMSF.createInstance(
80                                                "com.sun.star.linguistic2.ConversionDictionaryList");
81         } catch (com.sun.star.uno.Exception e) {
82             fail("Couldn't create ConversionDictionaryList");
83         }
84 
85         if (ConversionDictionaryList == null) {
86             fail("Couldn't create ConversionDictionaryList");
87         }
88 
89         boolean bList = checkXConversionDictionaryList(
90                                 ConversionDictionaryList);
91         assertTrue("XConversionDictionaryList doesnt work as expected", bList);
92     }
93 
94     private boolean checkXConversionDictionaryList(Object list) {
95         boolean res = true;
96         XConversionDictionaryList xCList = UnoRuntime.queryInterface(XConversionDictionaryList.class, list);
97         XConversionDictionary xDict = null;
98 
99         try {
100             xDict = xCList.addNewDictionary("addNewDictionary", aLocale,
101                                             dictType);
102         } catch (com.sun.star.lang.NoSupportException e) {
103             res = false;
104             fail("Couldn't add Dictionary");
105         } catch (com.sun.star.container.ElementExistException e) {
106             res = false;
107             fail("Couldn't add Dictionary");
108         }
109 
110         try {
111             xCList.addNewDictionary("addNewDictionary", aLocale, dictType);
112             res = false;
113             fail("wrong exception while adding Dictionary again");
114         } catch (com.sun.star.lang.NoSupportException e) {
115             res = false;
116             fail("wrong exception while adding Dictionary again");
117         } catch (com.sun.star.container.ElementExistException e) {
118         }
119 
120         boolean localRes = checkNameContainer(xCList.getDictionaryContainer());
121         res &= localRes;
122         assertTrue("getDictionaryContainer didn't work as expected", localRes);
123 
124         String FileToLoad = TestDocument.getUrl("hangulhanja.sxc");
125         // String FileToLoad = util.utils.getFullTestURL();
126 
127 XComponent xDoc = DesktopTools.loadDoc(xMSF, FileToLoad,
128                                                new PropertyValue[] {  });
129         XSpreadsheet xSheet = getSheet(xDoc);
130         boolean done = false;
131         int counter = 0;
132         int numberOfWords = 0;
133         String wordToCheck = "";
134         String expectedConversion = "";
135 
136         while (!done) {
137             String[] HangulHanja = getLeftAndRight(counter, xSheet);
138             done = (HangulHanja[0].equals(""));
139             counter++;
140 
141             if (!done) {
142                 numberOfWords++;
143 
144                 try {
145                     xDict.addEntry(HangulHanja[0], HangulHanja[1]);
146                     wordToCheck += HangulHanja[0];
147                     expectedConversion += HangulHanja[1];
148                 } catch (com.sun.star.lang.IllegalArgumentException e) {
149                     e.printStackTrace();
150                     res = false;
151                     fail("Exception while checking adding entry");
152                 } catch (com.sun.star.container.ElementExistException e) {
153                     //ignored
154                 }
155             }
156         }
157 
158         try {
159             xDict.addEntry(wordToCheck, expectedConversion);
160         } catch (com.sun.star.lang.IllegalArgumentException e) {
161             e.printStackTrace();
162             res = false;
163             fail("Exception while checking adding entry");
164         } catch (com.sun.star.container.ElementExistException e) {
165             //ignored
166         }
167 
168         localRes = xCList.queryMaxCharCount(aLocale, dictType,
169                                             ConversionDirection.FROM_LEFT) == 42;
170         res &= localRes;
171         assertTrue("queryMaxCharCount returned the wrong value", localRes);
172 
173         String[] conversion = null;
174 
175         try {
176             conversion = xCList.queryConversions(wordToCheck, 0,
177                                                  wordToCheck.length(), aLocale,
178                                                  dictType,
179                                                  ConversionDirection.FROM_LEFT,
180                                                  TextConversionOption.NONE);
181         } catch (com.sun.star.lang.IllegalArgumentException e) {
182             res = false;
183             fail("Exception while calling queryConversions");
184         } catch (com.sun.star.lang.NoSupportException e) {
185             res = false;
186             fail("Exception while calling queryConversions");
187         }
188 
189         localRes = conversion[0].equals(expectedConversion);
190         res &= localRes;
191         assertTrue("queryConversions didn't work as expected", localRes);
192 
193         try {
194             xCList.getDictionaryContainer().removeByName("addNewDictionary");
195         } catch (com.sun.star.container.NoSuchElementException e) {
196             res = false;
197             fail("exception while removing Dictionary again");
198         } catch (com.sun.star.lang.WrappedTargetException e) {
199             res = false;
200             fail("exception while removing Dictionary again");
201         }
202 
203         localRes = !xCList.getDictionaryContainer()
204                           .hasByName("addNewDictionary");
205         res &= localRes;
206         assertTrue("Dictionary hasn't been removed properly", localRes);
207 
208         XComponent dicList = UnoRuntime.queryInterface(XComponent.class, xCList);
209         XEventListener listen = new EventListener();
210         dicList.addEventListener(listen);
211         dicList.dispose();
212         assertTrue("dispose didn't work", disposed);
213         dicList.removeEventListener(listen);
214 
215         DesktopTools.closeDoc(xDoc);
216 
217         return res;
218     }
219 
220     private boolean checkNameContainer(XNameContainer xNC) {
221         boolean res = true;
222 
223         try {
224             res &= xNC.hasByName("addNewDictionary");
225 
226             XConversionDictionary myCD = new ConversionDictionary();
227             xNC.insertByName("insertByName", myCD);
228             res &= xNC.hasByName("insertByName");
229             xNC.removeByName("insertByName");
230             res &= !(xNC.hasByName("insertByName"));
231         } catch (com.sun.star.lang.IllegalArgumentException e) {
232             res = false;
233             e.printStackTrace();
234         } catch (com.sun.star.container.NoSuchElementException e) {
235             res = false;
236             e.printStackTrace();
237         } catch (com.sun.star.container.ElementExistException e) {
238             res = false;
239             e.printStackTrace();
240         } catch (com.sun.star.lang.WrappedTargetException e) {
241             res = false;
242             e.printStackTrace();
243         }
244 
245         return res;
246     }
247 
248     private XSpreadsheet getSheet(XComponent xDoc) {
249         XSpreadsheetDocument xSheetDoc = UnoRuntime.queryInterface(XSpreadsheetDocument.class, xDoc);
250         XSpreadsheet xSheet = null;
251 
252         try {
253             xSheet = UnoRuntime.queryInterface(XSpreadsheet.class, xSheetDoc.getSheets().getByName(xSheetDoc.getSheets().getElementNames()[0]));
254         } catch (com.sun.star.container.NoSuchElementException e) {
255             System.out.println("Couldn't get sheet");
256             e.printStackTrace();
257         } catch (com.sun.star.lang.WrappedTargetException e) {
258             System.out.println("Couldn't get sheet");
259             e.printStackTrace();
260         }
261 
262         return xSheet;
263     }
264 
265     private String[] getLeftAndRight(int counter, XSpreadsheet xSpreadsheet) {
266         String[] re = new String[2];
267         re[0] = getCell(0, counter, xSpreadsheet).getFormula().trim();
268         re[1] = getCell(1, counter, xSpreadsheet).getFormula().trim();
269 
270         return re;
271     }
272 
273     private XCell getCell(int x, int y, XSpreadsheet xSpreadsheet) {
274         XCell re = null;
275 
276         try {
277             re = xSpreadsheet.getCellByPosition(x, y);
278         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
279             System.out.println("Couldn't get word");
280             e.printStackTrace();
281         }
282 
283         return re;
284     }
285 
286     private class ConversionDictionary implements XConversionDictionary {
287         boolean active = false;
288 
289         public void addEntry(String str, String str1)
290                       throws IllegalArgumentException, ElementExistException {
291         }
292 
293         public void clear() {
294         }
295 
296         public String[] getConversionEntries(ConversionDirection conversionDirection) {
297             return new String[] { "getConversionEntries" };
298         }
299 
300         public short getConversionType() {
301             return ConversionDictionaryType.HANGUL_HANJA;
302         }
303 
304         public String[] getConversions(String str, int param, int param2,
305                                        ConversionDirection conversionDirection,
306                                        int param4)
307                                 throws IllegalArgumentException {
308             return new String[] { "getConversion" };
309         }
310 
311         public com.sun.star.lang.Locale getLocale() {
312             return new Locale("de", "DE", "");
313         }
314 
315         public short getMaxCharCount(ConversionDirection conversionDirection) {
316             return (short) 2;
317         }
318 
319         public String getName() {
320             return "insertByName";
321         }
322 
323         public boolean isActive() {
324             return active;
325         }
326 
327         public void removeEntry(String str, String str1)
328                          throws NoSuchElementException {
329         }
330 
331         public void setActive(boolean param) {
332             active = param;
333         }
334     }
335 
336     private class EventListener implements XEventListener {
337         public void disposing(com.sun.star.lang.EventObject eventObject) {
338             disposed = true;
339         }
340     }
341 
342     private XMultiServiceFactory getMSF()
343     {
344         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
345         return xMSF1;
346     }
347 
348     // setup and close connections
349     @BeforeClass public static void setUpConnection() throws Exception {
350         System.out.println("setUpConnection()");
351         connection.setUp();
352     }
353 
354     @AfterClass public static void tearDownConnection()
355         throws InterruptedException, com.sun.star.uno.Exception
356     {
357         System.out.println("tearDownConnection()");
358         connection.tearDown();
359     }
360 
361     private static final OfficeConnection connection = new OfficeConnection();
362 
363 }
364