2 ************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
32 import java
.util
.ArrayList
;
34 import com
.sun
.star
.beans
.PropertyValue
;
35 import com
.sun
.star
.beans
.XPropertySet
;
36 import com
.sun
.star
.container
.XNameAccess
;
37 import com
.sun
.star
.frame
.XComponentLoader
;
38 import com
.sun
.star
.frame
.XDesktop
;
39 import com
.sun
.star
.frame
.XModel
;
40 import com
.sun
.star
.frame
.XStorable
;
41 import com
.sun
.star
.lang
.XComponent
;
42 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.sdb
.XDocumentDataSource
;
44 import com
.sun
.star
.sdb
.XOfficeDatabaseDocument
;
45 import com
.sun
.star
.sdb
.XReportDocumentsSupplier
;
46 import com
.sun
.star
.sdb
.application
.XDatabaseDocumentUI
;
47 import com
.sun
.star
.uno
.UnoRuntime
;
48 import com
.sun
.star
.uno
.XInterface
;
49 import com
.sun
.star
.util
.XCloseable
;
50 import com
.sun
.star
.lang
.XServiceInfo
;
52 // import util.BasicMacroTools;
53 // import util.DesktopTools;
55 import complexlib
.ComplexTestCase
;
56 import helper
.OfficeProvider
;
57 import helper
.URLHelper
;
58 import helper
.OfficeWatcher
;
60 // import convwatch.DB;
62 // import java.util.Date;
63 // import java.text.SimpleDateFormat;
64 // import java.text.ParsePosition;
65 // import java.sql.Time;
67 // import java.io.BufferedReader;
68 // import java.io.File;
69 // import java.io.FileReader;
70 // import java.io.IOException;
71 // import java.io.FilenameFilter;
73 // import java.util.Vector;
75 // import helper.AppProvider;
76 // import java.text.DecimalFormat;
77 // import util.DynamicClassLoader;
78 // import java.util.StringTokenizer;
83 class PropertySetHelper
85 XPropertySet m_xPropertySet
;
86 public PropertySetHelper(Object _aObj
)
88 m_xPropertySet
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class, _aObj
);
92 get a property and don't convert it
93 @param _sName the string name of the property
94 @return the object value of the property without any conversion
96 public Object
getPropertyValueAsObject(String _sName
)
98 Object aObject
= null;
100 if (m_xPropertySet
!= null)
104 aObject
= m_xPropertySet
.getPropertyValue(_sName
);
106 catch (com
.sun
.star
.beans
.UnknownPropertyException e
)
108 System
.out
.println("ERROR: UnknownPropertyException caught. '" + _sName
+ "'");
109 System
.out
.println("Message: " + e
.getMessage());
111 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
113 System
.out
.println("ERROR: WrappedTargetException caught.");
114 System
.out
.println("Message: " + e
.getMessage());
124 Create a PropertyValue[] from a ArrayList
126 @return a PropertyValue[]
128 public static PropertyValue
[] createPropertyValueArrayFormArrayList(ArrayList _aPropertyList
)
130 // copy the whole PropertyValue List to an PropertyValue Array
131 PropertyValue
[] aSaveProperties
= null;
133 if (_aPropertyList
== null)
135 aSaveProperties
= new PropertyValue
[0];
139 if (_aPropertyList
.size() > 0)
141 aSaveProperties
= new PropertyValue
[_aPropertyList
.size()];
142 for (int i
= 0;i
<_aPropertyList
.size(); i
++)
144 aSaveProperties
[i
] = (PropertyValue
) _aPropertyList
.get(i
);
149 aSaveProperties
= new PropertyValue
[0];
152 return aSaveProperties
;
156 public class ReportDesignerTest
extends ComplexTestCase
{
158 String mTestDocumentPath
;
161 * This method returns a list of Strings, each string must be a function name in this class.
164 public String
[] getTestMethodNames()
166 return new String
[] {"ReportDesignTest"}; // MAIN
169 private void checkIfOfficeExists(String _sOfficePathWithTrash
)
171 String sOfficePath
= "";
172 int nIndex
= _sOfficePathWithTrash
.indexOf("soffice.exe");
175 sOfficePath
= _sOfficePathWithTrash
.substring(0, nIndex
+ 11);
179 nIndex
= _sOfficePathWithTrash
.indexOf("soffice");
182 sOfficePath
= _sOfficePathWithTrash
.substring(0, nIndex
+ 7);
186 // if (sOfficePath.startsWith("\"") ||
187 // sOfficePath.startsWith("'"))
189 // sOfficePath = sOfficePath.substring(1);
191 sOfficePath
= helper
.StringHelper
.removeQuoteIfExists(sOfficePath
);
193 log
.println(sOfficePath
);
194 File sOffice
= new File(sOfficePath
);
195 if (! sOffice
.exists())
197 log
.println("ERROR: There exists no office installation at given path: '" + sOfficePath
+ "'");
203 private static XDesktop m_xDesktop
= null;
204 public static XDesktop
getXDesktop()
207 if (m_xDesktop
== null)
211 XInterface xInterface
= (XInterface
) m_xXMultiServiceFactory
.createInstance( "com.sun.star.frame.Desktop" );
212 m_xDesktop
= (XDesktop
) UnoRuntime
.queryInterface(XDesktop
.class, xInterface
);
214 catch (com
.sun
.star
.uno
.Exception e
)
216 log
.println("ERROR: uno.Exception caught");
217 log
.println("Message: " + e
.getMessage());
223 private void showElements(XNameAccess _xNameAccess
)
225 if (_xNameAccess
!= null)
227 String
[] sElementNames
= _xNameAccess
.getElementNames();
228 for(int i
=0;i
<sElementNames
.length
; i
++)
230 System
.out
.println("Value: [" + i
+ "] := " + sElementNames
[i
]);
235 System
.out
.println("Warning: Given object is null.");
240 private OfficeProvider m_aProvider
= null;
241 private static XMultiServiceFactory m_xXMultiServiceFactory
= null;
242 private void startOffice()
244 // int tempTime = param.getInt("SingleTimeOut");
245 param
.put("TimeOut", new Integer(300000));
246 System
.out
.println("TimeOut: " + param
.getInt("TimeOut"));
247 System
.out
.println("ThreadTimeOut: " + param
.getInt("ThreadTimeOut"));
249 // OfficeProvider aProvider = null;
250 m_aProvider
= new OfficeProvider();
251 m_xXMultiServiceFactory
= (XMultiServiceFactory
) m_aProvider
.getManager(param
);
252 param
.put("ServiceFactory", m_xXMultiServiceFactory
);
255 private void stopOffice()
257 if (m_aProvider
!= null)
259 m_aProvider
.closeExistingOffice(param
, true);
262 TimeHelper
.waitInSeconds(2, "Give close Office some time.");
265 private String m_sMailAddress
= null;
266 private String m_sParentDistinct
= null;
268 // private String m_sUPDMinor;
269 // private String m_sCWS_WORK_STAMP;
271 private static String m_sSourceVersion
;
272 private static String m_sDestinationVersion
;
273 private static String m_sSourceName
;
274 private static String m_sDestinationName
;
276 private static final int WRITER
= 1;
277 private static final int CALC
= 2;
280 * This is the main test Function of current ReportDesignerTest
282 public void ReportDesignTest()
284 convwatch
.GlobalLogWriter
.set(log
);
286 GlobalLogWriter
.get().println("Set office watcher");
287 OfficeWatcher aWatcher
= (OfficeWatcher
)param
.get("Watcher");
288 GlobalLogWriter
.get().setWatcher(aWatcher
);
293 // -------------------- preconditions, try to find an office --------------------
295 String sAppExecutionCommand
= (String
) param
.get("AppExecutionCommand");
296 log
.println("sAppExecutionCommand='" + sAppExecutionCommand
+ "'");
298 String sUser
= System
.getProperty("user.name");
299 log
.println("user.name='" + sUser
+ "'");
301 // String sVCSID = System.getProperty("VCSID");
302 // log.println("VCSID='" + sVCSID + "'");
303 // m_sMailAddress = sVCSID + "@openoffice.org";
304 m_sMailAddress
= System
.getProperty("MailAddress");
305 log
.println("Assumed mail address: " + m_sMailAddress
);
307 m_sParentDistinct
= System
.getProperty("ParentDistinct");
309 m_sSourceVersion
= System
.getProperty("SourceVersion");
310 m_sSourceName
= System
.getProperty("SourceName");
311 m_sDestinationVersion
= System
.getProperty("DestinationVersion");
312 m_sDestinationName
= System
.getProperty("DestinationName");
314 // log.println("Current CWS: " + m_sCWS_WORK_STAMP);
315 // log.println("Current MWS: " + m_sUPDMinor);
317 if (m_sSourceVersion
== null)
319 System
.out
.println("Error, Sourceversion is null.");
323 sAppExecutionCommand
= sAppExecutionCommand
.replaceAll( "\\$\\{USERNAME\\}", sUser
);
324 log
.println("sAppExecutionCommand='" + sAppExecutionCommand
+ "'");
326 // an other way to replace strings
327 // sAppExecutionCommand = utils.replaceAll13(sAppExecutionCommand, "${USERNAME}", sUser);
329 checkIfOfficeExists(sAppExecutionCommand
);
330 param
.put("AppExecutionCommand", new String(sAppExecutionCommand
));
334 // --------------------------- Start the given Office ---------------------------
338 // ------------------------------ Start a test run ------------------------------
340 // String sCurrentDirectory = System.getProperty("user.dir");
341 // log.println("Current Dir: " + sCurrentDirectory);
342 String sDocument
= (String
) param
.get(convwatch
.PropertyName
.DOC_COMPARATOR_INPUT_PATH
);
343 sDocument
= helper
.StringHelper
.removeQuoteIfExists( sDocument
);
344 startTestForFile(sDocument
);
345 // if (sDocument.toLowerCase().indexOf("writer") >= 0)
347 // startTestForFile(sDocument, WRITER);
349 // else if (sDocument.toLowerCase().indexOf("calc") >= 0)
351 // startTestForFile(sDocument, CALC);
355 // assure("Can't identify the document no 'writer' nor 'calc' in it's name given.", false);
358 catch (AssureException e
)
361 throw new AssureException(e
.getMessage());
364 // ------------------------------ Office shutdown ------------------------------
368 // -----------------------------------------------------------------------------
369 private void startTestForFile(String _sDocument
/*, int _nType*/)
371 File aFile
= new File(_sDocument
);
372 assure("Test File '" + _sDocument
+ "' doesn't exist.", aFile
.exists());
374 String sFileURL
= URLHelper
.getFileURLFromSystemPath(_sDocument
);
375 log
.println("File URL: " + sFileURL
);
377 XComponent xDocComponent
= loadComponent(sFileURL
, getXDesktop(), null);
378 log
.println("Load done");
379 // context = createUnoService("com.sun.star.sdb.DatabaseContext")
380 // oDataBase = context.getByName("hh")
381 // oDBDoc = oDataBase.DatabaseDocument
383 // dim args(1) as new com.sun.star.beans.PropertyValue
384 // args(0).Name = "ActiveConnection"
385 // args(0).Value = oDBDoc.getCurrentController().getPropertyValue("ActiveConnection")
386 // reportContainer = oDBDoc.getReportDocuments()
387 // report = reportContainer.loadComponentFromURL("Report40","",0,args)
391 XInterface x
= (XInterface
)m_xXMultiServiceFactory
.createInstance("com.sun.star.sdb.DatabaseContext");
392 assure("can't create instance of com.sun.star.sdb.DatabaseContext", x
!= null);
393 log
.println("createInstance com.sun.star.sdb.DatabaseContext done");
395 XNameAccess xNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class, x
);
396 showElements(xNameAccess
);
397 Object aObj
= xNameAccess
.getByName(sFileURL
);
400 // PropertySetHelper aHelper = new PropertySetHelper(aObj);
401 XDocumentDataSource xDataSource
= (XDocumentDataSource
)UnoRuntime
.queryInterface(XDocumentDataSource
.class, aObj
);
402 // Object aDatabaseDocmuent = aHelper.getPropertyValueAsObject("DatabaseDocument");
403 XOfficeDatabaseDocument xOfficeDBDoc
= xDataSource
.getDatabaseDocument();
405 // XOfficeDatabaseDocument xOfficeDBDoc = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, aDatabaseDocument);
406 assure("can't access DatabaseDocument", xOfficeDBDoc
!= null);
409 XModel xDBSource
= (XModel
)UnoRuntime
.queryInterface(XModel
.class, xOfficeDBDoc
);
410 Object aController
= xDBSource
.getCurrentController();
411 assure("Controller of xOfficeDatabaseDocument is empty!", aController
!= null);
414 XDatabaseDocumentUI aDBDocUI
= (XDatabaseDocumentUI
)UnoRuntime
.queryInterface(XDatabaseDocumentUI
.class, aController
);
416 // if (aDBDocUI.isConnected())
418 // System.out.println("true");
422 // System.out.println("false");
426 // aHelper = new PropertySetHelper(aController);
428 // Object aActiveConnectionObj = aHelper.getPropertyValueAsObject("ActiveConnection");
429 Object aActiveConnectionObj
= aDBDocUI
.getActiveConnection();
430 assure("ActiveConnection is empty", aActiveConnectionObj
!= null);
433 XReportDocumentsSupplier xSupplier
= (XReportDocumentsSupplier
)UnoRuntime
.queryInterface(XReportDocumentsSupplier
.class, xOfficeDBDoc
);
434 xNameAccess
= xSupplier
.getReportDocuments();
435 assure("xOfficeDatabaseDocument returns no Report Document", xNameAccess
!= null);
438 showElements(xNameAccess
);
440 ArrayList aPropertyList
= new ArrayList();
442 PropertyValue aActiveConnection
= new PropertyValue();
443 aActiveConnection
.Name
= "ActiveConnection";
444 aActiveConnection
.Value
= aActiveConnectionObj
;
445 aPropertyList
.add(aActiveConnection
);
447 loadAndStoreReports(xNameAccess
, aPropertyList
/*, _nType*/ );
448 createDBEntry(/*_nType*/);
450 catch(com
.sun
.star
.uno
.Exception e
)
452 log
.println("ERROR: Exception caught");
453 log
.println("Message: " + e
.getMessage());
456 // String mTestDocumentPath = (String) param.get("TestDocumentPath");
457 // System.out.println("mTestDocumentPath: '" + mTestDocumentPath + "'");
458 // // workaround for issue using deprecated "DOCPTH" prop
459 // System.setProperty("DOCPTH", mTestDocumentPath);
461 // Close the document
462 closeComponent(xDocComponent
);
465 private String
getDocumentPoolName(/*int _nType*/)
467 return "AutogenReportDesignTest";
468 // return getFileFormat(_nType);
471 // -----------------------------------------------------------------------------
472 private void createDBEntry(/*int _nType*/)
474 // try to connect the database
475 String sDBConnection
= (String
)param
.get( convwatch
.PropertyName
.DB_CONNECTION_STRING
);
476 log
.println("DBConnection: " + sDBConnection
);
477 DB
.init(sDBConnection
);
479 // String sFixRefSubDirectory = "ReportDesign_qa_complex_" + getFileFormat(_nType);
480 String sFixRefSubDirectory
= "ReportDesignFixRef";
482 String sSourceVersion
= m_sSourceVersion
;
483 // String sSourceVersion = sFixRefSubDirectory;
484 String sSourceName
= m_sSourceName
;
485 // String sSourceCreatorType = "fixref";
486 String sSourceCreatorType
= "";
487 String sDestinationVersion
= m_sDestinationVersion
;
488 // if (sDestinationVersion.length() == 0)
490 // sDestinationVersion = m_sUPDMinor;
492 String sDestinationName
= m_sDestinationName
;
493 String sDestinationCreatorType
= "";
494 String sDocumentPoolDir
= getOutputPath(/*_nType*/);
495 String sDocumentPoolName
= getDocumentPoolName(/*_nType*/);
496 String sSpecial
= "";
498 DB
.insertinto_documentcompare(sSourceVersion
, sSourceName
, sSourceCreatorType
,
499 m_sDestinationVersion
, sDestinationName
, sDestinationCreatorType
,
500 sDocumentPoolDir
, sDocumentPoolName
, m_sMailAddress
,
501 sSpecial
, m_sParentDistinct
);
502 TimeHelper
.waitInSeconds(1, "wait for DB.");
507 private void loadAndStoreReports(XNameAccess _xNameAccess
, ArrayList _aPropertyList
/*, int _nType*/ )
509 if (_xNameAccess
!= null)
511 String
[] sElementNames
= _xNameAccess
.getElementNames();
512 for(int i
=0;i
<sElementNames
.length
; i
++)
514 String sReportName
= sElementNames
[i
];
515 XComponent xDoc
= loadComponent(sReportName
, _xNameAccess
, _aPropertyList
);
517 storeComponent(sReportName
, xDoc
/*, _nType*/);
518 closeComponent(xDoc
);
523 private String
getFormatExtension(Object _xComponent
/* int _nType*/ )
526 XServiceInfo xServiceInfo
= (XServiceInfo
) UnoRuntime
.queryInterface( XServiceInfo
.class, _xComponent
);
527 if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
532 else if (xServiceInfo
.supportsService("com.sun.star.text.TextDocument"))
539 sExtension
= ".UNKNOWN";
547 // sExtension = ".odt";
550 // sExtension = ".ods";
553 // sExtension = ".UNKNOWN";
555 // return sExtension;
557 // private String getFileFormat(int _nType)
563 // sFileType = "writer8";
566 // sFileType = "calc8";
569 // sFileType = "UNKNOWN";
574 private String m_sOutputPath
= null;
576 private String
getOutputPath(/*int _nType*/)
578 if (m_sOutputPath
== null)
580 String sOutputPath
= (String
)param
.get( convwatch
.PropertyName
.DOC_COMPARATOR_OUTPUT_PATH
);
581 sOutputPath
= helper
.StringHelper
.removeQuoteIfExists(sOutputPath
);
583 if (!sOutputPath
.endsWith("/") || // construct the output file name
584 !sOutputPath
.endsWith("\\"))
586 sOutputPath
+= System
.getProperty("file.separator");
588 // sOutputPath += "tmp_123";
589 sOutputPath
+= DateHelper
.getDateTimeForFilename();
590 sOutputPath
+= System
.getProperty("file.separator");
592 // sOutputPath += getFileFormat(_nType);
593 // sOutputPath += System.getProperty("file.separator");
595 File aOutputFile
= new File(sOutputPath
); // create the directory of the given output path
596 aOutputFile
.mkdirs();
597 m_sOutputPath
= sOutputPath
;
599 return m_sOutputPath
;
603 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH
605 private void storeComponent(String _sName
, Object _xComponent
/*, int _nType*/ )
607 String sOutputPath
= getOutputPath(/*_nType*/);
609 // add DocumentPoolName
610 sOutputPath
+= getDocumentPoolName(/*_nType*/);
611 sOutputPath
+= System
.getProperty("file.separator");
613 File aOutputFile
= new File(sOutputPath
); // create the directory of the given output path
614 aOutputFile
.mkdirs();
616 sOutputPath
+= _sName
;
617 sOutputPath
+= getFormatExtension(_xComponent
/*_nType*/);
619 String sOutputURL
= URLHelper
.getFileURLFromSystemPath(sOutputPath
);
621 ArrayList aPropertyList
= new ArrayList(); // set some properties for storeAsURL
623 // PropertyValue aFileFormat = new PropertyValue();
624 // aFileFormat.Name = "FilterName";
625 // aFileFormat.Value = getFileFormat(_nType);
626 // aPropertyList.add(aFileFormat);
628 PropertyValue aOverwrite
= new PropertyValue(); // always overwrite already exist files
629 aOverwrite
.Name
= "Overwrite";
630 aOverwrite
.Value
= Boolean
.TRUE
;
631 aPropertyList
.add(aOverwrite
);
633 // store the document in an other directory
634 XStorable aStorable
= (XStorable
) UnoRuntime
.queryInterface( XStorable
.class, _xComponent
);
635 if (aStorable
!= null)
637 log
.println("store document as URL: '" + sOutputURL
+ "'");
640 aStorable
.storeAsURL(sOutputURL
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
642 catch (com
.sun
.star
.io
.IOException e
)
644 log
.println("ERROR: Exception caught");
645 log
.println("Can't write document URL: '" + sOutputURL
+ "'");
646 log
.println("Message: " + e
.getMessage());
651 private XComponent
loadComponent(String _sName
, Object _xComponent
, ArrayList _aPropertyList
)
653 XComponent xDocComponent
= null;
654 XComponentLoader xComponentLoader
= (XComponentLoader
) UnoRuntime
.queryInterface( XComponentLoader
.class, _xComponent
);
658 PropertyValue
[] aLoadProperties
= PropertyHelper
.createPropertyValueArrayFormArrayList(_aPropertyList
);
659 log
.println("Load component: '" + _sName
+ "'");
660 xDocComponent
= xComponentLoader
.loadComponentFromURL(_sName
, "_blank", 0, aLoadProperties
);
661 log
.println("Load component: '" + _sName
+ "' done");
663 catch (com
.sun
.star
.io
.IOException e
)
665 log
.println("ERROR: Exception caught");
666 log
.println("Can't load document '" + _sName
+ "'");
667 log
.println("Message: " + e
.getMessage());
669 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
671 log
.println("ERROR: Exception caught");
672 log
.println("Illegal Arguments given to loadComponentFromURL.");
673 log
.println("Message: " + e
.getMessage());
675 return xDocComponent
;
678 private void closeComponent(XComponent _xDoc
)
680 // Close the document
681 XCloseable xCloseable
= (XCloseable
) UnoRuntime
.queryInterface(XCloseable
.class, _xDoc
);
684 xCloseable
.close(true);
686 catch (com
.sun
.star
.util
.CloseVetoException e
)
688 log
.println("ERROR: CloseVetoException caught");
689 log
.println("CloseVetoException occured Can't close document.");
690 log
.println("Message: " + e
.getMessage());