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 package mod._dbaccess;
28 
29 import java.io.PrintWriter;
30 
31 import lib.Status;
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.AccessibilityTools;
37 
38 import com.sun.star.accessibility.AccessibleRole;
39 import com.sun.star.accessibility.XAccessible;
40 import com.sun.star.awt.PosSize;
41 import com.sun.star.awt.Rectangle;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.container.XNameAccess;
46 import com.sun.star.container.XNameContainer;
47 import com.sun.star.frame.XModel;
48 import com.sun.star.frame.XStorable;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.lang.XMultiServiceFactory;
51 import com.sun.star.sdb.XDocumentDataSource;
52 import com.sun.star.sdb.XQueryDefinitionsSupplier;
53 import com.sun.star.sdbc.XConnection;
54 import com.sun.star.sdbc.XIsolatedConnection;
55 import com.sun.star.sdbc.XStatement;
56 import com.sun.star.ucb.XSimpleFileAccess;
57 import com.sun.star.uno.UnoRuntime;
58 import com.sun.star.uno.XInterface;
59 import util.DesktopTools;
60 import util.utils;
61 
62 
63 /**
64  * Object implements the following interfaces :
65  * <ul>
66  *   <li><code>::com::sun::star::accessibility::XAccessible</code></li>
67  *   <li><code>::com::sun::star::accessibility::XAccessibleContext
68  *   </code></li>
69  *   <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
70  *   </code></li>
71  * </ul><p>
72  * @see com.sun.star.accessibility.XAccessible
73  * @see com.sun.star.accessibility.XAccessibleContext
74  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
75  * @see ifc.accessibility._XAccessible
76  * @see ifc.accessibility._XAccessibleContext
77  * @see ifc.accessibility._XAccessibleEventBroadcaster
78  */
79 public class ConnectionLineAccessibility extends TestCase
80 {
81     XWindow xWindow = null;
82     Object oDBSource = null;
83     String aFile = "";
84     XConnection connection = null;
85     XIsolatedConnection isolConnection = null;
86     XComponent QueryComponent = null;
87     String user = "";
88     String password="";
89 
90     /**
91      * Creates a new DataSource and stores it.
92      * Creates a connection and using it
93      * creates two tables in database.
94      * Creates a new query and adds it to DefinitionContainer.
95      * Opens the QueryComponent.with loadComponentFromURL
96      * and gets the object with the role UNKNOWN and the Impplementation
97      * name that contains ConnectionLine
98      * @param Param test parameters
99      * @param log writer to log information while testing
100      * @return
101      * @throws StatusException
102      * @see TestEnvironment
103      */
104     protected TestEnvironment createTestEnvironment(TestParameters Param,
105             PrintWriter log)
106     {
107         XInterface oObj = null;
108 
109         Object oDBContext = null;
110         Object oDBSource = null;
111         Object newQuery = null;
112         Object toolkit = null;
113         XStorable store = null;
114 
115         try
116         {
117             oDBContext = ((XMultiServiceFactory) Param.getMSF())
118             .createInstance("com.sun.star.sdb.DatabaseContext");
119             oDBSource = ((XMultiServiceFactory) Param.getMSF())
120             .createInstance("com.sun.star.sdb.DataSource");
121             newQuery = ((XMultiServiceFactory) Param.getMSF())
122             .createInstance("com.sun.star.sdb.QueryDefinition");
123             toolkit = ((XMultiServiceFactory) Param.getMSF())
124             .createInstance("com.sun.star.awt.Toolkit");
125         }
126         catch (com.sun.star.uno.Exception e)
127         {
128             e.printStackTrace(log);
129             throw new StatusException(Status.failed("Couldn't create instance"));
130         }
131 
132         String mysqlURL = (String) Param.get("mysql.url");
133 
134         if (mysqlURL == null)
135         {
136             throw new StatusException(Status.failed(
137                     "Couldn't get 'mysql.url' from ini-file"));
138         }
139 
140         user = (String) Param.get("jdbc.user");
141         password = (String) Param.get("jdbc.password");
142 
143         if ((user == null) || (password == null))
144         {
145             throw new StatusException(Status.failed(
146                     "Couldn't get 'jdbc.user' or 'jdbc.password' from ini-file"));
147         }
148 
149         PropertyValue[] info = new PropertyValue[2];
150         info[0] = new PropertyValue();
151         info[0].Name = "user";
152         info[0].Value = user;
153         info[1] = new PropertyValue();
154         info[1].Name = "password";
155         info[1].Value = password;
156 
157         XPropertySet propSetDBSource = (XPropertySet) UnoRuntime.queryInterface(
158                 XPropertySet.class, oDBSource);
159 
160         try
161         {
162             propSetDBSource.setPropertyValue("URL", mysqlURL);
163             propSetDBSource.setPropertyValue("Info", info);
164         }
165         catch (com.sun.star.lang.WrappedTargetException e)
166         {
167             e.printStackTrace(log);
168             throw new StatusException(Status.failed(
169                     "Couldn't set property value"));
170         }
171         catch (com.sun.star.lang.IllegalArgumentException e)
172         {
173             e.printStackTrace(log);
174             throw new StatusException(Status.failed(
175                     "Couldn't set property value"));
176         }
177         catch (com.sun.star.beans.PropertyVetoException e)
178         {
179             e.printStackTrace(log);
180             throw new StatusException(Status.failed(
181                     "Couldn't set property value"));
182         }
183         catch (com.sun.star.beans.UnknownPropertyException e)
184         {
185             e.printStackTrace(log);
186             throw new StatusException(Status.failed(
187                     "Couldn't set property value"));
188         }
189 
190         try
191         {
192             log.println("writing database file ...");
193             XDocumentDataSource xDDS = (XDocumentDataSource)
194             UnoRuntime.queryInterface(XDocumentDataSource.class, oDBSource);
195             store = (XStorable) UnoRuntime.queryInterface(XStorable.class,
196                     xDDS.getDatabaseDocument());
197 
198             aFile = utils.getOfficeTemp((XMultiServiceFactory) Param.getMSF())+"ConnectionLine.odb";
199             log.println("... filename will be "+aFile);
200             store.storeAsURL(aFile,new PropertyValue[]
201             {});
202             log.println("... done");
203         }
204         catch (com.sun.star.uno.Exception e)
205         {
206             e.printStackTrace(log);
207             throw new StatusException(Status.failed("Couldn't register object"));
208         }
209 
210         isolConnection = (XIsolatedConnection) UnoRuntime.queryInterface(
211                 XIsolatedConnection.class,
212                 oDBSource);
213 
214         XConnection connection = null;
215         XStatement statement = null;
216 
217         final String tbl_name1 = "tst_table1";
218         final String tbl_name2 = "tst_table2";
219         final String col_name1 = "id1";
220         final String col_name2 = "id2";
221 
222         try
223         {
224             connection = isolConnection.getIsolatedConnection(user, password);
225             statement = connection.createStatement();
226             statement.executeUpdate("drop table if exists " + tbl_name1);
227             statement.executeUpdate("drop table if exists " + tbl_name2);
228             statement.executeUpdate("create table " + tbl_name1 + " (" +
229                     col_name1 + " int)");
230             statement.executeUpdate("create table " + tbl_name2 + " (" +
231                     col_name2 + " int)");
232         }
233         catch (com.sun.star.sdbc.SQLException e)
234         {
235             try
236             {
237                 shortWait();
238                 connection = isolConnection.getIsolatedConnection(user,
239                         password);
240                 statement = connection.createStatement();
241                 statement.executeUpdate("drop table if exists " + tbl_name1);
242                 statement.executeUpdate("drop table if exists " + tbl_name2);
243                 statement.executeUpdate("create table " + tbl_name1 + " (" +
244                         col_name1 + " int)");
245                 statement.executeUpdate("create table " + tbl_name2 + " (" +
246                         col_name2 + " int)");
247             }
248             catch (com.sun.star.sdbc.SQLException e2)
249             {
250                 e2.printStackTrace(log);
251                 throw new StatusException(Status.failed("SQLException"));
252             }
253         }
254 
255         XQueryDefinitionsSupplier querySuppl = (XQueryDefinitionsSupplier) UnoRuntime.queryInterface(
256                 XQueryDefinitionsSupplier.class,
257                 oDBSource);
258 
259         XNameAccess defContainer = querySuppl.getQueryDefinitions();
260 
261         XPropertySet queryProp = (XPropertySet) UnoRuntime.queryInterface(
262                 XPropertySet.class, newQuery);
263 
264         try
265         {
266             final String query = "select * from " + tbl_name1 + ", " +
267                     tbl_name2 + " where " + tbl_name1 + "." +
268                     col_name1 + "=" + tbl_name2 + "." +
269                     col_name2;
270             queryProp.setPropertyValue("Command", query);
271         }
272         catch (com.sun.star.lang.WrappedTargetException e)
273         {
274             e.printStackTrace(log);
275             throw new StatusException(Status.failed(
276                     "Couldn't set property value"));
277         }
278         catch (com.sun.star.lang.IllegalArgumentException e)
279         {
280             e.printStackTrace(log);
281             throw new StatusException(Status.failed(
282                     "Couldn't set property value"));
283         }
284         catch (com.sun.star.beans.PropertyVetoException e)
285         {
286             e.printStackTrace(log);
287             throw new StatusException(Status.failed(
288                     "Couldn't set property value"));
289         }
290         catch (com.sun.star.beans.UnknownPropertyException e)
291         {
292             e.printStackTrace(log);
293             throw new StatusException(Status.failed(
294                     "Couldn't set property value"));
295         }
296 
297         XNameContainer queryContainer = (XNameContainer) UnoRuntime.queryInterface(
298                 XNameContainer.class,
299                 defContainer);
300 
301         try
302         {
303             queryContainer.insertByName("Query1", newQuery);
304             store.store();
305             connection.close();
306         }
307         catch (com.sun.star.lang.WrappedTargetException e)
308         {
309             e.printStackTrace(log);
310             throw new StatusException(Status.failed("Couldn't insert query"));
311         }
312         catch (com.sun.star.container.ElementExistException e)
313         {
314             e.printStackTrace(log);
315             throw new StatusException(Status.failed("Couldn't insert query"));
316         }
317         catch (com.sun.star.lang.IllegalArgumentException e)
318         {
319             e.printStackTrace(log);
320             throw new StatusException(Status.failed("Couldn't insert query"));
321         }
322         catch (com.sun.star.io.IOException e)
323         {
324             e.printStackTrace(log);
325             throw new StatusException(Status.failed("Couldn't insert query"));
326         }
327         catch (com.sun.star.sdbc.SQLException e)
328         {
329             e.printStackTrace(log);
330             throw new StatusException(Status.failed("Couldn't insert query"));
331         }
332 
333         PropertyValue[] loadProps = new PropertyValue[3];
334         loadProps[0] = new PropertyValue();
335         loadProps[0].Name = "QueryDesignView";
336         loadProps[0].Value = Boolean.TRUE;
337 
338         loadProps[1] = new PropertyValue();
339         loadProps[1].Name = "CurrentQuery";
340         loadProps[1].Value = "Query1";
341 
342         loadProps[2] = new PropertyValue();
343         loadProps[2].Name = "DataSource";
344         loadProps[2].Value = oDBSource;
345 
346         QueryComponent = DesktopTools.loadDoc((XMultiServiceFactory) Param.getMSF(),".component:DB/QueryDesign",loadProps);
347 
348         util.utils.shortWait(1000);
349 
350         xWindow = UnoRuntime.queryInterface(XModel.class, QueryComponent).
351             getCurrentController().getFrame().getContainerWindow();
352 
353         XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
354 
355         AccessibilityTools.printAccessibleTree (log,xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
356 
357         oObj = AccessibilityTools.getAccessibleObjectForRoleIgnoreShowing(xRoot, AccessibleRole.UNKNOWN, "", "ConnectionLine");
358 
359         log.println("ImplementationName " + util.utils.getImplName(oObj));
360 
361         log.println("creating TestEnvironment");
362 
363         TestEnvironment tEnv = new TestEnvironment(oObj);
364 
365         shortWait();
366 
367         final XWindow queryWin = xWindow;
368 
369         tEnv.addObjRelation("EventProducer",
370                 new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer()
371         {
372             public void fireEvent()
373             {
374                 Rectangle rect = queryWin.getPosSize();
375                 queryWin.setPosSize(rect.X, rect.Y, rect.Height-5, rect.Width-5, PosSize.POSSIZE);
376             }
377         });
378 
379         return tEnv;
380     } // finish method getTestEnvironment
381 
382     /**
383      * Closes the DatasourceAdministration dialog and Query Dialog.
384      */
385     protected void cleanup(TestParameters Param, PrintWriter log)
386     {
387         try
388         {
389 
390             log.println("closing QueryComponent ...");
391             DesktopTools.closeDoc(QueryComponent);
392             log.println("... done");
393             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
394             Object sfa = xMSF.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
395             XSimpleFileAccess xSFA = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class, sfa);
396             log.println("deleting database file");
397             xSFA.kill(aFile);
398             log.println("Could delete file "+aFile+": "+!xSFA.exists(aFile));
399         }
400         catch (Exception e)
401         {
402             e.printStackTrace();
403         }
404     }
405 
406     /**
407      * Sleeps for 1.5 sec. to allow StarOffice to react on <code>
408      * reset</code> call.
409      */
410     private void shortWait()
411     {
412         try
413         {
414             Thread.sleep(1500);
415         }
416         catch (InterruptedException e)
417         {
418             log.println("While waiting :" + e);
419         }
420     }
421 }
422