package beaform.dao;

import static org.junit.Assert.assertFalse;

import java.util.concurrent.CountDownLatch;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Test;

@SuppressWarnings("static-method")
public class ShutDownHookTest {

	@Test
	public void testHook() throws InterruptedException {
		EntityManagerFactory entityManagerFact = Persistence.createEntityManagerFactory("test");
		EntityManager entityManager = entityManagerFact.createEntityManager();
		ShutDownHook hook = new ShutDownHook(entityManager, entityManagerFact);
		CountDownLatch latch = new CountDownLatch(1);

		Runnable withsync = new Runnable() {

			@Override
			public void run() {
				hook.run();
				latch.countDown();
			}
		};
		Thread thread = new Thread(withsync);
		thread.start();
		latch.await();
		assertFalse("Entity manager isn't closed", entityManager.isOpen());
		assertFalse("Entity manager factory isn't closed", entityManagerFact.isOpen());
	}
}

@@ -386,16 +386,22 @@
 ion %7B%0A%09%09
+final 
 EntityMa
@@ -483,16 +483,22 @@
 st%22);%0A%09%09
+final 
 EntityMa
@@ -562,16 +562,22 @@
 er();%0A%09%09
+final 
 ShutDown
@@ -642,16 +642,819 @@
 act);%0A%09%09
+final CountDownLatch latch = new CountDownLatch(1);%0A%0A%09%09Runnable withsync = new Runnable() %7B%0A%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09hook.run();%0A%09%09%09%09latch.countDown();%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09Thread thread = new Thread(withsync);%0A%09%09thread.start();%0A%09%09latch.await();%0A%09%09assertFalse(%22Entity manager isn't closed%22, entityManager.isOpen());%0A%09%09assertFalse(%22Entity manager factory isn't closed%22, entityManagerFact.isOpen());%0A%09%7D%0A%0A%09@Test%0A%09public void testHookAlreadyClosed() throws InterruptedException %7B%0A%09%09final EntityManagerFactory entityManagerFact = Persistence.createEntityManagerFactory(%22test%22);%0A%09%09final EntityManager entityManager = entityManagerFact.createEntityManager();%0A%09%09final ShutDownHook hook = new ShutDownHook(entityManager, entityManagerFact);%0A%0A%09%09entityManager.close();%0A%09%09entityManagerFact.close();%0A%0A%09%09final 
 CountDow

Add additional test for the shutdown hook