1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SwXMailMerge.java,v $
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 ************************************************************************/
33 import com
.sun
.star
.beans
.NamedValue
;
37 import com
.sun
.star
.beans
.PropertyVetoException
;
38 import com
.sun
.star
.beans
.UnknownPropertyException
;
39 import com
.sun
.star
.beans
.XPropertySet
;
40 import com
.sun
.star
.container
.NoSuchElementException
;
41 import com
.sun
.star
.container
.XNameAccess
;
42 import com
.sun
.star
.lang
.IllegalArgumentException
;
43 import com
.sun
.star
.lang
.WrappedTargetException
;
44 import com
.sun
.star
.lang
.XMultiServiceFactory
;
45 import com
.sun
.star
.sdb
.CommandType
;
46 import com
.sun
.star
.sdbc
.*;
47 import com
.sun
.star
.sdbcx
.XRowLocate
;
48 import com
.sun
.star
.task
.XJob
;
49 import com
.sun
.star
.text
.MailMergeType
;
50 import com
.sun
.star
.uno
.Exception
;
51 import com
.sun
.star
.uno
.UnoRuntime
;
52 import com
.sun
.star
.uno
.XInterface
;
53 import java
.io
.PrintWriter
;
54 import lib
.StatusException
;
56 import lib
.TestEnvironment
;
57 import lib
.TestParameters
;
60 * Here <code>com.sun.star.text.MailMerge</code> service is tested.<p>
61 * @see com.sun.star.text.MailMerge
62 * @see com.sun.star.task.XJob
63 * @see com.sun.star.text.XMailMergeBroadcaster
65 public class SwXMailMerge
extends TestCase
{
67 public void initialize( TestParameters Param
, PrintWriter log
) {
68 if (! Param
.containsKey("uniqueSuffix")){
69 Param
.put("uniqueSuffix", new Integer(0));
74 * Creating a Testenvironment for the interfaces to be tested. <p>
75 * Creates <code>MailMerge</code> service * Object relations created :
77 * <li> <code>'executeArgs'</code> for
78 * {@link ifc.text._XMailMergeBroadcaster} : NamedValue[]</li>
79 * <li> <code>'Job'</code> for
80 * {@link ifc.text._XMailMergeBroadcaster} : XJob</li>
81 * <li> <code>'XJobArgs'</code> for
82 * {@link ifc.task._XJob} : Object[]</li>
85 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
87 XInterface oObj
= null;
88 XInterface oRowSet
= null;
89 Object oConnection
= null;
92 log
.println(" instantiate MailMerge service");
94 oObj
= (XInterface
) ( (XMultiServiceFactory
) Param
.getMSF()).createInstance
95 ("com.sun.star.text.MailMerge");
96 } catch (Exception e
) {
97 throw new StatusException("Can't create object environment", e
) ;
100 // <set some variables>
101 String cTestDoc
= utils
.getFullTestURL("MailMerge.sxw");
102 //cMailMerge_DocumentURL = cTestDoc
103 String cOutputURL
= utils
.getOfficeTemp( (XMultiServiceFactory
) Param
.getMSF());
104 String cDataSourceName
= "Bibliography";
105 String cDataCommand
= "biblio";
106 Object
[] sel
= new Object
[2];
109 Object
[] myBookMarks
= new Object
[2];
110 // </set some variables>
112 // <create XResultSet>
113 log
.println("create a XResultSet");
115 oRowSet
= (XInterface
) ( (XMultiServiceFactory
) Param
.getMSF()).createInstance
116 ("com.sun.star.sdb.RowSet");
117 } catch (Exception e
) {
118 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e
);
120 XPropertySet oRowSetProps
= (XPropertySet
)
121 UnoRuntime
.queryInterface(XPropertySet
.class, oRowSet
);
122 XRowSet xRowSet
= (XRowSet
)
123 UnoRuntime
.queryInterface(XRowSet
.class, oRowSet
);
125 oRowSetProps
.setPropertyValue("DataSourceName",cDataSourceName
);
126 oRowSetProps
.setPropertyValue("Command",cDataCommand
);
127 oRowSetProps
.setPropertyValue("CommandType", new Integer(CommandType
.TABLE
));
128 } catch (UnknownPropertyException e
) {
129 throw new StatusException("Can't set properties on oRowSet", e
);
130 } catch (PropertyVetoException e
) {
131 throw new StatusException("Can't set properties on oRowSet", e
);
132 } catch (IllegalArgumentException e
) {
133 throw new StatusException("Can't set properties on oRowSet", e
);
134 } catch (WrappedTargetException e
) {
135 throw new StatusException("Can't set properties on oRowSet", e
);
139 } catch (SQLException e
) {
140 throw new StatusException("Can't execute oRowSet", e
);
143 XResultSet oResultSet
= (XResultSet
)
144 UnoRuntime
.queryInterface(XResultSet
.class, oRowSet
);
149 // <create Bookmarks>
150 log
.println("create bookmarks");
152 XRowLocate oRowLocate
= (XRowLocate
) UnoRuntime
.queryInterface(
153 XRowLocate
.class, oResultSet
);
155 myBookMarks
[0] = oRowLocate
.getBookmark();
157 myBookMarks
[1] = oRowLocate
.getBookmark();
158 } catch (SQLException e
) {
159 throw new StatusException("Cant get Bookmarks", e
);
161 // </create Bookmarks>
163 // <fill object with values>
165 log
.println("fill MailMerge with default connection");
167 XPropertySet oObjProps
= (XPropertySet
)
168 UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
170 oObjProps
.setPropertyValue("ActiveConnection", getLocalXConnection(Param
));
171 oObjProps
.setPropertyValue("DataSourceName", cDataSourceName
);
172 oObjProps
.setPropertyValue("Command", cDataCommand
);
173 oObjProps
.setPropertyValue("CommandType", new Integer(CommandType
.TABLE
));
174 oObjProps
.setPropertyValue("OutputType", new Short(MailMergeType
.FILE
));
175 oObjProps
.setPropertyValue("DocumentURL", cTestDoc
);
176 oObjProps
.setPropertyValue("OutputURL", cOutputURL
);
177 oObjProps
.setPropertyValue("FileNamePrefix", "Author");
178 oObjProps
.setPropertyValue("FileNameFromColumn", new Boolean(false));
179 oObjProps
.setPropertyValue("Selection", new Object
[0]);
181 } catch (UnknownPropertyException e
) {
182 throw new StatusException("Can't set properties on oObj", e
);
183 } catch (PropertyVetoException e
) {
184 throw new StatusException("Can't set properties on oObj", e
);
185 } catch (IllegalArgumentException e
) {
186 throw new StatusException("Can't set properties on oObj", e
);
187 } catch (WrappedTargetException e
) {
188 throw new StatusException("Can't set properties on oObj", e
);
190 // </fill object with values>
193 // <create object relations>
194 Object
[] vXJobArgs
= new Object
[4];
195 NamedValue
[] vXJobArg0
= new NamedValue
[8];
196 NamedValue
[] vXJobArg1
= new NamedValue
[7];
197 NamedValue
[] vXJobArg2
= new NamedValue
[10];
198 NamedValue
[] vXJobArg3
= new NamedValue
[0];
201 vXJobArg0
[0] = new NamedValue("DataSourceName", cDataSourceName
);
202 vXJobArg0
[1] = new NamedValue("Command", cDataCommand
);
203 vXJobArg0
[2] = new NamedValue("CommandType",new Integer(CommandType
.TABLE
));
204 vXJobArg0
[3] = new NamedValue("OutputType",new Short(MailMergeType
.FILE
));
205 vXJobArg0
[4] = new NamedValue("DocumentURL", cTestDoc
);
206 vXJobArg0
[5] = new NamedValue("OutputURL", cOutputURL
);
207 vXJobArg0
[6] = new NamedValue("FileNamePrefix", "Identifier");
208 vXJobArg0
[7] = new NamedValue("FileNameFromColumn", new Boolean(true));
211 vXJobArg1
[0] = new NamedValue("DataSourceName", cDataSourceName
);
212 vXJobArg1
[1] = new NamedValue("Command", cDataCommand
);
213 vXJobArg1
[2] = new NamedValue("CommandType",new Integer(CommandType
.TABLE
));
214 vXJobArg1
[3] = new NamedValue("OutputType",
215 new Short(MailMergeType
.PRINTER
));
216 vXJobArg1
[4] = new NamedValue("DocumentURL", cTestDoc
);
217 vXJobArg1
[5] = new NamedValue("FileNamePrefix", "Author");
218 vXJobArg1
[6] = new NamedValue("FileNameFromColumn", new Boolean(true));
221 vXJobArg2
[0] = new NamedValue("ActiveConnection", getLocalXConnection(Param
));
222 vXJobArg2
[1] = new NamedValue("DataSourceName", cDataSourceName
);
223 vXJobArg2
[2] = new NamedValue("Command", cDataCommand
);
224 vXJobArg2
[3] = new NamedValue("CommandType",new Integer(CommandType
.TABLE
));
225 vXJobArg2
[4] = new NamedValue("OutputType",
226 new Short(MailMergeType
.FILE
));
227 vXJobArg2
[5] = new NamedValue("ResultSet", oResultSet
);
228 vXJobArg2
[6] = new NamedValue("OutputURL", cOutputURL
);
229 vXJobArg2
[7] = new NamedValue("FileNamePrefix", "Identifier");
230 vXJobArg2
[8] = new NamedValue("FileNameFromColumn", new Boolean(true));
231 vXJobArg2
[9] = new NamedValue("Selection", myBookMarks
);
233 vXJobArgs
[0] = vXJobArg0
;
234 vXJobArgs
[1] = vXJobArg1
;
235 vXJobArgs
[2] = vXJobArg2
;
236 vXJobArgs
[3] = vXJobArg3
;
239 Job
= (XJob
) UnoRuntime
.queryInterface(XJob
.class, oObj
);
241 Job
.execute(vXJobArg2
);
242 } catch ( IllegalArgumentException e
){
243 System
.out
.println(e
.toString());
244 } catch ( Exception e
){
245 System
.out
.println(e
.toString());
249 // <create XResultSet>
250 log
.println("create XResultSet");
253 oRowSet
= (XInterface
) ( (XMultiServiceFactory
) Param
.getMSF()).createInstance
254 ("com.sun.star.sdb.RowSet");
255 } catch (Exception e
) {
256 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e
);
258 oRowSetProps
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, oRowSet
);
260 xRowSet
= (XRowSet
) UnoRuntime
.queryInterface(XRowSet
.class, oRowSet
);
263 oRowSetProps
.setPropertyValue("DataSourceName",cDataSourceName
);
264 oRowSetProps
.setPropertyValue("Command",cDataCommand
);
265 oRowSetProps
.setPropertyValue("CommandType", new Integer(CommandType
.TABLE
));
266 } catch (UnknownPropertyException e
) {
267 throw new StatusException("Can't set properties on oRowSet", e
);
268 } catch (PropertyVetoException e
) {
269 throw new StatusException("Can't set properties on oRowSet", e
);
270 } catch (IllegalArgumentException e
) {
271 throw new StatusException("Can't set properties on oRowSet", e
);
272 } catch (WrappedTargetException e
) {
273 throw new StatusException("Can't set properties on oRowSet", e
);
277 } catch (SQLException e
) {
278 throw new StatusException("Can't execute oRowSet", e
);
281 oResultSet
= (XResultSet
)
282 UnoRuntime
.queryInterface(XResultSet
.class, oRowSet
);
284 XResultSet oMMXResultSet
= null;
286 oMMXResultSet
= (XResultSet
)
287 UnoRuntime
.queryInterface(XResultSet
.class,
289 ( (XMultiServiceFactory
)
290 Param
.getMSF()).createInstance("com.sun.star.sdb.RowSet")));
292 } catch (Exception e
) {
293 throw new StatusException("Can't create com.sun.star.sdb.RowSet", e
);
295 // </create object relations>
297 TestEnvironment tEnv
= new TestEnvironment(oObj
) ;
299 // <adding object relations>
301 // com.sun.star.sdb.DataAccessDescriptor
302 tEnv
.addObjRelation("DataAccessDescriptor.XResultSet", oResultSet
);
303 tEnv
.addObjRelation("DataAccessDescriptor.XConnection", getRemoteXConnection(Param
));
305 // com.sun.star.text.MailMaerge
306 tEnv
.addObjRelation("MailMerge.XConnection", getRemoteXConnection(Param
));
307 tEnv
.addObjRelation("MailMerge.XResultSet", oMMXResultSet
);
309 // com.sun.star.text.XMailMergeBroadcaster
310 tEnv
.addObjRelation( "executeArgs", vXJobArg0
);
311 tEnv
.addObjRelation( "Job", Job
);
313 // com.sun.star.task.XJob
314 tEnv
.addObjRelation("XJobArgs", vXJobArgs
);
316 // </adding object relations>
321 private XConnection
getRemoteXConnection(TestParameters Param
){
323 log
.println("create remote connection");
325 String databaseName
= null ;
326 XDataSource oXDataSource
= null;
327 Object oInterface
= null;
328 XMultiServiceFactory xMSF
= null ;
329 int uniqueSuffix
= Param
.getInt("uniqueSuffix");
332 xMSF
= (XMultiServiceFactory
)Param
.getMSF();
333 oInterface
= xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
335 // retrieving temp directory for database
336 String tmpDatabaseUrl
= utils
.getOfficeTempDir((XMultiServiceFactory
)Param
.getMSF());
338 databaseName
= "NewDatabaseSource" + uniqueSuffix
;
340 String tmpDatabaseFile
= tmpDatabaseUrl
+ databaseName
+ ".odb";
341 System
.out
.println("try to delete '"+tmpDatabaseFile
+"'");
342 utils
.deleteFile(((XMultiServiceFactory
) Param
.getMSF()), tmpDatabaseFile
);
345 tmpDatabaseUrl
= "sdbc:dbase:file:///" + tmpDatabaseUrl
;
347 // Creating new DBase data source in the TEMP directory
348 XInterface newSource
= (XInterface
) xMSF
.createInstance
349 ("com.sun.star.sdb.DataSource") ;
351 XPropertySet xSrcProp
= (XPropertySet
)
352 UnoRuntime
.queryInterface(XPropertySet
.class, newSource
);
354 xSrcProp
.setPropertyValue("URL", tmpDatabaseUrl
) ;
356 DBTools dbt
= new DBTools(((XMultiServiceFactory
) Param
.getMSF()));
357 // registering source in DatabaseContext
358 log
.println("register database '"+tmpDatabaseUrl
+"' as '"+databaseName
+"'" );
359 dbt
.reRegisterDB(databaseName
, newSource
) ;
362 Param
.put("uniqueSuffix", new Integer(uniqueSuffix
));
364 return dbt
.connectToSource(newSource
);
366 catch( Exception e
) {
368 Param
.put("uniqueSuffix", new Integer(uniqueSuffix
));
369 log
.println("could not register new database" );
371 throw new StatusException("could not register new database", e
) ;
375 private XConnection
getLocalXConnection(TestParameters Param
){
377 log
.println("create local connection");
379 XInterface oDataCont
= null;
381 oDataCont
= (XInterface
)( (XMultiServiceFactory
) Param
.getMSF()).createInstance
382 ("com.sun.star.sdb.DatabaseContext");
383 } catch(Exception e
) {
384 throw new StatusException("Couldn't create instance of 'com.sun.star.sdb.DatabaseContext'", e
);
386 XNameAccess xNADataCont
= (XNameAccess
)
387 UnoRuntime
.queryInterface(XNameAccess
.class, oDataCont
);
389 String
[] dataNames
= xNADataCont
.getElementNames();
392 for (int i
= 0; i
< dataNames
.length
; i
++){
393 if (dataNames
[i
].startsWith("Biblio")) dataName
=dataNames
[i
];
398 Object oDataBase
= xNADataCont
.getByName(dataName
);
399 XDataSource xDataSource
= (XDataSource
)
400 UnoRuntime
.queryInterface(XDataSource
.class, oDataBase
);
402 return xDataSource
.getConnection("","");
404 } catch ( NoSuchElementException e
){
405 throw new StatusException("Couldn't get registered data base", e
);
406 } catch ( WrappedTargetException e
){
407 throw new StatusException("Couldn't get registered data base", e
);
408 } catch ( SQLException e
){
409 throw new StatusException("Couldn't get XConnection from registered data base", e
);
414 protected void cleanup(TestParameters Param
, PrintWriter log
) {
415 log
.println("closing connections...");
416 XMultiServiceFactory xMsf
= (XMultiServiceFactory
) Param
.getMSF();
417 DBTools dbt
= new DBTools(xMsf
);
419 if (Param
.containsKey("uniqueSuffix")){
420 int uniqueSuffix
= Param
.getInt("uniqueSuffix");
422 String databaseName
= "";
423 while (uniqueSuffix
>= 0){
425 databaseName
= "NewDatabaseSource" + uniqueSuffix
;
427 log
.println("revoke '"+databaseName
+"'");
430 dbt
.revokeDB(databaseName
);
431 } catch (com
.sun
.star
.uno
.Exception e
){