tdf#130857 qt weld: Introduce QtInstanceScrolledWindow
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXMailMerge.java
blobf3bb2b2f242cb45faa6a98e2b9ba17c0b5240d11
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.XPropertySet;
26 import com.sun.star.container.NoSuchElementException;
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.lang.IllegalArgumentException;
29 import com.sun.star.lang.WrappedTargetException;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.sdb.CommandType;
32 import com.sun.star.sdbc.*;
33 import com.sun.star.sdbcx.XRowLocate;
34 import com.sun.star.task.XJob;
35 import com.sun.star.text.MailMergeType;
36 import com.sun.star.uno.Exception;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import java.io.PrintWriter;
40 import lib.StatusException;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
45 /**
46 * Here <code>com.sun.star.text.MailMerge</code> service is tested.<p>
47 * @see com.sun.star.text.MailMerge
48 * @see com.sun.star.task.XJob
49 * @see com.sun.star.text.XMailMergeBroadcaster
51 public class SwXMailMerge extends TestCase {
53 @Override
54 public void initialize( TestParameters Param, PrintWriter log ) {
55 if (! Param.containsKey("uniqueSuffix")){
56 Param.put("uniqueSuffix", Integer.valueOf(0));
60 /**
61 * Creating a TestEnvironment for the interfaces to be tested. <p>
62 * Creates <code>MailMerge</code> service * Object relations created :
63 * <ul>
64 * <li> <code>'executeArgs'</code> for
65 * {@link ifc.text._XMailMergeBroadcaster} : NamedValue[]</li>
66 * <li> <code>'Job'</code> for
67 * {@link ifc.text._XMailMergeBroadcaster} : XJob</li>
68 * <li> <code>'XJobArgs'</code> for
69 * {@link ifc.task._XJob} : Object[]</li>
70 * </ul>
72 @Override
73 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) throws Exception {
75 XInterface oObj = null;
76 XInterface oRowSet = null;
77 XJob Job = null;
79 log.println(" instantiate MailMerge service");
80 oObj = (XInterface) Param.getMSF().createInstance
81 ("com.sun.star.text.MailMerge");
83 // <set some variables>
84 String cTestDoc = utils.getFullTestURL("MailMerge.sxw");
85 //cMailMerge_DocumentURL = cTestDoc
86 String cOutputURL = utils.getOfficeTemp( Param.getMSF());
87 String cDataSourceName = "Bibliography";
88 String cDataCommand = "biblio";
89 Object[] myBookMarks = new Object[2];
90 // </set some variables>
92 // <create XResultSet>
93 log.println("create a XResultSet");
94 oRowSet = (XInterface) Param.getMSF().createInstance
95 ("com.sun.star.sdb.RowSet");
96 XPropertySet oRowSetProps = UnoRuntime.queryInterface(XPropertySet.class, oRowSet);
97 XRowSet xRowSet = UnoRuntime.queryInterface(XRowSet.class, oRowSet);
98 oRowSetProps.setPropertyValue("DataSourceName",cDataSourceName);
99 oRowSetProps.setPropertyValue("Command",cDataCommand);
100 oRowSetProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
101 xRowSet.execute();
103 XResultSet oResultSet = UnoRuntime.queryInterface(XResultSet.class, oRowSet);
108 // <create Bookmarks>
109 log.println("create bookmarks");
110 XRowLocate oRowLocate = UnoRuntime.queryInterface(
111 XRowLocate.class, oResultSet);
112 oResultSet.first();
113 myBookMarks[0] = oRowLocate.getBookmark();
114 oResultSet.next();
115 myBookMarks[1] = oRowLocate.getBookmark();
116 // </create Bookmarks>
118 // <fill object with values>
120 log.println("fill MailMerge with default connection");
122 XPropertySet oObjProps = UnoRuntime.queryInterface(XPropertySet.class, oObj);
123 oObjProps.setPropertyValue("ActiveConnection", getLocalXConnection(Param));
124 oObjProps.setPropertyValue("DataSourceName", cDataSourceName);
125 oObjProps.setPropertyValue("Command", cDataCommand);
126 oObjProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
127 oObjProps.setPropertyValue("OutputType", Short.valueOf(MailMergeType.FILE));
128 oObjProps.setPropertyValue("DocumentURL", cTestDoc);
129 oObjProps.setPropertyValue("OutputURL", cOutputURL);
130 oObjProps.setPropertyValue("FileNamePrefix", "Author");
131 oObjProps.setPropertyValue("FileNameFromColumn", Boolean.FALSE);
132 oObjProps.setPropertyValue("Selection", new Object[0]);
134 // </fill object with values>
137 // <create object relations>
138 Object[] vXJobArgs = new Object[4];
139 NamedValue[] vXJobArg0 = new NamedValue[8];
140 NamedValue[] vXJobArg1 = new NamedValue[7];
141 NamedValue[] vXJobArg2 = new NamedValue[10];
142 NamedValue[] vXJobArg3 = new NamedValue[0];
144 // first Arguments
145 vXJobArg0[0] = new NamedValue("DataSourceName", cDataSourceName);
146 vXJobArg0[1] = new NamedValue("Command", cDataCommand);
147 vXJobArg0[2] = new NamedValue("CommandType",Integer.valueOf(CommandType.TABLE));
148 vXJobArg0[3] = new NamedValue("OutputType",Short.valueOf(MailMergeType.FILE));
149 vXJobArg0[4] = new NamedValue("DocumentURL", cTestDoc);
150 vXJobArg0[5] = new NamedValue("OutputURL", cOutputURL);
151 vXJobArg0[6] = new NamedValue("FileNamePrefix", "Identifier");
152 vXJobArg0[7] = new NamedValue("FileNameFromColumn", Boolean.TRUE);
154 //second Arguments
155 vXJobArg1[0] = new NamedValue("DataSourceName", cDataSourceName);
156 vXJobArg1[1] = new NamedValue("Command", cDataCommand);
157 vXJobArg1[2] = new NamedValue("CommandType",Integer.valueOf(CommandType.TABLE));
158 vXJobArg1[3] = new NamedValue("OutputType",
159 Short.valueOf(MailMergeType.PRINTER));
160 vXJobArg1[4] = new NamedValue("DocumentURL", cTestDoc);
161 vXJobArg1[5] = new NamedValue("FileNamePrefix", "Author");
162 vXJobArg1[6] = new NamedValue("FileNameFromColumn", Boolean.TRUE);
164 // third Arguments
165 vXJobArg2[0] = new NamedValue("ActiveConnection", getLocalXConnection(Param));
166 vXJobArg2[1] = new NamedValue("DataSourceName", cDataSourceName);
167 vXJobArg2[2] = new NamedValue("Command", cDataCommand);
168 vXJobArg2[3] = new NamedValue("CommandType",Integer.valueOf(CommandType.TABLE));
169 vXJobArg2[4] = new NamedValue("OutputType",
170 Short.valueOf(MailMergeType.FILE));
171 vXJobArg2[5] = new NamedValue("ResultSet", oResultSet);
172 vXJobArg2[6] = new NamedValue("OutputURL", cOutputURL);
173 vXJobArg2[7] = new NamedValue("FileNamePrefix", "Identifier");
174 vXJobArg2[8] = new NamedValue("FileNameFromColumn", Boolean.TRUE);
175 vXJobArg2[9] = new NamedValue("Selection", myBookMarks);
177 vXJobArgs[0] = vXJobArg0;
178 vXJobArgs[1] = vXJobArg1;
179 vXJobArgs[2] = vXJobArg2;
180 vXJobArgs[3] = vXJobArg3;
183 Job = UnoRuntime.queryInterface(XJob.class, oObj);
184 try{
185 Job.execute(vXJobArg2);
186 } catch ( IllegalArgumentException e){
187 System.out.println(e.toString());
188 } catch ( Exception e){
189 System.out.println(e.toString());
193 // <create XResultSet>
194 log.println("create XResultSet");
196 oRowSet = (XInterface) Param.getMSF().createInstance
197 ("com.sun.star.sdb.RowSet");
198 oRowSetProps = UnoRuntime.queryInterface(XPropertySet.class, oRowSet);
200 xRowSet = UnoRuntime.queryInterface(XRowSet.class, oRowSet);
202 oRowSetProps.setPropertyValue("DataSourceName",cDataSourceName);
203 oRowSetProps.setPropertyValue("Command",cDataCommand);
204 oRowSetProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
205 xRowSet.execute();
207 oResultSet = UnoRuntime.queryInterface(XResultSet.class, oRowSet);
209 XResultSet oMMXResultSet = null;
210 oMMXResultSet = UnoRuntime.queryInterface(XResultSet.class,
211 Param.getMSF().createInstance("com.sun.star.sdb.RowSet"));
212 // </create object relations>
214 TestEnvironment tEnv = new TestEnvironment(oObj) ;
216 // <adding object relations>
218 // com.sun.star.sdb.DataAccessDescriptor
219 tEnv.addObjRelation("DataAccessDescriptor.XResultSet", oResultSet);
220 tEnv.addObjRelation("DataAccessDescriptor.XConnection", getRemoteXConnection(Param));
222 // com.sun.star.text.MailMerge
223 tEnv.addObjRelation("MailMerge.XConnection", getRemoteXConnection(Param));
224 tEnv.addObjRelation("MailMerge.XResultSet", oMMXResultSet);
226 // com.sun.star.text.XMailMergeBroadcaster
227 tEnv.addObjRelation( "executeArgs", vXJobArg0);
228 tEnv.addObjRelation( "Job", Job);
230 // com.sun.star.task.XJob
231 tEnv.addObjRelation("XJobArgs", vXJobArgs);
233 // </adding object relations>
235 return tEnv ;
238 private XConnection getRemoteXConnection(TestParameters Param){
240 log.println("create remote connection");
242 String databaseName = null ;
243 XMultiServiceFactory xMSF = null ;
244 int uniqueSuffix = Param.getInt("uniqueSuffix");
246 try {
247 xMSF = Param.getMSF();
248 xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
250 // retrieving temp directory for database
251 String tmpDatabaseUrl = utils.getOfficeTempDir(Param.getMSF());
253 databaseName = "NewDatabaseSource" + uniqueSuffix ;
255 String tmpDatabaseFile = tmpDatabaseUrl + databaseName + ".odb";
256 System.out.println("try to delete '"+tmpDatabaseFile+"'");
257 utils.deleteFile(Param.getMSF(), tmpDatabaseFile);
260 tmpDatabaseUrl = "sdbc:dbase:file:///" + tmpDatabaseUrl ;
262 // Creating new DBase data source in the TEMP directory
263 XInterface newSource = (XInterface) xMSF.createInstance
264 ("com.sun.star.sdb.DataSource") ;
266 XPropertySet xSrcProp = UnoRuntime.queryInterface(XPropertySet.class, newSource);
268 xSrcProp.setPropertyValue("URL", tmpDatabaseUrl) ;
270 DBTools dbt = new DBTools( Param.getMSF() );
271 // registering source in DatabaseContext
272 log.println("register database '"+tmpDatabaseUrl+"' as '"+databaseName+"'" );
273 dbt.reRegisterDB(databaseName, newSource) ;
275 uniqueSuffix++;
276 Param.put("uniqueSuffix", Integer.valueOf(uniqueSuffix));
278 return dbt.connectToSource(newSource);
280 catch( Exception e ) {
281 uniqueSuffix++;
282 Param.put("uniqueSuffix", Integer.valueOf(uniqueSuffix));
283 log.println("could not register new database" );
284 e.printStackTrace();
285 throw new StatusException("could not register new database", e) ;
289 private XConnection getLocalXConnection(TestParameters Param){
291 log.println("create local connection");
293 XInterface oDataCont = null;
294 try {
295 oDataCont = (XInterface)Param.getMSF().createInstance
296 ("com.sun.star.sdb.DatabaseContext");
297 } catch(Exception e) {
298 throw new StatusException("Couldn't create instance of 'com.sun.star.sdb.DatabaseContext'", e);
300 XNameAccess xNADataCont = UnoRuntime.queryInterface(XNameAccess.class, oDataCont);
302 String[] dataNames = xNADataCont.getElementNames();
304 String dataName="";
305 for (int i = 0; i < dataNames.length; i++){
306 if (dataNames[i].startsWith("Biblio")) dataName=dataNames[i];
309 try{
311 Object oDataBase = xNADataCont.getByName(dataName);
312 XDataSource xDataSource = UnoRuntime.queryInterface(XDataSource.class, oDataBase);
314 return xDataSource.getConnection("","");
316 } catch ( NoSuchElementException e){
317 throw new StatusException("Couldn't get registered data base", e);
318 } catch ( WrappedTargetException e){
319 throw new StatusException("Couldn't get registered data base", e);
320 } catch ( SQLException e){
321 throw new StatusException("Couldn't get XConnection from registered data base", e);
326 @Override
327 protected void cleanup(TestParameters Param, PrintWriter log) {
328 log.println("closing connections...");
329 XMultiServiceFactory xMsf = Param.getMSF();
330 DBTools dbt = new DBTools( xMsf );
332 if (Param.containsKey("uniqueSuffix")){
333 int uniqueSuffix = Param.getInt("uniqueSuffix");
334 uniqueSuffix--;
335 String databaseName = "";
336 while (uniqueSuffix >= 0){
338 databaseName = "NewDatabaseSource" + uniqueSuffix ;
340 log.println("revoke '"+databaseName+"'");
342 try{
343 dbt.revokeDB(databaseName);
344 } catch (com.sun.star.uno.Exception e){
347 uniqueSuffix--;