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