1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.harmony.sql.tests.java.sql;
19
20import java.sql.Connection;
21import java.sql.Driver;
22import java.sql.DriverManager;
23import java.sql.DriverPropertyInfo;
24import java.sql.SQLException;
25import java.util.Properties;
26
27/**
28 * Basic JDBC driver implementation to help with tests
29 */
30public class TestHelper_Driver4 implements Driver {
31    int majorVersion = 1;
32
33    int minorVersion = 0;
34
35    String baseURL;
36
37    String[] dataSources = { "data1", "data2", "data3" };
38
39    static {
40        Driver theDriver = new TestHelper_Driver4();
41        try {
42            DriverManager.registerDriver(theDriver);
43        } catch (SQLException e) {
44            System.out.println("Failed to register driver!");
45        }
46    } // end static block initializer
47
48    protected TestHelper_Driver4() {
49        super();
50        baseURL = "jdbc:mikes4";
51    } // end constructor TestHelper_Driver4()
52
53    public boolean acceptsURL(String url) throws SQLException {
54        // Check on the supplied String...
55        if (url == null) {
56            return false;
57        }
58        // Everything's fine if the quoted url starts with the base url for this
59        // driver
60        if (url.startsWith(baseURL)) {
61            return true;
62        }
63        return false;
64    } // end method acceptsURL
65
66    static String validuser = "theuser";
67
68    static String validpassword = "thepassword";
69
70    static String userProperty = "user";
71
72    static String passwordProperty = "password";
73
74    public Connection connect(String url, Properties info) throws SQLException {
75        // Does the URL have the right form?
76        if (this.acceptsURL(url)) {
77            // The datasource name is the remainder of the url after the ":"
78            String datasource = url.substring(baseURL.length() + 1);
79            for (String element : dataSources) {
80                if (datasource.equals(element)) {
81                    /*
82                     * Check for user and password, except for datasource =
83                     * data1 which is set up not to require a user/password
84                     * combination
85                     */
86                    if (datasource.equals("data1")) {
87                        // do nothing...
88                    } else {
89                        if (info == null) {
90                            throw new SQLException("Properties bundle is null");
91                        }
92                        String user = (String) info.get(userProperty);
93                        String password = (String) info.get(passwordProperty);
94                        if (user == null || password == null) {
95                            throw new SQLException(
96                                    "Userid and/or password not supplied");
97                        }
98                        if (!user.equals(validuser)
99                                || !password.equals(validpassword)) {
100                            throw new SQLException(
101                                    "Userid and/or password not valid");
102                        } // end if
103                    } // end if
104                    // It all checks out - so return a connection
105                    Connection connection = new TestHelper_Connection1();
106                    return connection;
107                } // end if
108            } // end for
109        } // end if
110        return null;
111    } // end method connect(String, Properties)
112
113    public int getMajorVersion() {
114        return majorVersion;
115    } // end method getMajorVersion()
116
117    public int getMinorVersion() {
118        return minorVersion;
119    } // end method getMinorVersion()
120
121    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
122            throws SQLException {
123        DriverPropertyInfo[] theInfos = {
124                new DriverPropertyInfo(userProperty, "*"),
125                new DriverPropertyInfo(passwordProperty, "*"), };
126        return theInfos;
127    }
128
129    public boolean jdbcCompliant() {
130        // Basic version here returns false
131        return false;
132    }
133}
134