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.io.File;
023import java.sql.DriverManager;
024import java.sql.SQLException;
025
026import org.nuxeo.common.utils.FileUtils;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * @author Florent Guillaume
031 */
032public class DatabaseDerby extends DatabaseHelper {
033
034    public static final DatabaseHelper INSTANCE = new DatabaseDerby();
035
036    /** This directory will be deleted and recreated. */
037    private static final String DIRECTORY = "target/test/derby";
038
039    private static final String DEF_USER = "sa";
040
041    private static final String DEF_PASSWORD = "";
042
043    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-derby-contrib.xml";
044
045    private static final String LOG = "target/test/derby.log";
046
047    private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
048
049    protected String url;
050
051    protected void setProperties() {
052        setProperty(DATABASE_PROPERTY, new File(DIRECTORY).getAbsolutePath());
053        setProperty(USER_PROPERTY, DEF_USER);
054        setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
055        // for sql directory tests
056        setProperty(DRIVER_PROPERTY, DRIVER);
057        url = String.format("jdbc:derby:%s;create=true", Framework.getProperty(DATABASE_PROPERTY));
058        setProperty(URL_PROPERTY, url);
059    }
060
061    @Override
062    public void setUp() throws SQLException {
063        super.setUp();
064        System.setProperty("derby.stream.error.file", new File(LOG).getAbsolutePath());
065        // newInstance needed after a previous shutdown
066        try {
067            Class.forName(DRIVER).newInstance();
068        } catch (ReflectiveOperationException e) {
069            throw new RuntimeException(e);
070        }
071        File dbdir = new File(DIRECTORY);
072        File parent = dbdir.getParentFile();
073        FileUtils.deleteTree(dbdir);
074        parent.mkdirs();
075        // the following noticeably improves performance
076        System.setProperty("derby.system.durability", "test");
077        setProperties();
078    }
079
080    @Override
081    public void tearDown() throws SQLException {
082        Exception ex = null;
083        try {
084            DriverManager.getConnection("jdbc:derby:;shutdown=true");
085            // after this to reboot the driver a newInstance is needed
086        } catch (SQLException e) {
087            String message = e.getMessage();
088            if ("Derby system shutdown.".equals(message)) {
089                return;
090            }
091            if ("org.apache.derby.jdbc.EmbeddedDriver is not registered with the JDBC driver manager".equals(message)) {
092                // huh? happens for testClustering
093                return;
094            }
095            ex = e;
096        } finally {
097            super.tearDown();
098        }
099        throw new RuntimeException("Expected Derby shutdown exception instead", ex);
100    }
101
102    @Override
103    public String getDeploymentContrib() {
104        return CONTRIB_XML;
105    }
106
107    @Override
108    public RepositoryDescriptor getRepositoryDescriptor() {
109        RepositoryDescriptor descriptor = new RepositoryDescriptor();
110        return descriptor;
111    }
112
113    @Override
114    public boolean supportsMultipleFulltextIndexes() {
115        return false;
116    }
117
118}