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 .
21 import com
.sun
.star
.beans
.NamedValue
;
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
;
44 import lib
.TestEnvironment
;
45 import lib
.TestParameters
;
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 public void initialize( TestParameters Param
, PrintWriter log
) {
56 if (! Param
.containsKey("uniqueSuffix")){
57 Param
.put("uniqueSuffix", new Integer(0));
62 * Creating a Testenvironment for the interfaces to be tested. <p>
63 * Creates <code>MailMerge</code> service * Object relations created :
65 * <li> <code>'executeArgs'</code> for
66 * {@link ifc.text._XMailMergeBroadcaster} : NamedValue[]</li>
67 * <li> <code>'Job'</code> for
68 * {@link ifc.text._XMailMergeBroadcaster} : XJob</li>
69 * <li> <code>'XJobArgs'</code> for
70 * {@link ifc.task._XJob} : Object[]</li>
73 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
75 XInterface oObj
= null;
76 XInterface oRowSet
= null;
79 log
.println(" instantiate MailMerge service");
81 oObj
= (XInterface
) ( (XMultiServiceFactory
) Param
.getMSF()).createInstance
82 ("com.sun.star.text.MailMerge");
83 } catch (Exception e
) {
84 throw new StatusException("Can't create object environment", e
) ;
87 // <set some variables>
88 String cTestDoc
= utils
.getFullTestURL("MailMerge.sxw");
89 //cMailMerge_DocumentURL = cTestDoc
90 String cOutputURL
= utils
.getOfficeTemp( (XMultiServiceFactory
) Param
.getMSF());
91 String cDataSourceName
= "Bibliography";
92 String cDataCommand
= "biblio";
93 Object
[] sel
= new Object
[2];
96 Object
[] myBookMarks
= new Object
[2];
97 // </set some variables>
99 // <create XResultSet>
100 log
.println("create a XResultSet");
102 oRowSet
= (XInterface
) ( (XMultiServiceFactory
) Param
.getMSF()).createInstance
103 ("com.sun.star.sdb.RowSet");
104 } catch (Exception e
) {
105 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e
);
107 XPropertySet oRowSetProps
= UnoRuntime
.queryInterface(XPropertySet
.class, oRowSet
);
108 XRowSet xRowSet
= UnoRuntime
.queryInterface(XRowSet
.class, oRowSet
);
110 oRowSetProps
.setPropertyValue("DataSourceName",cDataSourceName
);
111 oRowSetProps
.setPropertyValue("Command",cDataCommand
);
112 oRowSetProps
.setPropertyValue("CommandType", new Integer(CommandType
.TABLE
));
113 } catch (UnknownPropertyException e
) {
114 throw new StatusException("Can't set properties on oRowSet", e
);
115 } catch (PropertyVetoException e
) {
116 throw new StatusException("Can't set properties on oRowSet", e
);
117 } catch (IllegalArgumentException e
) {
118 throw new StatusException("Can't set properties on oRowSet", e
);
119 } catch (WrappedTargetException e
) {
120 throw new StatusException("Can't set properties on oRowSet", e
);
124 } catch (SQLException e
) {
125 throw new StatusException("Can't execute oRowSet", e
);
128 XResultSet oResultSet
= UnoRuntime
.queryInterface(XResultSet
.class, oRowSet
);
133 // <create Bookmarks>
134 log
.println("create bookmarks");
136 XRowLocate oRowLocate
= UnoRuntime
.queryInterface(
137 XRowLocate
.class, oResultSet
);
139 myBookMarks
[0] = oRowLocate
.getBookmark();
141 myBookMarks
[1] = oRowLocate
.getBookmark();
142 } catch (SQLException e
) {
143 throw new StatusException("Cant get Bookmarks", e
);
145 // </create Bookmarks>
147 // <fill object with values>
149 log
.println("fill MailMerge with default connection");
151 XPropertySet oObjProps
= UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
153 oObjProps
.setPropertyValue("ActiveConnection", getLocalXConnection(Param
));
154 oObjProps
.setPropertyValue("DataSourceName", cDataSourceName
);
155 oObjProps
.setPropertyValue("Command", cDataCommand
);
156 oObjProps
.setPropertyValue("CommandType", new Integer(CommandType
.TABLE
));
157 oObjProps
.setPropertyValue("OutputType", new Short(MailMergeType
.FILE
));
158 oObjProps
.setPropertyValue("DocumentURL", cTestDoc
);
159 oObjProps
.setPropertyValue("OutputURL", cOutputURL
);
160 oObjProps
.setPropertyValue("FileNamePrefix", "Author");
161 oObjProps
.setPropertyValue("FileNameFromColumn", new Boolean(false));
162 oObjProps
.setPropertyValue("Selection", new Object
[0]);
164 } catch (UnknownPropertyException e
) {
165 throw new StatusException("Can't set properties on oObj", e
);
166 } catch (PropertyVetoException e
) {
167 throw new StatusException("Can't set properties on oObj", e
);
168 } catch (IllegalArgumentException e
) {
169 throw new StatusException("Can't set properties on oObj", e
);
170 } catch (WrappedTargetException e
) {
171 throw new StatusException("Can't set properties on oObj", e
);
173 // </fill object with values>
176 // <create object relations>
177 Object
[] vXJobArgs
= new Object
[4];
178 NamedValue
[] vXJobArg0
= new NamedValue
[8];
179 NamedValue
[] vXJobArg1
= new NamedValue
[7];
180 NamedValue
[] vXJobArg2
= new NamedValue
[10];
181 NamedValue
[] vXJobArg3
= new NamedValue
[0];
184 vXJobArg0
[0] = new NamedValue("DataSourceName", cDataSourceName
);
185 vXJobArg0
[1] = new NamedValue("Command", cDataCommand
);
186 vXJobArg0
[2] = new NamedValue("CommandType",new Integer(CommandType
.TABLE
));
187 vXJobArg0
[3] = new NamedValue("OutputType",new Short(MailMergeType
.FILE
));
188 vXJobArg0
[4] = new NamedValue("DocumentURL", cTestDoc
);
189 vXJobArg0
[5] = new NamedValue("OutputURL", cOutputURL
);
190 vXJobArg0
[6] = new NamedValue("FileNamePrefix", "Identifier");
191 vXJobArg0
[7] = new NamedValue("FileNameFromColumn", new Boolean(true));
194 vXJobArg1
[0] = new NamedValue("DataSourceName", cDataSourceName
);
195 vXJobArg1
[1] = new NamedValue("Command", cDataCommand
);
196 vXJobArg1
[2] = new NamedValue("CommandType",new Integer(CommandType
.TABLE
));
197 vXJobArg1
[3] = new NamedValue("OutputType",
198 new Short(MailMergeType
.PRINTER
));
199 vXJobArg1
[4] = new NamedValue("DocumentURL", cTestDoc
);
200 vXJobArg1
[5] = new NamedValue("FileNamePrefix", "Author");
201 vXJobArg1
[6] = new NamedValue("FileNameFromColumn", new Boolean(true));
204 vXJobArg2
[0] = new NamedValue("ActiveConnection", getLocalXConnection(Param
));
205 vXJobArg2
[1] = new NamedValue("DataSourceName", cDataSourceName
);
206 vXJobArg2
[2] = new NamedValue("Command", cDataCommand
);
207 vXJobArg2
[3] = new NamedValue("CommandType",new Integer(CommandType
.TABLE
));
208 vXJobArg2
[4] = new NamedValue("OutputType",
209 new Short(MailMergeType
.FILE
));
210 vXJobArg2
[5] = new NamedValue("ResultSet", oResultSet
);
211 vXJobArg2
[6] = new NamedValue("OutputURL", cOutputURL
);
212 vXJobArg2
[7] = new NamedValue("FileNamePrefix", "Identifier");
213 vXJobArg2
[8] = new NamedValue("FileNameFromColumn", new Boolean(true));
214 vXJobArg2
[9] = new NamedValue("Selection", myBookMarks
);
216 vXJobArgs
[0] = vXJobArg0
;
217 vXJobArgs
[1] = vXJobArg1
;
218 vXJobArgs
[2] = vXJobArg2
;
219 vXJobArgs
[3] = vXJobArg3
;
222 Job
= UnoRuntime
.queryInterface(XJob
.class, oObj
);
224 Job
.execute(vXJobArg2
);
225 } catch ( IllegalArgumentException e
){
226 System
.out
.println(e
.toString());
227 } catch ( Exception e
){
228 System
.out
.println(e
.toString());
232 // <create XResultSet>
233 log
.println("create XResultSet");
236 oRowSet
= (XInterface
) ( (XMultiServiceFactory
) Param
.getMSF()).createInstance
237 ("com.sun.star.sdb.RowSet");
238 } catch (Exception e
) {
239 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e
);
241 oRowSetProps
= UnoRuntime
.queryInterface(XPropertySet
.class, oRowSet
);
243 xRowSet
= UnoRuntime
.queryInterface(XRowSet
.class, oRowSet
);
246 oRowSetProps
.setPropertyValue("DataSourceName",cDataSourceName
);
247 oRowSetProps
.setPropertyValue("Command",cDataCommand
);
248 oRowSetProps
.setPropertyValue("CommandType", new Integer(CommandType
.TABLE
));
249 } catch (UnknownPropertyException e
) {
250 throw new StatusException("Can't set properties on oRowSet", e
);
251 } catch (PropertyVetoException e
) {
252 throw new StatusException("Can't set properties on oRowSet", e
);
253 } catch (IllegalArgumentException e
) {
254 throw new StatusException("Can't set properties on oRowSet", e
);
255 } catch (WrappedTargetException e
) {
256 throw new StatusException("Can't set properties on oRowSet", e
);
260 } catch (SQLException e
) {
261 throw new StatusException("Can't execute oRowSet", e
);
264 oResultSet
= UnoRuntime
.queryInterface(XResultSet
.class, oRowSet
);
266 XResultSet oMMXResultSet
= null;
268 oMMXResultSet
= UnoRuntime
.queryInterface(XResultSet
.class,
269 ( (XMultiServiceFactory
)
270 Param
.getMSF()).createInstance("com.sun.star.sdb.RowSet"));
272 } catch (Exception e
) {
273 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e
);
275 // </create object relations>
277 TestEnvironment tEnv
= new TestEnvironment(oObj
) ;
279 // <adding object relations>
281 // com.sun.star.sdb.DataAccessDescriptor
282 tEnv
.addObjRelation("DataAccessDescriptor.XResultSet", oResultSet
);
283 tEnv
.addObjRelation("DataAccessDescriptor.XConnection", getRemoteXConnection(Param
));
285 // com.sun.star.text.MailMaerge
286 tEnv
.addObjRelation("MailMerge.XConnection", getRemoteXConnection(Param
));
287 tEnv
.addObjRelation("MailMerge.XResultSet", oMMXResultSet
);
289 // com.sun.star.text.XMailMergeBroadcaster
290 tEnv
.addObjRelation( "executeArgs", vXJobArg0
);
291 tEnv
.addObjRelation( "Job", Job
);
293 // com.sun.star.task.XJob
294 tEnv
.addObjRelation("XJobArgs", vXJobArgs
);
296 // </adding object relations>
301 private XConnection
getRemoteXConnection(TestParameters Param
){
303 log
.println("create remote connection");
305 String databaseName
= null ;
306 XMultiServiceFactory xMSF
= null ;
307 int uniqueSuffix
= Param
.getInt("uniqueSuffix");
310 xMSF
= (XMultiServiceFactory
)Param
.getMSF();
311 xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
313 // retrieving temp directory for database
314 String tmpDatabaseUrl
= utils
.getOfficeTempDir((XMultiServiceFactory
)Param
.getMSF());
316 databaseName
= "NewDatabaseSource" + uniqueSuffix
;
318 String tmpDatabaseFile
= tmpDatabaseUrl
+ databaseName
+ ".odb";
319 System
.out
.println("try to delete '"+tmpDatabaseFile
+"'");
320 utils
.deleteFile(((XMultiServiceFactory
) Param
.getMSF()), tmpDatabaseFile
);
323 tmpDatabaseUrl
= "sdbc:dbase:file:///" + tmpDatabaseUrl
;
325 // Creating new DBase data source in the TEMP directory
326 XInterface newSource
= (XInterface
) xMSF
.createInstance
327 ("com.sun.star.sdb.DataSource") ;
329 XPropertySet xSrcProp
= UnoRuntime
.queryInterface(XPropertySet
.class, newSource
);
331 xSrcProp
.setPropertyValue("URL", tmpDatabaseUrl
) ;
333 DBTools dbt
= new DBTools( (XMultiServiceFactory
)Param
.getMSF(), log
);
334 // registering source in DatabaseContext
335 log
.println("register database '"+tmpDatabaseUrl
+"' as '"+databaseName
+"'" );
336 dbt
.reRegisterDB(databaseName
, newSource
) ;
339 Param
.put("uniqueSuffix", new Integer(uniqueSuffix
));
341 return dbt
.connectToSource(newSource
);
343 catch( Exception e
) {
345 Param
.put("uniqueSuffix", new Integer(uniqueSuffix
));
346 log
.println("could not register new database" );
348 throw new StatusException("could not register new database", e
) ;
352 private XConnection
getLocalXConnection(TestParameters Param
){
354 log
.println("create local connection");
356 XInterface oDataCont
= null;
358 oDataCont
= (XInterface
)( (XMultiServiceFactory
) Param
.getMSF()).createInstance
359 ("com.sun.star.sdb.DatabaseContext");
360 } catch(Exception e
) {
361 throw new StatusException("Couldn't create instance of 'com.sun.star.sdb.DatabaseContext'", e
);
363 XNameAccess xNADataCont
= UnoRuntime
.queryInterface(XNameAccess
.class, oDataCont
);
365 String
[] dataNames
= xNADataCont
.getElementNames();
368 for (int i
= 0; i
< dataNames
.length
; i
++){
369 if (dataNames
[i
].startsWith("Biblio")) dataName
=dataNames
[i
];
374 Object oDataBase
= xNADataCont
.getByName(dataName
);
375 XDataSource xDataSource
= UnoRuntime
.queryInterface(XDataSource
.class, oDataBase
);
377 return xDataSource
.getConnection("","");
379 } catch ( NoSuchElementException e
){
380 throw new StatusException("Couldn't get registered data base", e
);
381 } catch ( WrappedTargetException e
){
382 throw new StatusException("Couldn't get registered data base", e
);
383 } catch ( SQLException e
){
384 throw new StatusException("Couldn't get XConnection from registered data base", e
);
389 protected void cleanup(TestParameters Param
, PrintWriter log
) {
390 log
.println("closing connections...");
391 XMultiServiceFactory xMsf
= (XMultiServiceFactory
) Param
.getMSF();
392 DBTools dbt
= new DBTools( xMsf
, log
);
394 if (Param
.containsKey("uniqueSuffix")){
395 int uniqueSuffix
= Param
.getInt("uniqueSuffix");
397 String databaseName
= "";
398 while (uniqueSuffix
>= 0){
400 databaseName
= "NewDatabaseSource" + uniqueSuffix
;
402 log
.println("revoke '"+databaseName
+"'");
405 dbt
.revokeDB(databaseName
);
406 } catch (com
.sun
.star
.uno
.Exception e
){