Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXMailMerge.java
blob4bbd95dcf74a4940c92bd6931ee3d3c600dabbfe
1 /*
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 .
19 package mod._sw;
21 import com.sun.star.beans.NamedValue;
23 import util.DBTools;
24 import util.utils;
25 import com.sun.star.beans.PropertyVetoException;
26 import com.sun.star.beans.UnknownPropertyException;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.container.NoSuchElementException;
29 import com.sun.star.container.XNameAccess;
30 import com.sun.star.lang.IllegalArgumentException;
31 import com.sun.star.lang.WrappedTargetException;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.sdb.CommandType;
34 import com.sun.star.sdbc.*;
35 import com.sun.star.sdbcx.XRowLocate;
36 import com.sun.star.task.XJob;
37 import com.sun.star.text.MailMergeType;
38 import com.sun.star.uno.Exception;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import java.io.PrintWriter;
42 import lib.StatusException;
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
47 /**
48 * Here <code>com.sun.star.text.MailMerge</code> service is tested.<p>
49 * @see com.sun.star.text.MailMerge
50 * @see com.sun.star.task.XJob
51 * @see com.sun.star.text.XMailMergeBroadcaster
53 public class SwXMailMerge extends TestCase {
55 @Override
56 public void initialize( TestParameters Param, PrintWriter log ) {
57 if (! Param.containsKey("uniqueSuffix")){
58 Param.put("uniqueSuffix", Integer.valueOf(0));
62 /**
63 * Creating a Testenvironment for the interfaces to be tested. <p>
64 * Creates <code>MailMerge</code> service * Object relations created :
65 * <ul>
66 * <li> <code>'executeArgs'</code> for
67 * {@link ifc.text._XMailMergeBroadcaster} : NamedValue[]</li>
68 * <li> <code>'Job'</code> for
69 * {@link ifc.text._XMailMergeBroadcaster} : XJob</li>
70 * <li> <code>'XJobArgs'</code> for
71 * {@link ifc.task._XJob} : Object[]</li>
72 * </ul>
74 @Override
75 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
77 XInterface oObj = null;
78 XInterface oRowSet = null;
79 XJob Job = null;
81 log.println(" instantiate MailMerge service");
82 try {
83 oObj = (XInterface) Param.getMSF().createInstance
84 ("com.sun.star.text.MailMerge");
85 } catch (Exception e) {
86 throw new StatusException("Can't create object environment", e) ;
89 // <set some variables>
90 String cTestDoc = utils.getFullTestURL("MailMerge.sxw");
91 //cMailMerge_DocumentURL = cTestDoc
92 String cOutputURL = utils.getOfficeTemp( Param.getMSF());
93 String cDataSourceName = "Bibliography";
94 String cDataCommand = "biblio";
95 Object[] sel = new Object[2];
96 sel[0] = new int[2];
97 sel[1] = new int[5];
98 Object[] myBookMarks = new Object[2];
99 // </set some variables>
101 // <create XResultSet>
102 log.println("create a XResultSet");
103 try {
104 oRowSet = (XInterface) Param.getMSF().createInstance
105 ("com.sun.star.sdb.RowSet");
106 } catch (Exception e) {
107 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e);
109 XPropertySet oRowSetProps = UnoRuntime.queryInterface(XPropertySet.class, oRowSet);
110 XRowSet xRowSet = UnoRuntime.queryInterface(XRowSet.class, oRowSet);
111 try {
112 oRowSetProps.setPropertyValue("DataSourceName",cDataSourceName);
113 oRowSetProps.setPropertyValue("Command",cDataCommand);
114 oRowSetProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
115 } catch (UnknownPropertyException e) {
116 throw new StatusException("Can't set properties on oRowSet", e);
117 } catch (PropertyVetoException e) {
118 throw new StatusException("Can't set properties on oRowSet", e);
119 } catch (IllegalArgumentException e) {
120 throw new StatusException("Can't set properties on oRowSet", e);
121 } catch (WrappedTargetException e) {
122 throw new StatusException("Can't set properties on oRowSet", e);
124 try {
125 xRowSet.execute();
126 } catch (SQLException e) {
127 throw new StatusException("Can't execute oRowSet", e);
130 XResultSet oResultSet = UnoRuntime.queryInterface(XResultSet.class, oRowSet);
135 // <create Bookmarks>
136 log.println("create bookmarks");
137 try {
138 XRowLocate oRowLocate = UnoRuntime.queryInterface(
139 XRowLocate.class, oResultSet);
140 oResultSet.first();
141 myBookMarks[0] = oRowLocate.getBookmark();
142 oResultSet.next();
143 myBookMarks[1] = oRowLocate.getBookmark();
144 } catch (SQLException e) {
145 throw new StatusException("Can't get Bookmarks", e);
147 // </create Bookmarks>
149 // <fill object with values>
151 log.println("fill MailMerge with default connection");
153 XPropertySet oObjProps = UnoRuntime.queryInterface(XPropertySet.class, oObj);
154 try {
155 oObjProps.setPropertyValue("ActiveConnection", getLocalXConnection(Param));
156 oObjProps.setPropertyValue("DataSourceName", cDataSourceName);
157 oObjProps.setPropertyValue("Command", cDataCommand);
158 oObjProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
159 oObjProps.setPropertyValue("OutputType", Short.valueOf(MailMergeType.FILE));
160 oObjProps.setPropertyValue("DocumentURL", cTestDoc);
161 oObjProps.setPropertyValue("OutputURL", cOutputURL);
162 oObjProps.setPropertyValue("FileNamePrefix", "Author");
163 oObjProps.setPropertyValue("FileNameFromColumn", Boolean.FALSE);
164 oObjProps.setPropertyValue("Selection", new Object[0]);
166 } catch (UnknownPropertyException e) {
167 throw new StatusException("Can't set properties on oObj", e);
168 } catch (PropertyVetoException e) {
169 throw new StatusException("Can't set properties on oObj", e);
170 } catch (IllegalArgumentException e) {
171 throw new StatusException("Can't set properties on oObj", e);
172 } catch (WrappedTargetException e) {
173 throw new StatusException("Can't set properties on oObj", e);
175 // </fill object with values>
178 // <create object relations>
179 Object[] vXJobArgs = new Object[4];
180 NamedValue[] vXJobArg0 = new NamedValue[8];
181 NamedValue[] vXJobArg1 = new NamedValue[7];
182 NamedValue[] vXJobArg2 = new NamedValue[10];
183 NamedValue[] vXJobArg3 = new NamedValue[0];
185 // first Arguments
186 vXJobArg0[0] = new NamedValue("DataSourceName", cDataSourceName);
187 vXJobArg0[1] = new NamedValue("Command", cDataCommand);
188 vXJobArg0[2] = new NamedValue("CommandType",Integer.valueOf(CommandType.TABLE));
189 vXJobArg0[3] = new NamedValue("OutputType",Short.valueOf(MailMergeType.FILE));
190 vXJobArg0[4] = new NamedValue("DocumentURL", cTestDoc);
191 vXJobArg0[5] = new NamedValue("OutputURL", cOutputURL);
192 vXJobArg0[6] = new NamedValue("FileNamePrefix", "Identifier");
193 vXJobArg0[7] = new NamedValue("FileNameFromColumn", Boolean.TRUE);
195 //second Arguments
196 vXJobArg1[0] = new NamedValue("DataSourceName", cDataSourceName);
197 vXJobArg1[1] = new NamedValue("Command", cDataCommand);
198 vXJobArg1[2] = new NamedValue("CommandType",Integer.valueOf(CommandType.TABLE));
199 vXJobArg1[3] = new NamedValue("OutputType",
200 Short.valueOf(MailMergeType.PRINTER));
201 vXJobArg1[4] = new NamedValue("DocumentURL", cTestDoc);
202 vXJobArg1[5] = new NamedValue("FileNamePrefix", "Author");
203 vXJobArg1[6] = new NamedValue("FileNameFromColumn", Boolean.TRUE);
205 // third Arguments
206 vXJobArg2[0] = new NamedValue("ActiveConnection", getLocalXConnection(Param));
207 vXJobArg2[1] = new NamedValue("DataSourceName", cDataSourceName);
208 vXJobArg2[2] = new NamedValue("Command", cDataCommand);
209 vXJobArg2[3] = new NamedValue("CommandType",Integer.valueOf(CommandType.TABLE));
210 vXJobArg2[4] = new NamedValue("OutputType",
211 Short.valueOf(MailMergeType.FILE));
212 vXJobArg2[5] = new NamedValue("ResultSet", oResultSet);
213 vXJobArg2[6] = new NamedValue("OutputURL", cOutputURL);
214 vXJobArg2[7] = new NamedValue("FileNamePrefix", "Identifier");
215 vXJobArg2[8] = new NamedValue("FileNameFromColumn", Boolean.TRUE);
216 vXJobArg2[9] = new NamedValue("Selection", myBookMarks);
218 vXJobArgs[0] = vXJobArg0;
219 vXJobArgs[1] = vXJobArg1;
220 vXJobArgs[2] = vXJobArg2;
221 vXJobArgs[3] = vXJobArg3;
224 Job = UnoRuntime.queryInterface(XJob.class, oObj);
225 try{
226 Job.execute(vXJobArg2);
227 } catch ( IllegalArgumentException e){
228 System.out.println(e.toString());
229 } catch ( Exception e){
230 System.out.println(e.toString());
234 // <create XResultSet>
235 log.println("create XResultSet");
237 try {
238 oRowSet = (XInterface) Param.getMSF().createInstance
239 ("com.sun.star.sdb.RowSet");
240 } catch (Exception e) {
241 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e);
243 oRowSetProps = UnoRuntime.queryInterface(XPropertySet.class, oRowSet);
245 xRowSet = UnoRuntime.queryInterface(XRowSet.class, oRowSet);
247 try {
248 oRowSetProps.setPropertyValue("DataSourceName",cDataSourceName);
249 oRowSetProps.setPropertyValue("Command",cDataCommand);
250 oRowSetProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
251 } catch (UnknownPropertyException e) {
252 throw new StatusException("Can't set properties on oRowSet", e);
253 } catch (PropertyVetoException e) {
254 throw new StatusException("Can't set properties on oRowSet", e);
255 } catch (IllegalArgumentException e) {
256 throw new StatusException("Can't set properties on oRowSet", e);
257 } catch (WrappedTargetException e) {
258 throw new StatusException("Can't set properties on oRowSet", e);
260 try {
261 xRowSet.execute();
262 } catch (SQLException e) {
263 throw new StatusException("Can't execute oRowSet", e);
266 oResultSet = UnoRuntime.queryInterface(XResultSet.class, oRowSet);
268 XResultSet oMMXResultSet = null;
269 try {
270 oMMXResultSet = UnoRuntime.queryInterface(XResultSet.class,
271 Param.getMSF().createInstance("com.sun.star.sdb.RowSet"));
273 } catch (Exception e) {
274 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e);
276 // </create object relations>
278 TestEnvironment tEnv = new TestEnvironment(oObj) ;
280 // <adding object relations>
282 // com.sun.star.sdb.DataAccessDescriptor
283 tEnv.addObjRelation("DataAccessDescriptor.XResultSet", oResultSet);
284 tEnv.addObjRelation("DataAccessDescriptor.XConnection", getRemoteXConnection(Param));
286 // com.sun.star.text.MailMaerge
287 tEnv.addObjRelation("MailMerge.XConnection", getRemoteXConnection(Param));
288 tEnv.addObjRelation("MailMerge.XResultSet", oMMXResultSet);
290 // com.sun.star.text.XMailMergeBroadcaster
291 tEnv.addObjRelation( "executeArgs", vXJobArg0);
292 tEnv.addObjRelation( "Job", Job);
294 // com.sun.star.task.XJob
295 tEnv.addObjRelation("XJobArgs", vXJobArgs);
297 // </adding object relations>
299 return tEnv ;
302 private XConnection getRemoteXConnection(TestParameters Param){
304 log.println("create remote connection");
306 String databaseName = null ;
307 XMultiServiceFactory xMSF = null ;
308 int uniqueSuffix = Param.getInt("uniqueSuffix");
310 try {
311 xMSF = Param.getMSF();
312 xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
314 // retrieving temp directory for database
315 String tmpDatabaseUrl = utils.getOfficeTempDir(Param.getMSF());
317 databaseName = "NewDatabaseSource" + uniqueSuffix ;
319 String tmpDatabaseFile = tmpDatabaseUrl + databaseName + ".odb";
320 System.out.println("try to delete '"+tmpDatabaseFile+"'");
321 utils.deleteFile((Param.getMSF()), tmpDatabaseFile);
324 tmpDatabaseUrl = "sdbc:dbase:file:///" + tmpDatabaseUrl ;
326 // Creating new DBase data source in the TEMP directory
327 XInterface newSource = (XInterface) xMSF.createInstance
328 ("com.sun.star.sdb.DataSource") ;
330 XPropertySet xSrcProp = UnoRuntime.queryInterface(XPropertySet.class, newSource);
332 xSrcProp.setPropertyValue("URL", tmpDatabaseUrl) ;
334 DBTools dbt = new DBTools( Param.getMSF() );
335 // registering source in DatabaseContext
336 log.println("register database '"+tmpDatabaseUrl+"' as '"+databaseName+"'" );
337 dbt.reRegisterDB(databaseName, newSource) ;
339 uniqueSuffix++;
340 Param.put("uniqueSuffix", Integer.valueOf(uniqueSuffix));
342 return dbt.connectToSource(newSource);
344 catch( Exception e ) {
345 uniqueSuffix++;
346 Param.put("uniqueSuffix", Integer.valueOf(uniqueSuffix));
347 log.println("could not register new database" );
348 e.printStackTrace();
349 throw new StatusException("could not register new database", e) ;
353 private XConnection getLocalXConnection(TestParameters Param){
355 log.println("create local connection");
357 XInterface oDataCont = null;
358 try {
359 oDataCont = (XInterface)Param.getMSF().createInstance
360 ("com.sun.star.sdb.DatabaseContext");
361 } catch(Exception e) {
362 throw new StatusException("Couldn't create instance of 'com.sun.star.sdb.DatabaseContext'", e);
364 XNameAccess xNADataCont = UnoRuntime.queryInterface(XNameAccess.class, oDataCont);
366 String[] dataNames = xNADataCont.getElementNames();
368 String dataName="";
369 for (int i = 0; i < dataNames.length; i++){
370 if (dataNames[i].startsWith("Biblio")) dataName=dataNames[i];
373 try{
375 Object oDataBase = xNADataCont.getByName(dataName);
376 XDataSource xDataSource = UnoRuntime.queryInterface(XDataSource.class, oDataBase);
378 return xDataSource.getConnection("","");
380 } catch ( NoSuchElementException e){
381 throw new StatusException("Couldn't get registered data base", e);
382 } catch ( WrappedTargetException e){
383 throw new StatusException("Couldn't get registered data base", e);
384 } catch ( SQLException e){
385 throw new StatusException("Couldn't get XConnection from registered data base", e);
390 @Override
391 protected void cleanup(TestParameters Param, PrintWriter log) {
392 log.println("closing connections...");
393 XMultiServiceFactory xMsf = Param.getMSF();
394 DBTools dbt = new DBTools( xMsf );
396 if (Param.containsKey("uniqueSuffix")){
397 int uniqueSuffix = Param.getInt("uniqueSuffix");
398 uniqueSuffix--;
399 String databaseName = "";
400 while (uniqueSuffix >= 0){
402 databaseName = "NewDatabaseSource" + uniqueSuffix ;
404 log.println("revoke '"+databaseName+"'");
406 try{
407 dbt.revokeDB(databaseName);
408 } catch (com.sun.star.uno.Exception e){
411 uniqueSuffix--;