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