#2 - Polishing.

This commit is contained in:
Mark Paluch
2018-10-17 11:04:34 +02:00
parent ce0a707500
commit 12dc98bcbd
28 changed files with 254 additions and 753 deletions

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2018 the original author or authors.
*
* 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 org.springframework.data.r2dbc;
import static de.schauderhaft.degraph.check.JCheck.*;
import static org.junit.Assert.*;
import de.schauderhaft.degraph.check.JCheck;
import de.schauderhaft.degraph.configuration.NamedPattern;
import scala.runtime.AbstractFunction1;
import org.junit.Assume;
import org.junit.Test;
/**
* Test package dependencies for violations.
*
* @author Jens Schauder
*/
public class DependencyTests {
@Test // DATAJDBC-114
public void cycleFree() {
Assume.assumeThat( //
classpath() //
.noJars() //
.including("org.springframework.data.jdbc.**") //
.including("org.springframework.data.relational.**") //
.including("org.springframework.data.r2dbc.**") //
.filterClasspath("*target/classes") // exclude test code
.withSlicing("modules", "org.springframework.data.(*).**").printOnFailure("degraph.graphml"),
JCheck.violationFree());
}
@Test // DATAJDBC-220
public void acrossModules() {
assertThat( //
classpath() //
// include only Spring Data related classes (for example no JDK code)
.including("org.springframework.data.**") //
.filterClasspath(new AbstractFunction1<String, Object>() {
@Override
public Object apply(String s) { //
// only the current module + commons
return s.endsWith("target/classes") || s.contains("spring-data-commons");
}
}) // exclude test code
.withSlicing("sub-modules", // sub-modules are defined by any of the following pattern.
"org.springframework.data.jdbc.(**).*", //
"org.springframework.data.relational.(**).*", //
new NamedPattern("org.springframework.data.r2dbc.**", "repository.reactive"), //
"org.springframework.data.(**).*") //
.printTo("degraph-across-modules.graphml"), // writes a graphml to this location
JCheck.violationFree());
}
}

View File

@@ -28,7 +28,7 @@ import org.junit.Test;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.jdbc.core.JdbcTemplate;

View File

@@ -30,7 +30,7 @@ import java.util.concurrent.ArrayBlockingQueue;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.NoTransactionException;

View File

@@ -33,12 +33,12 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.function.TransactionalDatabaseClient;
import org.springframework.data.r2dbc.repository.query.Query;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.Table;

View File

@@ -27,7 +27,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.r2dbc.function.DatabaseClient;

View File

@@ -33,7 +33,7 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter;

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2018 the original author or authors.
*
* 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 org.springframework.data.r2dbc.testing;
import lombok.Builder;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import org.junit.AssumptionViolatedException;
import org.junit.rules.ExternalResource;
/**
* {@link ExternalResource} wrapper to encapsulate {@link ProvidedDatabase} and
* {@link org.testcontainers.containers.PostgreSQLContainer}.
*
* @author Mark Paluch
*/
public abstract class ExternalDatabase extends ExternalResource {
/**
* @return the post of the database service.
*/
public abstract int getPort();
/**
* @return hostname on which the database service runs.
*/
public abstract String getHostname();
/**
* @return name of the database.
*/
public abstract String getDatabase();
/**
* @return database user name.
*/
public abstract String getUsername();
@Override
protected void before() {
try (Socket socket = new Socket()) {
;
socket.connect(new InetSocketAddress(getHostname(), getPort()), Math.toIntExact(TimeUnit.SECONDS.toMillis(5)));
} catch (IOException e) {
throw new AssumptionViolatedException(
String.format("Cannot connect to %s:%d. Skiping tests.", getHostname(), getPort()));
}
}
/**
* @return password for the database user.
*/
public abstract String getPassword();
/**
* Provided (unmanaged resource) database connection coordinates.
*/
@Builder
public static class ProvidedDatabase extends ExternalDatabase {
private final int port;
private final String hostname;
private final String database;
private final String username;
private final String password;
/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
*/
@Override
public int getPort() {
return port;
}
/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
*/
@Override
public String getHostname() {
return hostname;
}
/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
*/
@Override
public String getDatabase() {
return database;
}
/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getUsername()
*/
@Override
public String getUsername() {
return username;
}
/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPassword()
*/
@Override
public String getPassword() {
return password;
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2018 the original author or authors.
*
* 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 org.springframework.data.r2dbc.testing;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import javax.sql.DataSource;
import org.junit.ClassRule;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Base class for R2DBC integration tests.
*
* @author Mark Paluch
*/
public abstract class R2dbcIntegrationTestSupport {
/**
* Local test database at {@code postgres:@localhost:5432/postgres}.
*/
@ClassRule public static final ExternalDatabase database = ProvidedDatabase.builder().hostname("localhost").port(5432)
.database("postgres").username("postgres").password("").build();
/**
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
*/
protected static ConnectionFactory createConnectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder().host(database.getHostname())
.database(database.getDatabase()).username(database.getUsername()).password(database.getPassword()).build());
}
/**
* Creates a new {@link DataSource} configured from the {@link ExternalDatabase}.
*/
protected static DataSource createDataSource() {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setUser(database.getUsername());
dataSource.setPassword(database.getPassword());
dataSource.setDatabaseName(database.getDatabase());
dataSource.setServerName(database.getHostname());
dataSource.setPortNumber(database.getPort());
return dataSource;
}
/**
* Creates a new {@link JdbcTemplate} for a {@link DataSource}.
*/
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}