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