Skip to content

Commit 2c9b4cc

Browse files
author
Tomáš Kraus
authored
Fixed DbClient H2 tests. (helidon-io#7639)
* Fixed DbClient H2 tests. Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com> * Removed suite from H2 tests. Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com> * Fixed Connection auto-commit settings. Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com> --------- Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com>
1 parent af48313 commit 2c9b4cc

42 files changed

Lines changed: 619 additions & 358 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClientBuilderBase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public abstract class DbClientBuilderBase<T extends DbClientBuilderBase<T>>
3434
implements DbClientBuilder<T> {
3535

3636
private final DbMapperManager.Builder dbMapperBuilder = DbMapperManager.builder();
37+
private final MapperManager.Builder mapperBuilder = MapperManager.builder();
38+
3739
private String url;
3840
private String username;
3941
private String password;
@@ -54,6 +56,9 @@ public DbClient build() {
5456
if (dbMapperManager == null) {
5557
dbMapperManager = dbMapperBuilder.build();
5658
}
59+
if (mapperManager == null) {
60+
mapperManager = mapperBuilder.build();
61+
}
5762
return doBuild();
5863
}
5964

dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcStatement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ protected PreparedStatement prepareStatement(DbClientServiceContext serviceConte
108108
* @return statement
109109
*/
110110
protected PreparedStatement prepareStatement(String stmtName, String stmt) {
111-
return prepareStatement(connectionPool.connection(), stmtName, stmt);
111+
Connection connection = connectionPool.connection();
112+
try {
113+
connection.setAutoCommit(true);
114+
} catch (SQLException e) {
115+
throw new DbClientException("Failed to set autocommit to true", e);
116+
}
117+
return prepareStatement(connection, stmtName, stmt);
112118
}
113119

114120
/**

dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcTransactionStatement.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package io.helidon.dbclient.jdbc;
1717

18+
import java.sql.Connection;
1819
import java.sql.PreparedStatement;
20+
import java.sql.SQLException;
1921

22+
import io.helidon.dbclient.DbClientException;
2023
import io.helidon.dbclient.DbStatement;
2124

2225
/**
@@ -44,6 +47,14 @@ protected JdbcTransactionStatement(JdbcConnectionPool connectionPool,
4447

4548
@Override
4649
protected PreparedStatement prepareStatement(String stmtName, String stmt) {
47-
return prepareStatement(transactionContext.connection(), stmtName, stmt);
50+
Connection connection = transactionContext.connection();
51+
try {
52+
connection.setAutoCommit(false);
53+
} catch (SQLException e) {
54+
throw new DbClientException("Failed to set autocommit to false", e);
55+
}
56+
return prepareStatement(connection, stmtName, stmt);
4857
}
58+
4959
}
60+

tests/integration/dbclient/app/src/main/resources/logging.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$
2222
io.helidon.level=INFO
2323
io.helidon.config.level=INFO
2424
io.helidon.webserver.http.level=WARNING
25-
io.helidon.tests.integration.dbclient.app.level=ALL
25+
io.helidon.tests.integration.dbclient.app.level=INFO

tests/integration/dbclient/app/src/test/resources/logging.properties

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
# limitations under the License.
1515
#
1616

17-
# Example Logging Configuration File
18-
# For more information see $JAVA_HOME/jre/lib/logging.properties
19-
# Send messages to the console
20-
2117
handlers=io.helidon.logging.jul.HelidonConsoleHandler
2218
# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
2319
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n
@@ -26,4 +22,4 @@ java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$
2622
io.helidon.level=INFO
2723
io.helidon.config.level=INFO
2824
io.helidon.webserver.http.level=WARNING
29-
io.helidon.tests.integration.dbclient.app.level=ALL
25+
io.helidon.tests.integration.dbclient.app.level=INFO

tests/integration/dbclient/common/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@
8484
<artifactId>micrometer-core</artifactId>
8585
</dependency>
8686
</dependencies>
87+
8788
</project>

tests/integration/dbclient/common/src/main/java/io/helidon/tests/integration/dbclient/common/tests/AbstractIT.java renamed to tests/integration/dbclient/common/src/main/java/io/helidon/tests/integration/dbclient/common/spi/SetupProvider.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,19 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.helidon.tests.integration.dbclient.common.tests;
16+
package io.helidon.tests.integration.dbclient.common.spi;
1717

1818
import io.helidon.config.Config;
19-
import io.helidon.config.ConfigSources;
20-
import io.helidon.tests.integration.dbclient.common.utils.TestConfig;
19+
import io.helidon.dbclient.DbClient;
2120

22-
/**
23-
* Common testing code.
24-
*/
25-
public abstract class AbstractIT {
21+
public interface SetupProvider {
2622

2723
/**
28-
* Last used id in {@code Pokemon} table.
24+
* Provide root {@Config} instance for the tests.
2925
*/
30-
public static final int LAST_POKEMON_ID = 5;
26+
Config config();
27+
28+
/**
29+
* Provide {@link DbClient} instance for the tests.
30+
*
31+
* @return the {@link DbClient} instance
32+
*/
33+
DbClient dbClient();
34+
3135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.tests.integration.dbclient.common.tests;
17+
18+
import java.util.List;
19+
import java.util.ServiceLoader;
20+
21+
import io.helidon.common.HelidonServiceLoader;
22+
import io.helidon.config.Config;
23+
import io.helidon.dbclient.DbClient;
24+
import io.helidon.tests.integration.dbclient.common.spi.SetupProvider;
25+
26+
import org.junit.jupiter.api.extension.ExtensionContext;
27+
import org.junit.jupiter.api.extension.ParameterContext;
28+
import org.junit.jupiter.api.extension.ParameterResolutionException;
29+
import org.junit.jupiter.api.extension.ParameterResolver;
30+
31+
public class DbClientParameterResolver implements ParameterResolver {
32+
33+
private static final SetupProvider SETUP_PROVIDER = initSetupProvider();
34+
35+
private static SetupProvider initSetupProvider() {
36+
ServiceLoader<SetupProvider> loader = ServiceLoader.load(SetupProvider.class);
37+
List<SetupProvider> providers = HelidonServiceLoader
38+
.builder(loader)
39+
.build()
40+
.asList();
41+
switch (providers.size()) {
42+
case 0: throw new IllegalStateException("No SetupProvider instance found on the classpath");
43+
case 1: return providers.getFirst();
44+
default: throw new IllegalStateException("Multiple SetupProvider instances found on the classpath");
45+
}
46+
}
47+
48+
@Override
49+
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
50+
throws ParameterResolutionException {
51+
Class<?> type = parameterContext.getParameter().getType();
52+
if (DbClient.class.isAssignableFrom(type)) {
53+
return true;
54+
}
55+
if (Config.class.isAssignableFrom(type)) {
56+
return true;
57+
}
58+
return false;
59+
}
60+
61+
@Override
62+
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
63+
throws ParameterResolutionException {
64+
Class<?> type = parameterContext.getParameter().getType();
65+
if (DbClient.class.isAssignableFrom(type)) {
66+
return SETUP_PROVIDER.dbClient();
67+
}
68+
if (Config.class.isAssignableFrom(type)) {
69+
return SETUP_PROVIDER.config();
70+
}
71+
return null;
72+
}
73+
}

tests/integration/dbclient/common/src/main/java/io/helidon/tests/integration/dbclient/common/tests/ExceptionalStmtIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
import io.helidon.dbclient.DbClientException;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
2425

2526
import static io.helidon.tests.integration.dbclient.common.model.Pokemon.POKEMONS;
2627
import static org.junit.jupiter.api.Assertions.fail;
2728

2829
/**
2930
* Test exceptional states.
3031
*/
31-
class ExceptionalStmtIT extends AbstractIT {
32+
@ExtendWith(DbClientParameterResolver.class)
33+
class ExceptionalStmtIT {
3234

3335
private static final System.Logger LOGGER = System.getLogger(ExceptionalStmtIT.class.getName());
3436

tests/integration/dbclient/common/src/main/java/io/helidon/tests/integration/dbclient/common/tests/FlowControlIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.helidon.tests.integration.dbclient.common.model.Type;
2525

2626
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
2728

2829
import static io.helidon.tests.integration.dbclient.common.model.Type.TYPES;
2930
import static org.hamcrest.MatcherAssert.assertThat;
@@ -35,6 +36,7 @@
3536
/**
3637
* Verify proper flow control handling in query processing.
3738
*/
39+
@ExtendWith(DbClientParameterResolver.class)
3840
public class FlowControlIT {
3941

4042
private static final System.Logger LOGGER = System.getLogger(FlowControlIT.class.getName());

0 commit comments

Comments
 (0)