001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.storage.sql;
021
022import java.sql.Connection;
023import java.sql.DriverManager;
024import java.sql.SQLException;
025import java.sql.Statement;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * @author Florent Guillaume
033 */
034public class DatabaseH2 extends DatabaseHelper {
035
036    private static final Log log = LogFactory.getLog(DatabaseH2.class);
037
038    /** This directory will be deleted and recreated. */
039    protected static final String DIRECTORY = "target";
040
041    protected static final String DEF_USER = "sa";
042
043    protected static final String DEF_PASSWORD = "";
044
045    protected static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-h2-contrib.xml";
046
047    protected static final String DRIVER = "org.h2.Driver";
048
049    protected static final String URL_FORMAT = "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1";
050
051    protected String url;
052
053    protected String user;
054
055    protected String password;
056
057    protected void setProperties() {
058        url = setProperty(URL_PROPERTY, String.format(URL_FORMAT, databaseName));
059
060        setProperty(DATABASE_PROPERTY, databaseName);
061        user = setProperty(USER_PROPERTY, DEF_USER);
062        password = setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
063        // for sql directory tests
064        setProperty(DRIVER_PROPERTY, DRIVER);
065    }
066
067    @Override
068    public void setUp() throws SQLException {
069        super.setUp();
070        try {
071            Class.forName(DRIVER);
072        } catch (ReflectiveOperationException e) {
073            throw new RuntimeException(e);
074        }
075        setProperties();
076        checkDatabaseLive();
077    }
078
079    protected void checkDatabaseLive() throws SQLException {
080        try (Connection connection = DriverManager.getConnection(url, Framework.getProperty(USER_PROPERTY, "sa"),
081                Framework.getProperty(PASSWORD_PROPERTY, null))) {
082            try (Statement st = connection.createStatement()) {
083                st.execute("SELECT 1");
084            }
085        }
086    }
087
088    protected String getId() {
089        return "nuxeo";
090    }
091
092    @Override
093    public void tearDown() throws SQLException {
094        if (owner == null) {
095            return;
096        }
097        try {
098            tearDownDatabase(url);
099        } finally {
100            super.tearDown();
101        }
102    }
103
104    protected void tearDownDatabase(String url) throws SQLException {
105        Connection connection = DriverManager.getConnection(url, user, password);
106        try {
107            Statement st = connection.createStatement();
108            try {
109                String sql = "SHUTDOWN";
110                log.trace(sql);
111                st.execute(sql);
112            } finally {
113                st.close();
114            }
115        } finally {
116            connection.close();
117        }
118    }
119
120    @Override
121    public String getDeploymentContrib() {
122        return CONTRIB_XML;
123    }
124
125    @Override
126    public RepositoryDescriptor getRepositoryDescriptor() {
127        RepositoryDescriptor descriptor = new RepositoryDescriptor();
128        return descriptor;
129    }
130
131    @Override
132    public boolean supportsClustering() {
133        return true;
134    }
135
136}