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