Problem removing directory with database [message #1326370] |
Thu, 01 May 2014 18:14 |
|
Hi.
I'm using EclipseLink 2.5.1 and Apache Derby 10.10.1.1. I wrote a simple JUnit test class. At the end I close the EntitiyManager and the EntityManagerFactory and try to delete the Directory containing the database. But this fails because a file could not be deleted. For deletion of the Directory I use Apache Common IO because it has a simple method for deleting non-empty Directory. Also log4j2 is used for logging Messages.
In my example I've make a Workaround which only prints an error message. But I want to delete the directory after the tests because they were only used for tests.
Any idea how to reach the Goal?
Here my code:
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import rh.jpa.entities.ToDo;
/**
* Example from http://www.vogella.com/tutorials/JavaPersistenceAPI/article.html.
* (modified to use JUnit)
*/
// CHECKSTYLE:OFF
public final class JPAexample {
// CHECKSTYLE:ON
/** The one and only logging instance of this class. */
private static final Logger LOGGER = LogManager.getLogger(JPAexample.class);
/**
* Name of the persistence unit.<br>
* (Have to be the same as in persistence.xml)
*/
private static final String PERSISTENCE_UNIT_NAME = "todos"; //$NON-NLS-1$
/** The entity manager factory. */
private static EntityManagerFactory factory;
/** An entity manager. Used in all tests. */
private static EntityManager em;
/** Constructor requested by checkstyle. */
public JPAexample() {
super();
}
/** Removing the database. */
private static void removeDatabase() {
final File directory = new File("databases"); //$NON-NLS-1$
try {
FileUtils.deleteDirectory(directory);
} catch (final IOException exception) {
// FIXME often the file databases\simpleDB\seg0\cf0.dat will not be deleted: why, is it open
JPAexample.LOGGER.warn(exception);
}
}
/**
* Do some preparations for all tests.<br>
* (here: create an EntityManagerFactory)
* @throws Exception if any error occurs(e.g. EntityManagerFactory could not created
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
JPAexample.removeDatabase();
JPAexample.factory = Persistence.createEntityManagerFactory(JPAexample.PERSISTENCE_UNIT_NAME);
JPAexample.em = JPAexample.factory.createEntityManager();
JPAexample.LOGGER.debug(JPAexample.factory);
}
/**
* Do some cleanup after the tests. (e.g. remove created database)
* @throws Exception if any error occurs
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
JPAexample.em.close();
JPAexample.em = null;
JPAexample.factory.close();
JPAexample.factory = null;
JPAexample.removeDatabase();
}
/** Checks whether the initialization in <code>setUpBeforeClass()</code> was successful. */
@SuppressWarnings("static-method")
@Test
public void initialize() {
Assert.assertNotNull("EntityManagerFactory not initialized", JPAexample.factory); //$NON-NLS-1$
}
/**
* Read the existing ToDo's from the database.<br>
* No special tests are defined. (Failures cause exceptions.)
*/
@SuppressWarnings("static-method")
@Test
public void read() {
// read the existing entries and write to the logger
final TypedQuery<ToDo> q = JPAexample.em.createQuery("select t from ToDo t", ToDo.class); //$NON-NLS-1$
final List<ToDo> todoList = q.getResultList();
// JAVA8 todoList.forEach(element -> JPA_3_1_7.LOGGER.debug(element));
for (final ToDo todo : todoList) {
JPAexample.LOGGER.debug(todo);
}
JPAexample.LOGGER.debug("size: {}", Integer.valueOf(todoList.size())); //$NON-NLS-1$
}
/**
* Create a new ToDo and save it in the database.<br>
* No special tests are defined. (Failures cause exceptions.)
*/
@SuppressWarnings("static-method")
@Test
public void write() {
// create new todo
JPAexample.em.getTransaction().begin();
final ToDo todo = new ToDo();
todo.setSummary("test: summary"); //$NON-NLS-1$
todo.setDescription("test: description"); //$NON-NLS-1$
JPAexample.em.persist(todo);
JPAexample.em.getTransaction().commit();
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.02938 seconds