Automatically apply JPA type converters (#305)

* Automatically apply JPA type converters

* Include converters in tests and schema generation too
This commit is contained in:
Ben McIlwain 2019-10-08 13:16:39 -04:00 committed by GitHub
parent 0fd7cf29b5
commit 5dc058ec99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 20 deletions

View file

@ -19,6 +19,7 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.JUnitBackports.assertThrows;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.persistence.CreateAutoTimestampConverter;
import google.registry.schema.domain.RegistryLock;
import google.registry.schema.domain.RegistryLock.Action;
import google.registry.testing.AppEngineRule;
@ -37,7 +38,9 @@ public final class RegistryLockDaoTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().withEntityClass(RegistryLock.class).build();
new JpaTransactionManagerRule.Builder()
.withEntityClass(RegistryLock.class, CreateAutoTimestampConverter.class)
.build();
@Test
public void testSaveAndLoad_success() {

View file

@ -18,6 +18,7 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import google.registry.persistence.PersistenceModule;
import google.registry.testing.FakeClock;
@ -145,9 +146,9 @@ public class JpaTransactionManagerRule extends ExternalResource {
return this;
}
/** Adds an annotated class to the known entities for the database. */
public Builder withEntityClass(Class clazz) {
this.extraEntityClasses.add(clazz);
/** Adds annotated class(es) to the known entities for the database. */
public Builder withEntityClass(Class... classes) {
this.extraEntityClasses.addAll(ImmutableSet.copyOf(classes));
return this;
}

View file

@ -19,7 +19,6 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.cfg.Environment;
@ -44,7 +43,7 @@ public class CreateAutoTimestampConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.withEntityClass(TestEntity.class, CreateAutoTimestampConverter.class)
.withProperty(Environment.HBM2DDL_AUTO, "update")
.build();
@ -78,7 +77,6 @@ public class CreateAutoTimestampConverterTest {
@Id String name;
@Convert(converter = CreateAutoTimestampConverter.class)
CreateAutoTimestamp cat;
public TestEntity() {}

View file

@ -19,7 +19,6 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.ImmutableObject;
import google.registry.model.UpdateAutoTimestamp;
import google.registry.model.transaction.JpaTransactionManagerRule;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.cfg.Environment;
@ -43,7 +42,7 @@ public class UpdateAutoTimestampConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.withEntityClass(TestEntity.class, UpdateAutoTimestampConverter.class)
.withProperty(Environment.HBM2DDL_AUTO, "update")
.build();
@ -89,7 +88,6 @@ public class UpdateAutoTimestampConverterTest {
@Id String name;
@Convert(converter = UpdateAutoTimestampConverter.class)
UpdateAutoTimestamp uat;
public TestEntity() {}

View file

@ -20,6 +20,7 @@ import static google.registry.testing.JUnitBackports.assertThrows;
import com.google.common.collect.ImmutableMap;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.persistence.CreateAutoTimestampConverter;
import java.math.BigDecimal;
import javax.persistence.PersistenceException;
import org.joda.money.CurrencyUnit;
@ -34,7 +35,9 @@ public class PremiumListDaoTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().withEntityClass(PremiumList.class).build();
new JpaTransactionManagerRule.Builder()
.withEntityClass(PremiumList.class, CreateAutoTimestampConverter.class)
.build();
private static final ImmutableMap<String, BigDecimal> TEST_PRICES =
ImmutableMap.of(