-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue 597 -- entity manager is properly removed even if close operation fails + unit test. #820
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
extensions/persist/test/com/google/inject/persist/jpa/JpaPersistServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you confirm this test is actually running? i didn't think we used junit4. might need to convert to junit3. |
||
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 { | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry i missed this before: please add the apache copyright header to this file. you can copy it from the top of the other ones, just fix up the year.