001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012 013package org.nuxeo.ecm.core.storage.sql; 014 015import java.io.File; 016import java.sql.DriverManager; 017import java.sql.SQLException; 018import java.util.HashMap; 019import java.util.Map; 020 021import org.nuxeo.common.utils.FileUtils; 022import org.nuxeo.runtime.api.Framework; 023 024/** 025 * @author Florent Guillaume 026 */ 027public class DatabaseDerby extends DatabaseHelper { 028 029 public static final DatabaseHelper INSTANCE = new DatabaseDerby(); 030 031 /** This directory will be deleted and recreated. */ 032 private static final String DIRECTORY = "target/test/derby"; 033 034 private static final String DEF_USER = "sa"; 035 036 private static final String DEF_PASSWORD = ""; 037 038 private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-derby-contrib.xml"; 039 040 private static final String LOG = "target/test/derby.log"; 041 042 private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; 043 044 protected String url; 045 046 protected void setProperties() { 047 Framework.getProperties().setProperty(REPOSITORY_PROPERTY, repositoryName); 048 setProperty(DATABASE_PROPERTY, new File(DIRECTORY).getAbsolutePath()); 049 setProperty(USER_PROPERTY, DEF_USER); 050 setProperty(PASSWORD_PROPERTY, DEF_PASSWORD); 051 // for sql directory tests 052 setProperty(DRIVER_PROPERTY, DRIVER); 053 url = String.format("jdbc:derby:%s;create=true", Framework.getProperty(DATABASE_PROPERTY)); 054 setProperty(URL_PROPERTY, url); 055 } 056 057 @Override 058 public void setUp() throws Exception { 059 super.setUp(); 060 System.setProperty("derby.stream.error.file", new File(LOG).getAbsolutePath()); 061 // newInstance needed after a previous shutdown 062 Class.forName(DRIVER).newInstance(); 063 File dbdir = new File(DIRECTORY); 064 File parent = dbdir.getParentFile(); 065 FileUtils.deleteTree(dbdir); 066 parent.mkdirs(); 067 // the following noticeably improves performance 068 System.setProperty("derby.system.durability", "test"); 069 setProperties(); 070 } 071 072 @Override 073 public void tearDown() throws SQLException { 074 Exception ex = null; 075 try { 076 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 077 // after this to reboot the driver a newInstance is needed 078 } catch (SQLException e) { 079 String message = e.getMessage(); 080 if ("Derby system shutdown.".equals(message)) { 081 return; 082 } 083 if ("org.apache.derby.jdbc.EmbeddedDriver is not registered with the JDBC driver manager".equals(message)) { 084 // huh? happens for testClustering 085 return; 086 } 087 ex = e; 088 } finally { 089 super.tearDown(); 090 } 091 throw new RuntimeException("Expected Derby shutdown exception instead", ex); 092 } 093 094 @Override 095 public String getDeploymentContrib() { 096 return CONTRIB_XML; 097 } 098 099 @Override 100 public RepositoryDescriptor getRepositoryDescriptor() { 101 RepositoryDescriptor descriptor = new RepositoryDescriptor(); 102 descriptor.xaDataSourceName = "org.apache.derby.jdbc.EmbeddedXADataSource"; 103 Map<String, String> properties = new HashMap<String, String>(); 104 properties.put("createDatabase", "create"); 105 properties.put("databaseName", Framework.getProperty(DATABASE_PROPERTY)); 106 properties.put("user", Framework.getProperty(USER_PROPERTY)); 107 properties.put("password", Framework.getProperty(PASSWORD_PROPERTY)); 108 descriptor.properties = properties; 109 return descriptor; 110 } 111 112 @Override 113 public boolean supportsMultipleFulltextIndexes() { 114 return false; 115 } 116 117}