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