|
| 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.configuration; |
| 7 | + |
| 8 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2; |
| 9 | + |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.CompletionStage; |
| 13 | + |
| 14 | +import org.hibernate.engine.jdbc.internal.JdbcServicesImpl; |
| 15 | +import org.hibernate.engine.jdbc.spi.JdbcServices; |
| 16 | +import org.hibernate.engine.jdbc.spi.SqlStatementLogger; |
| 17 | +import org.hibernate.reactive.pool.ReactiveConnectionPool; |
| 18 | +import org.hibernate.reactive.pool.impl.H2SqlClientPool; |
| 19 | +import org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration; |
| 20 | +import org.hibernate.reactive.pool.impl.SqlClientPoolConfiguration; |
| 21 | +import org.hibernate.reactive.provider.Settings; |
| 22 | +import org.hibernate.reactive.testing.DatabaseSelectionRule; |
| 23 | +import org.hibernate.reactive.testing.TestingRegistryRule; |
| 24 | + |
| 25 | +import org.junit.Rule; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +import io.vertx.ext.unit.Async; |
| 30 | +import io.vertx.ext.unit.TestContext; |
| 31 | +import io.vertx.ext.unit.junit.RunTestOnContext; |
| 32 | +import io.vertx.ext.unit.junit.Timeout; |
| 33 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 34 | + |
| 35 | +@RunWith(VertxUnitRunner.class) |
| 36 | +public class H2ReactiveConnectionPoolTest { |
| 37 | + |
| 38 | + @Rule |
| 39 | + public DatabaseSelectionRule dbRule = DatabaseSelectionRule.runOnlyFor( H2 ); |
| 40 | + |
| 41 | + @Rule |
| 42 | + public Timeout rule = Timeout.seconds( 3600 ); |
| 43 | + |
| 44 | + @Rule |
| 45 | + public TestingRegistryRule registryRule = new TestingRegistryRule(); |
| 46 | + |
| 47 | + @Rule |
| 48 | + public RunTestOnContext vertxContextRule = new RunTestOnContext(); |
| 49 | + |
| 50 | + protected static void test(TestContext context, CompletionStage<?> cs) { |
| 51 | + // this will be added to TestContext in the next vert.x release |
| 52 | + Async async = context.async(); |
| 53 | + cs.whenComplete( (res, err) -> { |
| 54 | + if ( err != null ) { |
| 55 | + context.fail( err ); |
| 56 | + } |
| 57 | + else { |
| 58 | + async.complete(); |
| 59 | + } |
| 60 | + } ); |
| 61 | + } |
| 62 | + |
| 63 | + private ReactiveConnectionPool configureAndStartPool(Map<String, Object> config) { |
| 64 | + DefaultSqlClientPoolConfiguration poolConfig = new DefaultSqlClientPoolConfiguration(); |
| 65 | + poolConfig.configure( config ); |
| 66 | + registryRule.addService( SqlClientPoolConfiguration.class, poolConfig ); |
| 67 | + registryRule.addService( JdbcServices.class, new JdbcServicesImpl() { |
| 68 | + @Override |
| 69 | + public SqlStatementLogger getSqlStatementLogger() { |
| 70 | + return new SqlStatementLogger(); |
| 71 | + } |
| 72 | + } ); |
| 73 | + H2SqlClientPool reactivePool = new H2SqlClientPool(); |
| 74 | + reactivePool.injectServices( registryRule.getServiceRegistry() ); |
| 75 | + reactivePool.configure( config ); |
| 76 | + reactivePool.start(); |
| 77 | + return reactivePool; |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void configureWithJdbcUrl(TestContext context) { |
| 82 | + String url = "jdbc:h2:~/test"; |
| 83 | + Map<String,Object> config = new HashMap<>(); |
| 84 | + config.put( Settings.URL, url ); |
| 85 | + ReactiveConnectionPool reactivePool = configureAndStartPool( config ); |
| 86 | + test( context, verifyConnectivity( context, reactivePool ) ); |
| 87 | + } |
| 88 | + |
| 89 | + private CompletionStage<Void> verifyConnectivity(TestContext context, ReactiveConnectionPool reactivePool) { |
| 90 | + return reactivePool.getConnection().thenCompose( |
| 91 | + connection -> connection.select( "SELECT 1" ) |
| 92 | + .thenAccept( rows -> { |
| 93 | + context.assertNotNull( rows ); |
| 94 | + context.assertEquals( 1, rows.size() ); |
| 95 | + context.assertEquals( 1, rows.next()[0] ); |
| 96 | + } ) ); |
| 97 | + } |
| 98 | +} |
0 commit comments