Skip to content

Commit

Permalink
feature: ArchUnit 0.x to 1.x migration (#432)
Browse files Browse the repository at this point in the history
* feature: ArchUnit 0.x to 1.x migration

Resolves #431

* feat: add testcases for ArchUnit 0 to 1 migration

* Minor test fixes

* Upgrade any ArchUnit dependency

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
SimonVerhoeven and timtebeek authored Nov 24, 2023
1 parent 595a273 commit 598d7cd
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ dependencies {

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")

testRuntimeOnly("com.tngtech.archunit:archunit:0.23.1")

// testImplementation("org.hamcrest:hamcrest:latest.release")
// testImplementation("org.assertj:assertj-core:latest.release")
}
49 changes: 49 additions & 0 deletions src/main/resources/META-INF/rewrite/archunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright 2023 the original author or authors.
# <p>
# 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
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# 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.
#
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.archunit.ArchUnit0to1Migration
displayName: ArchUnit 0.x upgrade
description: Upgrade ArchUnit from 0.x to 1.x.
recipeList:
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: com.tngtech.archunit
artifactId: archunit*
newVersion: 1.x
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage getAllClasses()
newMethodName: getClassesInPackageTree
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage getAllSubpackages()
newMethodName: getSubpackagesInTree
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage getClassDependenciesFromSelf()
newMethodName: getClassDependenciesFromThisPackageTree
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage getClassDependenciesToSelf()
newMethodName: getClassDependenciesToThisPackageTree
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage getPackageDependenciesFromSelf()
newMethodName: getPackageDependenciesFromThisPackageTree
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage getPackageDependenciesToSelf()
newMethodName: getPackageDependenciesToThisPackageTree
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.tngtech.archunit.core.domain.JavaPackage accept(..)
newMethodName: traversePackageTree
- org.openrewrite.java.ChangePackage:
oldPackageName: com.tngtech.archunit.library.plantuml
newPackageName: com.tngtech.archunit.library.plantuml.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.openrewrite.java.testing.archunit;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;

class ArchUnit0To1MigrationTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpath("archunit-0.23.1"))
.recipeFromResource("/META-INF/rewrite/archunit.yml", "org.openrewrite.java.testing.archunit.ArchUnit0to1Migration");
}

@Test
void shouldMigrateMavenDependency() {
rewriteRun(
mavenProject("project",
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>0.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""",
spec -> spec.after(pom -> {
Matcher matcher = Pattern.compile("<version>(1\\..+)</version>").matcher(pom);
matcher.find();
String version = matcher.group(1);
return """
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>%s</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""".formatted(version);
})
)
)
);
}

@Test
void shouldUseGetClassesInPackageTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaClass;
public class ArchUnitTest {
public Set<JavaClass> sample(JavaPackage javaPackage) {
return javaPackage.getAllClasses();
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaClass;
public class ArchUnitTest {
public Set<JavaClass> sample(JavaPackage javaPackage) {
return javaPackage.getClassesInPackageTree();
}
}
"""
)
);
}

@Test
void shouldUseGetSubpackagesInTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaClass;
public class ArchUnitTest {
public Set<JavaClass> sample(JavaPackage javaPackage) {
return javaPackage.getAllSubpackages();
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaClass;
public class ArchUnitTest {
public Set<JavaClass> sample(JavaPackage javaPackage) {
return javaPackage.getSubpackagesInTree();
}
}
"""
)
);
}

@Test
void shouldUseGetClassDependenciesFromThisPackageTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.Dependency;
public class ArchUnitTest {
public Set<Dependency> sample(JavaPackage javaPackage) {
return javaPackage.getClassDependenciesFromSelf();
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.Dependency;
public class ArchUnitTest {
public Set<Dependency> sample(JavaPackage javaPackage) {
return javaPackage.getClassDependenciesFromThisPackageTree();
}
}
"""
)
);
}

@Test
void shouldUseGetClassDependenciesToThisPackageTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.Dependency;
public class ArchUnitTest {
public Set<Dependency> sample(JavaPackage javaPackage) {
return javaPackage.getClassDependenciesToSelf();
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.Dependency;
public class ArchUnitTest {
public Set<Dependency> sample(JavaPackage javaPackage) {
return javaPackage.getClassDependenciesToThisPackageTree();
}
}
"""
)
);
}

@Test
void shouldUseGetPackageDependenciesFromThisPackageTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaPackage;
public class ArchUnitTest {
public Set<JavaPackage> sample(JavaPackage javaPackage) {
return javaPackage.getPackageDependenciesFromSelf();
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaPackage;
public class ArchUnitTest {
public Set<JavaPackage> sample(JavaPackage javaPackage) {
return javaPackage.getPackageDependenciesFromThisPackageTree();
}
}
"""
)
);
}

@Test
void shouldUseGetPackageDependenciesToThisPackageTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaPackage;
public class ArchUnitTest {
public Set<JavaPackage> sample(JavaPackage javaPackage) {
return javaPackage.getPackageDependenciesToSelf();
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import java.util.Set;
import com.tngtech.archunit.core.domain.JavaPackage;
public class ArchUnitTest {
public Set<JavaPackage> sample(JavaPackage javaPackage) {
return javaPackage.getPackageDependenciesToThisPackageTree();
}
}
"""
)
);
}

@Test
void shouldUseTraversePackageTree() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import static com.tngtech.archunit.core.domain.JavaPackage.ClassVisitor;
public class ArchUnitTest {
public void sample(JavaPackage javaPackage, ClassVisitor visitor) {
javaPackage.accept(null, visitor);
}
}
""",
"""
import com.tngtech.archunit.core.domain.JavaPackage;
import static com.tngtech.archunit.core.domain.JavaPackage.ClassVisitor;
public class ArchUnitTest {
public void sample(JavaPackage javaPackage, ClassVisitor visitor) {
javaPackage.traversePackageTree(null, visitor);
}
}
"""
)
);
}

@Test
void shouldUsePlantUmlRulesPackage() {
//language=java
rewriteRun(
java(
"""
import com.tngtech.archunit.library.plantuml.PlantUmlArchCondition;
public class ArchUnitTest {
public void sample(PlantUmlArchCondition condition) {
condition.ignoreDependencies("origin", "");
}
}
""",
"""
import com.tngtech.archunit.library.plantuml.rules.PlantUmlArchCondition;
public class ArchUnitTest {
public void sample(PlantUmlArchCondition condition) {
condition.ignoreDependencies("origin", "");
}
}
"""
)
);
}
}

0 comments on commit 598d7cd

Please sign in to comment.