Skip to content
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
@@ -0,0 +1,44 @@
/*
* 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.
*
* Copyright 2019-2022 the original author or authors.
*/
package org.quickperf.spring.springboottest;

import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.quickperf.spring.springboottest.datasourceexception.SqlExceptionOfDataSource;

import static org.assertj.core.api.Assertions.assertThat;

public class SpringBoot1DataSourceExceptionJunit4Test {

@Test
public void should_fail_with_the_sql_exception_of_a_datasource_bean_refusing_the_connection() {

// GIVEN
Class<?> testClass = SqlExceptionOfDataSource.class;

// WHEN
PrintableResult printableResult = PrintableResult.testResult(testClass);

// THEN
assertThat(printableResult.failureCount()).isOne();

String testReport = printableResult.toString();
// The SQLException starts a line: it is the exception of the failure, not the cause of another one.
assertThat(testReport)
.contains(System.lineSeparator() + "java.sql.SQLException: Connection refused")
.doesNotContain("UndeclaredThrowableException")
.doesNotContain("InvocationTargetException");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*
* Copyright 2019-2022 the original author or authors.
*/
package org.quickperf.spring.springboottest.datasourceexception;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.quickperf.spring.junit4.QuickPerfSpringRunner;
import org.quickperf.spring.springboottest.FootballApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
* Verifies that a test getting a connection from a DataSource bean that refuses it fails
* with the SQLException thrown by the DataSource.
*/
@RunWith(QuickPerfSpringRunner.class)
@SpringBootTest(classes = {FootballApplication.class})
@Import(SqlExceptionOfDataSource.Config.class)
public class SqlExceptionOfDataSource {

@Autowired
@Qualifier("dataSourceRefusingConnections")
private DataSource dataSourceRefusingConnections;

@Test
public void get_and_close_a_connection() throws SQLException {
dataSourceRefusingConnections.getConnection().close();
}

static class DataSourceRefusingConnections extends AbstractDataSource {

@Override
public Connection getConnection() throws SQLException {
throw new SQLException("Connection refused");
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new SQLException("Connection refused");
}
}

static class Config {

// Declaring a DataSource bean disables the one auto-configured by Spring Boot. This working
// database replaces it: JPA gets a connection from the primary DataSource at startup.
@Bean
@Primary
DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.generateUniqueName(true)
.build();
}

// The DataSource of the test method, injected with @Qualifier because the other one is primary.
@Bean
DataSource dataSourceRefusingConnections() {
return new DataSourceRefusingConnections();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*
* Copyright 2019-2022 the original author or authors.
*/
package org.quickperf.spring.springboottest;

import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.quickperf.spring.springboottest.datasourceexception.SqlExceptionOfDataSource;

import static org.assertj.core.api.Assertions.assertThat;

public class SpringBoot2DataSourceExceptionJunit4Test {

@Test
public void should_fail_with_the_sql_exception_of_a_datasource_bean_refusing_the_connection() {

// GIVEN
Class<?> testClass = SqlExceptionOfDataSource.class;

// WHEN
PrintableResult printableResult = PrintableResult.testResult(testClass);

// THEN
assertThat(printableResult.failureCount()).isOne();

String testReport = printableResult.toString();
// The SQLException starts a line: it is the exception of the failure, not the cause of another one.
assertThat(testReport)
.contains(System.lineSeparator() + "java.sql.SQLException: Connection refused")
.doesNotContain("UndeclaredThrowableException")
.doesNotContain("InvocationTargetException");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*
* Copyright 2019-2022 the original author or authors.
*/
package org.quickperf.spring.springboottest.datasourceexception;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.quickperf.spring.junit4.QuickPerfSpringRunner;
import org.quickperf.spring.springboottest.FootballApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
* Verifies that a test getting a connection from a DataSource bean that refuses it fails
* with the SQLException thrown by the DataSource.
*/
@RunWith(QuickPerfSpringRunner.class)
@SpringBootTest(classes = {FootballApplication.class})
@Import(SqlExceptionOfDataSource.Config.class)
public class SqlExceptionOfDataSource {

@Autowired
@Qualifier("dataSourceRefusingConnections")
private DataSource dataSourceRefusingConnections;

@Test
public void get_and_close_a_connection() throws SQLException {
dataSourceRefusingConnections.getConnection().close();
}

static class DataSourceRefusingConnections extends AbstractDataSource {

@Override
public Connection getConnection() throws SQLException {
throw new SQLException("Connection refused");
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new SQLException("Connection refused");
}
}

static class Config {

// Declaring a DataSource bean disables the one auto-configured by Spring Boot. This working
// database replaces it: JPA gets a connection from the primary DataSource at startup.
@Bean
@Primary
DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.generateUniqueName(true)
.build();
}

// The DataSource of the test method, injected with @Qualifier because the other one is primary.
@Bean
DataSource dataSourceRefusingConnections() {
return new DataSourceRefusingConnections();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.ReflectionUtils;

import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

// Inspiration from https://blog.arnoldgalovics.com/configuring-a-datasource-proxy-in-spring-boot/
Expand Down Expand Up @@ -78,7 +79,13 @@ public Object invoke(final MethodInvocation invocation) throws Throwable {
Method proxyMethod = ReflectionUtils.findMethod( this.datasourceProxy.getClass()
, invocation.getMethod().getName());
if (proxyMethod != null) {
return proxyMethod.invoke(this.datasourceProxy, invocation.getArguments());
// Unwrapped so that the caller gets what the DataSource threw, such as an SQLException.
try {
return proxyMethod.invoke(this.datasourceProxy, invocation.getArguments());
}
catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
return invocation.proceed();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.
*
* Copyright 2019-2022 the original author or authors.
*/
package org.quickperf.spring;

import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.quickperf.jvm.JVM;
import org.quickperf.spring.datasourceexception.SqlExceptionOfDataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;

public class Spring3DataSourceExceptionJunit4Test {

@Test
public void should_fail_with_the_sql_exception_of_a_datasource_bean_refusing_the_connection() {

assumeTrue(JVM.INSTANCE.version.isLessThanTo16());

// GIVEN
Class<?> testClass = SqlExceptionOfDataSource.class;

// WHEN
PrintableResult printableResult = PrintableResult.testResult(testClass);

// THEN
assertThat(printableResult.failureCount()).isOne();

String testReport = printableResult.toString();
// The SQLException starts a line: it is the exception of the failure, not the cause of another one.
assertThat(testReport)
.contains(System.lineSeparator() + "java.sql.SQLException: Connection refused")
.doesNotContain("UndeclaredThrowableException")
.doesNotContain("InvocationTargetException");

}

}
Loading
Loading