Reimplemented dependency tests using ArchUnit.

Moved RelationalAuditingCallback and JdbcArrayColumns to remove dependency cycle.

Closes #1058
Original pull request #1107
This commit is contained in:
Jens Schauder
2021-12-01 15:01:53 +01:00
parent 235d9bc196
commit 4042522474
15 changed files with 349 additions and 164 deletions

View File

@@ -41,7 +41,7 @@
<!-- test utilities-->
<awaitility.version>4.0.3</awaitility.version>
<degraph-check.version>0.1.4</degraph-check.version>
<archunit.version>0.22.0</archunit.version>
</properties>
<inceptionYear>2017</inceptionYear>

View File

@@ -207,14 +207,6 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.schauderhaft.degraph</groupId>
<artifactId>degraph-check</artifactId>
<version>${degraph-check.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
@@ -264,6 +256,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>${archunit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -18,7 +18,6 @@ package org.springframework.data.jdbc.core.convert;
import java.sql.Array;
import java.sql.JDBCType;
import org.springframework.data.jdbc.core.dialect.JdbcArrayColumns;
import org.springframework.data.jdbc.support.JdbcUtil;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.dialect;
package org.springframework.data.jdbc.core.convert;
import java.sql.SQLType;

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.core.dialect;
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
import org.springframework.data.relational.core.dialect.Dialect;
/**

View File

@@ -18,6 +18,7 @@ package org.springframework.data.jdbc.core.dialect;
import java.sql.JDBCType;
import java.sql.SQLType;
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
import org.springframework.data.relational.core.dialect.PostgresDialect;
/**

View File

@@ -42,7 +42,7 @@ import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
import org.springframework.data.jdbc.core.convert.RelationResolver;
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
import org.springframework.data.jdbc.core.dialect.JdbcArrayColumns;
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;

View File

@@ -24,7 +24,7 @@ import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport;
import org.springframework.data.auditing.config.AuditingConfiguration;
import org.springframework.data.relational.core.mapping.event.RelationalAuditingCallback;
import org.springframework.data.relational.auditing.RelationalAuditingCallback;
import org.springframework.data.repository.config.PersistentEntitiesFactoryBean;
import org.springframework.util.Assert;

View File

@@ -0,0 +1,169 @@
/*
* Copyright 2017-2021 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
*
* https://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.jdbc;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.data.auditing.config.AuditingHandlerBeanDefinitionParser;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.library.dependencies.SliceAssignment;
import com.tngtech.archunit.library.dependencies.SliceIdentifier;
import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition;
/**
* Test package dependencies for violations.
*
* @author Jens Schauder
*/
public class DependencyTests {
@Test
void cycleFree() {
JavaClasses importedClasses = new ClassFileImporter() //
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) //
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS) // we just analyze the code of this module.
.importPackages("org.springframework.data.jdbc")
.that( //
onlySpringData() //
);
ArchRule rule = SlicesRuleDefinition.slices() //
.matching("org.springframework.data.jdbc.(**)") //
.should() //
.beFreeOfCycles();
rule.check(importedClasses);
}
@Test
void acrossModules() {
JavaClasses importedClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages( //
"org.springframework.data.jdbc", // Spring Data Relational
"org.springframework.data.relational", // Spring Data Relational
"org.springframework.data" // Spring Data Commons
).that(onlySpringData()) //
.that(ignore(AuditingHandlerBeanDefinitionParser.class));
ArchRule rule = SlicesRuleDefinition.slices() //
.assignedFrom(subModuleSlicing()) //
.should().beFreeOfCycles();
rule.check(importedClasses);
}
@Test // GH-1058
void testGetFirstPackagePart() {
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(getFirstPackagePart("a.b.c")).isEqualTo("a");
softly.assertThat(getFirstPackagePart("a")).isEqualTo("a");
});
}
@Test // GH-1058
void testSubModule() {
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(subModule("a.b", "a.b.c.d")).isEqualTo("c");
softly.assertThat(subModule("a.b", "a.b.c")).isEqualTo("c");
softly.assertThat(subModule("a.b", "a.b")).isEqualTo("");
});
}
private DescribedPredicate<JavaClass> onlySpringData() {
return new DescribedPredicate<>("Spring Data Classes") {
@Override
public boolean apply(JavaClass input) {
return input.getPackageName().startsWith("org.springframework.data");
}
};
}
private DescribedPredicate<JavaClass> ignore(Class<?> type) {
return new DescribedPredicate<>("ignored class " + type.getName()) {
@Override
public boolean apply(JavaClass input) {
return !input.getFullName().startsWith(type.getName());
}
};
}
private String getFirstPackagePart(String subpackage) {
int index = subpackage.indexOf(".");
if (index < 0) {
return subpackage;
}
return subpackage.substring(0, index);
}
private String subModule(String basePackage, String packageName) {
if (packageName.startsWith(basePackage) && packageName.length() > basePackage.length()) {
final int index = basePackage.length() + 1;
String subpackage = packageName.substring(index);
return getFirstPackagePart(subpackage);
}
return "";
}
private SliceAssignment subModuleSlicing() {
return new SliceAssignment() {
@Override
public SliceIdentifier getIdentifierOf(JavaClass javaClass) {
String packageName = javaClass.getPackageName();
String subModule = subModule("org.springframework.data.jdbc", packageName);
if (!subModule.isEmpty()) {
return SliceIdentifier.of(subModule);
}
subModule = subModule("org.springframework.data.relational", packageName);
if (!subModule.isEmpty()) {
return SliceIdentifier.of(subModule);
}
subModule = subModule("org.springframework.data", packageName);
if (!subModule.isEmpty()) {
return SliceIdentifier.of(subModule);
}
return SliceIdentifier.ignore();
}
@Override
public String getDescription() {
return "Submodule";
}
};
}
}

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2017-2021 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
*
* https://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.jdbc.degraph;
import static de.schauderhaft.degraph.check.JCheck.*;
import static org.hamcrest.MatcherAssert.*;
import de.schauderhaft.degraph.check.JCheck;
import scala.runtime.AbstractFunction1;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* Test package dependencies for violations.
*
* @author Jens Schauder
*/
@Disabled("org.springframework.data.jdbc.core.dialect.** needs rework")
public class DependencyTests {
@Test // DATAJDBC-114
@Disabled // Replace by ArchUnit test
public void cycleFree() {
assertThat( //
classpath() //
.noJars() //
.including("org.springframework.data.jdbc.**") //
.filterClasspath("*target/classes") // exclude test code
.printOnFailure("degraph-jdbc.graphml"),
JCheck.violationFree());
}
@Test // DATAJDBC-220
@Disabled // Replace by ArchUnit test
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.(*).**", //
"org.springframework.data.(*).**") //
.printTo("degraph-across-modules.graphml"), // writes a graphml to this location
JCheck.violationFree());
}
}

View File

@@ -41,7 +41,7 @@ import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
import org.springframework.data.jdbc.core.convert.RelationResolver;
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
import org.springframework.data.jdbc.core.dialect.JdbcArrayColumns;
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;

View File

@@ -65,9 +65,9 @@
</dependency>
<dependency>
<groupId>de.schauderhaft.degraph</groupId>
<artifactId>degraph-check</artifactId>
<version>${degraph-check.version}</version>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>${archunit.version}</version>
<scope>test</scope>
</dependency>

View File

@@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.mapping.event;
package org.springframework.data.relational.auditing;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.data.relational.core.mapping.event.BeforeConvertCallback;
import org.springframework.util.Assert;
/**

View File

@@ -0,0 +1,161 @@
/*
* Copyright 2017-2021 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
*
* https://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.relational;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.sql.render.SelectRenderContext;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.library.dependencies.SliceAssignment;
import com.tngtech.archunit.library.dependencies.SliceIdentifier;
import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition;
/**
* Test package dependencies for violations.
*
* @author Jens Schauder
* @author Mark Paluch
*/
public class DependencyTests {
@Test
void cycleFree() {
JavaClasses importedClasses = new ClassFileImporter() //
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) //
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS) // we just analyze the code of this module.
.importPackages("org.springframework.data.relational") //
.that(onlySpringData()) //
.that(ignore(SelectRenderContext.class)) //
.that(ignore(RenderContextFactory.class));
ArchRule rule = SlicesRuleDefinition.slices() //
.matching("org.springframework.data.relational.(**)") //
.should() //
.beFreeOfCycles();
rule.check(importedClasses);
}
@Test
void acrossModules() {
JavaClasses importedClasses = new ClassFileImporter() //
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) //
.importPackages( //
"org.springframework.data.relational", // Spring Data Relational
"org.springframework.data" // Spring Data Commons
).that(onlySpringData());
ArchRule rule = SlicesRuleDefinition.slices() //
.assignedFrom(subModuleSlicing()) //
.should().beFreeOfCycles();
rule.check(importedClasses);
}
@Test // GH-1058
void testGetFirstPackagePart() {
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(getFirstPackagePart("a.b.c")).isEqualTo("a");
softly.assertThat(getFirstPackagePart("a")).isEqualTo("a");
});
}
@Test // GH-1058
void testSubModule() {
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(subModule("a.b", "a.b.c.d")).isEqualTo("c");
softly.assertThat(subModule("a.b", "a.b.c")).isEqualTo("c");
softly.assertThat(subModule("a.b", "a.b")).isEqualTo("");
});
}
private DescribedPredicate<JavaClass> onlySpringData() {
return new DescribedPredicate<>("Spring Data Classes") {
@Override
public boolean apply(JavaClass input) {
return input.getPackageName().startsWith("org.springframework.data");
}
};
}
private DescribedPredicate<JavaClass> ignore(Class<?> type) {
return new DescribedPredicate<>("ignored class " + type.getName()) {
@Override
public boolean apply(JavaClass input) {
return !input.getFullName().startsWith(type.getName());
}
};
}
private String getFirstPackagePart(String subpackage) {
int index = subpackage.indexOf(".");
if (index < 0) {
return subpackage;
}
return subpackage.substring(0, index);
}
private String subModule(String basePackage, String packageName) {
if (packageName.startsWith(basePackage) && packageName.length() > basePackage.length()) {
final int index = basePackage.length() + 1;
String subpackage = packageName.substring(index);
return getFirstPackagePart(subpackage);
}
return "";
}
private SliceAssignment subModuleSlicing() {
return new SliceAssignment() {
@Override
public SliceIdentifier getIdentifierOf(JavaClass javaClass) {
String packageName = javaClass.getPackageName();
String subModule = subModule("org.springframework.data.relational", packageName);
if (!subModule.isEmpty()) {
return SliceIdentifier.of(subModule);
}
subModule = subModule("org.springframework.data", packageName);
if (!subModule.isEmpty()) {
return SliceIdentifier.of(subModule);
}
return SliceIdentifier.ignore();
}
@Override
public String getDescription() {
return "Submodule";
}
};
}
}

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2017-2021 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
*
* https://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.relational.degraph;
import static de.schauderhaft.degraph.check.JCheck.*;
import static org.hamcrest.MatcherAssert.*;
import de.schauderhaft.degraph.check.JCheck;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import scala.runtime.AbstractFunction1;
import org.junit.jupiter.api.Test;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.sql.render.SelectRenderContext;
/**
* Test package dependencies for violations.
*
* @author Jens Schauder
* @author Mark Paluch
*/
public class DependencyTests {
@Test // DATAJDBC-114
@Disabled // Replace by ArchUnit test
public void cycleFree() {
assertThat( //
classpath() //
.noJars() //
.including("org.springframework.data.relational.**") //
.excluding(SelectRenderContext.class.getName()) //
.excluding(RenderContextFactory.class.getName() + "*") //
.filterClasspath("*target/classes") // exclude test code
.printOnFailure("degraph-relational.graphml"),
JCheck.violationFree());
}
@Test // DATAJDBC-220
@Disabled // Replace by ArchUnit test
public void acrossModules() {
assertThat( //
classpath() //
// include only Spring Data related classes (for example no JDK code)
.including("org.springframework.data.**") //
.excluding("org.springframework.data.relational.core.sql.**") //
.excluding("org.springframework.data.repository.query.parser.**") //
.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.relational.(**).*", //
"org.springframework.data.(**).*") //
.printTo("degraph-across-modules.graphml"), // writes a graphml to this location
JCheck.violationFree());
}
}