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 24 package complex.reportdesign; 25 26 import java.io.File; 27 import java.util.ArrayList; 28 29 import com.sun.star.beans.PropertyValue; 30 import com.sun.star.beans.XPropertySet; 31 import com.sun.star.container.XNameAccess; 32 import com.sun.star.frame.XComponentLoader; 33 import com.sun.star.frame.XDesktop; 34 import com.sun.star.frame.XModel; 35 import com.sun.star.frame.XStorable; 36 import com.sun.star.lang.XComponent; 37 import com.sun.star.lang.XMultiServiceFactory; 38 import com.sun.star.sdb.XDocumentDataSource; 39 import com.sun.star.sdb.XOfficeDatabaseDocument; 40 import com.sun.star.sdb.XReportDocumentsSupplier; 41 import com.sun.star.sdb.application.XDatabaseDocumentUI; 42 import com.sun.star.uno.UnoRuntime; 43 import com.sun.star.uno.XInterface; 44 import com.sun.star.util.XCloseable; 45 46 import helper.OfficeProvider; 47 import helper.URLHelper; 48 49 import convwatch.DB; 50 51 import org.junit.After; 52 import org.junit.AfterClass; 53 import org.junit.Before; 54 import org.junit.BeforeClass; 55 import org.junit.Test; 56 import org.openoffice.test.OfficeConnection; 57 import static org.junit.Assert.*; 58 59 60 class PropertySetHelper 61 { 62 XPropertySet m_xPropertySet; PropertySetHelper(Object _aObj)63 public PropertySetHelper(Object _aObj) 64 { 65 m_xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _aObj); 66 } 67 68 /** 69 get a property and don't convert it 70 @param _sName the string name of the property 71 @return the object value of the property without any conversion 72 */ getPropertyValueAsObject(String _sName)73 public Object getPropertyValueAsObject(String _sName) 74 { 75 Object aObject = null; 76 77 if (m_xPropertySet != null) 78 { 79 try 80 { 81 aObject = m_xPropertySet.getPropertyValue(_sName); 82 } 83 catch (com.sun.star.beans.UnknownPropertyException e) 84 { 85 System.out.println("ERROR: UnknownPropertyException caught. '" + _sName + "'"); 86 System.out.println("Message: " + e.getMessage()); 87 } 88 catch (com.sun.star.lang.WrappedTargetException e) 89 { 90 System.out.println("ERROR: WrappedTargetException caught."); 91 System.out.println("Message: " + e.getMessage()); 92 } 93 } 94 return aObject; 95 } 96 } 97 98 class PropertyHelper 99 { 100 /** 101 Create a PropertyValue[] from a ArrayList 102 @param _aArrayList 103 @return a PropertyValue[] 104 */ createPropertyValueArrayFormArrayList(ArrayList _aPropertyList)105 public static PropertyValue[] createPropertyValueArrayFormArrayList(ArrayList _aPropertyList) 106 { 107 // copy the whole PropertyValue List to an PropertyValue Array 108 PropertyValue[] aSaveProperties = null; 109 110 if (_aPropertyList == null) 111 { 112 aSaveProperties = new PropertyValue[0]; 113 } 114 else 115 { 116 if (_aPropertyList.size() > 0) 117 { 118 aSaveProperties = new PropertyValue[_aPropertyList.size()]; 119 for (int i = 0;i<_aPropertyList.size(); i++) 120 { 121 aSaveProperties[i] = (PropertyValue) _aPropertyList.get(i); 122 } 123 } 124 else 125 { 126 aSaveProperties = new PropertyValue[0]; 127 } 128 } 129 return aSaveProperties; 130 } 131 } 132 133 public class ReportDesignerTest 134 { 135 136 String mTestDocumentPath; 137 before()138 @Before public void before() 139 { 140 System.out.println("before"); 141 // String tempdir = System.getProperty("java.io.tmpdir"); 142 // 143 int dummy = 0; 144 // m_xXMultiServiceFactory = getMSF(); 145 } 146 after()147 @After public void after() 148 { 149 System.out.println("after"); 150 } 151 152 // private void checkIfOfficeExists(String _sOfficePathWithTrash) 153 // { 154 // String sOfficePath = ""; 155 // int nIndex = _sOfficePathWithTrash.indexOf("soffice.exe"); 156 // if (nIndex > 0) 157 // { 158 // sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 11); 159 // } 160 // else 161 // { 162 // nIndex = _sOfficePathWithTrash.indexOf("soffice"); 163 // if (nIndex > 0) 164 // { 165 // sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 7); 166 // } 167 // } 168 // 169 // System.out.println(sOfficePath); 170 // File sOffice = new File(sOfficePath); 171 // if (! sOffice.exists()) 172 // { 173 // System.out.println("ERROR: There exists no office installation at given path: '" + sOfficePath + "'"); 174 // System.exit(0); 175 // } 176 // } 177 178 179 private XDesktop m_xDesktop = null; getXDesktop()180 public XDesktop getXDesktop() 181 { 182 183 if (m_xDesktop == null) 184 { 185 try 186 { 187 XInterface xInterface = (XInterface) getMSF().createInstance( "com.sun.star.frame.Desktop" ); 188 m_xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface); 189 assertNotNull("Can't get XDesktop", m_xDesktop); 190 } 191 catch (com.sun.star.uno.Exception e) 192 { 193 System.out.println("ERROR: uno.Exception caught"); 194 System.out.println("Message: " + e.getMessage()); 195 } 196 } 197 return m_xDesktop; 198 } 199 showElements(XNameAccess _xNameAccess)200 private void showElements(XNameAccess _xNameAccess) 201 { 202 if (_xNameAccess != null) 203 { 204 String[] sElementNames = _xNameAccess.getElementNames(); 205 for(int i=0;i<sElementNames.length; i++) 206 { 207 System.out.println("Value: [" + i + "] := " + sElementNames[i]); 208 } 209 } 210 else 211 { 212 System.out.println("Warning: Given object is null."); 213 } 214 } 215 216 217 private OfficeProvider m_aProvider = null; 218 // private void startOffice() 219 // { 220 // // int tempTime = param.getInt("SingleTimeOut"); 221 // param.put("TimeOut", new Integer(300000)); 222 // System.out.println("TimeOut: " + param.getInt("TimeOut")); 223 // System.out.println("ThreadTimeOut: " + param.getInt("ThreadTimeOut")); 224 // 225 // // OfficeProvider aProvider = null; 226 // m_aProvider = new OfficeProvider(); 227 // m_xXMultiServiceFactory = (XMultiServiceFactory) m_aProvider.getManager(param); 228 // param.put("ServiceFactory", m_xXMultiServiceFactory); 229 // } 230 // 231 // private void stopOffice() 232 // { 233 // if (m_aProvider != null) 234 // { 235 // m_aProvider.closeExistingOffice(param, true); 236 // m_aProvider = null; 237 // } 238 // } 239 240 private String m_sMailAddress = null; 241 private String m_sUPDMinor; 242 private String m_sCWS_WORK_STAMP; 243 244 private static final int WRITER = 1; 245 private static final int CALC = 2; 246 firsttest()247 @Test public void firsttest() 248 { 249 // convwatch.GlobalLogWriter.set(log); 250 251 // -------------------- preconditions, try to find an office -------------------- 252 253 // String sAppExecutionCommand = (String) param.get("AppExecutionCommand"); 254 255 String sUser = System.getProperty("user.name"); 256 System.out.println("user.name='" + sUser + "'"); 257 258 String sVCSID = System.getProperty("VCSID"); 259 System.out.println("VCSID='" + sVCSID + "'"); 260 m_sMailAddress = sVCSID + "@openoffice.org"; 261 System.out.println("Assumed mail address: " + m_sMailAddress); 262 263 m_sUPDMinor = System.getProperty("UPDMINOR"); 264 m_sCWS_WORK_STAMP = System.getProperty("CWS_WORK_STAMP"); 265 // createDBEntry(); 266 System.out.println("Current CWS: " + m_sCWS_WORK_STAMP); 267 System.out.println("Current MWS: " + m_sUPDMinor); 268 269 // System.exit(1); 270 271 // sAppExecutionCommand = sAppExecutionCommand.replaceAll( "\\$\\{USERNAME\\}", sUser); 272 // System.out.println("sAppExecutionCommand='" + sAppExecutionCommand + "'"); 273 // 274 // an other way to replace strings 275 // sAppExecutionCommand = utils.replaceAll13(sAppExecutionCommand, "${USERNAME}", sUser); 276 277 // checkIfOfficeExists(sAppExecutionCommand); 278 // param.put("AppExecutionCommand", new String(sAppExecutionCommand)); 279 280 // --------------------------- Start the given Office --------------------------- 281 282 // startOffice(); 283 284 // ------------------------------ Start a test run ------------------------------ 285 286 // String sCurrentDirectory = System.getProperty("user.dir"); 287 // System.out.println("Current Dir: " + sCurrentDirectory); 288 // 289 String sWriterDocument = TestDocument.getUrl("RPTWriterTests.odb"); 290 startTestForFile(sWriterDocument, WRITER); 291 292 String sCalcDocument = TestDocument.getUrl("RPTCalcTests.odb"); 293 startTestForFile(sCalcDocument, CALC); 294 // catch (AssureException e) 295 // { 296 // stopOffice(); 297 // throw new AssureException(e.getMessage()); 298 // } 299 // 300 // ------------------------------ Office shutdown ------------------------------ 301 // stopOffice(); 302 } 303 304 // ----------------------------------------------------------------------------- startTestForFile(String _sDocument, int _nType)305 private void startTestForFile(String _sDocument, int _nType) 306 { 307 FileURL aFileURL = new FileURL(_sDocument); 308 assertTrue("Test File doesn't '" + _sDocument + "'exist.", aFileURL.exists()); 309 310 String sFileURL = _sDocument; // URLHelper.getFileURLFromSystemPath(_sDocument); 311 System.out.println("File URL: " + sFileURL); 312 313 XComponent xDocComponent = loadComponent(sFileURL, getXDesktop(), null); 314 System.out.println("Load done"); 315 assertNotNull("Can't load document ", xDocComponent); 316 317 // context = createUnoService("com.sun.star.sdb.DatabaseContext") 318 // oDataBase = context.getByName("hh") 319 // oDBDoc = oDataBase.DatabaseDocument 320 // 321 // dim args(1) as new com.sun.star.beans.PropertyValue 322 // args(0).Name = "ActiveConnection" 323 // args(0).Value = oDBDoc.getCurrentController().getPropertyValue("ActiveConnection") 324 // reportContainer = oDBDoc.getReportDocuments() 325 // report = reportContainer.loadComponentFromURL("Report40","",0,args) 326 327 try 328 { 329 XInterface x = (XInterface)getMSF().createInstance("com.sun.star.sdb.DatabaseContext"); 330 assertNotNull("can't create instance of com.sun.star.sdb.DatabaseContext", x); 331 System.out.println("createInstance com.sun.star.sdb.DatabaseContext done"); 332 333 XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, x); 334 showElements(xNameAccess); 335 Object aObj = xNameAccess.getByName(sFileURL); 336 // System.out.println("1"); 337 338 // PropertySetHelper aHelper = new PropertySetHelper(aObj); 339 XDocumentDataSource xDataSource = UnoRuntime.queryInterface(XDocumentDataSource.class, aObj); 340 // Object aDatabaseDocmuent = aHelper.getPropertyValueAsObject("DatabaseDocument"); 341 XOfficeDatabaseDocument xOfficeDBDoc = xDataSource.getDatabaseDocument(); 342 343 // XOfficeDatabaseDocument xOfficeDBDoc = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, aDatabaseDocument); 344 assertNotNull("can't access DatabaseDocument", xOfficeDBDoc); 345 // System.out.println("2"); 346 347 XModel xDBSource = UnoRuntime.queryInterface(XModel.class, xOfficeDBDoc); 348 Object aController = xDBSource.getCurrentController(); 349 assertNotNull("Controller of xOfficeDatabaseDocument is empty!", aController); 350 // System.out.println("3"); 351 352 XDatabaseDocumentUI aDBDocUI = UnoRuntime.queryInterface(XDatabaseDocumentUI.class, aController); 353 /* boolean isConnect = */ 354 // TODO: throws an exception in DEV300m78 355 aDBDocUI.connect(); 356 // if (isConnect) 357 // { 358 // System.out.println("true"); 359 // } 360 // else 361 // { 362 // System.out.println("false"); 363 // } 364 // System.out.println("4"); 365 366 // aHelper = new PropertySetHelper(aController); 367 368 // Object aActiveConnectionObj = aHelper.getPropertyValueAsObject("ActiveConnection"); 369 Object aActiveConnectionObj = aDBDocUI.getActiveConnection(); 370 assertNotNull("ActiveConnection is empty", aActiveConnectionObj); 371 // System.out.println("5"); 372 373 XReportDocumentsSupplier xSupplier = UnoRuntime.queryInterface(XReportDocumentsSupplier.class, xOfficeDBDoc); 374 xNameAccess = xSupplier.getReportDocuments(); 375 assertNotNull("xOfficeDatabaseDocument returns no Report Document", xNameAccess); 376 // System.out.println("5"); 377 378 showElements(xNameAccess); 379 380 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); 381 382 PropertyValue aActiveConnection = new PropertyValue(); 383 aActiveConnection.Name = "ActiveConnection"; 384 aActiveConnection.Value = aActiveConnectionObj; 385 aPropertyList.add(aActiveConnection); 386 387 loadAndStoreReports(xNameAccess, aPropertyList, _nType); 388 createDBEntry(_nType); 389 } 390 catch(com.sun.star.uno.Exception e) 391 { 392 fail("ERROR: Exception caught" + e.getMessage()); 393 } 394 395 // String mTestDocumentPath = (String) param.get("TestDocumentPath"); 396 // System.out.println("mTestDocumentPath: '" + mTestDocumentPath + "'"); 397 // // workaround for issue using deprecated "DOCPTH" prop 398 // System.setProperty("DOCPTH", mTestDocumentPath); 399 400 // Close the document 401 closeComponent(xDocComponent); 402 } 403 getDocumentPoolName(int _nType)404 private String getDocumentPoolName(int _nType) 405 { 406 return getFileFormat(_nType); 407 } 408 409 // ----------------------------------------------------------------------------- createDBEntry(int _nType)410 private void createDBEntry(int _nType) 411 { 412 // try to connect the database 413 String sDBConnection = ""; // (String)param.get( convwatch.PropertyName.DB_CONNECTION_STRING ); 414 System.out.println("DBConnection: " + sDBConnection); 415 DB.init(sDBConnection); 416 String sDestinationVersion = m_sCWS_WORK_STAMP; 417 if (sDestinationVersion.length() == 0) 418 { 419 sDestinationVersion = m_sUPDMinor; 420 } 421 String sDestinationName = ""; 422 String sDestinationCreatorType = ""; 423 String sDocumentPoolDir = getOutputPath(_nType); 424 String sDocumentPoolName = getDocumentPoolName(_nType); 425 String sSpecial = ""; 426 427 String sFixRefSubDirectory = "ReportDesign_qa_complex_" + getFileFormat(_nType); 428 // DB.insertinto_documentcompare(sFixRefSubDirectory, "", "fixref", 429 // sDestinationVersion, sDestinationName, sDestinationCreatorType, 430 // sDocumentPoolDir, sDocumentPoolName, m_sMailAddress, 431 // sSpecial); 432 // DB.test(); 433 // System.exit(1); 434 } 435 loadAndStoreReports(XNameAccess _xNameAccess, ArrayList _aPropertyList, int _nType)436 private void loadAndStoreReports(XNameAccess _xNameAccess, ArrayList _aPropertyList, int _nType) 437 { 438 if (_xNameAccess != null) 439 { 440 String[] sElementNames = _xNameAccess.getElementNames(); 441 for(int i=0;i<sElementNames.length; i++) 442 { 443 String sReportName = sElementNames[i]; 444 XComponent xDoc = loadComponent(sReportName, _xNameAccess, _aPropertyList); 445 // print? or store? 446 storeComponent(sReportName, xDoc, _nType); 447 closeComponent(xDoc); 448 } 449 } 450 } 451 getFormatExtension(int _nType)452 private String getFormatExtension(int _nType) 453 { 454 String sExtension; 455 switch(_nType) 456 { 457 case WRITER: 458 sExtension = ".odt"; 459 break; 460 case CALC: 461 sExtension = ".ods"; 462 break; 463 default: 464 sExtension = ".UNKNOWN"; 465 } 466 return sExtension; 467 } getFileFormat(int _nType)468 private String getFileFormat(int _nType) 469 { 470 String sFileType; 471 switch(_nType) 472 { 473 case WRITER: 474 sFileType = "writer8"; 475 break; 476 case CALC: 477 sFileType = "calc8"; 478 break; 479 default: 480 sFileType = "UNKNOWN"; 481 } 482 return sFileType; 483 } 484 getOutputPath(int _nType)485 private String getOutputPath(int _nType) 486 { 487 String sOutputPath = util.utils.getOfficeTemp/*Dir*/(getMSF());// (String)param.get( convwatch.PropertyName.DOC_COMPARATOR_OUTPUT_PATH ); 488 489 if (!sOutputPath.endsWith("/") || // construct the output file name 490 !sOutputPath.endsWith("\\")) 491 { 492 sOutputPath += System.getProperty("file.separator"); 493 } 494 sOutputPath += "tmp_123"; 495 sOutputPath += System.getProperty("file.separator"); 496 497 // sOutputPath += getFileFormat(_nType); 498 // sOutputPath += System.getProperty("file.separator"); 499 500 File aOutputFile = new File(sOutputPath); // create the directory of the given output path 501 aOutputFile.mkdirs(); 502 503 return sOutputPath; 504 } 505 506 /* 507 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH 508 */ storeComponent(String _sName, Object _xComponent, int _nType)509 private void storeComponent(String _sName, Object _xComponent, int _nType) 510 { 511 String sOutputPath = getOutputPath(_nType); 512 513 // add DocumentPoolName 514 sOutputPath += getDocumentPoolName(_nType); 515 sOutputPath += System.getProperty("file.separator"); 516 517 File aOutputFile = new File(sOutputPath); // create the directory of the given output path 518 aOutputFile.mkdirs(); 519 520 sOutputPath += _sName; 521 sOutputPath += getFormatExtension(_nType); 522 523 String sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputPath); 524 525 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); // set some properties for storeAsURL 526 527 PropertyValue aFileFormat = new PropertyValue(); 528 aFileFormat.Name = "FilterName"; 529 aFileFormat.Value = getFileFormat(_nType); 530 aPropertyList.add(aFileFormat); 531 532 PropertyValue aOverwrite = new PropertyValue(); // always overwrite already exist files 533 aOverwrite.Name = "Overwrite"; 534 aOverwrite.Value = Boolean.TRUE; 535 aPropertyList.add(aOverwrite); 536 537 // store the document in an other directory 538 XStorable aStorable = UnoRuntime.queryInterface(XStorable.class, _xComponent); 539 if (aStorable != null) 540 { 541 System.out.println("store document as URL: '" + sOutputURL + "'"); 542 try 543 { 544 aStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList)); 545 } 546 catch (com.sun.star.io.IOException e) 547 { 548 System.out.println("ERROR: Exception caught"); 549 System.out.println("Can't write document URL: '" + sOutputURL + "'"); 550 System.out.println("Message: " + e.getMessage()); 551 } 552 } 553 } 554 loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList)555 private XComponent loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList) 556 { 557 XComponent xDocComponent = null; 558 XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, _xComponent); 559 560 try 561 { 562 PropertyValue[] aLoadProperties = PropertyHelper.createPropertyValueArrayFormArrayList(_aPropertyList); 563 System.out.println("Load component: '" + _sName + "'"); 564 xDocComponent = xComponentLoader.loadComponentFromURL(_sName, "_blank", 0, aLoadProperties); 565 } 566 catch (com.sun.star.io.IOException e) 567 { 568 System.out.println("ERROR: Exception caught"); 569 System.out.println("Can't load document '" + _sName + "'"); 570 System.out.println("Message: " + e.getMessage()); 571 } 572 catch (com.sun.star.lang.IllegalArgumentException e) 573 { 574 System.out.println("ERROR: Exception caught"); 575 System.out.println("Illegal Arguments given to loadComponentFromURL."); 576 System.out.println("Message: " + e.getMessage()); 577 } 578 return xDocComponent; 579 } 580 closeComponent(XComponent _xDoc)581 private void closeComponent(XComponent _xDoc) 582 { 583 // Close the document 584 XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, _xDoc); 585 try 586 { 587 xCloseable.close(true); 588 } 589 catch (com.sun.star.util.CloseVetoException e) 590 { 591 System.out.println("ERROR: CloseVetoException caught"); 592 System.out.println("CloseVetoException occurred Can't close document."); 593 System.out.println("Message: " + e.getMessage()); 594 } 595 } 596 597 getMSF()598 private XMultiServiceFactory getMSF() 599 { 600 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 601 return xMSF1; 602 } 603 604 // setup and close connections setUpConnection()605 @BeforeClass public static void setUpConnection() throws Exception { 606 System.out.println("setUpConnection()"); 607 connection.setUp(); 608 } 609 tearDownConnection()610 @AfterClass public static void tearDownConnection() 611 throws InterruptedException, com.sun.star.uno.Exception 612 { 613 System.out.println("tearDownConnection()"); 614 connection.tearDown(); 615 } 616 617 private static final OfficeConnection connection = new OfficeConnection(); 618 619 } 620