1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._jdbc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.Status;
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.DBTools;
38 
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.XInterface;
42 
43 
44 /**
45 * Here <code>com.sun.star.sdbc.Driver</code> service is tested.<p>
46 * Test allows to run object tests in several threads concurently.
47 * @see com.sun.star.sdbc.Driver
48 * @see com.sun.star.sdbc.XDriver
49 * @see ifc.sdbc._XDriver
50 */
51 public class JDBCDriver extends TestCase {
52     /**
53      * Creates an instance of the service
54      * <code>com.sun.star.sdbc.Driver</code>. <p>
55      * Object relations created :
56      * <ul>
57      *  <li> <code>'XDriver.URL'</code> for {@link ifc.sdbc._XDriver}:
58      *      is the URL of the database to which to connect.
59      *      The URL is obtained from the parameter <code>jdbc.url</code></li>
60      *  <li> <code>'XDriver.UNSUITABLE_URL'</code> for {@link ifc.sdbc._XDriver}:
61      *      the wrong kind of URL to connect using given driver.
62      *      The URL is obtained from the parameter <code>flat.url</code></li>
63      *  <li> <code>'XDriver.INFO'</code> for {@link ifc.sdbc._XDriver}:
64      *      a list of arbitrary string tag/value pairs as connection arguments.
65      *      The values for list are obtained from the parameter
66      *      <code>jdbc.user</code> and <code>jdbc.password</code>.</li>
67      * </ul>
68      */
69     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
70 
71         XInterface oObj = null;
72 
73         try {
74             oObj = (XInterface)((XMultiServiceFactory)Param.getMSF()).createInstance(
75                 "com.sun.star.comp.sdbc.JDBCDriver");
76         } catch (com.sun.star.uno.Exception e) {
77             e.printStackTrace(log);
78             throw new StatusException(Status.failed("Couldn't create object"));
79         }
80 
81         log.println("creating a new environment for JDBCDriver object");
82         TestEnvironment tEnv = new TestEnvironment(oObj);
83 
84         //adding relation for sdbc.XDriver
85         String jdbcURL = (String) Param.get("jdbc.url");
86         if (jdbcURL == null) {
87             throw new StatusException(Status.failed(
88                 "Couldn't get 'jdbc.url' from ini-file"));
89         }
90         tEnv.addObjRelation("XDriver.URL", "jdbc:" + jdbcURL);
91 
92         String user = (String) Param.get("jdbc.user");
93         String password = (String) Param.get("jdbc.password");
94         if (user == null || password == null) {
95             throw new StatusException(Status.failed(
96                 "Couldn't get 'jdbc.user' or 'jdbc.password' from ini-file"));
97         }
98         PropertyValue[] info = new PropertyValue[4];
99         info[0] = new PropertyValue();
100         info[0].Name = "JavaDriverClass";
101         info[0].Value = DBTools.TST_JDBC_DRIVER;
102         info[1] = new PropertyValue();
103         info[1].Name = "user";
104         info[1].Value = user;
105         info[2] = new PropertyValue();
106         info[2].Name = "password";
107         info[2].Value = password;
108         info[3] = new PropertyValue();
109         info[3].Name = "isPasswordRequired";
110         info[3].Value = new Boolean(true);
111 
112         tEnv.addObjRelation("XDriver.INFO", info);
113 
114         String flatUrl = (String) Param.get("flat.url");
115         if (flatUrl == null) {
116             throw new StatusException(Status.failed(
117                 "Couldn't get 'flat.url' from ini-file"));
118         }
119         tEnv.addObjRelation("XDriver.UNSUITABLE_URL", "sdbc:flat:" + flatUrl);
120 
121         return tEnv;
122     }
123 }
124