StubDriverDA.java revision 10904:126b5b4a9ce4
1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package util;
24
25import java.io.File;
26import java.io.IOException;
27import java.sql.DriverAction;
28import java.sql.DriverManager;
29import java.sql.SQLException;
30import java.util.logging.Level;
31import java.util.logging.Logger;
32
33/**
34 * Simple java.sql.Driver stub class that registers the driver via a static
35 * block with a DriverAction Implementation
36 * @author ljanders
37 */
38public class StubDriverDA extends StubDriver {
39
40    public static final String DriverActionCalled = "DriverActionCalled.txt";
41
42    static DriverAction da;
43
44    static {
45        try {
46            DriverManager.registerDriver(new StubDriverDA(), da);
47        } catch (SQLException ex) {
48            Logger.getLogger(StubDriverDA.class.getName()).log(Level.SEVERE, null, ex);
49        }
50    }
51
52    public StubDriverDA() {
53        da = new DriverActionImpl(this);
54    }
55
56    @Override
57    public boolean acceptsURL(String url) throws SQLException {
58        return url.matches("^jdbc:luckydog:.*");
59    }
60
61    /**
62     * This method will write out a text file when called by the
63     * DriverActionImpl.release method when DriverManager.deregisterDriver
64     * is called. This is used by DriverManagerTests to validate that
65     * DriverAction.release was called
66     */
67    protected void release() {
68        File file = new File(DriverActionCalled);
69        try {
70            file.createNewFile();
71        } catch (IOException ex) {
72            throw new RuntimeException(ex);
73        }
74    }
75}
76