Update ooo320-m1
[ooovba.git] / reportdesign / qa / complex / ReportDesignerTest.java
blob50025bdc1d801edb47f6f8fa16d2875ac17f825d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ReportDesignerTest.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package complex;
33 import java.io.File;
34 import java.util.ArrayList;
36 import com.sun.star.beans.PropertyValue;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.container.XNameAccess;
39 import com.sun.star.frame.XComponentLoader;
40 import com.sun.star.frame.XDesktop;
41 import com.sun.star.frame.XModel;
42 import com.sun.star.frame.XStorable;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.sdb.XDocumentDataSource;
46 import com.sun.star.sdb.XOfficeDatabaseDocument;
47 import com.sun.star.sdb.XReportDocumentsSupplier;
48 import com.sun.star.sdb.application.XDatabaseDocumentUI;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import com.sun.star.util.XCloseable;
53 // import util.BasicMacroTools;
54 // import util.DesktopTools;
55 // import util.dbg;
56 import complexlib.ComplexTestCase;
57 import util.utils;
58 import helper.OfficeProvider;
59 import helper.URLHelper;
60 import helper.OfficeWatcher;
62 import convwatch.DB;
64 // import java.util.Date;
65 // import java.text.SimpleDateFormat;
66 // import java.text.ParsePosition;
67 // import java.sql.Time;
68 //
69 // import java.io.BufferedReader;
70 // import java.io.File;
71 // import java.io.FileReader;
72 // import java.io.IOException;
73 // import java.io.FilenameFilter;
74 //
75 // import java.util.Vector;
76 //
77 // import helper.AppProvider;
78 // import java.text.DecimalFormat;
79 // import util.DynamicClassLoader;
80 // import java.util.StringTokenizer;
85 class PropertySetHelper
87 XPropertySet m_xPropertySet;
88 public PropertySetHelper(Object _aObj)
90 m_xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, _aObj);
93 /**
94 get a property and don't convert it
95 @param _sName the string name of the property
96 @return the object value of the property without any conversion
98 public Object getPropertyValueAsObject(String _sName)
100 Object aObject = null;
102 if (m_xPropertySet != null)
106 aObject = m_xPropertySet.getPropertyValue(_sName);
108 catch (com.sun.star.beans.UnknownPropertyException e)
110 System.out.println("ERROR: UnknownPropertyException caught. '" + _sName + "'");
111 System.out.println("Message: " + e.getMessage());
113 catch (com.sun.star.lang.WrappedTargetException e)
115 System.out.println("ERROR: WrappedTargetException caught.");
116 System.out.println("Message: " + e.getMessage());
119 return aObject;
123 class PropertyHelper
126 Create a PropertyValue[] from a ArrayList
127 @param _aArrayList
128 @return a PropertyValue[]
130 public static PropertyValue[] createPropertyValueArrayFormArrayList(ArrayList _aPropertyList)
132 // copy the whole PropertyValue List to an PropertyValue Array
133 PropertyValue[] aSaveProperties = null;
135 if (_aPropertyList == null)
137 aSaveProperties = new PropertyValue[0];
139 else
141 if (_aPropertyList.size() > 0)
143 aSaveProperties = new PropertyValue[_aPropertyList.size()];
144 for (int i = 0;i<_aPropertyList.size(); i++)
146 aSaveProperties[i] = (PropertyValue) _aPropertyList.get(i);
149 else
151 aSaveProperties = new PropertyValue[0];
154 return aSaveProperties;
158 public class ReportDesignerTest extends ComplexTestCase {
160 String mTestDocumentPath;
162 public String[] getTestMethodNames()
164 return new String[] {"firsttest"};
167 private void checkIfOfficeExists(String _sOfficePathWithTrash)
169 String sOfficePath = "";
170 int nIndex = _sOfficePathWithTrash.indexOf("soffice.exe");
171 if (nIndex > 0)
173 sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 11);
175 else
177 nIndex = _sOfficePathWithTrash.indexOf("soffice");
178 if (nIndex > 0)
180 sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 7);
184 log.println(sOfficePath);
185 File sOffice = new File(sOfficePath);
186 if (! sOffice.exists())
188 log.println("ERROR: There exists no office installation at given path: '" + sOfficePath + "'");
189 System.exit(0);
194 private static XDesktop m_xDesktop = null;
195 public static XDesktop getXDesktop()
198 if (m_xDesktop == null)
200 try
202 XInterface xInterface = (XInterface) m_xXMultiServiceFactory.createInstance( "com.sun.star.frame.Desktop" );
203 m_xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, xInterface);
205 catch (com.sun.star.uno.Exception e)
207 log.println("ERROR: uno.Exception caught");
208 log.println("Message: " + e.getMessage());
211 return m_xDesktop;
214 private void showElements(XNameAccess _xNameAccess)
216 if (_xNameAccess != null)
218 String[] sElementNames = _xNameAccess.getElementNames();
219 for(int i=0;i<sElementNames.length; i++)
221 System.out.println("Value: [" + i + "] := " + sElementNames[i]);
224 else
226 System.out.println("Warning: Given object is null.");
231 private OfficeProvider m_aProvider = null;
232 private static XMultiServiceFactory m_xXMultiServiceFactory = null;
233 private void startOffice()
235 // int tempTime = param.getInt("SingleTimeOut");
236 param.put("TimeOut", new Integer(300000));
237 System.out.println("TimeOut: " + param.getInt("TimeOut"));
238 System.out.println("ThreadTimeOut: " + param.getInt("ThreadTimeOut"));
240 // OfficeProvider aProvider = null;
241 m_aProvider = new OfficeProvider();
242 m_xXMultiServiceFactory = (XMultiServiceFactory) m_aProvider.getManager(param);
243 param.put("ServiceFactory", m_xXMultiServiceFactory);
246 private void stopOffice()
248 if (m_aProvider != null)
250 m_aProvider.closeExistingOffice(param, true);
251 m_aProvider = null;
255 private String m_sMailAddress = null;
256 private String m_sUPDMinor;
257 private String m_sCWS_WORK_STAMP;
259 private static final int WRITER = 1;
260 private static final int CALC = 2;
262 public void firsttest()
264 convwatch.GlobalLogWriter.set(log);
268 // -------------------- preconditions, try to find an office --------------------
270 String sAppExecutionCommand = (String) param.get("AppExecutionCommand");
272 String sUser = System.getProperty("user.name");
273 log.println("user.name='" + sUser + "'");
275 String sVCSID = System.getProperty("VCSID");
276 log.println("VCSID='" + sVCSID + "'");
277 m_sMailAddress = sVCSID + "@openoffice.org";
278 log.println("Assumed mail address: " + m_sMailAddress);
280 m_sUPDMinor = System.getProperty("UPDMINOR");
281 m_sCWS_WORK_STAMP = System.getProperty("CWS_WORK_STAMP");
282 // createDBEntry();
283 log.println("Current CWS: " + m_sCWS_WORK_STAMP);
284 log.println("Current MWS: " + m_sUPDMinor);
286 // System.exit(1);
288 sAppExecutionCommand = sAppExecutionCommand.replaceAll( "\\$\\{USERNAME\\}", sUser);
289 log.println("sAppExecutionCommand='" + sAppExecutionCommand + "'");
291 // an other way to replace strings
292 // sAppExecutionCommand = utils.replaceAll13(sAppExecutionCommand, "${USERNAME}", sUser);
294 checkIfOfficeExists(sAppExecutionCommand);
295 param.put("AppExecutionCommand", new String(sAppExecutionCommand));
297 // --------------------------- Start the given Office ---------------------------
299 startOffice();
301 // ------------------------------ Start a test run ------------------------------
303 String sCurrentDirectory = System.getProperty("user.dir");
304 log.println("Current Dir: " + sCurrentDirectory);
306 String sWriterDocument = sCurrentDirectory + "/" + "RPTWriterTests.odb";
307 startTestForFile(sWriterDocument, WRITER);
309 String sCalcDocument = sCurrentDirectory + "/" + "RPTCalcTests.odb";
310 startTestForFile(sCalcDocument, CALC);
312 catch (AssureException e)
314 stopOffice();
315 throw new AssureException(e.getMessage());
318 // ------------------------------ Office shutdown ------------------------------
319 stopOffice();
322 // -----------------------------------------------------------------------------
323 private void startTestForFile(String _sDocument, int _nType)
325 File aFile = new File(_sDocument);
326 assure("Test File doesn't '" + _sDocument + "'exist.", aFile.exists());
328 String sFileURL = URLHelper.getFileURLFromSystemPath(_sDocument);
329 log.println("File URL: " + sFileURL);
331 XComponent xDocComponent = loadComponent(sFileURL, getXDesktop(), null);
332 log.println("Load done");
333 // context = createUnoService("com.sun.star.sdb.DatabaseContext")
334 // oDataBase = context.getByName("hh")
335 // oDBDoc = oDataBase.DatabaseDocument
337 // dim args(1) as new com.sun.star.beans.PropertyValue
338 // args(0).Name = "ActiveConnection"
339 // args(0).Value = oDBDoc.getCurrentController().getPropertyValue("ActiveConnection")
340 // reportContainer = oDBDoc.getReportDocuments()
341 // report = reportContainer.loadComponentFromURL("Report40","",0,args)
345 XInterface x = (XInterface)m_xXMultiServiceFactory.createInstance("com.sun.star.sdb.DatabaseContext");
346 assure("can't create instance of com.sun.star.sdb.DatabaseContext", x != null);
347 log.println("createInstance com.sun.star.sdb.DatabaseContext done");
349 XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, x);
350 showElements(xNameAccess);
351 Object aObj = xNameAccess.getByName(sFileURL);
352 // log.println("1");
354 // PropertySetHelper aHelper = new PropertySetHelper(aObj);
355 XDocumentDataSource xDataSource = (XDocumentDataSource)UnoRuntime.queryInterface(XDocumentDataSource.class, aObj);
356 // Object aDatabaseDocmuent = aHelper.getPropertyValueAsObject("DatabaseDocument");
357 XOfficeDatabaseDocument xOfficeDBDoc = xDataSource.getDatabaseDocument();
359 // XOfficeDatabaseDocument xOfficeDBDoc = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, aDatabaseDocument);
360 assure("can't access DatabaseDocument", xOfficeDBDoc != null);
361 // log.println("2");
363 XModel xDBSource = (XModel)UnoRuntime.queryInterface(XModel.class, xOfficeDBDoc);
364 Object aController = xDBSource.getCurrentController();
365 assure("Controller of xOfficeDatabaseDocument is empty!", aController != null);
366 // log.println("3");
368 XDatabaseDocumentUI aDBDocUI = (XDatabaseDocumentUI)UnoRuntime.queryInterface(XDatabaseDocumentUI.class, aController);
369 boolean isConnect = aDBDocUI.connect();
370 // if (isConnect)
371 // {
372 // System.out.println("true");
373 // }
374 // else
375 // {
376 // System.out.println("false");
377 // }
378 // log.println("4");
380 // aHelper = new PropertySetHelper(aController);
382 // Object aActiveConnectionObj = aHelper.getPropertyValueAsObject("ActiveConnection");
383 Object aActiveConnectionObj = aDBDocUI.getActiveConnection();
384 assure("ActiveConnection is empty", aActiveConnectionObj != null);
385 // log.println("5");
387 XReportDocumentsSupplier xSupplier = (XReportDocumentsSupplier)UnoRuntime.queryInterface(XReportDocumentsSupplier.class, xOfficeDBDoc);
388 xNameAccess = xSupplier.getReportDocuments();
389 assure("xOfficeDatabaseDocument returns no Report Document", xNameAccess != null);
390 // log.println("5");
392 showElements(xNameAccess);
394 ArrayList aPropertyList = new ArrayList();
396 PropertyValue aActiveConnection = new PropertyValue();
397 aActiveConnection.Name = "ActiveConnection";
398 aActiveConnection.Value = aActiveConnectionObj;
399 aPropertyList.add(aActiveConnection);
401 loadAndStoreReports(xNameAccess, aPropertyList, _nType);
402 createDBEntry(_nType);
404 catch(com.sun.star.uno.Exception e)
406 log.println("ERROR: Exception caught");
409 // String mTestDocumentPath = (String) param.get("TestDocumentPath");
410 // System.out.println("mTestDocumentPath: '" + mTestDocumentPath + "'");
411 // // workaround for issue using deprecated "DOCPTH" prop
412 // System.setProperty("DOCPTH", mTestDocumentPath);
414 // Close the document
415 closeComponent(xDocComponent);
418 private String getDocumentPoolName(int _nType)
420 return getFileFormat(_nType);
423 // -----------------------------------------------------------------------------
424 private void createDBEntry(int _nType)
426 // try to connect the database
427 String sDBConnection = (String)param.get( convwatch.PropertyName.DB_CONNECTION_STRING );
428 log.println("DBConnection: " + sDBConnection);
429 DB.init(sDBConnection);
430 String sDestinationVersion = m_sCWS_WORK_STAMP;
431 if (sDestinationVersion.length() == 0)
433 sDestinationVersion = m_sUPDMinor;
435 String sDestinationName = "";
436 String sDestinationCreatorType = "";
437 String sDocumentPoolDir = getOutputPath(_nType);
438 String sDocumentPoolName = getDocumentPoolName(_nType);
439 String sSpecial = "";
441 String sFixRefSubDirectory = "ReportDesign_qa_complex_" + getFileFormat(_nType);
442 DB.insertinto_documentcompare(sFixRefSubDirectory, "", "fixref",
443 sDestinationVersion, sDestinationName, sDestinationCreatorType,
444 sDocumentPoolDir, sDocumentPoolName, m_sMailAddress,
445 sSpecial);
446 // DB.test();
447 // System.exit(1);
450 private void loadAndStoreReports(XNameAccess _xNameAccess, ArrayList _aPropertyList, int _nType)
452 if (_xNameAccess != null)
454 String[] sElementNames = _xNameAccess.getElementNames();
455 for(int i=0;i<sElementNames.length; i++)
457 String sReportName = sElementNames[i];
458 XComponent xDoc = loadComponent(sReportName, _xNameAccess, _aPropertyList);
459 // print? or store?
460 storeComponent(sReportName, xDoc, _nType);
461 closeComponent(xDoc);
466 private String getFormatExtension(int _nType)
468 String sExtension;
469 switch(_nType)
471 case WRITER:
472 sExtension = ".odt";
473 break;
474 case CALC:
475 sExtension = ".ods";
476 break;
477 default:
478 sExtension = ".UNKNOWN";
480 return sExtension;
482 private String getFileFormat(int _nType)
484 String sFileType;
485 switch(_nType)
487 case WRITER:
488 sFileType = "writer8";
489 break;
490 case CALC:
491 sFileType = "calc8";
492 break;
493 default:
494 sFileType = "UNKNOWN";
496 return sFileType;
499 private String getOutputPath(int _nType)
501 String sOutputPath = (String)param.get( convwatch.PropertyName.DOC_COMPARATOR_OUTPUT_PATH );
503 if (!sOutputPath.endsWith("/") || // construct the output file name
504 !sOutputPath.endsWith("\\"))
506 sOutputPath += System.getProperty("file.separator");
508 sOutputPath += "tmp_123";
509 sOutputPath += System.getProperty("file.separator");
511 // sOutputPath += getFileFormat(_nType);
512 // sOutputPath += System.getProperty("file.separator");
514 File aOutputFile = new File(sOutputPath); // create the directory of the given output path
515 aOutputFile.mkdirs();
517 return sOutputPath;
521 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH
523 private void storeComponent(String _sName, Object _xComponent, int _nType)
525 String sOutputPath = getOutputPath(_nType);
527 // add DocumentPoolName
528 sOutputPath += getDocumentPoolName(_nType);
529 sOutputPath += System.getProperty("file.separator");
531 File aOutputFile = new File(sOutputPath); // create the directory of the given output path
532 aOutputFile.mkdirs();
534 sOutputPath += _sName;
535 sOutputPath += getFormatExtension(_nType);
537 String sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputPath);
539 ArrayList aPropertyList = new ArrayList(); // set some properties for storeAsURL
541 PropertyValue aFileFormat = new PropertyValue();
542 aFileFormat.Name = "FilterName";
543 aFileFormat.Value = getFileFormat(_nType);
544 aPropertyList.add(aFileFormat);
546 PropertyValue aOverwrite = new PropertyValue(); // always overwrite already exist files
547 aOverwrite.Name = "Overwrite";
548 aOverwrite.Value = Boolean.TRUE;
549 aPropertyList.add(aOverwrite);
551 // store the document in an other directory
552 XStorable aStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, _xComponent);
553 if (aStorable != null)
555 log.println("store document as URL: '" + sOutputURL + "'");
558 aStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
560 catch (com.sun.star.io.IOException e)
562 log.println("ERROR: Exception caught");
563 log.println("Can't write document URL: '" + sOutputURL + "'");
564 log.println("Message: " + e.getMessage());
569 private XComponent loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList)
571 XComponent xDocComponent = null;
572 XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, _xComponent );
576 PropertyValue[] aLoadProperties = PropertyHelper.createPropertyValueArrayFormArrayList(_aPropertyList);
577 log.println("Load component: '" + _sName + "'");
578 xDocComponent = xComponentLoader.loadComponentFromURL(_sName, "_blank", 0, aLoadProperties);
580 catch (com.sun.star.io.IOException e)
582 log.println("ERROR: Exception caught");
583 log.println("Can't load document '" + _sName + "'");
584 log.println("Message: " + e.getMessage());
586 catch (com.sun.star.lang.IllegalArgumentException e)
588 log.println("ERROR: Exception caught");
589 log.println("Illegal Arguments given to loadComponentFromURL.");
590 log.println("Message: " + e.getMessage());
592 return xDocComponent;
595 private void closeComponent(XComponent _xDoc)
597 // Close the document
598 XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, _xDoc);
601 xCloseable.close(true);
603 catch (com.sun.star.util.CloseVetoException e)
605 log.println("ERROR: CloseVetoException caught");
606 log.println("CloseVetoException occured Can't close document.");
607 log.println("Message: " + e.getMessage());