Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed DbClient H2 tests. #7639

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public abstract class DbClientBuilderBase<T extends DbClientBuilderBase<T>>
implements DbClientBuilder<T> {

private final DbMapperManager.Builder dbMapperBuilder = DbMapperManager.builder();
private final MapperManager.Builder mapperBuilder = MapperManager.builder();

private String url;
private String username;
private String password;
Expand All @@ -54,6 +56,9 @@ public DbClient build() {
if (dbMapperManager == null) {
dbMapperManager = dbMapperBuilder.build();
}
if (mapperManager == null) {
mapperManager = mapperBuilder.build();
}
return doBuild();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ protected PreparedStatement prepareStatement(DbClientServiceContext serviceConte
* @return statement
*/
protected PreparedStatement prepareStatement(String stmtName, String stmt) {
return prepareStatement(connectionPool.connection(), stmtName, stmt);
Connection connection = connectionPool.connection();
try {
connection.setAutoCommit(true);
} catch (SQLException e) {
throw new DbClientException("Failed to set autocommit to true", e);
}
return prepareStatement(connection, stmtName, stmt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package io.helidon.dbclient.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import io.helidon.dbclient.DbClientException;
import io.helidon.dbclient.DbStatement;

/**
Expand Down Expand Up @@ -44,6 +47,14 @@ protected JdbcTransactionStatement(JdbcConnectionPool connectionPool,

@Override
protected PreparedStatement prepareStatement(String stmtName, String stmt) {
return prepareStatement(transactionContext.connection(), stmtName, stmt);
Connection connection = transactionContext.connection();
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
throw new DbClientException("Failed to set autocommit to false", e);
}
return prepareStatement(connection, stmtName, stmt);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$
io.helidon.level=INFO
io.helidon.config.level=INFO
io.helidon.webserver.http.level=WARNING
io.helidon.tests.integration.dbclient.app.level=ALL
io.helidon.tests.integration.dbclient.app.level=INFO
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
# limitations under the License.
#

# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties
# Send messages to the console

handlers=io.helidon.logging.jul.HelidonConsoleHandler
# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
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
Expand All @@ -26,4 +22,4 @@ java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$
io.helidon.level=INFO
io.helidon.config.level=INFO
io.helidon.webserver.http.level=WARNING
io.helidon.tests.integration.dbclient.app.level=ALL
io.helidon.tests.integration.dbclient.app.level=INFO
1 change: 1 addition & 0 deletions tests/integration/dbclient/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@
<artifactId>micrometer-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,19 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.tests.integration.dbclient.common.tests;
package io.helidon.tests.integration.dbclient.common.spi;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.tests.integration.dbclient.common.utils.TestConfig;
import io.helidon.dbclient.DbClient;

/**
* Common testing code.
*/
public abstract class AbstractIT {
public interface SetupProvider {

/**
* Last used id in {@code Pokemon} table.
* Provide root {@Config} instance for the tests.
*/
public static final int LAST_POKEMON_ID = 5;
Config config();

/**
* Provide {@link DbClient} instance for the tests.
*
* @return the {@link DbClient} instance
*/
DbClient dbClient();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.tests.integration.dbclient.common.tests;

import java.util.List;
import java.util.ServiceLoader;

import io.helidon.common.HelidonServiceLoader;
import io.helidon.config.Config;
import io.helidon.dbclient.DbClient;
import io.helidon.tests.integration.dbclient.common.spi.SetupProvider;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

public class DbClientParameterResolver implements ParameterResolver {

private static final SetupProvider SETUP_PROVIDER = initSetupProvider();

private static SetupProvider initSetupProvider() {
ServiceLoader<SetupProvider> loader = ServiceLoader.load(SetupProvider.class);
List<SetupProvider> providers = HelidonServiceLoader
.builder(loader)
.build()
.asList();
switch (providers.size()) {
case 0: throw new IllegalStateException("No SetupProvider instance found on the classpath");
case 1: return providers.getFirst();
default: throw new IllegalStateException("Multiple SetupProvider instances found on the classpath");
}
}

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Class<?> type = parameterContext.getParameter().getType();
if (DbClient.class.isAssignableFrom(type)) {
return true;
}
if (Config.class.isAssignableFrom(type)) {
return true;
}
return false;
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Class<?> type = parameterContext.getParameter().getType();
if (DbClient.class.isAssignableFrom(type)) {
return SETUP_PROVIDER.dbClient();
}
if (Config.class.isAssignableFrom(type)) {
return SETUP_PROVIDER.config();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import io.helidon.dbclient.DbClientException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

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

/**
* Test exceptional states.
*/
class ExceptionalStmtIT extends AbstractIT {
@ExtendWith(DbClientParameterResolver.class)
class ExceptionalStmtIT {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.helidon.tests.integration.dbclient.common.model.Type;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

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

private static final System.Logger LOGGER = System.getLogger(FlowControlIT.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import io.helidon.tests.integration.dbclient.common.utils.RangePoJo;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static io.helidon.tests.integration.dbclient.common.utils.VerifyData.verifyPokemonsIdRange;

/**
* Test DbStatementGet methods.
*/
@ExtendWith(DbClientParameterResolver.class)
public class GetStatementIT {

private final DbClient dbClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

import io.helidon.config.Config;
import io.helidon.dbclient.DbClient;
import io.helidon.dbclient.health.DbClientHealthCheck;
import io.helidon.health.HealthCheck;
import io.helidon.health.HealthCheckResponse;
import io.helidon.dbclient.health.DbClientHealthCheck;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand All @@ -33,6 +34,7 @@
/**
* Verify that health check works.
*/
@ExtendWith(DbClientParameterResolver.class)
public class HealthCheckIT {

private static final System.Logger LOGGER = System.getLogger(HealthCheckIT.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.helidon.dbclient.DbClientServiceContext;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static io.helidon.tests.integration.dbclient.common.model.Pokemon.POKEMONS;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -29,6 +30,7 @@
/**
* Verify services handling.
*/
@ExtendWith(DbClientParameterResolver.class)
public class InterceptorIT {

private final Config config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import io.helidon.dbclient.DbExecute;
import io.helidon.dbclient.DbRow;
import io.helidon.tests.integration.dbclient.common.model.Pokemon;
import io.helidon.tests.integration.harness.SetUp;
import io.helidon.tests.integration.dbclient.common.utils.TestConfig;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static io.helidon.tests.integration.dbclient.common.model.Pokemon.POKEMONS;
import static io.helidon.tests.integration.dbclient.common.model.Type.TYPES;
Expand All @@ -40,10 +42,11 @@
* Verify mapping interface.
*/
@SuppressWarnings("SpellCheckingInspection")
public class MapperIT extends AbstractIT {
@ExtendWith(DbClientParameterResolver.class)
public class MapperIT {

private static final System.Logger LOGGER = System.getLogger(MapperIT.class.getName());
private static final int BASE_ID = LAST_POKEMON_ID + 400;
private static final int BASE_ID = TestConfig.LAST_POKEMON_ID + 400;

private final DbClient dbClient;

Expand All @@ -58,7 +61,7 @@ private static void addPokemon(DbClient dbClient, Pokemon pokemon) {
verifyInsertPokemon(dbClient, result, pokemon);
}

@SetUp
@BeforeAll
public static void setup(DbClient dbClient) throws ExecutionException, InterruptedException {
try {
// BASE_ID+1, 2 is used for inserts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import io.helidon.tests.integration.dbclient.common.utils.RangePoJo;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static io.helidon.tests.integration.dbclient.common.utils.VerifyData.verifyPokemonsIdRange;

/**
* Test DbStatementQuery methods.
*/
public class QueryStatementIT extends AbstractIT {
@ExtendWith(DbClientParameterResolver.class)
public class QueryStatementIT {

private final DbClient dbClient;

Expand Down
Loading