Skip to content

Reimplemented dependency tests using ArchUnit. #1107

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-1058-archunit-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-1058-archunit-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
19 changes: 9 additions & 10 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-1058-archunit-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-1058-archunit-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -264,6 +256,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>0.22.0</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer keeping all the versions as property settings at the top?

<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.JDBCType;
import java.sql.SQLType;

import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
import org.springframework.data.relational.core.dialect.PostgresDialect;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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");

final JavaClasses relevantClasses = importedClasses.that( //
onlySpringData() //
);

ArchRule rule = SlicesRuleDefinition.slices() //
.matching("org.springframework.data.jdbc.(**)") //
.should() //
.beFreeOfCycles();

rule.check(relevantClasses);
}

@Test
void acrossModules() {

JavaClasses importedClasses = new ClassFileImporter().withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages( //
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this consistent with the previous test.

"org.springframework.data.jdbc", // Spring Data Relational
"org.springframework.data.relational", // Spring Data Relational
"org.springframework.data" // Spring Data Commons
).that(onlySpringData() //
.and(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) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the blank line if there is only one line in the method?

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";
}
};
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
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;
Expand Down
10 changes: 5 additions & 5 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-1058-archunit-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-1058-archunit-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -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>0.22.0</version>
<scope>test</scope>
</dependency>

Expand Down
Loading