|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive.schema; |
| 7 | + |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.CompletionStage; |
| 11 | + |
| 12 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | +import org.hibernate.dialect.temptable.TemporaryTable; |
| 15 | +import org.hibernate.reactive.BaseReactiveTest; |
| 16 | +import org.hibernate.reactive.testing.SqlStatementTracker; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import io.vertx.junit5.Timeout; |
| 22 | +import io.vertx.junit5.VertxTestContext; |
| 23 | +import jakarta.persistence.Column; |
| 24 | +import jakarta.persistence.Entity; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.SecondaryTable; |
| 27 | +import jakarta.persistence.Table; |
| 28 | + |
| 29 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableStrategy.CREATE_ID_TABLES; |
| 32 | +import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture; |
| 33 | + |
| 34 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 35 | +public class GlobalTemporaryTableTest extends BaseReactiveTest { |
| 36 | + |
| 37 | + private static SqlStatementTracker sqlTracker; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected Collection<Class<?>> annotatedEntities() { |
| 41 | + return List.of( Bar.class ); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + @Override |
| 46 | + public CompletionStage<Void> deleteEntities(Class<?>... entities) { |
| 47 | + // Skip delete step. |
| 48 | + // We don't insert any value, so there's nothing to delete |
| 49 | + // Avoid having extra stuff in the log that's not relevant to the test |
| 50 | + return voidFuture(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void setProperties(Configuration configuration) { |
| 55 | + super.setProperties( configuration ); |
| 56 | + configuration.setProperty( CREATE_ID_TABLES, "false" ); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected Configuration constructConfiguration() { |
| 61 | + Configuration configuration = super.constructConfiguration(); |
| 62 | + sqlTracker = new SqlStatementTracker( GlobalTemporaryTableTest::filter, configuration.getProperties() ); |
| 63 | + return configuration; |
| 64 | + } |
| 65 | + |
| 66 | + @AfterEach |
| 67 | + public void clearLogger() { |
| 68 | + sqlTracker.clear(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void addServices(StandardServiceRegistryBuilder builder) { |
| 73 | + sqlTracker.registerService( builder ); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testGlobalTemporaryTableHaveNotBeenCreated(VertxTestContext context) { |
| 79 | + test( context, getSessionFactory() |
| 80 | + .withTransaction( session -> { |
| 81 | + Bar bar = new Bar(); |
| 82 | + bar.id = 1; |
| 83 | + bar.name = "Noble"; |
| 84 | + bar.name2 = "Experiment"; |
| 85 | + |
| 86 | + return session.persist( bar ); |
| 87 | + } ) |
| 88 | + .thenAccept( v -> { |
| 89 | + List<String> loggedQueries = sqlTracker.getLoggedQueries(); |
| 90 | + assertThat( loggedQueries ).isNotEmpty(); |
| 91 | + loggedQueries.forEach( q -> { |
| 92 | + assertThat( q ).doesNotContain( TemporaryTable.ID_TABLE_PREFIX ).as(""); |
| 93 | + assertThat( q ).doesNotContain( TemporaryTable.ENTITY_TABLE_PREFIX ); |
| 94 | + }); |
| 95 | + |
| 96 | + } ) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + private static boolean filter(String s) { |
| 101 | + String[] accepted = { "create " }; |
| 102 | + for ( String valid : accepted ) { |
| 103 | + if ( s.toLowerCase().startsWith( valid ) ) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + } |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + @Entity(name = "BAR") |
| 111 | + @Table(name = "BAR") |
| 112 | + @SecondaryTable(name = "BAR2") |
| 113 | + public static class Bar { |
| 114 | + @Id |
| 115 | + @Column(name = "ID") |
| 116 | + public Integer id; |
| 117 | + |
| 118 | + @Column(name = "name") |
| 119 | + public String name; |
| 120 | + |
| 121 | + @Column(name = "name2", table = "BAR2") |
| 122 | + public String name2; |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments