[Teneo] Delete Database for Unit Tests [message #1228470] |
Tue, 07 January 2014 05:01  |
Eclipse User |
|
|
|
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 09:03   |
Eclipse User |
|
|
|
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
|
|
|
|
|
Re: [Teneo] Delete Database for Unit Tests [message #1229344 is a reply to message #1228470] |
Thu, 09 January 2014 03:43  |
Eclipse User |
|
|
|
Hi!
It's me again and first of all Thanks for the answer and support!
I played around a little bit more and since you mentioned it may be a hsqldb problem i tried the h2 database. There the remove of the database file works fine but when my second test run tries to write to a newly created database there comes a new exception:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):
...
So again, i think i may forgot something to close or deinitialise!
Do you or anybody else have an idea where this problem could come from.
In the evening i will try it on my os x machine, maybe everthing is ok there.
Thank again and
best regards
|
|
|
Powered by
FUDForum. Page generated in 0.03355 seconds