Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problem removing directory with database
Problem removing directory with database [message #1326370] Thu, 01 May 2014 18:14 Go to next message
Rainer H. is currently offline Rainer H.Friend
Messages: 21
Registered: July 2009
Junior Member
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();
	}

}

Re: Problem removing directory with database [message #1328327 is a reply to message #1326370] Fri, 02 May 2014 16:40 Go to previous message
Shaun Smith is currently offline Shaun SmithFriend
Messages: 197
Registered: July 2009
Senior Member
For testing I use the embedded Derby and use Maven to clean up the database file by placing it in the target folder. Look at the pom.xml in this example: http://git.eclipse.org/c/eclipselink/examples.git/tree/jpa/employee/employee.model

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.12.4</version>
				<configuration>
					<systemProperties>
						<property>
							<name>derby.stream.error.file</name>
							<value>target/derby.log</value>
						</property>



This doesn't exactly answer your question but I hope it helps!

--Shaun

[Updated on: Fri, 02 May 2014 16:40]

Report message to a moderator

Previous Topic:Unique constraints in unidirectional one to one relationship
Next Topic:Unmapped Columns in ResultSet
Goto Forum:
  


Current Time: Thu Apr 25 06:05:18 GMT 2024

Powered by FUDForum. Page generated in 0.02938 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top