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.ecm.core.api.NuxeoException;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.runtime.test.runner.FeaturesRunner;
032
033/**
034 * @author Florent Guillaume
035 */
036public class DatabaseH2 extends DatabaseHelper {
037
038    private static final Log log = LogFactory.getLog(DatabaseH2.class);
039
040    /**
041     * This directory will be deleted and recreated.
042     *
043     * @deprecated since 11.1, unused
044     */
045    @Deprecated(since = "11.1", forRemoval = true)
046    protected static final String DIRECTORY = FeaturesRunner.getBuildDirectory();
047
048    protected static final String DEF_USER = "sa";
049
050    protected static final String DEF_PASSWORD = "";
051
052    protected static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-h2-contrib.xml";
053
054    protected static final String DRIVER = "org.h2.Driver";
055
056    protected static final String URL_FORMAT = "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1";
057
058    protected String url;
059
060    protected String user;
061
062    protected String password;
063
064    protected void setProperties() {
065        url = setProperty(URL_PROPERTY, String.format(URL_FORMAT, databaseName));
066
067        setProperty(DATABASE_PROPERTY, databaseName);
068        user = setProperty(USER_PROPERTY, DEF_USER);
069        password = setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
070        // for sql directory tests
071        setProperty(DRIVER_PROPERTY, DRIVER);
072    }
073
074    @Override
075    public void setUp() throws SQLException {
076        super.setUp();
077        try {
078            Class.forName(DRIVER);
079        } catch (ReflectiveOperationException e) {
080            throw new NuxeoException(e);
081        }
082        setProperties();
083        checkDatabaseLive();
084    }
085
086    protected void checkDatabaseLive() throws SQLException {
087        try (Connection connection = DriverManager.getConnection(url, Framework.getProperty(USER_PROPERTY, "sa"),
088                Framework.getProperty(PASSWORD_PROPERTY, null))) {
089            try (Statement st = connection.createStatement()) {
090                st.execute("SELECT 1");
091            }
092        }
093    }
094
095    /**
096     * @deprecated since 11.1, unused
097     */
098    @Deprecated(since = "11.1", forRemoval = true)
099    protected String getId() {
100        return "nuxeo"; // NOSONAR
101    }
102
103    @Override
104    public void tearDown() throws SQLException {
105        if (owner == null) {
106            return;
107        }
108        try {
109            tearDownDatabase(url);
110        } finally {
111            super.tearDown();
112        }
113    }
114
115    protected void tearDownDatabase(String url) throws SQLException {
116        Connection connection = DriverManager.getConnection(url, user, password);
117        try {
118            Statement st = connection.createStatement();
119            try {
120                String sql = "SHUTDOWN";
121                log.trace(sql);
122                st.execute(sql);
123            } finally {
124                st.close();
125            }
126        } finally {
127            connection.close();
128        }
129    }
130
131    @Override
132    public String getDeploymentContrib() {
133        return CONTRIB_XML;
134    }
135
136    @Override
137    public RepositoryDescriptor getRepositoryDescriptor() {
138        return new RepositoryDescriptor();
139    }
140
141    @Override
142    public boolean supportsFulltextSearch() {
143        return false;
144    }
145
146    @Override
147    public boolean supportsClustering() {
148        return true;
149    }
150
151}