From 6cdf74f3514a984ce681b77dda0cb1e1700b882c Mon Sep 17 00:00:00 2001 From: wea_ondara Date: Thu, 8 Sep 2022 17:20:16 +0200 Subject: [PATCH] more tests --- .../java/jef/model/EntityInitializer.java | 6 +-- ...rCollectionAnnotationClassInheritTest.java | 45 ++++++++++++++++++ ...alizerCollectionAnnotationMissingTest.java | 46 +++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/test/java/jef/model/EntityInitializerCollectionAnnotationClassInheritTest.java create mode 100644 src/test/java/jef/model/EntityInitializerCollectionAnnotationMissingTest.java diff --git a/src/main/java/jef/model/EntityInitializer.java b/src/main/java/jef/model/EntityInitializer.java index c39406f..6a67930 100644 --- a/src/main/java/jef/model/EntityInitializer.java +++ b/src/main/java/jef/model/EntityInitializer.java @@ -20,7 +20,7 @@ class EntityInitializer { throw new ModelException("DbSet " + ctxField.getName() + " is missing the " + Clazz.class.getSimpleName() + " annotation"); } if (!SerializableObject.class.isAssignableFrom(clazzAnnotation.value())) { - throw new ModelException("DbSet " + ctxField.getName() + " has a class in its " + Clazz.class.getSimpleName() + " annotation that does not inherit from " + SerializableObject.class.getSimpleName() + ": " + clazzAnnotation.value().getSimpleName()); //TODO testcase for this + throw new ModelException("DbSet " + ctxField.getName() + " has a class in its " + Clazz.class.getSimpleName() + " annotation that does not inherit from " + SerializableObject.class.getSimpleName() + ": " + clazzAnnotation.value().getSimpleName()); } var dbSetClazz = (Class) clazzAnnotation.value(); initEntity(mb, dbSetClazz, ctxField.getName(), true); @@ -52,11 +52,11 @@ class EntityInitializer { //e.g. class Entity { @Clazz(Entity2.class) List ent; @Clazz(Entity2.class) Set ent2; } var clazzAnnotation = f.getAnnotation(Clazz.class); if (clazzAnnotation == null) { - throw new ModelException("Field " + f.getClass().getSimpleName() + "::" + f.getName() + " is missing the " + Clazz.class.getSimpleName() + " annotation"); + throw new ModelException("Field " + clazz.getSimpleName() + "::" + f.getName() + " is missing the " + Clazz.class.getSimpleName() + " annotation"); } var fClazz = clazzAnnotation.value(); if (!SerializableObject.class.isAssignableFrom(fClazz)) { - throw new ModelException("Field " + f.getClass().getSimpleName() + "::" + f.getName() + " has a class in its " + Clazz.class.getSimpleName() + " annotation that does not inherit from " + SerializableObject.class.getSimpleName() + ": " + fClazz.getSimpleName()); //TODO testcase for this + throw new ModelException("Field " + clazz.getSimpleName() + "::" + f.getName() + " has a class in its " + Clazz.class.getSimpleName() + " annotation that does not inherit from " + SerializableObject.class.getSimpleName() + ": " + fClazz.getSimpleName()); } var foundCollection = entity.fields().stream() .filter(e -> Collection.class.isAssignableFrom(e.getField().getType()) diff --git a/src/test/java/jef/model/EntityInitializerCollectionAnnotationClassInheritTest.java b/src/test/java/jef/model/EntityInitializerCollectionAnnotationClassInheritTest.java new file mode 100644 index 0000000..e6e7cd8 --- /dev/null +++ b/src/test/java/jef/model/EntityInitializerCollectionAnnotationClassInheritTest.java @@ -0,0 +1,45 @@ +package jef.model; + +import jef.DbSet; +import jef.model.annotations.Clazz; +import jef.model.annotations.Id; +import jef.serializable.SerializableObject; +import lombok.Getter; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; + +class EntityInitializerCollectionAnnotationClassInheritTest { + @Test + public void test() { + assertEquals("Field TestClass2::nested is missing the Clazz annotation", + assertThrowsExactly(ModelException.class, () -> ModelBuilder.from(Ctx.class)).getMessage()); + } + + public static class Ctx extends DbContext { + @Clazz(TestClass2.class) + private DbSet objects2; + @Clazz(TestClass.class) + private DbSet objects; + } + + @Getter + public static class TestClass2 extends SerializableObject { + @Id + public int i2 = 1; + public List nested; + } + + @Getter + public static class TestClass extends SerializableObject { + @Id + public int i = 1; + public Object o = new Object(); + public double d; + public float f; + public long l; + } +} \ No newline at end of file diff --git a/src/test/java/jef/model/EntityInitializerCollectionAnnotationMissingTest.java b/src/test/java/jef/model/EntityInitializerCollectionAnnotationMissingTest.java new file mode 100644 index 0000000..7673c59 --- /dev/null +++ b/src/test/java/jef/model/EntityInitializerCollectionAnnotationMissingTest.java @@ -0,0 +1,46 @@ +package jef.model; + +import jef.DbSet; +import jef.model.annotations.Clazz; +import jef.model.annotations.Id; +import jef.serializable.SerializableObject; +import lombok.Getter; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; + +class EntityInitializerCollectionAnnotationMissingTest { + @Test + public void test() { + assertEquals("Field TestClass2::nested has a class in its Clazz annotation that does not inherit from SerializableObject: int", + assertThrowsExactly(ModelException.class, () -> ModelBuilder.from(Ctx.class)).getMessage()); + } + + public static class Ctx extends DbContext { + @Clazz(TestClass2.class) + private DbSet objects2; + @Clazz(TestClass.class) + private DbSet objects; + } + + @Getter + public static class TestClass2 extends SerializableObject { + @Id + public int i2 = 1; + @Clazz(int.class) + public List nested; + } + + @Getter + public static class TestClass extends SerializableObject { + @Id + public int i = 1; + public Object o = new Object(); + public double d; + public float f; + public long l; + } +} \ No newline at end of file