Automatically refactor more exception testing to use new JUnit rules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178911894
This commit is contained in:
mcilwain 2017-08-14 09:20:03 -04:00 committed by Ben McIlwain
parent 36ad38e5df
commit 7dc224627f
125 changed files with 1970 additions and 1982 deletions

View file

@ -19,6 +19,7 @@ import static google.registry.model.ofy.CommitLogBucket.getBucketKey;
import static google.registry.model.ofy.CommitLogBucket.loadAllBuckets;
import static google.registry.model.ofy.CommitLogBucket.loadBucket;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import com.google.common.base.Suppliers;
@ -29,7 +30,6 @@ import google.registry.testing.InjectRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -44,10 +44,6 @@ public class CommitLogBucketTest {
@Rule
public final InjectRule inject = new InjectRule();
@Rule
public final ExpectedException thrown = ExpectedException.none();
CommitLogBucket bucket;
@Before
@ -69,16 +65,16 @@ public class CommitLogBucketTest {
@Test
public void test_getBucketKey_bucketNumberTooLow_throws() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("0 not in [");
getBucketKey(0);
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> getBucketKey(0));
assertThat(thrown).hasMessageThat().contains("0 not in [");
}
@Test
public void test_getBucketKey_bucketNumberTooHigh_throws() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("11 not in [");
getBucketKey(11);
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> getBucketKey(11));
assertThat(thrown).hasMessageThat().contains("11 not in [");
}
@Test

View file

@ -15,6 +15,7 @@
package google.registry.model.ofy;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.time.DateTimeZone.UTC;
@ -23,7 +24,6 @@ import google.registry.testing.AppEngineRule;
import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -36,9 +36,6 @@ public class CommitLogCheckpointTest {
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private static final DateTime T1 = START_OF_TIME;
private static final DateTime T2 = START_OF_TIME.plusMillis(1);
private static final DateTime T3 = START_OF_TIME.plusMillis(2);
@ -60,29 +57,43 @@ public class CommitLogCheckpointTest {
@Test
public void test_create_notEnoughBucketTimestamps_throws() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Bucket ids are incorrect");
CommitLogCheckpoint.create(DateTime.now(UTC), ImmutableMap.of(1, T1, 2, T2));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> CommitLogCheckpoint.create(DateTime.now(UTC), ImmutableMap.of(1, T1, 2, T2)));
assertThat(thrown).hasMessageThat().contains("Bucket ids are incorrect");
}
@Test
public void test_create_tooManyBucketTimestamps_throws() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Bucket ids are incorrect");
CommitLogCheckpoint.create(DateTime.now(UTC), ImmutableMap.of(1, T1, 2, T2, 3, T3, 4, T1));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
CommitLogCheckpoint.create(
DateTime.now(UTC), ImmutableMap.of(1, T1, 2, T2, 3, T3, 4, T1)));
assertThat(thrown).hasMessageThat().contains("Bucket ids are incorrect");
}
@Test
public void test_create_wrongBucketIds_throws() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Bucket ids are incorrect");
CommitLogCheckpoint.create(DateTime.now(UTC), ImmutableMap.of(0, T1, 1, T2, 2, T3));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
CommitLogCheckpoint.create(
DateTime.now(UTC), ImmutableMap.of(0, T1, 1, T2, 2, T3)));
assertThat(thrown).hasMessageThat().contains("Bucket ids are incorrect");
}
@Test
public void test_create_wrongBucketIdOrder_throws() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Bucket ids are incorrect");
CommitLogCheckpoint.create(DateTime.now(UTC), ImmutableMap.of(2, T2, 1, T1, 3, T3));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
CommitLogCheckpoint.create(
DateTime.now(UTC), ImmutableMap.of(2, T2, 1, T1, 3, T3)));
assertThat(thrown).hasMessageThat().contains("Bucket ids are incorrect");
}
}

View file

@ -20,6 +20,7 @@ import static com.googlecode.objectify.ObjectifyService.register;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.CommitLogBucket.getBucketKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
@ -37,7 +38,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -50,9 +50,6 @@ public class OfyCommitLogTest {
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -163,74 +160,91 @@ public class OfyCommitLogTest {
public void testTransactNew_deleteNotBackedUpKind_throws() throws Exception {
final CommitLogManifest backupsArentAllowedOnMe =
CommitLogManifest.create(getBucketKey(1), clock.nowUtc(), ImmutableSet.<Key<?>>of());
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Can't save/delete a @NotBackedUp");
ofy().transactNew(() -> ofy().delete().entity(backupsArentAllowedOnMe));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> ofy().transactNew(() -> ofy().delete().entity(backupsArentAllowedOnMe)));
assertThat(thrown).hasMessageThat().contains("Can't save/delete a @NotBackedUp");
}
@Test
public void testTransactNew_saveNotBackedUpKind_throws() throws Exception {
final CommitLogManifest backupsArentAllowedOnMe =
CommitLogManifest.create(getBucketKey(1), clock.nowUtc(), ImmutableSet.<Key<?>>of());
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Can't save/delete a @NotBackedUp");
ofy().transactNew(() -> ofy().save().entity(backupsArentAllowedOnMe));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> ofy().transactNew(() -> ofy().save().entity(backupsArentAllowedOnMe)));
assertThat(thrown).hasMessageThat().contains("Can't save/delete a @NotBackedUp");
}
@Test
public void testTransactNew_deleteVirtualEntityKey_throws() throws Exception {
final Key<TestVirtualObject> virtualEntityKey = TestVirtualObject.createKey("virtual");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Can't save/delete a @VirtualEntity");
ofy().transactNew(() -> ofy().delete().key(virtualEntityKey));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> ofy().transactNew(() -> ofy().delete().key(virtualEntityKey)));
assertThat(thrown).hasMessageThat().contains("Can't save/delete a @VirtualEntity");
}
@Test
public void testTransactNew_saveVirtualEntity_throws() throws Exception {
final TestVirtualObject virtualEntity = TestVirtualObject.create("virtual");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Can't save/delete a @VirtualEntity");
ofy().transactNew(() -> ofy().save().entity(virtualEntity));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> ofy().transactNew(() -> ofy().save().entity(virtualEntity)));
assertThat(thrown).hasMessageThat().contains("Can't save/delete a @VirtualEntity");
}
@Test
public void test_deleteWithoutBackup_withVirtualEntityKey_throws() throws Exception {
final Key<TestVirtualObject> virtualEntityKey = TestVirtualObject.createKey("virtual");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Can't save/delete a @VirtualEntity");
ofy().deleteWithoutBackup().key(virtualEntityKey);
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> ofy().deleteWithoutBackup().key(virtualEntityKey));
assertThat(thrown).hasMessageThat().contains("Can't save/delete a @VirtualEntity");
}
@Test
public void test_saveWithoutBackup_withVirtualEntity_throws() throws Exception {
final TestVirtualObject virtualEntity = TestVirtualObject.create("virtual");
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Can't save/delete a @VirtualEntity");
ofy().saveWithoutBackup().entity(virtualEntity);
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class, () -> ofy().saveWithoutBackup().entity(virtualEntity));
assertThat(thrown).hasMessageThat().contains("Can't save/delete a @VirtualEntity");
}
@Test
public void testTransact_twoSavesOnSameKey_throws() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
ofy()
.transact(
() -> {
ofy().save().entity(Root.create(1, getCrossTldKey()));
ofy().save().entity(Root.create(1, getCrossTldKey()));
});
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
() -> {
ofy().save().entity(Root.create(1, getCrossTldKey()));
ofy().save().entity(Root.create(1, getCrossTldKey()));
}));
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
}
@Test
public void testTransact_saveAndDeleteSameKey_throws() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
ofy()
.transact(
() -> {
ofy().save().entity(Root.create(1, getCrossTldKey()));
ofy().delete().entity(Root.create(1, getCrossTldKey()));
});
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
() -> {
ofy().save().entity(Root.create(1, getCrossTldKey()));
ofy().delete().entity(Root.create(1, getCrossTldKey()));
}));
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
}
@Test

View file

@ -30,9 +30,7 @@ import com.googlecode.objectify.annotation.Id;
import google.registry.model.contact.ContactResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -63,9 +61,6 @@ public class OfyFilterTest {
helper.tearDown();
}
@Rule
public final ExpectedException thrown = ExpectedException.none();
/**
* Key.create looks up kind metadata for the class of the object it is given. If this happens
* before the first reference to ObjectifyService, which statically triggers type registrations,

View file

@ -23,6 +23,8 @@ import static google.registry.model.ofy.Ofy.getBaseEntityClassFromEntityOrKey;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newContactResource;
import static google.registry.testing.DatastoreHelper.persistActiveContact;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -55,7 +57,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -67,10 +68,6 @@ public class OfyTest {
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
/** An entity to use in save and delete tests. */
private HistoryEntry someObject;
@ -92,13 +89,15 @@ public class OfyTest {
.getUpdateAutoTimestamp().getTimestamp();
// Set the clock in Ofy to the same time as the backup group root's save time.
Ofy ofy = new Ofy(new FakeClock(groupTimestamp));
thrown.expect(TimestampInversionException.class);
thrown.expectMessage(String.format(
"Timestamp inversion between transaction time (%s) and entities rooted under:\n"
+ "{Key<?>(ContactResource(\"2-ROID\"))=%s}",
groupTimestamp,
groupTimestamp));
ofy.transact(work);
TimestampInversionException thrown =
expectThrows(TimestampInversionException.class, () -> ofy.transact(work));
assertThat(thrown)
.hasMessageThat()
.contains(
String.format(
"Timestamp inversion between transaction time (%s) and entities rooted under:\n"
+ "{Key<?>(ContactResource(\"2-ROID\"))=%s}",
groupTimestamp, groupTimestamp));
}
@Test
@ -121,60 +120,89 @@ public class OfyTest {
@Test
public void testSavingKeyTwice() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().save().entity(someObject);
ofy().save().entity(someObject);
}});
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy().save().entity(someObject);
ofy().save().entity(someObject);
}
}));
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
}
@Test
public void testDeletingKeyTwice() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().delete().entity(someObject);
ofy().delete().entity(someObject);
}});
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy().delete().entity(someObject);
ofy().delete().entity(someObject);
}
}));
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
}
@Test
public void testSaveDeleteKey() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().save().entity(someObject);
ofy().delete().entity(someObject);
}});
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy().save().entity(someObject);
ofy().delete().entity(someObject);
}
}));
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
}
@Test
public void testDeleteSaveKey() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().delete().entity(someObject);
ofy().save().entity(someObject);
}});
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy().delete().entity(someObject);
ofy().save().entity(someObject);
}
}));
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
}
@Test
public void testSavingKeyTwiceInOneCall() {
thrown.expect(IllegalArgumentException.class);
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().save().entities(someObject, someObject);
}});
assertThrows(
IllegalArgumentException.class,
() ->
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy().save().entities(someObject, someObject);
}
}));
}
/** Simple entity class with lifecycle callbacks. */
@ -375,17 +403,24 @@ public class OfyTest {
@Test
public void test_getBaseEntityClassFromEntityOrKey_unregisteredEntity() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("SystemClock");
getBaseEntityClassFromEntityOrKey(new SystemClock());
IllegalStateException thrown =
expectThrows(
IllegalStateException.class,
() -> getBaseEntityClassFromEntityOrKey(new SystemClock()));
assertThat(thrown).hasMessageThat().contains("SystemClock");
}
@Test
public void test_getBaseEntityClassFromEntityOrKey_unregisteredEntityKey() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("UnknownKind");
getBaseEntityClassFromEntityOrKey(Key.create(
com.google.appengine.api.datastore.KeyFactory.createKey("UnknownKind", 1)));
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() ->
getBaseEntityClassFromEntityOrKey(
Key.create(
com.google.appengine.api.datastore.KeyFactory.createKey(
"UnknownKind", 1))));
assertThat(thrown).hasMessageThat().contains("UnknownKind");
}
@Test