|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.reactive.it; |
| 6 | + |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.CompletionStage; |
| 10 | + |
| 11 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 12 | +import org.hibernate.cfg.Configuration; |
| 13 | +import org.hibernate.reactive.it.dirtychecking.Fruit; |
| 14 | + |
| 15 | +import org.hibernate.testing.SqlStatementTracker; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import io.vertx.junit5.VertxTestContext; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture; |
| 23 | + |
| 24 | +public class DirtyCheckingIT extends BaseReactiveIT { |
| 25 | + |
| 26 | + private static SqlStatementTracker sqlTracker; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected Configuration constructConfiguration() { |
| 30 | + Configuration configuration = super.constructConfiguration(); |
| 31 | + |
| 32 | + // Construct a tracker that collects query statements via the SqlStatementLogger framework. |
| 33 | + // Pass in configuration properties to hand off any actual logging properties |
| 34 | + sqlTracker = new SqlStatementTracker( DirtyCheckingIT::updateQueryFilter, configuration.getProperties() ); |
| 35 | + return configuration; |
| 36 | + } |
| 37 | + |
| 38 | + private static boolean updateQueryFilter(String s) { |
| 39 | + return s.toLowerCase().startsWith( "update " ); |
| 40 | + } |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + public void clearTracker() { |
| 44 | + sqlTracker.clear(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void addServices(StandardServiceRegistryBuilder builder) { |
| 49 | + sqlTracker.registerService( builder ); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Collection<Class<?>> annotatedEntities() { |
| 54 | + return List.of( Fruit.class ); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected CompletionStage<Void> cleanDb() { |
| 59 | + // There's only one test, so we don't need to clean the db. |
| 60 | + // This prevents extra queries in the log when the test is over. |
| 61 | + return voidFuture(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testDirtyCheck(VertxTestContext context) { |
| 66 | + test( |
| 67 | + context, |
| 68 | + getMutinySessionFactory() |
| 69 | + .withTransaction( s -> s.persist( new Fruit().setId( 5 ).setName( "Apple" ) ) ) |
| 70 | + .chain( () -> getMutinySessionFactory().withTransaction( s -> s.find( Fruit.class, 5 ) ) ) |
| 71 | + .invoke( fruit -> { |
| 72 | + assertThat( fruit ).hasFieldOrPropertyWithValue( "name", "Apple" ); |
| 73 | + assertThat( sqlTracker.getLoggedQueries() ) |
| 74 | + .as( "Dirty field detection failed, unexpected SQL mutation query" ) |
| 75 | + .isEmpty(); |
| 76 | + } ) |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
0 commit comments