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