forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support StatementInspector as
@PersistenceUnitExtension
managed bean
- Fixes quarkusio#27403
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
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
92 changes: 92 additions & 0 deletions
92
.../src/test/java/io/quarkus/hibernate/orm/jdbc/ApplicationScopedStatementInspectorTest.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,92 @@ | ||
package io.quarkus.hibernate.orm.jdbc; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.inject.Inject; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import javax.transaction.UserTransaction; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.resource.jdbc.spi.StatementInspector; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.PersistenceUnitExtension; | ||
import io.quarkus.runtime.StartupEvent; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ApplicationScopedStatementInspectorTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClass(MyEntity.class) | ||
.addClass(ApplicationStatementInspector.class)) | ||
.withConfigurationResource("application.properties"); | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Inject | ||
Session session; | ||
|
||
@Inject | ||
UserTransaction transaction; | ||
|
||
public void initData(@Observes StartupEvent event) throws Exception { | ||
transaction.begin(); | ||
for (int i = 0; i < 3; i++) { | ||
MyEntity entity = new MyEntity(i); | ||
session.persist(entity); | ||
} | ||
transaction.commit(); | ||
} | ||
|
||
@BeforeEach | ||
public void clearStatementInspector() { | ||
ApplicationStatementInspector.statements.clear(); | ||
} | ||
|
||
@Test | ||
public void testStatementInspectorIsLoaded() throws Exception { | ||
transaction.begin(); | ||
session.find(MyEntity.class, 0); | ||
transaction.commit(); | ||
assertThat(ApplicationStatementInspector.statements).hasSize(1); | ||
} | ||
|
||
@Entity(name = "myentity") | ||
@Table | ||
public static class MyEntity { | ||
|
||
@Id | ||
public Integer id; | ||
|
||
public MyEntity() { | ||
} | ||
|
||
public MyEntity(int id) { | ||
this.id = id; | ||
} | ||
} | ||
|
||
@PersistenceUnitExtension // @ApplicationScoped is the default | ||
public static class ApplicationStatementInspector implements StatementInspector { | ||
|
||
static List<String> statements = new ArrayList<>(); | ||
|
||
@Override | ||
public String inspect(String sql) { | ||
statements.add(sql); | ||
return sql; | ||
} | ||
} | ||
} |
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