1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._sw;
25 
26 import com.sun.star.beans.NamedValue;
27 
28 import util.DBTools;
29 import util.utils;
30 import com.sun.star.beans.PropertyVetoException;
31 import com.sun.star.beans.UnknownPropertyException;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.container.NoSuchElementException;
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.lang.IllegalArgumentException;
36 import com.sun.star.lang.WrappedTargetException;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sdb.CommandType;
39 import com.sun.star.sdbc.*;
40 import com.sun.star.sdbcx.XRowLocate;
41 import com.sun.star.task.XJob;
42 import com.sun.star.text.MailMergeType;
43 import com.sun.star.uno.Exception;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 import java.io.PrintWriter;
47 import lib.StatusException;
48 import lib.TestCase;
49 import lib.TestEnvironment;
50 import lib.TestParameters;
51 
52 /**
53 * Here <code>com.sun.star.text.MailMerge</code> service is tested.<p>
54 * @see com.sun.star.text.MailMerge
55 * @see com.sun.star.task.XJob
56 * @see com.sun.star.text.XMailMergeBroadcaster
57 */
58 public class SwXMailMerge extends TestCase {
59 
initialize( TestParameters Param, PrintWriter log )60     public void initialize( TestParameters Param, PrintWriter log ) {
61         if (! Param.containsKey("uniqueSuffix")){
62             Param.put("uniqueSuffix", new Integer(0));
63         }
64     }
65 
66     /**
67      * Creating a Testenvironment for the interfaces to be tested. <p>
68      * Creates <code>MailMerge</code> service     * Object relations created :
69     * <ul>
70     *  <li> <code>'executeArgs'</code> for
71     *    {@link ifc.text._XMailMergeBroadcaster} : NamedValue[]</li>
72     *  <li> <code>'Job'</code> for
73     *    {@link ifc.text._XMailMergeBroadcaster} : XJob</li>
74     *  <li> <code>'XJobArgs'</code> for
75     *    {@link ifc.task._XJob} : Object[]</li>
76     * </ul>
77      */
createTestEnvironment(TestParameters Param, PrintWriter log)78     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
79 
80         XInterface oObj = null;
81         XInterface oRowSet = null;
82         Object oConnection = null;
83         XJob Job = null;
84 
85         log.println(" instantiate MailMerge service");
86         try {
87             oObj = (XInterface) ( (XMultiServiceFactory) Param.getMSF()).createInstance
88                 ("com.sun.star.text.MailMerge");
89         } catch (Exception e) {
90             throw new StatusException("Can't create object environment", e) ;
91         }
92 
93         // <set some variables>
94         String cTestDoc = utils.getFullTestURL("MailMerge.sxw");
95         //cMailMerge_DocumentURL = cTestDoc
96         String cOutputURL = utils.getOfficeTemp( (XMultiServiceFactory) Param.getMSF());
97         String cDataSourceName  = "Bibliography";
98         String cDataCommand = "biblio";
99         Object[] sel = new Object[2];
100         sel[0] = new int[2];
101         sel[1] = new int[5];
102         Object[] myBookMarks = new Object[2];
103         // </set some variables>
104 
105         // <create XResultSet>
106         log.println("create a XResultSet");
107         try {
108             oRowSet = (XInterface) ( (XMultiServiceFactory) Param.getMSF()).createInstance
109                 ("com.sun.star.sdb.RowSet");
110         } catch (Exception e) {
111             throw new StatusException("Can't create com.sun.star.sdb.RowSet", e);
112         }
113         XPropertySet oRowSetProps = (XPropertySet)
114                         UnoRuntime.queryInterface(XPropertySet.class, oRowSet);
115         XRowSet xRowSet = (XRowSet)
116                         UnoRuntime.queryInterface(XRowSet.class, oRowSet);
117         try {
118             oRowSetProps.setPropertyValue("DataSourceName",cDataSourceName);
119             oRowSetProps.setPropertyValue("Command",cDataCommand);
120             oRowSetProps.setPropertyValue("CommandType", new Integer(CommandType.TABLE));
121         } catch (UnknownPropertyException e) {
122             throw new StatusException("Can't set properties on oRowSet", e);
123         } catch (PropertyVetoException e) {
124             throw new StatusException("Can't set properties on oRowSet", e);
125         } catch (IllegalArgumentException e) {
126             throw new StatusException("Can't set properties on oRowSet", e);
127         } catch (WrappedTargetException e) {
128             throw new StatusException("Can't set properties on oRowSet", e);
129         }
130         try {
131             xRowSet.execute();
132         } catch (SQLException e) {
133             throw new StatusException("Can't execute oRowSet", e);
134         }
135 
136         XResultSet oResultSet = (XResultSet)
137                            UnoRuntime.queryInterface(XResultSet.class, oRowSet);
138 
139 
140 
141 
142         // <create Bookmarks>
143         log.println("create bookmarks");
144         try {
145             XRowLocate oRowLocate = (XRowLocate) UnoRuntime.queryInterface(
146                                                   XRowLocate.class, oResultSet);
147             oResultSet.first();
148             myBookMarks[0] = oRowLocate.getBookmark();
149             oResultSet.next();
150             myBookMarks[1] = oRowLocate.getBookmark();
151         } catch (SQLException e) {
152             throw new StatusException("Can't get Bookmarks", e);
153         }
154         // </create Bookmarks>
155 
156         // <fill object with values>
157 
158         log.println("fill MailMerge with default connection");
159 
160         XPropertySet oObjProps = (XPropertySet)
161                             UnoRuntime.queryInterface(XPropertySet.class, oObj);
162         try {
163             oObjProps.setPropertyValue("ActiveConnection", getLocalXConnection(Param));
164             oObjProps.setPropertyValue("DataSourceName", cDataSourceName);
165             oObjProps.setPropertyValue("Command", cDataCommand);
166             oObjProps.setPropertyValue("CommandType", new Integer(CommandType.TABLE));
167             oObjProps.setPropertyValue("OutputType", new Short(MailMergeType.FILE));
168             oObjProps.setPropertyValue("DocumentURL", cTestDoc);
169             oObjProps.setPropertyValue("OutputURL", cOutputURL);
170             oObjProps.setPropertyValue("FileNamePrefix", "Author");
171             oObjProps.setPropertyValue("FileNameFromColumn", new Boolean(false));
172             oObjProps.setPropertyValue("Selection", new Object[0]);
173 
174         } catch (UnknownPropertyException e) {
175             throw new StatusException("Can't set properties on oObj", e);
176         } catch (PropertyVetoException e) {
177             throw new StatusException("Can't set properties on oObj", e);
178         } catch (IllegalArgumentException e) {
179             throw new StatusException("Can't set properties on oObj", e);
180         } catch (WrappedTargetException e) {
181             throw new StatusException("Can't set properties on oObj", e);
182         }
183         // </fill object with values>
184 
185 
186         // <create object relations>
187         Object[] vXJobArgs = new Object[4];
188         NamedValue[] vXJobArg0 = new NamedValue[8];
189         NamedValue[] vXJobArg1 = new NamedValue[7];
190         NamedValue[] vXJobArg2 = new NamedValue[10];
191         NamedValue[] vXJobArg3 = new NamedValue[0];
192 
193         // first Arguments
194         vXJobArg0[0] = new NamedValue("DataSourceName", cDataSourceName);
195         vXJobArg0[1] = new NamedValue("Command", cDataCommand);
196         vXJobArg0[2] = new NamedValue("CommandType",new Integer(CommandType.TABLE));
197         vXJobArg0[3] = new NamedValue("OutputType",new Short(MailMergeType.FILE));
198         vXJobArg0[4] = new NamedValue("DocumentURL", cTestDoc);
199         vXJobArg0[5] = new NamedValue("OutputURL", cOutputURL);
200         vXJobArg0[6] = new NamedValue("FileNamePrefix", "Identifier");
201         vXJobArg0[7] = new NamedValue("FileNameFromColumn", new Boolean(true));
202 
203         //second Arguments
204         vXJobArg1[0] = new NamedValue("DataSourceName", cDataSourceName);
205         vXJobArg1[1] = new NamedValue("Command", cDataCommand);
206         vXJobArg1[2] = new NamedValue("CommandType",new Integer(CommandType.TABLE));
207         vXJobArg1[3] = new NamedValue("OutputType",
208                              new Short(MailMergeType.PRINTER));
209         vXJobArg1[4] = new NamedValue("DocumentURL", cTestDoc);
210         vXJobArg1[5] = new NamedValue("FileNamePrefix", "Author");
211         vXJobArg1[6] = new NamedValue("FileNameFromColumn", new Boolean(true));
212 
213         // third Arguments
214         vXJobArg2[0] = new NamedValue("ActiveConnection", getLocalXConnection(Param));
215         vXJobArg2[1] = new NamedValue("DataSourceName", cDataSourceName);
216         vXJobArg2[2] = new NamedValue("Command", cDataCommand);
217         vXJobArg2[3] = new NamedValue("CommandType",new Integer(CommandType.TABLE));
218         vXJobArg2[4] = new NamedValue("OutputType",
219                              new Short(MailMergeType.FILE));
220         vXJobArg2[5] = new NamedValue("ResultSet", oResultSet);
221         vXJobArg2[6] = new NamedValue("OutputURL", cOutputURL);
222         vXJobArg2[7] = new NamedValue("FileNamePrefix", "Identifier");
223         vXJobArg2[8] = new NamedValue("FileNameFromColumn", new Boolean(true));
224         vXJobArg2[9] = new NamedValue("Selection", myBookMarks);
225 
226         vXJobArgs[0] = vXJobArg0;
227         vXJobArgs[1] = vXJobArg1;
228         vXJobArgs[2] = vXJobArg2;
229         vXJobArgs[3] = vXJobArg3;
230 
231 
232         Job = (XJob) UnoRuntime.queryInterface(XJob.class, oObj);
233         try{
234             Job.execute(vXJobArg2);
235         } catch ( IllegalArgumentException e){
236             System.out.println(e.toString());
237         } catch (  Exception e){
238             System.out.println(e.toString());
239         }
240 
241 
242         // <create XResultSet>
243         log.println("create XResultSet");
244 
245         try {
246             oRowSet = (XInterface) ( (XMultiServiceFactory) Param.getMSF()).createInstance
247                 ("com.sun.star.sdb.RowSet");
248         } catch (Exception e) {
249             throw new StatusException("Can't create com.sun.star.sdb.RowSet", e);
250         }
251         oRowSetProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oRowSet);
252 
253         xRowSet = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, oRowSet);
254 
255         try {
256             oRowSetProps.setPropertyValue("DataSourceName",cDataSourceName);
257             oRowSetProps.setPropertyValue("Command",cDataCommand);
258             oRowSetProps.setPropertyValue("CommandType", new Integer(CommandType.TABLE));
259         } catch (UnknownPropertyException e) {
260             throw new StatusException("Can't set properties on oRowSet", e);
261         } catch (PropertyVetoException e) {
262             throw new StatusException("Can't set properties on oRowSet", e);
263         } catch (IllegalArgumentException e) {
264             throw new StatusException("Can't set properties on oRowSet", e);
265         } catch (WrappedTargetException e) {
266             throw new StatusException("Can't set properties on oRowSet", e);
267         }
268         try {
269             xRowSet.execute();
270         } catch (SQLException e) {
271             throw new StatusException("Can't execute oRowSet", e);
272         }
273 
274         oResultSet = (XResultSet)
275                            UnoRuntime.queryInterface(XResultSet.class, oRowSet);
276 
277         XResultSet oMMXResultSet = null;
278         try {
279             oMMXResultSet = (XResultSet)
280                            UnoRuntime.queryInterface(XResultSet.class,
281                                ( (XInterface)
282                                 ( (XMultiServiceFactory)
283                                 Param.getMSF()).createInstance("com.sun.star.sdb.RowSet")));
284 
285         } catch (Exception e) {
286             throw new StatusException("Can't create com.sun.star.sdb.RowSet", e);
287         }
288         // </create object relations>
289 
290         TestEnvironment tEnv = new TestEnvironment(oObj) ;
291 
292         // <adding object relations>
293 
294         // com.sun.star.sdb.DataAccessDescriptor
295         tEnv.addObjRelation("DataAccessDescriptor.XResultSet", oResultSet);
296         tEnv.addObjRelation("DataAccessDescriptor.XConnection", getRemoteXConnection(Param));
297 
298         // com.sun.star.text.MailMaerge
299         tEnv.addObjRelation("MailMerge.XConnection", getRemoteXConnection(Param));
300         tEnv.addObjRelation("MailMerge.XResultSet", oMMXResultSet);
301 
302         // com.sun.star.text.XMailMergeBroadcaster
303         tEnv.addObjRelation( "executeArgs", vXJobArg0);
304         tEnv.addObjRelation( "Job", Job);
305 
306         // com.sun.star.task.XJob
307         tEnv.addObjRelation("XJobArgs", vXJobArgs);
308 
309         // </adding object relations>
310 
311         return tEnv ;
312     }
313 
getRemoteXConnection(TestParameters Param)314     private XConnection getRemoteXConnection(TestParameters Param){
315 
316         log.println("create remote connection");
317 
318         String databaseName = null ;
319         XDataSource oXDataSource = null;
320         Object oInterface = null;
321         XMultiServiceFactory xMSF = null ;
322         int uniqueSuffix = Param.getInt("uniqueSuffix");
323 
324         try {
325             xMSF = (XMultiServiceFactory)Param.getMSF();
326             oInterface = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
327 
328             // retrieving temp directory for database
329             String tmpDatabaseUrl = utils.getOfficeTempDir((XMultiServiceFactory)Param.getMSF());
330 
331             databaseName = "NewDatabaseSource" + uniqueSuffix ;
332 
333             String tmpDatabaseFile = tmpDatabaseUrl + databaseName + ".odb";
334             System.out.println("try to delete '"+tmpDatabaseFile+"'");
335             utils.deleteFile(((XMultiServiceFactory) Param.getMSF()), tmpDatabaseFile);
336 
337 
338             tmpDatabaseUrl = "sdbc:dbase:file:///" + tmpDatabaseUrl ;
339 
340             // Creating new DBase data source in the TEMP directory
341             XInterface newSource = (XInterface) xMSF.createInstance
342                 ("com.sun.star.sdb.DataSource") ;
343 
344             XPropertySet xSrcProp = (XPropertySet)
345                 UnoRuntime.queryInterface(XPropertySet.class, newSource);
346 
347             xSrcProp.setPropertyValue("URL", tmpDatabaseUrl) ;
348 
349             DBTools dbt = new DBTools( (XMultiServiceFactory)Param.getMSF(), log );
350             // registering source in DatabaseContext
351             log.println("register database '"+tmpDatabaseUrl+"' as '"+databaseName+"'" );
352             dbt.reRegisterDB(databaseName, newSource) ;
353 
354             uniqueSuffix++;
355             Param.put("uniqueSuffix", new Integer(uniqueSuffix));
356 
357             return dbt.connectToSource(newSource);
358         }
359         catch( Exception e ) {
360             uniqueSuffix++;
361             Param.put("uniqueSuffix", new Integer(uniqueSuffix));
362             log.println("could not register new database" );
363             e.printStackTrace();
364             throw new StatusException("could not register new database", e) ;
365         }
366     }
367 
getLocalXConnection(TestParameters Param)368     private XConnection getLocalXConnection(TestParameters Param){
369 
370         log.println("create local connection");
371 
372         XInterface oDataCont = null;
373         try {
374             oDataCont = (XInterface)( (XMultiServiceFactory) Param.getMSF()).createInstance
375                                    ("com.sun.star.sdb.DatabaseContext");
376         } catch(Exception e) {
377             throw new StatusException("Couldn't create instance of 'com.sun.star.sdb.DatabaseContext'", e);
378         }
379         XNameAccess xNADataCont = (XNameAccess)
380             UnoRuntime.queryInterface(XNameAccess.class, oDataCont);
381 
382         String[] dataNames = xNADataCont.getElementNames();
383 
384         String dataName="";
385         for (int i = 0; i < dataNames.length; i++){
386             if (dataNames[i].startsWith("Biblio")) dataName=dataNames[i];
387         }
388 
389         try{
390 
391             Object oDataBase = xNADataCont.getByName(dataName);
392             XDataSource xDataSource = (XDataSource)
393                 UnoRuntime.queryInterface(XDataSource.class, oDataBase);
394 
395             return xDataSource.getConnection("","");
396 
397         } catch ( NoSuchElementException e){
398             throw new StatusException("Couldn't get registered data base", e);
399         } catch (  WrappedTargetException e){
400             throw new StatusException("Couldn't get registered data base", e);
401         } catch (   SQLException e){
402             throw new StatusException("Couldn't get XConnection from registered data base", e);
403         }
404 
405     }
406 
cleanup(TestParameters Param, PrintWriter log)407     protected void cleanup(TestParameters Param, PrintWriter log) {
408         log.println("closing connections...");
409         XMultiServiceFactory xMsf = (XMultiServiceFactory) Param.getMSF();
410         DBTools dbt = new DBTools( xMsf, log );
411 
412         if (Param.containsKey("uniqueSuffix")){
413             int uniqueSuffix = Param.getInt("uniqueSuffix");
414             uniqueSuffix--;
415             String databaseName =  "";
416             while (uniqueSuffix >= 0){
417 
418                 databaseName = "NewDatabaseSource" + uniqueSuffix ;
419 
420                 log.println("revoke '"+databaseName+"'");
421 
422                 try{
423                     dbt.revokeDB(databaseName);
424                 } catch (com.sun.star.uno.Exception e){
425                 }
426 
427                 uniqueSuffix--;
428             }
429         }
430 
431     }
432 
433 }
434 
435 
436