Update ooo320-m1
[ooovba.git] / qadevOOo / runner / convwatch / ReportDesignerTest.java
blobebe36b751bd7ae9345ee2b3c17afd40df557d893
1 /*
2 ************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * Copyright 2008 by Sun Microsystems, Inc.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * $RCSfile: ReportDesignerTest.java,v $
11 * $Revision: 1.4.8.1 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 package convwatch;
34 import java.io.File;
35 import java.util.ArrayList;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.container.XNameAccess;
40 import com.sun.star.frame.XComponentLoader;
41 import com.sun.star.frame.XDesktop;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.frame.XStorable;
44 import com.sun.star.lang.XComponent;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.sdb.XDocumentDataSource;
47 import com.sun.star.sdb.XOfficeDatabaseDocument;
48 import com.sun.star.sdb.XReportDocumentsSupplier;
49 import com.sun.star.sdb.application.XDatabaseDocumentUI;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 import com.sun.star.util.XCloseable;
53 import com.sun.star.lang.XServiceInfo;
55 // import util.BasicMacroTools;
56 // import util.DesktopTools;
57 // import util.dbg;
58 import complexlib.ComplexTestCase;
59 import helper.OfficeProvider;
60 import helper.URLHelper;
61 import helper.OfficeWatcher;
63 // import convwatch.DB;
65 // import java.util.Date;
66 // import java.text.SimpleDateFormat;
67 // import java.text.ParsePosition;
68 // import java.sql.Time;
69 //
70 // import java.io.BufferedReader;
71 // import java.io.File;
72 // import java.io.FileReader;
73 // import java.io.IOException;
74 // import java.io.FilenameFilter;
75 //
76 // import java.util.Vector;
77 //
78 // import helper.AppProvider;
79 // import java.text.DecimalFormat;
80 // import util.DynamicClassLoader;
81 // import java.util.StringTokenizer;
86 class PropertySetHelper
88 XPropertySet m_xPropertySet;
89 public PropertySetHelper(Object _aObj)
91 m_xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, _aObj);
94 /**
95 get a property and don't convert it
96 @param _sName the string name of the property
97 @return the object value of the property without any conversion
99 public Object getPropertyValueAsObject(String _sName)
101 Object aObject = null;
103 if (m_xPropertySet != null)
107 aObject = m_xPropertySet.getPropertyValue(_sName);
109 catch (com.sun.star.beans.UnknownPropertyException e)
111 System.out.println("ERROR: UnknownPropertyException caught. '" + _sName + "'");
112 System.out.println("Message: " + e.getMessage());
114 catch (com.sun.star.lang.WrappedTargetException e)
116 System.out.println("ERROR: WrappedTargetException caught.");
117 System.out.println("Message: " + e.getMessage());
120 return aObject;
124 class PropertyHelper
127 Create a PropertyValue[] from a ArrayList
128 @param _aArrayList
129 @return a PropertyValue[]
131 public static PropertyValue[] createPropertyValueArrayFormArrayList(ArrayList _aPropertyList)
133 // copy the whole PropertyValue List to an PropertyValue Array
134 PropertyValue[] aSaveProperties = null;
136 if (_aPropertyList == null)
138 aSaveProperties = new PropertyValue[0];
140 else
142 if (_aPropertyList.size() > 0)
144 aSaveProperties = new PropertyValue[_aPropertyList.size()];
145 for (int i = 0;i<_aPropertyList.size(); i++)
147 aSaveProperties[i] = (PropertyValue) _aPropertyList.get(i);
150 else
152 aSaveProperties = new PropertyValue[0];
155 return aSaveProperties;
159 public class ReportDesignerTest extends ComplexTestCase {
161 String mTestDocumentPath;
164 * This method returns a list of Strings, each string must be a function name in this class.
165 * @return
167 public String[] getTestMethodNames()
169 return new String[] {"ReportDesignTest"}; // MAIN
172 private void checkIfOfficeExists(String _sOfficePathWithTrash)
174 String sOfficePath = "";
175 int nIndex = _sOfficePathWithTrash.indexOf("soffice.exe");
176 if (nIndex > 0)
178 sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 11);
180 else
182 nIndex = _sOfficePathWithTrash.indexOf("soffice");
183 if (nIndex > 0)
185 sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 7);
189 // if (sOfficePath.startsWith("\"") ||
190 // sOfficePath.startsWith("'"))
191 // {
192 // sOfficePath = sOfficePath.substring(1);
193 // }
194 sOfficePath = helper.StringHelper.removeQuoteIfExists(sOfficePath);
196 log.println(sOfficePath);
197 File sOffice = new File(sOfficePath);
198 if (! sOffice.exists())
200 log.println("ERROR: There exists no office installation at given path: '" + sOfficePath + "'");
201 System.exit(0);
206 private static XDesktop m_xDesktop = null;
207 public static XDesktop getXDesktop()
210 if (m_xDesktop == null)
212 try
214 XInterface xInterface = (XInterface) m_xXMultiServiceFactory.createInstance( "com.sun.star.frame.Desktop" );
215 m_xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, xInterface);
217 catch (com.sun.star.uno.Exception e)
219 log.println("ERROR: uno.Exception caught");
220 log.println("Message: " + e.getMessage());
223 return m_xDesktop;
226 private void showElements(XNameAccess _xNameAccess)
228 if (_xNameAccess != null)
230 String[] sElementNames = _xNameAccess.getElementNames();
231 for(int i=0;i<sElementNames.length; i++)
233 System.out.println("Value: [" + i + "] := " + sElementNames[i]);
236 else
238 System.out.println("Warning: Given object is null.");
243 private OfficeProvider m_aProvider = null;
244 private static XMultiServiceFactory m_xXMultiServiceFactory = null;
245 private void startOffice()
247 // int tempTime = param.getInt("SingleTimeOut");
248 param.put("TimeOut", new Integer(300000));
249 System.out.println("TimeOut: " + param.getInt("TimeOut"));
250 System.out.println("ThreadTimeOut: " + param.getInt("ThreadTimeOut"));
252 // OfficeProvider aProvider = null;
253 m_aProvider = new OfficeProvider();
254 m_xXMultiServiceFactory = (XMultiServiceFactory) m_aProvider.getManager(param);
255 param.put("ServiceFactory", m_xXMultiServiceFactory);
258 private void stopOffice()
260 if (m_aProvider != null)
262 m_aProvider.closeExistingOffice(param, true);
263 m_aProvider = null;
265 TimeHelper.waitInSeconds(2, "Give close Office some time.");
268 private String m_sMailAddress = null;
269 private String m_sParentDistinct = null;
271 // private String m_sUPDMinor;
272 // private String m_sCWS_WORK_STAMP;
274 private static String m_sSourceVersion;
275 private static String m_sDestinationVersion;
276 private static String m_sSourceName;
277 private static String m_sDestinationName;
279 private static final int WRITER = 1;
280 private static final int CALC = 2;
283 * This is the main test Function of current ReportDesignerTest
285 public void ReportDesignTest()
287 convwatch.GlobalLogWriter.set(log);
289 GlobalLogWriter.get().println("Set office watcher");
290 OfficeWatcher aWatcher = (OfficeWatcher)param.get("Watcher");
291 GlobalLogWriter.get().setWatcher(aWatcher);
296 // -------------------- preconditions, try to find an office --------------------
298 String sAppExecutionCommand = (String) param.get("AppExecutionCommand");
299 log.println("sAppExecutionCommand='" + sAppExecutionCommand + "'");
301 String sUser = System.getProperty("user.name");
302 log.println("user.name='" + sUser + "'");
304 // String sVCSID = System.getProperty("VCSID");
305 // log.println("VCSID='" + sVCSID + "'");
306 // m_sMailAddress = sVCSID + "@openoffice.org";
307 m_sMailAddress = System.getProperty("MailAddress");
308 log.println("Assumed mail address: " + m_sMailAddress);
310 m_sParentDistinct = System.getProperty("ParentDistinct");
312 m_sSourceVersion = System.getProperty("SourceVersion");
313 m_sSourceName = System.getProperty("SourceName");
314 m_sDestinationVersion = System.getProperty("DestinationVersion");
315 m_sDestinationName = System.getProperty("DestinationName");
316 // createDBEntry();
317 // log.println("Current CWS: " + m_sCWS_WORK_STAMP);
318 // log.println("Current MWS: " + m_sUPDMinor);
320 if (m_sSourceVersion == null)
322 System.out.println("Error, Sourceversion is null.");
323 System.exit(1);
326 sAppExecutionCommand = sAppExecutionCommand.replaceAll( "\\$\\{USERNAME\\}", sUser);
327 log.println("sAppExecutionCommand='" + sAppExecutionCommand + "'");
329 // an other way to replace strings
330 // sAppExecutionCommand = utils.replaceAll13(sAppExecutionCommand, "${USERNAME}", sUser);
332 checkIfOfficeExists(sAppExecutionCommand);
333 param.put("AppExecutionCommand", new String(sAppExecutionCommand));
335 // System.exit(1);
337 // --------------------------- Start the given Office ---------------------------
339 startOffice();
341 // ------------------------------ Start a test run ------------------------------
343 // String sCurrentDirectory = System.getProperty("user.dir");
344 // log.println("Current Dir: " + sCurrentDirectory);
345 String sDocument = (String) param.get(convwatch.PropertyName.DOC_COMPARATOR_INPUT_PATH);
346 sDocument = helper.StringHelper.removeQuoteIfExists( sDocument );
347 startTestForFile(sDocument);
348 // if (sDocument.toLowerCase().indexOf("writer") >= 0)
349 // {
350 // startTestForFile(sDocument, WRITER);
351 // }
352 // else if (sDocument.toLowerCase().indexOf("calc") >= 0)
353 // {
354 // startTestForFile(sDocument, CALC);
355 // }
356 // else
357 // {
358 // assure("Can't identify the document no 'writer' nor 'calc' in it's name given.", false);
359 // }
361 catch (AssureException e)
363 stopOffice();
364 throw new AssureException(e.getMessage());
367 // ------------------------------ Office shutdown ------------------------------
368 stopOffice();
371 // -----------------------------------------------------------------------------
372 private void startTestForFile(String _sDocument /*, int _nType*/)
374 File aFile = new File(_sDocument);
375 assure("Test File '" + _sDocument + "' doesn't exist.", aFile.exists());
377 String sFileURL = URLHelper.getFileURLFromSystemPath(_sDocument);
378 log.println("File URL: " + sFileURL);
380 XComponent xDocComponent = loadComponent(sFileURL, getXDesktop(), null);
381 log.println("Load done");
382 // context = createUnoService("com.sun.star.sdb.DatabaseContext")
383 // oDataBase = context.getByName("hh")
384 // oDBDoc = oDataBase.DatabaseDocument
386 // dim args(1) as new com.sun.star.beans.PropertyValue
387 // args(0).Name = "ActiveConnection"
388 // args(0).Value = oDBDoc.getCurrentController().getPropertyValue("ActiveConnection")
389 // reportContainer = oDBDoc.getReportDocuments()
390 // report = reportContainer.loadComponentFromURL("Report40","",0,args)
394 XInterface x = (XInterface)m_xXMultiServiceFactory.createInstance("com.sun.star.sdb.DatabaseContext");
395 assure("can't create instance of com.sun.star.sdb.DatabaseContext", x != null);
396 log.println("createInstance com.sun.star.sdb.DatabaseContext done");
398 XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, x);
399 showElements(xNameAccess);
400 Object aObj = xNameAccess.getByName(sFileURL);
401 // log.println("1");
403 // PropertySetHelper aHelper = new PropertySetHelper(aObj);
404 XDocumentDataSource xDataSource = (XDocumentDataSource)UnoRuntime.queryInterface(XDocumentDataSource.class, aObj);
405 // Object aDatabaseDocmuent = aHelper.getPropertyValueAsObject("DatabaseDocument");
406 XOfficeDatabaseDocument xOfficeDBDoc = xDataSource.getDatabaseDocument();
408 // XOfficeDatabaseDocument xOfficeDBDoc = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, aDatabaseDocument);
409 assure("can't access DatabaseDocument", xOfficeDBDoc != null);
410 // log.println("2");
412 XModel xDBSource = (XModel)UnoRuntime.queryInterface(XModel.class, xOfficeDBDoc);
413 Object aController = xDBSource.getCurrentController();
414 assure("Controller of xOfficeDatabaseDocument is empty!", aController != null);
415 // log.println("3");
417 XDatabaseDocumentUI aDBDocUI = (XDatabaseDocumentUI)UnoRuntime.queryInterface(XDatabaseDocumentUI.class, aController);
418 aDBDocUI.connect();
419 // if (aDBDocUI.isConnected())
420 // {
421 // System.out.println("true");
422 // }
423 // else
424 // {
425 // System.out.println("false");
426 // }
427 // log.println("4");
429 // aHelper = new PropertySetHelper(aController);
431 // Object aActiveConnectionObj = aHelper.getPropertyValueAsObject("ActiveConnection");
432 Object aActiveConnectionObj = aDBDocUI.getActiveConnection();
433 assure("ActiveConnection is empty", aActiveConnectionObj != null);
434 // log.println("5");
436 XReportDocumentsSupplier xSupplier = (XReportDocumentsSupplier)UnoRuntime.queryInterface(XReportDocumentsSupplier.class, xOfficeDBDoc);
437 xNameAccess = xSupplier.getReportDocuments();
438 assure("xOfficeDatabaseDocument returns no Report Document", xNameAccess != null);
439 // log.println("5");
441 showElements(xNameAccess);
443 ArrayList aPropertyList = new ArrayList();
445 PropertyValue aActiveConnection = new PropertyValue();
446 aActiveConnection.Name = "ActiveConnection";
447 aActiveConnection.Value = aActiveConnectionObj;
448 aPropertyList.add(aActiveConnection);
450 loadAndStoreReports(xNameAccess, aPropertyList /*, _nType*/ );
451 createDBEntry(/*_nType*/);
453 catch(com.sun.star.uno.Exception e)
455 log.println("ERROR: Exception caught");
456 log.println("Message: " + e.getMessage());
459 // String mTestDocumentPath = (String) param.get("TestDocumentPath");
460 // System.out.println("mTestDocumentPath: '" + mTestDocumentPath + "'");
461 // // workaround for issue using deprecated "DOCPTH" prop
462 // System.setProperty("DOCPTH", mTestDocumentPath);
464 // Close the document
465 closeComponent(xDocComponent);
468 private String getDocumentPoolName(/*int _nType*/)
470 return "AutogenReportDesignTest";
471 // return getFileFormat(_nType);
474 // -----------------------------------------------------------------------------
475 private void createDBEntry(/*int _nType*/)
477 // try to connect the database
478 String sDBConnection = (String)param.get( convwatch.PropertyName.DB_CONNECTION_STRING );
479 log.println("DBConnection: " + sDBConnection);
480 DB.init(sDBConnection);
482 // String sFixRefSubDirectory = "ReportDesign_qa_complex_" + getFileFormat(_nType);
483 String sFixRefSubDirectory = "ReportDesignFixRef";
485 String sSourceVersion = m_sSourceVersion;
486 // String sSourceVersion = sFixRefSubDirectory;
487 String sSourceName = m_sSourceName;
488 // String sSourceCreatorType = "fixref";
489 String sSourceCreatorType = "";
490 String sDestinationVersion = m_sDestinationVersion;
491 // if (sDestinationVersion.length() == 0)
492 // {
493 // sDestinationVersion = m_sUPDMinor;
494 // }
495 String sDestinationName = m_sDestinationName;
496 String sDestinationCreatorType = "";
497 String sDocumentPoolDir = getOutputPath(/*_nType*/);
498 String sDocumentPoolName = getDocumentPoolName(/*_nType*/);
499 String sSpecial = "";
501 DB.insertinto_documentcompare(sSourceVersion, sSourceName, sSourceCreatorType,
502 m_sDestinationVersion, sDestinationName, sDestinationCreatorType,
503 sDocumentPoolDir, sDocumentPoolName, m_sMailAddress,
504 sSpecial, m_sParentDistinct);
505 TimeHelper.waitInSeconds(1, "wait for DB.");
506 // DB.test();
507 // System.exit(1);
510 private void loadAndStoreReports(XNameAccess _xNameAccess, ArrayList _aPropertyList /*, int _nType*/ )
512 if (_xNameAccess != null)
514 String[] sElementNames = _xNameAccess.getElementNames();
515 for(int i=0;i<sElementNames.length; i++)
517 String sReportName = sElementNames[i];
518 XComponent xDoc = loadComponent(sReportName, _xNameAccess, _aPropertyList);
519 // print? or store?
520 storeComponent(sReportName, xDoc /*, _nType*/);
521 closeComponent(xDoc);
526 private String getFormatExtension(Object _xComponent /* int _nType*/ )
528 String sExtension;
529 XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface( XServiceInfo.class, _xComponent );
530 if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
532 // calc
533 sExtension = ".ods";
535 else if (xServiceInfo.supportsService("com.sun.star.text.TextDocument"))
537 //writer
538 sExtension = ".odt";
540 else
542 sExtension = ".UNKNOWN";
544 return sExtension;
547 // switch(_nType)
548 // {
549 // case WRITER:
550 // sExtension = ".odt";
551 // break;
552 // case CALC:
553 // sExtension = ".ods";
554 // break;
555 // default:
556 // sExtension = ".UNKNOWN";
557 // }
558 // return sExtension;
559 // }
560 // private String getFileFormat(int _nType)
561 // {
562 // String sFileType;
563 // switch(_nType)
564 // {
565 // case WRITER:
566 // sFileType = "writer8";
567 // break;
568 // case CALC:
569 // sFileType = "calc8";
570 // break;
571 // default:
572 // sFileType = "UNKNOWN";
573 // }
574 // return sFileType;
575 // }
577 private String m_sOutputPath = null;
579 private String getOutputPath(/*int _nType*/)
581 if (m_sOutputPath == null)
583 String sOutputPath = (String)param.get( convwatch.PropertyName.DOC_COMPARATOR_OUTPUT_PATH );
584 sOutputPath = helper.StringHelper.removeQuoteIfExists(sOutputPath);
586 if (!sOutputPath.endsWith("/") || // construct the output file name
587 !sOutputPath.endsWith("\\"))
589 sOutputPath += System.getProperty("file.separator");
591 // sOutputPath += "tmp_123";
592 sOutputPath += DateHelper.getDateTimeForFilename();
593 sOutputPath += System.getProperty("file.separator");
595 // sOutputPath += getFileFormat(_nType);
596 // sOutputPath += System.getProperty("file.separator");
598 File aOutputFile = new File(sOutputPath); // create the directory of the given output path
599 aOutputFile.mkdirs();
600 m_sOutputPath = sOutputPath;
602 return m_sOutputPath;
606 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH
608 private void storeComponent(String _sName, Object _xComponent /*, int _nType*/ )
610 String sOutputPath = getOutputPath(/*_nType*/);
612 // add DocumentPoolName
613 sOutputPath += getDocumentPoolName(/*_nType*/);
614 sOutputPath += System.getProperty("file.separator");
616 File aOutputFile = new File(sOutputPath); // create the directory of the given output path
617 aOutputFile.mkdirs();
619 sOutputPath += _sName;
620 sOutputPath += getFormatExtension(_xComponent /*_nType*/);
622 String sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputPath);
624 ArrayList aPropertyList = new ArrayList(); // set some properties for storeAsURL
626 // PropertyValue aFileFormat = new PropertyValue();
627 // aFileFormat.Name = "FilterName";
628 // aFileFormat.Value = getFileFormat(_nType);
629 // aPropertyList.add(aFileFormat);
631 PropertyValue aOverwrite = new PropertyValue(); // always overwrite already exist files
632 aOverwrite.Name = "Overwrite";
633 aOverwrite.Value = Boolean.TRUE;
634 aPropertyList.add(aOverwrite);
636 // store the document in an other directory
637 XStorable aStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, _xComponent);
638 if (aStorable != null)
640 log.println("store document as URL: '" + sOutputURL + "'");
643 aStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
645 catch (com.sun.star.io.IOException e)
647 log.println("ERROR: Exception caught");
648 log.println("Can't write document URL: '" + sOutputURL + "'");
649 log.println("Message: " + e.getMessage());
654 private XComponent loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList)
656 XComponent xDocComponent = null;
657 XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, _xComponent );
661 PropertyValue[] aLoadProperties = PropertyHelper.createPropertyValueArrayFormArrayList(_aPropertyList);
662 log.println("Load component: '" + _sName + "'");
663 xDocComponent = xComponentLoader.loadComponentFromURL(_sName, "_blank", 0, aLoadProperties);
664 log.println("Load component: '" + _sName + "' done");
666 catch (com.sun.star.io.IOException e)
668 log.println("ERROR: Exception caught");
669 log.println("Can't load document '" + _sName + "'");
670 log.println("Message: " + e.getMessage());
672 catch (com.sun.star.lang.IllegalArgumentException e)
674 log.println("ERROR: Exception caught");
675 log.println("Illegal Arguments given to loadComponentFromURL.");
676 log.println("Message: " + e.getMessage());
678 return xDocComponent;
681 private void closeComponent(XComponent _xDoc)
683 // Close the document
684 XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, _xDoc);
687 xCloseable.close(true);
689 catch (com.sun.star.util.CloseVetoException e)
691 log.println("ERROR: CloseVetoException caught");
692 log.println("CloseVetoException occured Can't close document.");
693 log.println("Message: " + e.getMessage());