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