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