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