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._jdbc;
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.DBTools;
34 
35 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.uno.XInterface;
38 
39 
40 /**
41 * Here <code>com.sun.star.sdbc.Driver</code> service is tested.<p>
42 * Test allows to run object tests in several threads concurently.
43 * @see com.sun.star.sdbc.Driver
44 * @see com.sun.star.sdbc.XDriver
45 * @see ifc.sdbc._XDriver
46 */
47 public class JDBCDriver extends TestCase {
48     /**
49      * Creates an instance of the service
50      * <code>com.sun.star.sdbc.Driver</code>. <p>
51      * Object relations created :
52      * <ul>
53      *  <li> <code>'XDriver.URL'</code> for {@link ifc.sdbc._XDriver}:
54      *      is the URL of the database to which to connect.
55      *      The URL is obtained from the parameter <code>jdbc.url</code></li>
56      *  <li> <code>'XDriver.UNSUITABLE_URL'</code> for {@link ifc.sdbc._XDriver}:
57      *      the wrong kind of URL to connect using given driver.
58      *      The URL is obtained from the parameter <code>flat.url</code></li>
59      *  <li> <code>'XDriver.INFO'</code> for {@link ifc.sdbc._XDriver}:
60      *      a list of arbitrary string tag/value pairs as connection arguments.
61      *      The values for list are obtained from the parameter
62      *      <code>jdbc.user</code> and <code>jdbc.password</code>.</li>
63      * </ul>
64      */
createTestEnvironment(TestParameters Param, PrintWriter log)65     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
66 
67         XInterface oObj = null;
68 
69         try {
70             oObj = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance(
71                 "com.sun.star.comp.sdbc.JDBCDriver");
72         } catch (com.sun.star.uno.Exception e) {
73             e.printStackTrace(log);
74             throw new StatusException(Status.failed("Couldn't create object"));
75         }
76 
77         log.println("creating a new environment for JDBCDriver object");
78         TestEnvironment tEnv = new TestEnvironment(oObj);
79 
80         //adding relation for sdbc.XDriver
81         String jdbcURL = (String) Param.get("jdbc.url");
82         if (jdbcURL == null) {
83             throw new StatusException(Status.failed(
84                 "Couldn't get 'jdbc.url' from ini-file"));
85         }
86         tEnv.addObjRelation("XDriver.URL", "jdbc:" + jdbcURL);
87 
88         String user = (String) Param.get("jdbc.user");
89         String password = (String) Param.get("jdbc.password");
90         if (user == null || password == null) {
91             throw new StatusException(Status.failed(
92                 "Couldn't get 'jdbc.user' or 'jdbc.password' from ini-file"));
93         }
94         PropertyValue[] info = new PropertyValue[4];
95         info[0] = new PropertyValue();
96         info[0].Name = "JavaDriverClass";
97         info[0].Value = DBTools.TST_JDBC_DRIVER;
98         info[1] = new PropertyValue();
99         info[1].Name = "user";
100         info[1].Value = user;
101         info[2] = new PropertyValue();
102         info[2].Name = "password";
103         info[2].Value = password;
104         info[3] = new PropertyValue();
105         info[3].Name = "isPasswordRequired";
106         info[3].Value = new Boolean(true);
107 
108         tEnv.addObjRelation("XDriver.INFO", info);
109 
110         String flatUrl = (String) Param.get("flat.url");
111         if (flatUrl == null) {
112             throw new StatusException(Status.failed(
113                 "Couldn't get 'flat.url' from ini-file"));
114         }
115         tEnv.addObjRelation("XDriver.UNSUITABLE_URL", "sdbc:flat:" + flatUrl);
116 
117         return tEnv;
118     }
119 }
120