2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 import java
.util
.ArrayList
;
24 import com
.sun
.star
.beans
.PropertyValue
;
25 import com
.sun
.star
.beans
.XPropertySet
;
26 import com
.sun
.star
.container
.XNameAccess
;
27 import com
.sun
.star
.frame
.XComponentLoader
;
28 import com
.sun
.star
.frame
.XDesktop
;
29 import com
.sun
.star
.frame
.XModel
;
30 import com
.sun
.star
.frame
.XStorable
;
31 import com
.sun
.star
.lang
.XComponent
;
32 import com
.sun
.star
.lang
.XMultiServiceFactory
;
33 import com
.sun
.star
.sdb
.XDocumentDataSource
;
34 import com
.sun
.star
.sdb
.XOfficeDatabaseDocument
;
35 import com
.sun
.star
.sdb
.XReportDocumentsSupplier
;
36 import com
.sun
.star
.sdb
.application
.XDatabaseDocumentUI
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.uno
.XInterface
;
39 import com
.sun
.star
.util
.XCloseable
;
40 import com
.sun
.star
.lang
.XServiceInfo
;
42 // import util.BasicMacroTools;
43 // import util.DesktopTools;
45 import complexlib
.ComplexTestCase
;
46 import helper
.OfficeProvider
;
47 import helper
.URLHelper
;
48 import helper
.OfficeWatcher
;
50 // import convwatch.DB;
52 // import java.util.Date;
53 // import java.text.SimpleDateFormat;
54 // import java.text.ParsePosition;
55 // import java.sql.Time;
57 // import java.io.BufferedReader;
58 // import java.io.File;
59 // import java.io.FileReader;
60 // import java.io.IOException;
61 // import java.io.FilenameFilter;
63 // import java.util.Vector;
65 // import helper.AppProvider;
66 // import java.text.DecimalFormat;
67 // import util.DynamicClassLoader;
68 // import java.util.StringTokenizer;
73 class PropertySetHelper
75 XPropertySet m_xPropertySet
;
76 public PropertySetHelper(Object _aObj
)
78 m_xPropertySet
= UnoRuntime
.queryInterface(XPropertySet
.class, _aObj
);
82 get a property and don't convert it
83 @param _sName the string name of the property
84 @return the object value of the property without any conversion
86 public Object
getPropertyValueAsObject(String _sName
)
88 Object aObject
= null;
90 if (m_xPropertySet
!= null)
94 aObject
= m_xPropertySet
.getPropertyValue(_sName
);
96 catch (com
.sun
.star
.beans
.UnknownPropertyException e
)
98 System
.out
.println("ERROR: UnknownPropertyException caught. '" + _sName
+ "'");
99 System
.out
.println("Message: " + e
.getMessage());
101 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
103 System
.out
.println("ERROR: WrappedTargetException caught.");
104 System
.out
.println("Message: " + e
.getMessage());
114 Create a PropertyValue[] from a ArrayList
115 @param _aPropertyList
116 @return a PropertyValue[]
118 public static PropertyValue
[] createPropertyValueArrayFormArrayList(ArrayList
<PropertyValue
> _aPropertyList
)
120 // copy the whole PropertyValue List to an PropertyValue Array
121 PropertyValue
[] aSaveProperties
= null;
123 if (_aPropertyList
== null)
125 aSaveProperties
= new PropertyValue
[0];
129 if (_aPropertyList
.size() > 0)
131 aSaveProperties
= new PropertyValue
[_aPropertyList
.size()];
132 for (int i
= 0;i
<_aPropertyList
.size(); i
++)
134 aSaveProperties
[i
] = _aPropertyList
.get(i
);
139 aSaveProperties
= new PropertyValue
[0];
142 return aSaveProperties
;
146 public class ReportDesignerTest
extends ComplexTestCase
{
148 String mTestDocumentPath
;
151 * This method returns a list of Strings, each string must be a function name in this class.
154 public String
[] getTestMethodNames()
156 return new String
[] {"ReportDesignTest"}; // MAIN
159 private void checkIfOfficeExists(String _sOfficePathWithTrash
)
161 String sOfficePath
= "";
162 int nIndex
= _sOfficePathWithTrash
.indexOf("soffice.exe");
165 sOfficePath
= _sOfficePathWithTrash
.substring(0, nIndex
+ 11);
169 nIndex
= _sOfficePathWithTrash
.indexOf("soffice");
172 sOfficePath
= _sOfficePathWithTrash
.substring(0, nIndex
+ 7);
176 // if (sOfficePath.startsWith("\"") ||
177 // sOfficePath.startsWith("'"))
179 // sOfficePath = sOfficePath.substring(1);
181 sOfficePath
= helper
.StringHelper
.removeQuoteIfExists(sOfficePath
);
183 log
.println(sOfficePath
);
184 File sOffice
= new File(sOfficePath
);
185 if (! sOffice
.exists())
187 log
.println("ERROR: There exists no office installation at given path: '" + sOfficePath
+ "'");
193 private static XDesktop m_xDesktop
= null;
194 public static XDesktop
getXDesktop()
197 if (m_xDesktop
== null)
201 XInterface xInterface
= (XInterface
) m_xXMultiServiceFactory
.createInstance( "com.sun.star.frame.Desktop" );
202 m_xDesktop
= UnoRuntime
.queryInterface(XDesktop
.class, xInterface
);
204 catch (com
.sun
.star
.uno
.Exception e
)
206 log
.println("ERROR: uno.Exception caught");
207 log
.println("Message: " + e
.getMessage());
213 private void showElements(XNameAccess _xNameAccess
)
215 if (_xNameAccess
!= null)
217 String
[] sElementNames
= _xNameAccess
.getElementNames();
218 for(int i
=0;i
<sElementNames
.length
; i
++)
220 System
.out
.println("Value: [" + i
+ "] := " + sElementNames
[i
]);
225 System
.out
.println("Warning: Given object is null.");
230 private OfficeProvider m_aProvider
= null;
231 private static XMultiServiceFactory m_xXMultiServiceFactory
= null;
232 private void startOffice()
234 // int tempTime = param.getInt("SingleTimeOut");
235 param
.put("TimeOut", new Integer(300000));
236 System
.out
.println("TimeOut: " + param
.getInt("TimeOut"));
237 System
.out
.println("ThreadTimeOut: " + param
.getInt("ThreadTimeOut"));
239 // OfficeProvider aProvider = null;
240 m_aProvider
= new OfficeProvider();
241 m_xXMultiServiceFactory
= (XMultiServiceFactory
) m_aProvider
.getManager(param
);
242 param
.put("ServiceFactory", m_xXMultiServiceFactory
);
245 private void stopOffice()
247 if (m_aProvider
!= null)
249 m_aProvider
.closeExistingOffice(param
, true);
252 TimeHelper
.waitInSeconds(2, "Give close Office some time.");
255 private String m_sMailAddress
= null;
256 private String m_sParentDistinct
= null;
258 // private String m_sUPDMinor;
259 // private String m_sCWS_WORK_STAMP;
261 private static String m_sSourceVersion
;
262 private static String m_sDestinationVersion
;
263 private static String m_sSourceName
;
264 private static String m_sDestinationName
;
267 * This is the main test Function of current ReportDesignerTest
269 public void ReportDesignTest()
271 convwatch
.GlobalLogWriter
.set(log
);
273 GlobalLogWriter
.get().println("Set office watcher");
274 OfficeWatcher aWatcher
= (OfficeWatcher
)param
.get("Watcher");
275 GlobalLogWriter
.get().setWatcher(aWatcher
);
280 // -------------------- preconditions, try to find an office --------------------
282 String sAppExecutionCommand
= (String
) param
.get("AppExecutionCommand");
283 log
.println("sAppExecutionCommand='" + sAppExecutionCommand
+ "'");
285 String sUser
= System
.getProperty("user.name");
286 log
.println("user.name='" + sUser
+ "'");
288 // String sVCSID = System.getProperty("VCSID");
289 // log.println("VCSID='" + sVCSID + "'");
290 // m_sMailAddress = sVCSID + "@openoffice.org";
291 m_sMailAddress
= System
.getProperty("MailAddress");
292 log
.println("Assumed mail address: " + m_sMailAddress
);
294 m_sParentDistinct
= System
.getProperty("ParentDistinct");
296 m_sSourceVersion
= System
.getProperty("SourceVersion");
297 m_sSourceName
= System
.getProperty("SourceName");
298 m_sDestinationVersion
= System
.getProperty("DestinationVersion");
299 m_sDestinationName
= System
.getProperty("DestinationName");
301 // log.println("Current CWS: " + m_sCWS_WORK_STAMP);
302 // log.println("Current MWS: " + m_sUPDMinor);
304 if (m_sSourceVersion
== null)
306 System
.out
.println("Error, Sourceversion is null.");
310 sAppExecutionCommand
= sAppExecutionCommand
.replaceAll( "\\$\\{USERNAME\\}", sUser
);
311 log
.println("sAppExecutionCommand='" + sAppExecutionCommand
+ "'");
313 // an other way to replace strings
314 // sAppExecutionCommand = utils.replaceAll13(sAppExecutionCommand, "${USERNAME}", sUser);
316 checkIfOfficeExists(sAppExecutionCommand
);
317 param
.put("AppExecutionCommand", new String(sAppExecutionCommand
));
321 // --------------------------- Start the given Office ---------------------------
325 // ------------------------------ Start a test run ------------------------------
327 // String sCurrentDirectory = System.getProperty("user.dir");
328 // log.println("Current Dir: " + sCurrentDirectory);
329 String sDocument
= (String
) param
.get(convwatch
.PropertyName
.DOC_COMPARATOR_INPUT_PATH
);
330 sDocument
= helper
.StringHelper
.removeQuoteIfExists( sDocument
);
331 startTestForFile(sDocument
);
332 // if (sDocument.toLowerCase().indexOf("writer") >= 0)
334 // startTestForFile(sDocument, WRITER);
336 // else if (sDocument.toLowerCase().indexOf("calc") >= 0)
338 // startTestForFile(sDocument, CALC);
342 // assure("Can't identify the document no 'writer' nor 'calc' in it's name given.", false);
345 catch (AssureException e
)
348 throw new AssureException(e
.getMessage());
351 // ------------------------------ Office shutdown ------------------------------
355 // -----------------------------------------------------------------------------
356 private void startTestForFile(String _sDocument
/*, int _nType*/)
358 File aFile
= new File(_sDocument
);
359 assure("Test File '" + _sDocument
+ "' doesn't exist.", aFile
.exists());
361 String sFileURL
= URLHelper
.getFileURLFromSystemPath(_sDocument
);
362 log
.println("File URL: " + sFileURL
);
364 XComponent xDocComponent
= loadComponent(sFileURL
, getXDesktop(), null);
365 log
.println("Load done");
366 // context = createUnoService("com.sun.star.sdb.DatabaseContext")
367 // oDataBase = context.getByName("hh")
368 // oDBDoc = oDataBase.DatabaseDocument
370 // dim args(1) as new com.sun.star.beans.PropertyValue
371 // args(0).Name = "ActiveConnection"
372 // args(0).Value = oDBDoc.getCurrentController().getPropertyValue("ActiveConnection")
373 // reportContainer = oDBDoc.getReportDocuments()
374 // report = reportContainer.loadComponentFromURL("Report40","",0,args)
378 XInterface x
= (XInterface
)m_xXMultiServiceFactory
.createInstance("com.sun.star.sdb.DatabaseContext");
379 assure("can't create instance of com.sun.star.sdb.DatabaseContext", x
!= null);
380 log
.println("createInstance com.sun.star.sdb.DatabaseContext done");
382 XNameAccess xNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, x
);
383 showElements(xNameAccess
);
384 Object aObj
= xNameAccess
.getByName(sFileURL
);
387 // PropertySetHelper aHelper = new PropertySetHelper(aObj);
388 XDocumentDataSource xDataSource
= UnoRuntime
.queryInterface(XDocumentDataSource
.class, aObj
);
389 // Object aDatabaseDocmuent = aHelper.getPropertyValueAsObject("DatabaseDocument");
390 XOfficeDatabaseDocument xOfficeDBDoc
= xDataSource
.getDatabaseDocument();
392 // XOfficeDatabaseDocument xOfficeDBDoc = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, aDatabaseDocument);
393 assure("can't access DatabaseDocument", xOfficeDBDoc
!= null);
396 XModel xDBSource
= UnoRuntime
.queryInterface(XModel
.class, xOfficeDBDoc
);
397 Object aController
= xDBSource
.getCurrentController();
398 assure("Controller of xOfficeDatabaseDocument is empty!", aController
!= null);
401 XDatabaseDocumentUI aDBDocUI
= UnoRuntime
.queryInterface(XDatabaseDocumentUI
.class, aController
);
403 // if (aDBDocUI.isConnected())
405 // System.out.println("true");
409 // System.out.println("false");
413 // aHelper = new PropertySetHelper(aController);
415 // Object aActiveConnectionObj = aHelper.getPropertyValueAsObject("ActiveConnection");
416 Object aActiveConnectionObj
= aDBDocUI
.getActiveConnection();
417 assure("ActiveConnection is empty", aActiveConnectionObj
!= null);
420 XReportDocumentsSupplier xSupplier
= UnoRuntime
.queryInterface(XReportDocumentsSupplier
.class, xOfficeDBDoc
);
421 xNameAccess
= xSupplier
.getReportDocuments();
422 assure("xOfficeDatabaseDocument returns no Report Document", xNameAccess
!= null);
425 showElements(xNameAccess
);
427 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
429 PropertyValue aActiveConnection
= new PropertyValue();
430 aActiveConnection
.Name
= "ActiveConnection";
431 aActiveConnection
.Value
= aActiveConnectionObj
;
432 aPropertyList
.add(aActiveConnection
);
434 loadAndStoreReports(xNameAccess
, aPropertyList
/*, _nType*/ );
435 createDBEntry(/*_nType*/);
437 catch(com
.sun
.star
.uno
.Exception e
)
439 log
.println("ERROR: Exception caught");
440 log
.println("Message: " + e
.getMessage());
443 // String mTestDocumentPath = (String) param.get("TestDocumentPath");
444 // System.out.println("mTestDocumentPath: '" + mTestDocumentPath + "'");
445 // // workaround for issue using deprecated "DOCPTH" prop
446 // System.setProperty("DOCPTH", mTestDocumentPath);
448 // Close the document
449 closeComponent(xDocComponent
);
452 private String
getDocumentPoolName(/*int _nType*/)
454 return "AutogenReportDesignTest";
455 // return getFileFormat(_nType);
458 // -----------------------------------------------------------------------------
459 private void createDBEntry(/*int _nType*/)
461 // try to connect the database
462 String sDBConnection
= (String
)param
.get( convwatch
.PropertyName
.DB_CONNECTION_STRING
);
463 log
.println("DBConnection: " + sDBConnection
);
464 DB
.init(sDBConnection
);
466 String sSourceVersion
= m_sSourceVersion
;
467 // String sSourceVersion = sFixRefSubDirectory;
468 String sSourceName
= m_sSourceName
;
469 // String sSourceCreatorType = "fixref";
470 String sSourceCreatorType
= "";
471 // if (sDestinationVersion.length() == 0)
473 // sDestinationVersion = m_sUPDMinor;
475 String sDestinationName
= m_sDestinationName
;
476 String sDestinationCreatorType
= "";
477 String sDocumentPoolDir
= getOutputPath(/*_nType*/);
478 String sDocumentPoolName
= getDocumentPoolName(/*_nType*/);
479 String sSpecial
= "";
481 DB
.insertinto_documentcompare(sSourceVersion
, sSourceName
, sSourceCreatorType
,
482 m_sDestinationVersion
, sDestinationName
, sDestinationCreatorType
,
483 sDocumentPoolDir
, sDocumentPoolName
, m_sMailAddress
,
484 sSpecial
, m_sParentDistinct
);
485 TimeHelper
.waitInSeconds(1, "wait for DB.");
490 private void loadAndStoreReports(XNameAccess _xNameAccess
, ArrayList
<PropertyValue
> _aPropertyList
/*, int _nType*/ )
492 if (_xNameAccess
!= null)
494 String
[] sElementNames
= _xNameAccess
.getElementNames();
495 for(int i
=0;i
<sElementNames
.length
; i
++)
497 String sReportName
= sElementNames
[i
];
498 XComponent xDoc
= loadComponent(sReportName
, _xNameAccess
, _aPropertyList
);
500 storeComponent(sReportName
, xDoc
/*, _nType*/);
501 closeComponent(xDoc
);
506 private String
getFormatExtension(Object _xComponent
/* int _nType*/ )
509 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, _xComponent
);
510 if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
515 else if (xServiceInfo
.supportsService("com.sun.star.text.TextDocument"))
522 sExtension
= ".UNKNOWN";
530 // sExtension = ".odt";
533 // sExtension = ".ods";
536 // sExtension = ".UNKNOWN";
538 // return sExtension;
540 // private String getFileFormat(int _nType)
546 // sFileType = "writer8";
549 // sFileType = "calc8";
552 // sFileType = "UNKNOWN";
557 private String m_sOutputPath
= null;
559 private String
getOutputPath(/*int _nType*/)
561 if (m_sOutputPath
== null)
563 String sOutputPath
= (String
)param
.get( convwatch
.PropertyName
.DOC_COMPARATOR_OUTPUT_PATH
);
564 sOutputPath
= helper
.StringHelper
.removeQuoteIfExists(sOutputPath
);
566 if (!sOutputPath
.endsWith("/") || // construct the output file name
567 !sOutputPath
.endsWith("\\"))
569 sOutputPath
+= System
.getProperty("file.separator");
571 // sOutputPath += "tmp_123";
572 sOutputPath
+= DateHelper
.getDateTimeForFilename();
573 sOutputPath
+= System
.getProperty("file.separator");
575 // sOutputPath += getFileFormat(_nType);
576 // sOutputPath += System.getProperty("file.separator");
578 File aOutputFile
= new File(sOutputPath
); // create the directory of the given output path
579 aOutputFile
.mkdirs();
580 m_sOutputPath
= sOutputPath
;
582 return m_sOutputPath
;
586 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH
588 private void storeComponent(String _sName
, Object _xComponent
/*, int _nType*/ )
590 String sOutputPath
= getOutputPath(/*_nType*/);
592 // add DocumentPoolName
593 sOutputPath
+= getDocumentPoolName(/*_nType*/);
594 sOutputPath
+= System
.getProperty("file.separator");
596 File aOutputFile
= new File(sOutputPath
); // create the directory of the given output path
597 aOutputFile
.mkdirs();
599 sOutputPath
+= _sName
;
600 sOutputPath
+= getFormatExtension(_xComponent
/*_nType*/);
602 String sOutputURL
= URLHelper
.getFileURLFromSystemPath(sOutputPath
);
604 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>(); // set some properties for storeAsURL
606 // PropertyValue aFileFormat = new PropertyValue();
607 // aFileFormat.Name = "FilterName";
608 // aFileFormat.Value = getFileFormat(_nType);
609 // aPropertyList.add(aFileFormat);
611 PropertyValue aOverwrite
= new PropertyValue(); // always overwrite already exist files
612 aOverwrite
.Name
= "Overwrite";
613 aOverwrite
.Value
= Boolean
.TRUE
;
614 aPropertyList
.add(aOverwrite
);
616 // store the document in an other directory
617 XStorable aStorable
= UnoRuntime
.queryInterface( XStorable
.class, _xComponent
);
618 if (aStorable
!= null)
620 log
.println("store document as URL: '" + sOutputURL
+ "'");
623 aStorable
.storeAsURL(sOutputURL
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
625 catch (com
.sun
.star
.io
.IOException e
)
627 log
.println("ERROR: Exception caught");
628 log
.println("Can't write document URL: '" + sOutputURL
+ "'");
629 log
.println("Message: " + e
.getMessage());
634 private XComponent
loadComponent(String _sName
, Object _xComponent
, ArrayList
<PropertyValue
> _aPropertyList
)
636 XComponent xDocComponent
= null;
637 XComponentLoader xComponentLoader
= UnoRuntime
.queryInterface( XComponentLoader
.class, _xComponent
);
641 PropertyValue
[] aLoadProperties
= PropertyHelper
.createPropertyValueArrayFormArrayList(_aPropertyList
);
642 log
.println("Load component: '" + _sName
+ "'");
643 xDocComponent
= xComponentLoader
.loadComponentFromURL(_sName
, "_blank", 0, aLoadProperties
);
644 log
.println("Load component: '" + _sName
+ "' done");
646 catch (com
.sun
.star
.io
.IOException e
)
648 log
.println("ERROR: Exception caught");
649 log
.println("Can't load document '" + _sName
+ "'");
650 log
.println("Message: " + e
.getMessage());
652 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
654 log
.println("ERROR: Exception caught");
655 log
.println("Illegal Arguments given to loadComponentFromURL.");
656 log
.println("Message: " + e
.getMessage());
658 return xDocComponent
;
661 private void closeComponent(XComponent _xDoc
)
663 // Close the document
664 XCloseable xCloseable
= UnoRuntime
.queryInterface(XCloseable
.class, _xDoc
);
667 xCloseable
.close(true);
669 catch (com
.sun
.star
.util
.CloseVetoException e
)
671 log
.println("ERROR: CloseVetoException caught");
672 log
.println("CloseVetoException occurred Can't close document.");
673 log
.println("Message: " + e
.getMessage());