Add annotation processor to generate converter for VKey (#566)

This commit is contained in:
Shicong Huang 2020-04-29 17:29:05 -04:00 committed by GitHub
parent c361c9e601
commit 19bc1c9c9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 695 additions and 51 deletions

View file

@ -37,9 +37,10 @@ public class VKeyTest {
@Test
public void testOptionalAccessors() {
VKey<TestObject> key = VKey.create(TestObject.class, null, null);
assertThat(key.maybeGetSqlKey().isPresent()).isFalse();
assertThat(key.maybeGetOfyKey().isPresent()).isFalse();
VKey<TestObject> key =
VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo")));
assertThat(key.maybeGetSqlKey().isPresent()).isTrue();
assertThat(key.maybeGetOfyKey().isPresent()).isTrue();
Key<TestObject> ofyKey = Key.create(TestObject.create("foo"));
assertThat(VKey.createOfy(TestObject.class, ofyKey).maybeGetOfyKey().get()).isEqualTo(ofyKey);

View file

@ -0,0 +1,65 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import google.registry.persistence.VKey;
import google.registry.persistence.WithLongVKey;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Test SQL persistence of VKey. */
@RunWith(JUnit4.class)
public class LongVKeyConverterTest {
@Rule
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder()
.withEntityClass(TestEntity.class, VKeyConverter_LongType.class)
.buildUnitTestRule();
@Test
public void testRoundTrip() {
TestEntity original = new TestEntity(VKey.createSql(TestEntity.class, 10L));
jpaTm().transact(() -> jpaTm().getEntityManager().persist(original));
TestEntity retrieved =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(retrieved.number.getSqlKey()).isEqualTo(10L);
}
@Entity(name = "TestEntity")
@WithLongVKey(classNameSuffix = "LongType")
static class TestEntity {
@Id String id = "id";
VKey<TestEntity> number;
TestEntity(VKey<TestEntity> number) {
this.number = number;
}
/** Default constructor, needed for hibernate. */
public TestEntity() {}
}
}

View file

@ -18,9 +18,9 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import google.registry.persistence.VKey;
import google.registry.persistence.WithStringVKey;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Rule;
@ -30,13 +30,15 @@ import org.junit.runners.JUnit4;
/** Test SQL persistence of VKey. */
@RunWith(JUnit4.class)
public class VKeyConverterTest {
public class StringVKeyConverterTest {
@Rule
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
new JpaTestRules.Builder()
.withEntityClass(TestEntity.class, VKeyConverter_StringType.class)
.buildUnitTestRule();
public VKeyConverterTest() {}
public StringVKeyConverterTest() {}
@Test
public void testRoundTrip() {
@ -50,21 +52,11 @@ public class VKeyConverterTest {
assertThat(retrieved.other.getSqlKey()).isEqualTo("ImSpartacus!");
}
static class TestEntityVKeyConverter extends VKeyConverter<TestEntity> {
@Override
protected Class<TestEntity> getAttributeClass() {
return TestEntity.class;
}
}
@Entity(name = "TestEntity")
@WithStringVKey(classNameSuffix = "StringType")
static class TestEntity {
@Id String id;
// Specifying "@Converter(autoApply = true) on TestEntityVKeyConverter this doesn't seem to
// work.
@Convert(converter = TestEntityVKeyConverter.class)
VKey<TestEntity> other;
TestEntity(String id, VKey<TestEntity> other) {