forked from hibernate/hibernate-reactive
-
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.
[hibernate#2055] Add test for Setting hibernate.query.mutation_strate…
…gy.global_temporary.create_tables to false has no effect
- Loading branch information
Showing
1 changed file
with
125 additions
and
0 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
...e-reactive-core/src/test/java/org/hibernate/reactive/schema/GlobalTemporaryTableTest.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,125 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.schema; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.dialect.temptable.TemporaryTable; | ||
import org.hibernate.reactive.BaseReactiveTest; | ||
import org.hibernate.reactive.testing.SqlStatementTracker; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.SecondaryTable; | ||
import jakarta.persistence.Table; | ||
|
||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableStrategy.CREATE_ID_TABLES; | ||
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture; | ||
|
||
@Timeout(value = 10, timeUnit = MINUTES) | ||
public class GlobalTemporaryTableTest extends BaseReactiveTest { | ||
|
||
private static SqlStatementTracker sqlTracker; | ||
|
||
@Override | ||
protected Collection<Class<?>> annotatedEntities() { | ||
return List.of( Bar.class ); | ||
} | ||
|
||
|
||
@Override | ||
public CompletionStage<Void> deleteEntities(Class<?>... entities) { | ||
// Skip delete step. | ||
// We don't insert any value, so there's nothing to delete | ||
// Avoid having extra stuff in the log that's not relevant to the test | ||
return voidFuture(); | ||
} | ||
|
||
@Override | ||
protected void setProperties(Configuration configuration) { | ||
super.setProperties( configuration ); | ||
configuration.setProperty( CREATE_ID_TABLES, "false" ); | ||
} | ||
|
||
@Override | ||
protected Configuration constructConfiguration() { | ||
Configuration configuration = super.constructConfiguration(); | ||
sqlTracker = new SqlStatementTracker( GlobalTemporaryTableTest::filter, configuration.getProperties() ); | ||
return configuration; | ||
} | ||
|
||
@AfterEach | ||
public void clearLogger() { | ||
sqlTracker.clear(); | ||
} | ||
|
||
@Override | ||
protected void addServices(StandardServiceRegistryBuilder builder) { | ||
sqlTracker.registerService( builder ); | ||
} | ||
|
||
|
||
@Test | ||
public void testGlobalTemporaryTableHaveNotBeenCreated(VertxTestContext context) { | ||
test( context, getSessionFactory() | ||
.withTransaction( session -> { | ||
Bar bar = new Bar(); | ||
bar.id = 1; | ||
bar.name = "Noble"; | ||
bar.name2 = "Experiment"; | ||
|
||
return session.persist( bar ); | ||
} ) | ||
.thenAccept( v -> { | ||
List<String> loggedQueries = sqlTracker.getLoggedQueries(); | ||
assertThat( loggedQueries ).isNotEmpty(); | ||
loggedQueries.forEach( q -> { | ||
assertThat( q ).doesNotContain( TemporaryTable.ID_TABLE_PREFIX ).as(""); | ||
assertThat( q ).doesNotContain( TemporaryTable.ENTITY_TABLE_PREFIX ); | ||
}); | ||
|
||
} ) | ||
); | ||
} | ||
|
||
private static boolean filter(String s) { | ||
String[] accepted = { "create " }; | ||
for ( String valid : accepted ) { | ||
if ( s.toLowerCase().startsWith( valid ) ) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Entity(name = "BAR") | ||
@Table(name = "BAR") | ||
@SecondaryTable(name = "BAR2") | ||
public static class Bar { | ||
@Id | ||
@Column(name = "ID") | ||
public Integer id; | ||
|
||
@Column(name = "name") | ||
public String name; | ||
|
||
@Column(name = "name2", table = "BAR2") | ||
public String name2; | ||
} | ||
|
||
} |