From 8c73b967a5e477c14b6422b1bff9064b20582351 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 10 Dec 2020 17:37:55 -0500 Subject: [PATCH] validate for multiple ID annotations like the java side does convert the tests to kotlin fixes #11887 --- .../deployment/pom.xml | 35 +++++++++++++++ .../KotlinPanacheResourceProcessor.java | 25 ++++++++++- .../DefaultPanacheEntityToStringTest.java | 25 ----------- .../kotlin/deployment/test/NoConfigTest.java | 20 --------- .../DefaultPersistenceUnitConfigTest.java | 36 --------------- .../DefaultPersistenceUnitFileTest.java | 41 ----------------- .../ErroneousPersistenceUnitConfigTest.java | 26 ----------- .../MultiplePersistenceUnitConfigTest.java | 36 --------------- .../test/multiple_pu/PanacheTestResource.java | 37 --------------- .../test/multiple_pu/first/FirstEntity.java | 11 ----- .../test/multiple_pu/second/SecondEntity.java | 11 ----- .../test/DefaultPanacheEntityToStringTest.kt | 21 +++++++++ .../deployment/test/DuplicateIdEntity.kt | 9 ++++ .../deployment/test/DuplicateIdEntityTest.kt | 27 +++++++++++ .../kotlin/deployment/test/NoConfigTest.kt | 20 +++++++++ .../DefaultPersistenceUnitConfigTest.kt | 38 ++++++++++++++++ .../DefaultPersistenceUnitFileTest.kt | 45 +++++++++++++++++++ .../ErroneousPersistenceUnitConfigTest.kt | 29 ++++++++++++ .../MultiplePersistenceUnitConfigTest.kt | 38 ++++++++++++++++ .../test/multiple_pu/PanacheTestResource.kt | 35 +++++++++++++++ .../test/multiple_pu/first/FirstEntity.kt | 9 ++++ .../test/multiple_pu/second/SecondEntity.kt | 9 ++++ 22 files changed, 339 insertions(+), 244 deletions(-) delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.java delete mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.java create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntity.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntityTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.kt create mode 100644 extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.kt diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/pom.xml b/extensions/panache/hibernate-orm-panache-kotlin/deployment/pom.xml index 07654f326f616d..67133b7cac96d0 100644 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/pom.xml +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/pom.xml @@ -86,6 +86,41 @@ + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + src/main/java + + + + + test-compile + test-compile + + test-compile + + + + src/test/java + src/test/kotlin + + + + + + 1.8 + + diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/main/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/KotlinPanacheResourceProcessor.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/main/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/KotlinPanacheResourceProcessor.java index 0a4c4a09ed0646..de7289f94bbcea 100644 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/main/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/KotlinPanacheResourceProcessor.java +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/main/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/KotlinPanacheResourceProcessor.java @@ -16,14 +16,18 @@ import java.util.stream.Collectors; import javax.persistence.EntityManager; +import javax.persistence.Id; import javax.persistence.Transient; +import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.Type; import io.quarkus.arc.deployment.UnremovableBeanBuildItem; +import io.quarkus.arc.deployment.ValidationPhaseBuildItem; import io.quarkus.arc.processor.BeanInfo; +import io.quarkus.builder.BuildException; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -34,6 +38,7 @@ import io.quarkus.deployment.builditem.CombinedIndexBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import io.quarkus.deployment.util.JandexUtil; import io.quarkus.hibernate.orm.deployment.AdditionalJpaModelBuildItem; import io.quarkus.hibernate.orm.deployment.HibernateEnhancersRegisteredBuildItem; import io.quarkus.hibernate.orm.deployment.JpaModelPersistenceUnitMappingBuildItem; @@ -47,7 +52,8 @@ import io.quarkus.panache.common.deployment.TypeBundle; public final class KotlinPanacheResourceProcessor { - + private static final DotName DOTNAME_ID = DotName.createSimple(Id.class.getName()); + private static final DotName DOTNAME_PANACHE_ENTITY = DotName.createSimple(PanacheEntity.class.getName()); private static final Set UNREMOVABLE_BEANS = singleton(createSimple(EntityManager.class.getName())); static final DotName TRANSIENT = DotName.createSimple(Transient.class.getName()); @@ -206,4 +212,21 @@ List produceModel() { // only transforms classes from the application jar, so we do our own transforming return Collections.singletonList(new AdditionalJpaModelBuildItem(PanacheEntity.class)); } + + @BuildStep + ValidationPhaseBuildItem.ValidationErrorBuildItem validate(ValidationPhaseBuildItem validationPhase, + CombinedIndexBuildItem index) throws BuildException { + // we verify that no ID fields are defined (via @Id) when extending PanacheEntity + for (AnnotationInstance annotationInstance : index.getIndex().getAnnotations(DOTNAME_ID)) { + ClassInfo info = JandexUtil.getEnclosingClass(annotationInstance); + if (JandexUtil.isSubclassOf(index.getIndex(), info, DOTNAME_PANACHE_ENTITY)) { + BuildException be = new BuildException("You provide a JPA identifier via @Id inside '" + info.name() + + "' but one is already provided by PanacheEntity, " + + "your class should extend PanacheEntityBase instead, or use the id provided by PanacheEntity", + Collections.emptyList()); + return new ValidationPhaseBuildItem.ValidationErrorBuildItem(be); + } + } + return null; + } } diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.java deleted file mode 100644 index 20cda2c91c4da5..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity; - -public class DefaultPanacheEntityToStringTest { - - @Test - public void testDefaultToStringMethod() { - MyPanacheEntity myPanacheEntityWithId = new MyPanacheEntity(2912l); - assertEquals("MyPanacheEntity<2912>", myPanacheEntityWithId.toString()); - - MyPanacheEntity myPanacheEntityWithNullId = new MyPanacheEntity(null); - assertEquals("MyPanacheEntity", myPanacheEntityWithNullId.toString()); - } - - static class MyPanacheEntity extends PanacheEntity { - public MyPanacheEntity(Long id) { - this.setId(id); - } - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.java deleted file mode 100644 index b0d88c9d4a0571..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test; - -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.test.QuarkusUnitTest; - -public class NoConfigTest { - - @RegisterExtension - static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer( - () -> ShrinkWrap.create(JavaArchive.class)); - - @Test - public void testNoConfig() { - // we should be able to start the application, even with no configuration at all - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.java deleted file mode 100644 index 27e4631e9ce6c9..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu; - -import org.hamcrest.Matchers; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity; -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity; -import io.quarkus.test.QuarkusUnitTest; -import io.restassured.RestAssured; - -public class DefaultPersistenceUnitConfigTest { - - @RegisterExtension - static QuarkusUnitTest runner = new QuarkusUnitTest() - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) - .addClasses(FirstEntity.class, SecondEntity.class, PanacheTestResource.class) - .addAsResource("application.properties")); - - @Test - public void panacheOperations() { - /** - * First entity operations - */ - RestAssured.when().get("/persistence-unit/first/name-1").then().body(Matchers.is("name-1")); - RestAssured.when().get("/persistence-unit/first/name-2").then().body(Matchers.is("name-2")); - - /** - * second entity operations - */ - RestAssured.when().get("/persistence-unit/second/name-1").then().body(Matchers.is("name-1")); - RestAssured.when().get("/persistence-unit/second/name-2").then().body(Matchers.is("name-2")); - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.java deleted file mode 100644 index 644544bc297834..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu; - -import org.hamcrest.Matchers; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.asset.StringAsset; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity; -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity; -import io.quarkus.test.QuarkusUnitTest; -import io.restassured.RestAssured; - -public class DefaultPersistenceUnitFileTest { - - @RegisterExtension - static QuarkusUnitTest runner = new QuarkusUnitTest() - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) - .addClasses(FirstEntity.class, SecondEntity.class, PanacheTestResource.class) - .addAsManifestResource("META-INF/some-persistence.xml", "persistence.xml") - .addAsResource(new StringAsset( - "quarkus.datasource.db-kind=h2\n" + - "quarkus.datasource.jdbc.url=jdbc:h2:mem:default;DB_CLOSE_DELAY=-1"), - "application.properties")); - - @Test - public void panacheOperations() { - /** - * First entity operations - */ - RestAssured.when().get("/persistence-unit/first/name-1").then().body(Matchers.is("name-1")); - RestAssured.when().get("/persistence-unit/first/name-2").then().body(Matchers.is("name-2")); - - /** - * second entity operations - */ - RestAssured.when().get("/persistence-unit/second/name-1").then().body(Matchers.is("name-1")); - RestAssured.when().get("/persistence-unit/second/name-2").then().body(Matchers.is("name-2")); - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.java deleted file mode 100644 index b627ce9e1ab85c..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu; - -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity; -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity; -import io.quarkus.test.QuarkusUnitTest; - -public class ErroneousPersistenceUnitConfigTest { - - @RegisterExtension - static QuarkusUnitTest runner = new QuarkusUnitTest() - .setExpectedException(IllegalStateException.class) - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) - .addClasses(FirstEntity.class, SecondEntity.class, PanacheTestResource.class) - .addAsResource("application-erroneous-multiple-persistence-units.properties", "application.properties")); - - @Test - public void shouldNotReachHere() { - Assertions.fail(); - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.java deleted file mode 100644 index fe0112835e775d..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu; - -import org.hamcrest.Matchers; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity; -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity; -import io.quarkus.test.QuarkusUnitTest; -import io.restassured.RestAssured; - -public class MultiplePersistenceUnitConfigTest { - - @RegisterExtension - static QuarkusUnitTest runner = new QuarkusUnitTest() - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) - .addClasses(FirstEntity.class, SecondEntity.class, PanacheTestResource.class) - .addAsResource("application-multiple-persistence-units.properties", "application.properties")); - - @Test - public void panacheOperations() { - /** - * First entity operations - */ - RestAssured.when().get("/persistence-unit/first/name-1").then().body(Matchers.is("name-1")); - RestAssured.when().get("/persistence-unit/first/name-2").then().body(Matchers.is("name-2")); - - /** - * second entity operations - */ - RestAssured.when().get("/persistence-unit/second/name-1").then().body(Matchers.is("name-1")); - RestAssured.when().get("/persistence-unit/second/name-2").then().body(Matchers.is("name-2")); - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.java deleted file mode 100644 index ece78ca2826f66..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu; - -import javax.transaction.Transactional; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity; -import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity; - -@Path("/persistence-unit") -public class PanacheTestResource { - - @GET - @Path("/first/{name}") - @Produces(MediaType.TEXT_PLAIN) - @Transactional - public String createWithFirstPuAndReturnCount(@PathParam("name") String name) { - FirstEntity entity = new FirstEntity(); - entity.name = name; - entity.persistAndFlush(); - return name; - } - - @GET - @Path("/second/{name}") - @Produces(MediaType.TEXT_PLAIN) - @Transactional - public String createWithSecondPUAndReturnCount(@PathParam("name") String name) { - SecondEntity entity = new SecondEntity(); - entity.name = name; - entity.persistAndFlush(); - return name; - } -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.java deleted file mode 100644 index 013e6a87e2f3e3..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first; - -import javax.persistence.Entity; - -import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity; - -@Entity -public class FirstEntity extends PanacheEntity { - - public String name; -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.java b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.java deleted file mode 100644 index ef664e9153ce3e..00000000000000 --- a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/java/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second; - -import javax.persistence.Entity; - -import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity; - -@Entity -public class SecondEntity extends PanacheEntity { - - public String name; -} diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.kt new file mode 100644 index 00000000000000..05f2a5a63abf89 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DefaultPanacheEntityToStringTest.kt @@ -0,0 +1,21 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test + +import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class DefaultPanacheEntityToStringTest { + @Test + fun testDefaultToStringMethod() { + val myPanacheEntityWithId = MyPanacheEntity(2912L) + Assertions.assertEquals("MyPanacheEntity<2912>", myPanacheEntityWithId.toString()) + val myPanacheEntityWithNullId = MyPanacheEntity(null) + Assertions.assertEquals("MyPanacheEntity", myPanacheEntityWithNullId.toString()) + } + + internal class MyPanacheEntity(id: Long?) : PanacheEntity() { + init { + this.id = id + } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntity.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntity.kt new file mode 100644 index 00000000000000..95de85ad86e6f5 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntity.kt @@ -0,0 +1,9 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test + +import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity +import javax.persistence.Id + +class DuplicateIdEntity : PanacheEntity() { + @Id + var customId: String? = null +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntityTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntityTest.kt new file mode 100644 index 00000000000000..126c527efd55e7 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/DuplicateIdEntityTest.kt @@ -0,0 +1,27 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test + +import io.quarkus.builder.BuildException +import io.quarkus.test.QuarkusUnitTest +import org.jboss.shrinkwrap.api.spec.JavaArchive +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +class DuplicateIdEntityTest { + @Test + fun shouldThrow() { + Assertions.fail("A BuildException should have been thrown due to duplicate entity ID") + } + + companion object { + @RegisterExtension + @JvmField + var runner = QuarkusUnitTest() + .setExpectedException(BuildException::class.java) + .setArchiveProducer { + ShrinkWrap.create(JavaArchive::class.java) + .addClasses(DuplicateIdEntity::class.java) + } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.kt new file mode 100644 index 00000000000000..a0a7ee0180d10e --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/NoConfigTest.kt @@ -0,0 +1,20 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test + +import io.quarkus.test.QuarkusUnitTest +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.JavaArchive +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +class NoConfigTest { + @Test + fun testNoConfig() { + // we should be able to start the application, even with no configuration at all + } + + companion object { + @RegisterExtension + @JvmField + val config = QuarkusUnitTest().setArchiveProducer { ShrinkWrap.create(JavaArchive::class.java) } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.kt new file mode 100644 index 00000000000000..ed1df30f9b7b2f --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitConfigTest.kt @@ -0,0 +1,38 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu + +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity +import io.quarkus.test.QuarkusUnitTest +import io.restassured.RestAssured +import org.hamcrest.Matchers +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.JavaArchive +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +class DefaultPersistenceUnitConfigTest { + @Test + fun panacheOperations() { + /** + * First entity operations + */ + RestAssured.`when`()["/persistence-unit/first/name-1"].then().body(Matchers.`is`("name-1")) + RestAssured.`when`()["/persistence-unit/first/name-2"].then().body(Matchers.`is`("name-2")) + /** + * second entity operations + */ + RestAssured.`when`()["/persistence-unit/second/name-1"].then().body(Matchers.`is`("name-1")) + RestAssured.`when`()["/persistence-unit/second/name-2"].then().body(Matchers.`is`("name-2")) + } + + companion object { + @RegisterExtension + @JvmField + var runner = QuarkusUnitTest() + .setArchiveProducer { + ShrinkWrap.create(JavaArchive::class.java) + .addClasses(FirstEntity::class.java, SecondEntity::class.java, PanacheTestResource::class.java) + .addAsResource("application.properties") + } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.kt new file mode 100644 index 00000000000000..709925e92ec586 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/DefaultPersistenceUnitFileTest.kt @@ -0,0 +1,45 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu + +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity +import io.quarkus.test.QuarkusUnitTest +import io.restassured.RestAssured +import org.hamcrest.Matchers +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.asset.StringAsset +import org.jboss.shrinkwrap.api.spec.JavaArchive +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +class DefaultPersistenceUnitFileTest { + @Test + fun panacheOperations() { + /** + * First entity operations + */ + RestAssured.`when`()["/persistence-unit/first/name-1"].then().body(Matchers.`is`("name-1")) + RestAssured.`when`()["/persistence-unit/first/name-2"].then().body(Matchers.`is`("name-2")) + /** + * second entity operations + */ + RestAssured.`when`()["/persistence-unit/second/name-1"].then().body(Matchers.`is`("name-1")) + RestAssured.`when`()["/persistence-unit/second/name-2"].then().body(Matchers.`is`("name-2")) + } + + companion object { + @RegisterExtension + @JvmField + var runner = QuarkusUnitTest() + .setArchiveProducer { + ShrinkWrap.create(JavaArchive::class.java) + .addClasses(FirstEntity::class.java, SecondEntity::class.java, PanacheTestResource::class.java) + .addAsManifestResource("META-INF/some-persistence.xml", "persistence.xml") + .addAsResource(StringAsset( + """ + quarkus.datasource.db-kind=h2 + quarkus.datasource.jdbc.url=jdbc:h2:mem:default;DB_CLOSE_DELAY=-1 + """.trimIndent()), + "application.properties") + } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.kt new file mode 100644 index 00000000000000..b6eafd19511db1 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/ErroneousPersistenceUnitConfigTest.kt @@ -0,0 +1,29 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu + +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity +import io.quarkus.test.QuarkusUnitTest +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.JavaArchive +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +class ErroneousPersistenceUnitConfigTest { + @Test + fun shouldNotReachHere() { + Assertions.fail() + } + + companion object { + @RegisterExtension + @JvmField + var runner = QuarkusUnitTest() + .setExpectedException(IllegalStateException::class.java) + .setArchiveProducer { + ShrinkWrap.create(JavaArchive::class.java) + .addClasses(FirstEntity::class.java, SecondEntity::class.java, PanacheTestResource::class.java) + .addAsResource("application-erroneous-multiple-persistence-units.properties", "application.properties") + } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.kt new file mode 100644 index 00000000000000..de835e2076c3c1 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/MultiplePersistenceUnitConfigTest.kt @@ -0,0 +1,38 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu + +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity +import io.quarkus.test.QuarkusUnitTest +import io.restassured.RestAssured +import org.hamcrest.Matchers +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.JavaArchive +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +class MultiplePersistenceUnitConfigTest { + @Test + fun panacheOperations() { + /** + * First entity operations + */ + RestAssured.`when`()["/persistence-unit/first/name-1"].then().body(Matchers.`is`("name-1")) + RestAssured.`when`()["/persistence-unit/first/name-2"].then().body(Matchers.`is`("name-2")) + /** + * second entity operations + */ + RestAssured.`when`()["/persistence-unit/second/name-1"].then().body(Matchers.`is`("name-1")) + RestAssured.`when`()["/persistence-unit/second/name-2"].then().body(Matchers.`is`("name-2")) + } + + companion object { + @RegisterExtension + @JvmField + var runner = QuarkusUnitTest() + .setArchiveProducer { + ShrinkWrap.create(JavaArchive::class.java) + .addClasses(FirstEntity::class.java, SecondEntity::class.java, PanacheTestResource::class.java) + .addAsResource("application-multiple-persistence-units.properties", "application.properties") + } + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.kt new file mode 100644 index 00000000000000..8d871527c14818 --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/PanacheTestResource.kt @@ -0,0 +1,35 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu + +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first.FirstEntity +import io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second.SecondEntity +import javax.transaction.Transactional +import javax.ws.rs.GET +import javax.ws.rs.Path +import javax.ws.rs.PathParam +import javax.ws.rs.Produces +import javax.ws.rs.core.MediaType + +@Path("/persistence-unit") +class PanacheTestResource { + @GET + @Path("/first/{name}") + @Produces(MediaType.TEXT_PLAIN) + @Transactional + fun createWithFirstPuAndReturnCount(@PathParam("name") name: String?): String? { + val entity = FirstEntity() + entity.name = name + entity.persistAndFlush() + return name + } + + @GET + @Path("/second/{name}") + @Produces(MediaType.TEXT_PLAIN) + @Transactional + fun createWithSecondPUAndReturnCount(@PathParam("name") name: String?): String? { + val entity = SecondEntity() + entity.name = name + entity.persistAndFlush() + return name + } +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.kt new file mode 100644 index 00000000000000..0f6d2488bf018c --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/first/FirstEntity.kt @@ -0,0 +1,9 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.first + +import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity +import javax.persistence.Entity + +@Entity +class FirstEntity : PanacheEntity() { + var name: String? = null +} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.kt b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.kt new file mode 100644 index 00000000000000..94cbff744517ef --- /dev/null +++ b/extensions/panache/hibernate-orm-panache-kotlin/deployment/src/test/kotlin/io/quarkus/hibernate/orm/panache/kotlin/deployment/test/multiple_pu/second/SecondEntity.kt @@ -0,0 +1,9 @@ +package io.quarkus.hibernate.orm.panache.kotlin.deployment.test.multiple_pu.second + +import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity +import javax.persistence.Entity + +@Entity +class SecondEntity : PanacheEntity() { + var name: String? = null +} \ No newline at end of file