[Teneo] Delete Database for Unit Tests [message #1228470] |
Tue, 07 January 2014 10:01 |
Jürgen Weinberger Messages: 42 Registered: August 2012 |
Member |
|
|
Hi!
I am very new to Teneo and am currently playing around with it.
I wrote some Unit Tests where i always want to start with a clean database. The database is file based so i thought i just remove the database directory at the setUp in my unit tests.
This doesn't work because some process seems to use the .log file and this leads to an IOException (java.io.IOException: Unable to delete file: c:\tmp\hsqldb.log).
My question now is how do i completly "shut down" the database so that no process uses this file anymore. I already tried it in the tearDown method but had no success. Here is some of my code:
private final static String dbFilePath = "c:/tmp/";
private final static Properties hibernateProperties = new Properties();
static {
hibernateProperties.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
hibernateProperties.setProperty(Environment.USER, "sa");
hibernateProperties.setProperty(Environment.URL, "jdbc:hsqldb:file:"+dbFilePath+"hsqldb");
hibernateProperties.setProperty(Environment.PASS, "");
hibernateProperties.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT, "CREATE,REFRESH,PERSIST,MERGE");
hibernateProperties.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");
}
@Before
public void setup() {
cleanUp();
String dataStoreName = "LocationStore";
adapterFactory = new ComposedAdapterFactory();
domain = new AdapterFactoryEditingDomain(
adapterFactory, new BasicCommandStack());
hbDataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);
hbDataStore.setDataStoreProperties(hibernateProperties);
hbDataStore.setEPackages(new EPackage[] {LocationPackage.eINSTANCE});
hbDataStore.initialize();
getLogger().debug(hbDataStore.getMappingXML());
sc = new SessionController();
sc.setHbDataStore(hbDataStore);
SessionController.registerSessionController("testsc", sc);
String uriStr = "hibernate://?" + HibernateResource.SESSION_CONTROLLER_PARAM + "=" + "testsc";
resource = domain.getResourceSet().createResource(URI.createURI(uriStr));
locationStore = getLocationStore();
createDefault();
}
@After
public void tearDown() {
resource.unload();
resource = null;
hbDataStore.close();
SessionController.deRegisterSessionController("testsc");
sc.getSessionWrapper().close();
sc = null;
HbHelper.INSTANCE.closeAll();
HbHelper.INSTANCE.deRegisterDataStore(hbDataStore);
hbDataStore = null;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void cleanUp() {
try {
FileUtils.deleteDirectory(new File(dbFilePath));
} catch (IOException e) {
e.printStackTrace();
}
}
What did i miss ?
Thanks in advanced and best regards
Weinma
|
|
|
Re: [Teneo] Delete Database for Unit Tests [message #1228565 is a reply to message #1228470] |
Tue, 07 January 2014 14:03 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Jurgen,
For Teneo I do the same (see the drop database done there):
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/stores/HsqldbTestDatabaseAdapter.java
But I am on linux/ubuntu which makes it apparently possible to do this. So not sure if/what other ways there are to
delete a db for hsqldb...
gr. Martin
On 01/07/2014 11:01 AM, Jürgen Weinberger wrote:
> Hi!
>
> I am very new to Teneo and am currently playing around with it. I wrote some Unit Tests where i always want to start
> with a clean database. The database is file based so i thought i just remove the database directory at the setUp in my
> unit tests.
>
> This doesn't work because some process seems to use the .log file and this leads to an IOException (java.io.IOException:
> Unable to delete file: c:\tmp\hsqldb.log).
>
> My question now is how do i completly "shut down" the database so that no process uses this file anymore. I already
> tried it in the tearDown method but had no success. Here is some of my code:
>
>
> private final static String dbFilePath = "c:/tmp/";
> private final static Properties hibernateProperties = new Properties();
> static {
> hibernateProperties.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
> hibernateProperties.setProperty(Environment.USER, "sa");
> hibernateProperties.setProperty(Environment.URL, "jdbc:hsqldb:file:"+dbFilePath+"hsqldb");
> hibernateProperties.setProperty(Environment.PASS, "");
> hibernateProperties.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
> hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT,
> "CREATE,REFRESH,PERSIST,MERGE");
> hibernateProperties.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");
> }
>
>
> @Before
> public void setup() {
> cleanUp();
> String dataStoreName = "LocationStore";
>
> adapterFactory = new ComposedAdapterFactory();
> domain = new AdapterFactoryEditingDomain(
> adapterFactory, new BasicCommandStack());
>
> hbDataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);
> hbDataStore.setDataStoreProperties(hibernateProperties);
> hbDataStore.setEPackages(new EPackage[] {LocationPackage.eINSTANCE});
> hbDataStore.initialize();
> getLogger().debug(hbDataStore.getMappingXML());
>
> sc = new SessionController();
> sc.setHbDataStore(hbDataStore); SessionController.registerSessionController("testsc", sc);
>
> String uriStr = "hibernate://?" + HibernateResource.SESSION_CONTROLLER_PARAM + "=" + "testsc";
> resource = domain.getResourceSet().createResource(URI.createURI(uriStr));
>
> locationStore = getLocationStore();
>
> createDefault();
> }
>
> @After
> public void tearDown() {
> resource.unload();
> resource = null;
> hbDataStore.close();
>
> SessionController.deRegisterSessionController("testsc");
> sc.getSessionWrapper().close();
> sc = null;
>
> HbHelper.INSTANCE.closeAll();
> HbHelper.INSTANCE.deRegisterDataStore(hbDataStore);
> hbDataStore = null;
>
> try {
> Thread.sleep(5000);
> } catch (InterruptedException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> public void cleanUp() {
> try {
> FileUtils.deleteDirectory(new File(dbFilePath));
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
>
> What did i miss ?
>
> Thanks in advanced and best regards
> Weinma
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02613 seconds