package com.google.inject.persist.jpa;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;

import org.junit.Before;
import org.junit.Test;

public class JpaPersistServiceTest {

  private static final String PERSISTENCE_UNIT_NAME = "test_persistence_unit_name";
  private static final Properties PERSISTENCE_PROPERTIES = new Properties();

  private final JpaPersistService sut = new JpaPersistService(PERSISTENCE_UNIT_NAME, PERSISTENCE_PROPERTIES);
  private final PersistenceProvider provider = mock(PersistenceProvider.class);
  private final EntityManagerFactory factory = mock(EntityManagerFactory.class);
  private final EntityManager entityManager = mock(EntityManager.class);

  @Before
  public void setUp() throws Exception {
    when(provider.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, PERSISTENCE_PROPERTIES)).thenReturn(factory);
    when(factory.createEntityManager()).thenReturn(entityManager);
  }

  @Test
  public void givenErrorOnEntityManagerClose_whenEndIsCalled_thenEntityManagerIsRemoved() {
    sut.start(factory);
    sut.begin();

    // arrange an exception on sut.end(), which invokes entityManager.close()
    doThrow(SimulatedException.class).when(entityManager).close();
    try {
      sut.end();
      fail("Exception expected");
    }
    catch (SimulatedException expected) {
      assertThat(sut.isWorking(), is(false));
    }
  }

  private class SimulatedException extends RuntimeException {
  }
}

@@ -33,16 +33,49 @@
 t.jpa;%0A%0A
+import junit.framework.TestCase;%0A
 import s
@@ -106,16 +106,16 @@
 ers.is;%0A
-
 import s
@@ -153,45 +153,8 @@
 at;%0A
-import static org.junit.Assert.fail;%0A
 impo
@@ -445,57 +445,8 @@
 r;%0A%0A
-import org.junit.Before;%0Aimport org.junit.Test;%0A%0A
 publ
@@ -476,16 +476,33 @@
 iceTest 
+extends TestCase 
 %7B%0A%0A  pri
@@ -1010,13 +1010,15 @@
 %0A  @
-Befor
+Overrid
 e%0A  
@@ -1246,16 +1246,8 @@
  %7D%0A%0A
-  @Test%0A
   pu
@@ -1256,16 +1256,21 @@
 ic void 
+test_
 givenErr

Fix issue 597 -- JUnit 3 style