#992 - API Change - Change Transaction.close() to throw PersistenceException rather than IOException

This commit is contained in:
Rob Bygrave
2017-03-13 22:10:08 +13:00
parent 41a7f2c97e
commit 1d99a5c5a4
5 changed files with 95 additions and 10 deletions
@@ -0,0 +1,71 @@
package org.tests.transaction;
import io.ebean.BaseTestCase;
import io.ebean.DuplicateKeyException;
import io.ebean.Ebean;
import io.ebean.Transaction;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tests.model.draftable.Document;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestTransactionTryResources extends BaseTestCase {
private static final Logger log = LoggerFactory.getLogger(TestTransactionTryResources.class);
@Test
public void tryWithResources_simple() {
Document doc = new Document();
try (Transaction transaction = Ebean.beginTransaction()) {
doc.setTitle("tryWithResources");
doc.setBody("stuff");
doc.save();
transaction.commit();
}
Document document = Document.find.asDraft(doc.getId());
assertThat(document).isNotNull();
}
@Test
public void tryWithResources_catch() {
try (Transaction transaction = Ebean.beginTransaction()) {
Document doc = new Document();
doc.setTitle("tryWithResources_catch");
doc.setBody("tryWithResources_catch_1");
doc.save();
Document doc2 = new Document();
doc2.setTitle("tryWithResources_catch");
doc2.setBody("tryWithResources_catch_2");
doc2.save();
transaction.commit();
} catch (DuplicateKeyException e) {
log.info("catch duplicate ... " + e);
Document doc3 = new Document();
doc3.setTitle("tryWithResources_catch");
doc3.setBody("tryWithResources_catch_3");
doc3.save();
List<Document> docs = Document.find.query()
.where().startsWith("body", "tryWithResources_catch")
.asDraft()
.findList();
assertThat(docs).hasSize(1);
}
}
}