File: DropDatabaseSetup.java

package info (click to toggle)
derby 10.14.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,740 kB
  • sloc: java: 691,931; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 46
file content (145 lines) | stat: -rw-r--r-- 4,794 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 *
 * Derby - Class org.apache.derbyTesting.junit.DropDatabaseSetup
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific 
 * language governing permissions and limitations under the License.
 */
package org.apache.derbyTesting.junit;

import java.io.File;
import java.sql.SQLException;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;

/**
 * Shutdown and drop the database identified by the logical
 * name passed in when creating this decorator.
 *
 */
class DropDatabaseSetup extends BaseTestSetup {

    private final String logicalDBName;
    private final boolean shutdownBeforeDrop;

    DropDatabaseSetup(Test test, String logicalDBName) {
        this(test, logicalDBName, true);
    }

    DropDatabaseSetup(Test test, String logicalDBName, boolean shutdown) {
        super(test);
        this.logicalDBName = logicalDBName;
        this.shutdownBeforeDrop = shutdown;
    }
    
    /**
     * Shutdown the database and then remove all of its files.
     */
    @Override
    protected void tearDown() throws Exception {
        if (shutdownBeforeDrop) {
            shutdownDatabase();
        }

        removeDatabase();
    }

    private void shutdownDatabase() throws SQLException {

        TestConfiguration config = TestConfiguration.getCurrent();
        
        // Ensure the database is booted
        // since that is what shutdownDatabase() requires.
        boolean shutdown;
        try {
            config.openConnection(logicalDBName).close();
            shutdown = true;
        } catch (SQLException e) {
            String  sqlState = e.getSQLState();
            // If the database cannot be booted due
            // to some restrictions such as authentication
            // or encrypted (ie here we don't know the 
            // correct authentication tokens, then it's
            // ok since we just want it shutdown anyway!
            if ( "XJ040".equals( sqlState ) || "08004".equals( sqlState ) || "4251I".equals( sqlState ) )
            {
                shutdown = false;
            }
            else
            {
                throw e;
            }
        }
        if (shutdown)
        {
            DataSource ds = JDBCDataSource.getDataSourceLogical(logicalDBName);
            JDBCDataSource.shutdownDatabase(ds);
        }
    }

    void removeDatabase()
    {
        TestConfiguration config = TestConfiguration.getCurrent();
        String dbName = config.getPhysicalDatabaseName(logicalDBName);
        dbName = dbName.replace('/', File.separatorChar);
        String dsh = BaseTestCase.getSystemProperty("derby.system.home");
        if (dsh == null) {
            fail("not implemented");
        } else {
            dbName = dsh + File.separator + dbName;
        }
        removeDirectory(dbName);
        //DERBY-5995 (Add a test case to check the 3 readme files get created 
        // even when log directory has been changed with jdbc url attribute 
        // logDevice )
        String logDevice = config.getConnectionAttributes().getProperty("logDevice");
        if (logDevice != null) {
            removeDirectory(logDevice);
        }
    }


    static void removeDirectory(String path)
    {
        final File dir = new File(path);
        removeDirectory(dir);
    }
    
    static void removeDirectory(final File dir) {
        // Check if anything to do!
        // Database may not have been created.
        if (!PrivilegedFileOpsForTests.exists(dir)) {
            return;
        }

        BaseTestCase.assertDirectoryDeleted(dir);
    }

    /**
     * Remove all the files in the list
     * @param list the list of files that will be deleted
     **/
    static void removeFiles(String[] list) {
        for (int i = 0; i < list.length; i++) {
             try {
                 File dfile = new File(list[i].toString());            
                 assertTrue(list[i].toString(), dfile.delete());
             } catch (IllegalArgumentException e) {
                 fail("open file error");
             }
        }
    }
}