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