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