Skip to content

Commit

Permalink
Polish tests, fix dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Feb 20, 2024
1 parent e22efc2 commit 5106afc
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 244 deletions.
20 changes: 4 additions & 16 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,18 @@ val rewriteVersion = rewriteRecipe.rewriteVersion.get()
dependencies {
implementation(platform("org.openrewrite:rewrite-bom:$rewriteVersion"))
implementation("org.openrewrite:rewrite-maven")
// temporarily use SNAPSHOT version to release, to be changed back
implementation("org.openrewrite:rewrite-gradle")
implementation("org.openrewrite:rewrite-groovy")
runtimeOnly("org.openrewrite:rewrite-java-8")
runtimeOnly("org.openrewrite:rewrite-java-11")
runtimeOnly("org.openrewrite:rewrite-java-17")

testRuntimeOnly("org.openrewrite:rewrite-java-8")
testRuntimeOnly("org.openrewrite:rewrite-java-11")
testRuntimeOnly("org.openrewrite:rewrite-java-17")
testRuntimeOnly("org.projectlombok:lombok:latest.release")

implementation("com.google.guava:guava:latest.release")
runtimeOnly("org.openrewrite:rewrite-java-17")

implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-csv")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")
testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
compileOnly("org.projectlombok:lombok:latest.release")
annotationProcessor("org.projectlombok:lombok:latest.release")
testImplementation("org.openrewrite.gradle.tooling:model:${rewriteVersion}")
testImplementation(gradleApi())
testRuntimeOnly("com.google.guava:guava:latest.release")
testRuntimeOnly("ch.qos.logback:logback-classic:1.2.+")

testImplementation("org.assertj:assertj-core:3.24.+")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package org.openrewrite.java.dependencies.internal;

import com.google.common.collect.ImmutableMap;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

Expand All @@ -29,12 +28,18 @@
* One example where this comparator is inappropriate is if versions should be retained in a TreeMap/TreeSet.
*/
public class StaticVersionComparator implements Comparator<Version> {
static final Map<String, Integer> SPECIAL_MEANINGS = ImmutableMap.<String, Integer>builderWithExpectedSize(7)
.put("dev", -1)
.put("rc", 1)
.put("snapshot", 2)
.put("final", 3).put("ga", 4).put("release", 5)
.put("sp", 6).build();
static final Map<String, Integer> SPECIAL_MEANINGS = new HashMap<>();

static {
SPECIAL_MEANINGS.put("dev", -1);
SPECIAL_MEANINGS.put("rc", 1);
SPECIAL_MEANINGS.put("snapshot", 2);
SPECIAL_MEANINGS.put("final", 3);
SPECIAL_MEANINGS.put("ga", 4);
SPECIAL_MEANINGS.put("release", 5);
SPECIAL_MEANINGS.put("sp", 6);
}

/**
* Compares 2 versions. Algorithm is inspired by PHP version_compare one.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package org.openrewrite.java.dependencies.internal;

import com.google.common.collect.Maps;
import com.google.common.primitives.Longs;
import org.openrewrite.internal.lang.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class VersionParser {
private final Map<String, Version> cache = Maps.newConcurrentMap();
private final Map<String, Version> cache = new ConcurrentHashMap<>();

public VersionParser() {
}
Expand All @@ -39,7 +36,6 @@ private Version parse(String original) {
boolean digit = false;
int startPart = 0;
int pos = 0;
int endBase = 0;
int endBaseStr = 0;
for (; pos < original.length(); pos++) {
char ch = original.charAt(pos);
Expand All @@ -48,13 +44,11 @@ private Version parse(String original) {
startPart = pos + 1;
digit = false;
if (ch != '.' && endBaseStr == 0) {
endBase = parts.size();
endBaseStr = pos;
}
} else if (ch >= '0' && ch <= '9') {
if (!digit && pos > startPart) {
if (endBaseStr == 0) {
endBase = parts.size() + 1;
endBaseStr = pos;
}
parts.add(original.substring(startPart, pos));
Expand All @@ -64,7 +58,6 @@ private Version parse(String original) {
} else {
if (digit) {
if (endBaseStr == 0) {
endBase = parts.size() + 1;
endBaseStr = pos;
}
parts.add(original.substring(startPart, pos));
Expand All @@ -76,27 +69,24 @@ private Version parse(String original) {
if (pos > startPart) {
parts.add(original.substring(startPart, pos));
}
DefaultVersion base = null;
if (endBaseStr > 0) {
base = new DefaultVersion(original.substring(0, endBaseStr), parts.subList(0, endBase), null);
}
return new DefaultVersion(original, parts, base);
return new DefaultVersion(original, parts);
}

private static class DefaultVersion implements Version {
private final String source;
private final String[] parts;
private final Long[] numericParts;
private final DefaultVersion baseVersion;

public DefaultVersion(String source, List<String> parts, @Nullable DefaultVersion baseVersion) {
public DefaultVersion(String source, List<String> parts) {
this.source = source;
this.parts = parts.toArray(new String[0]);
this.numericParts = new Long[this.parts.length];
for (int i = 0; i < parts.size(); i++) {
this.numericParts[i] = Longs.tryParse(this.parts[i]);
try {
this.numericParts[i] = Long.parseLong(this.parts[i]);
} catch (NumberFormatException ignored) {
}
}
this.baseVersion = baseVersion == null ? this : baseVersion;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import static org.openrewrite.maven.Assertions.pomXml;

class AddDependencyTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion()
.classpath("junit-jupiter-api", "guava", "jackson-databind", "jackson-core", "lombok"));
spec.parser(JavaParser.fromJavaVersion().classpath("guava"));
}

@SuppressWarnings("UnstableApiUsage")
@Language("java")
private final String usingGuavaIntMath = """
import com.google.common.math.IntMath;
Expand All @@ -58,12 +57,13 @@ void addGradleDependencyWithOnlyIfUsingTestScope(String onlyIfUsing) {
srcTestJava(
java(usingGuavaIntMath)
),
//language=groovy
buildGradle(
"""
plugins {
id "java-library"
}
repositories {
mavenCentral()
}
Expand All @@ -72,11 +72,11 @@ void addGradleDependencyWithOnlyIfUsingTestScope(String onlyIfUsing) {
plugins {
id "java-library"
}
repositories {
mavenCentral()
}
dependencies {
testImplementation "com.google.guava:guava:29.0-jre"
}
Expand All @@ -96,28 +96,29 @@ void addMavenDependencyWithSystemScope() {
srcMainJava(
java(usingGuavaIntMath)
),
//language=xml
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>doesnotexist</groupId>
<artifactId>doesnotexist</artifactId>
<version>1</version>
<scope>system</scope>
</dependency>
</dependencies>
</project>
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>doesnotexist</groupId>
<artifactId>doesnotexist</artifactId>
<version>1</version>
<scope>system</scope>
</dependency>
</dependencies>
</project>
"""
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,31 @@

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

import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.maven.Assertions.pomXml;

class ChangeDependencyTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion()
.classpath("junit-jupiter-api", "guava", "jackson-databind", "jackson-core", "lombok"));
}

@DocumentExample("Change Gradle dependency")
@Test
void changeGradleDependency() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi())
.recipe(new ChangeDependency("commons-lang", "commons-lang", "org.apache.commons", "commons-lang3", "3.11.x", null, null)),
//language=groovy
buildGradle(
"""
plugins {
id "java-library"
}
repositories {
mavenCentral()
}
dependencies {
implementation "commons-lang:commons-lang:2.6"
}
Expand All @@ -56,11 +50,11 @@ void changeGradleDependency() {
plugins {
id "java-library"
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.apache.commons:commons-lang3:3.11"
}
Expand All @@ -74,6 +68,7 @@ void changeGradleDependency() {
void changeMavenDependency() {
rewriteRun(
spec -> spec.recipe(new ChangeDependency("commons-lang", "commons-lang", "org.apache.commons", "commons-lang3", "3.11.x", null, null)),
//language=xml
pomXml(
"""
<project>
Expand Down Expand Up @@ -113,18 +108,19 @@ void doNotPinWhenNotVersionedGradle() {
spec -> spec
.beforeRecipe(withToolingApi())
.recipe(new ChangeDependency("mysql", "mysql-connector-java", "com.mysql", "mysql-connector-j", "8.0.x", null, null)),
//language=groovy
buildGradle(
"""
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
runtimeOnly 'mysql:mysql-connector-java'
}
Expand All @@ -135,15 +131,16 @@ void doNotPinWhenNotVersionedGradle() {
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
}
""")
"""
)
);
}

Expand All @@ -153,18 +150,19 @@ void pinWhenOverrideManagedVersionGradle() {
spec -> spec
.beforeRecipe(withToolingApi())
.recipe(new ChangeDependency("mysql", "mysql-connector-java", "com.mysql", "mysql-connector-j", "8.0.x", null, true)),
//language=groovy
buildGradle(
"""
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
runtimeOnly 'mysql:mysql-connector-java'
}
Expand All @@ -175,15 +173,16 @@ void pinWhenOverrideManagedVersionGradle() {
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
runtimeOnly 'com.mysql:mysql-connector-j:8.0.33'
}
""")
"""
)
);
}
}
Loading

0 comments on commit 5106afc

Please sign in to comment.