-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-17824 - Extend the use of @jpa to test methods
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
- Loading branch information
Showing
7 changed files
with
266 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,4 @@ public void testBasicUsage(EntityManagerFactoryScope scope) { | |
); | ||
} | ||
|
||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
...test/java/org/hibernate/testing/annotations/methods/EntityManagerFactoryScopeTesting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.testing.annotations.methods; | ||
|
||
import java.util.Set; | ||
|
||
import org.hibernate.dialect.H2Dialect; | ||
|
||
import org.hibernate.testing.annotations.AnEntity; | ||
import org.hibernate.testing.annotations.AnotherEntity; | ||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
import org.hibernate.testing.orm.junit.Jpa; | ||
import org.hibernate.testing.orm.junit.RequiresDialect; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.metamodel.EntityType; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@RequiresDialect(H2Dialect.class) | ||
@Jpa( | ||
annotatedClasses = { | ||
AnEntity.class | ||
} | ||
) | ||
public class EntityManagerFactoryScopeTesting { | ||
|
||
@BeforeAll | ||
public void setup(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
AnEntity ae = new AnEntity(1, "AnEntity_1"); | ||
entityManager.persist( ae ); | ||
} | ||
); | ||
} | ||
|
||
@AfterAll | ||
public void tearDown(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
entityManager.createQuery( "delete from AnEntity" ).executeUpdate(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testBasicUsage(EntityManagerFactoryScope scope) { | ||
assertThat( scope, notNullValue() ); | ||
assertThat( scope.getEntityManagerFactory(), notNullValue() ); | ||
// check we can use the EMF to create EMs | ||
scope.inTransaction( | ||
(session) -> session.createQuery( "select a from AnEntity a" ).getResultList() | ||
); | ||
} | ||
|
||
@Test | ||
public void nonAnnotatedMethodTest(EntityManagerFactoryScope scope) { | ||
Set<EntityType<?>> entities = scope.getEntityManagerFactory().getMetamodel().getEntities(); | ||
assertEquals( 1, entities.size() ); | ||
assertEquals( "AnEntity", entities.iterator().next().getName() ); | ||
assertEquals( Boolean.FALSE, scope.getEntityManagerFactory().getProperties().get( "hibernate.jpa.compliance.query" ) ); | ||
scope.inEntityManager( | ||
entityManager -> { | ||
AnEntity ae = entityManager.find( AnEntity.class, 1 ); | ||
assertNotNull( ae ); | ||
assertEquals( 1, ae.getId() ); | ||
assertEquals( "AnEntity_1", ae.getName() ); | ||
} | ||
); | ||
} | ||
|
||
@Jpa( | ||
annotatedClasses = AnotherEntity.class, | ||
queryComplianceEnabled = true | ||
) | ||
@Test | ||
public void annotatedMethodTest(EntityManagerFactoryScope scope) { | ||
assertThat( scope, notNullValue() ); | ||
assertThat( scope.getEntityManagerFactory(), notNullValue() ); | ||
Set<EntityType<?>> entities = scope.getEntityManagerFactory().getMetamodel().getEntities(); | ||
assertEquals( 1, entities.size() ); | ||
assertEquals( "AnotherEntity", entities.iterator().next().getName() ); | ||
assertEquals( Boolean.TRUE, scope.getEntityManagerFactory().getProperties().get( "hibernate.jpa.compliance.query" ) ); | ||
scope.inTransaction( | ||
entityManager -> { | ||
AnotherEntity aoe = new AnotherEntity( 2, "AnotherEntity_1" ); | ||
entityManager.persist( aoe ); | ||
} | ||
); | ||
scope.inTransaction( | ||
entityManager -> { | ||
AnotherEntity aoe = entityManager.find( AnotherEntity.class, 2 ); | ||
assertNotNull( aoe ); | ||
assertEquals( 2, aoe.getId() ); | ||
assertEquals( "AnotherEntity_1", aoe.getName() ); | ||
} | ||
); | ||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> scope.inTransaction( | ||
entityManager -> { | ||
AnEntity ae = entityManager.find( AnEntity.class, 1 ); | ||
} | ||
) | ||
); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
.../java/org/hibernate/testing/annotations/methods/MoreEntityManagerFactoryScopeTesting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.testing.annotations.methods; | ||
|
||
import java.util.Set; | ||
|
||
import org.hibernate.dialect.H2Dialect; | ||
|
||
import org.hibernate.testing.annotations.AnEntity; | ||
import org.hibernate.testing.annotations.AnotherEntity; | ||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
import org.hibernate.testing.orm.junit.Jpa; | ||
import org.hibernate.testing.orm.junit.RequiresDialect; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.metamodel.EntityType; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@RequiresDialect(H2Dialect.class) | ||
public class MoreEntityManagerFactoryScopeTesting { | ||
|
||
@Jpa( | ||
annotatedClasses = { | ||
AnEntity.class | ||
} | ||
) | ||
@Test | ||
public void testBasicUsage(EntityManagerFactoryScope scope) { | ||
assertThat( scope, notNullValue() ); | ||
assertThat( scope.getEntityManagerFactory(), notNullValue() ); | ||
// check we can use the EMF to create EMs | ||
scope.inTransaction( | ||
(session) -> session.createQuery( "select a from AnEntity a" ).getResultList() | ||
); | ||
} | ||
|
||
@Jpa( | ||
annotatedClasses = AnotherEntity.class, | ||
queryComplianceEnabled = true | ||
) | ||
@Test | ||
public void annotatedMethodTest(EntityManagerFactoryScope scope) { | ||
assertThat( scope, notNullValue() ); | ||
assertThat( scope.getEntityManagerFactory(), notNullValue() ); | ||
Set<EntityType<?>> entities = scope.getEntityManagerFactory().getMetamodel().getEntities(); | ||
assertEquals( 1, entities.size() ); | ||
assertEquals( "AnotherEntity", entities.iterator().next().getName() ); | ||
assertEquals( Boolean.TRUE, scope.getEntityManagerFactory().getProperties().get( "hibernate.jpa.compliance.query" ) ); | ||
scope.inTransaction( | ||
entityManager -> { | ||
AnotherEntity aoe = new AnotherEntity( 2, "AnotherEntity_1" ); | ||
entityManager.persist( aoe ); | ||
} | ||
); | ||
scope.inTransaction( | ||
entityManager -> { | ||
AnotherEntity aoe = entityManager.find( AnotherEntity.class, 2 ); | ||
assertNotNull( aoe ); | ||
assertEquals( 2, aoe.getId() ); | ||
assertEquals( "AnotherEntity_1", aoe.getName() ); | ||
} | ||
); | ||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> scope.inTransaction( | ||
entityManager -> { | ||
AnEntity ae = entityManager.find( AnEntity.class, 1 ); | ||
} | ||
) | ||
); | ||
} | ||
|
||
} |