Skip to content
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

Migrate existing Junit 4 usages to Junit 5 #998

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ dependencies {

testImplementation("org.spockframework:spock-core:2.3-groovy-3.0") {
exclude(group = "org.codehaus.groovy")
exclude(group = "org.hamcrest")
}
testImplementation("org.spockframework:spock-junit4:2.3-groovy-3.0")
testImplementation("xmlunit:xmlunit:1.6")
testImplementation("org.xmlunit:xmlunit-legacy:2.10.0")
testImplementation("org.apache.commons:commons-lang3:3.17.0")
testImplementation("com.google.guava:guava:33.3.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.11.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation(platform("org.junit:junit-bom:5.11.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.platform:junit-platform-suite-engine")
}

val isCI = providers.environmentVariable("CI").isPresent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import org.apache.commons.io.FileUtils
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.TempDir

import java.nio.file.Path

import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

abstract class AbstractCachingSpec extends PluginSpecification {
@Rule TemporaryFolder alternateDir
@TempDir Path alternateDir

@Override
def setup() {
Expand Down Expand Up @@ -41,7 +42,7 @@ abstract class AbstractCachingSpec extends PluginSpecification {
List<String> cacheArguments = [ '--build-cache' ]
cacheArguments.addAll(arguments)
// TODO: Use PluginSpecification.run here to reuse flags, but cache tests failed for now, need to investigate.
return runner.withProjectDir(alternateDir.root).withArguments(cacheArguments).build()
return runner.withProjectDir(alternateDir.toFile()).withArguments(cacheArguments).build()
}

private String escapedPath(File file) {
Expand All @@ -59,9 +60,9 @@ abstract class AbstractCachingSpec extends PluginSpecification {
}

void copyToAlternateDir() {
FileUtils.deleteDirectory(alternateDir.root)
FileUtils.forceMkdir(alternateDir.root)
FileUtils.copyDirectory(dir.root, alternateDir.root)
FileUtils.deleteDirectory(alternateDir.toFile())
FileUtils.forceMkdir(alternateDir.toFile())
FileUtils.copyDirectory(dir.toFile(), alternateDir.toFile())
}

void assertShadowJarIsCachedAndRelocatable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.github.jengelman.gradle.plugins.shadow.caching

import org.gradle.testkit.runner.BuildResult

import static org.gradle.testkit.runner.TaskOutcome.*

class MinimizationCachingSpec extends AbstractCachingSpec {
File output
String shadowJarTask = ":server:shadowJar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.execute
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.SnippetFixture
import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import org.gradle.testkit.runner.GradleRunner
import org.junit.rules.TemporaryFolder

import java.util.function.Function

Expand Down Expand Up @@ -34,19 +33,15 @@ class GradleBuildExecuter implements SnippetExecuter {
}

@Override
void execute(TestCodeSnippet snippet) throws Exception {
TemporaryFolder tempDir = new TemporaryFolder()
tempDir.create()
File dir = tempDir.newFolder()

addSubProject(dir)
File settings = new File(dir, "settings.gradle")
void execute(File tempDir, TestCodeSnippet snippet) throws Exception {
addSubProject(tempDir)
File settings = new File(tempDir, "settings.gradle")
settings.text = """
rootProject.name = 'shadowTest'
include 'api', 'main'
"""

File mainDir = new File(dir, "main")
File mainDir = new File(tempDir, "main")
mainDir.mkdirs()
File buildFile = new File(mainDir, buildFile)

Expand All @@ -59,7 +54,7 @@ include 'api', 'main'

buildFile.text = replaceTokens(fullSnippet)

GradleRunner runner = GradleRunner.create().withProjectDir(dir).withPluginClasspath().forwardOutput()
GradleRunner runner = GradleRunner.create().withProjectDir(tempDir).withPluginClasspath().forwardOutput()

runner.withArguments(":main:build", "-m").build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NoopExecuter implements SnippetExecuter {
}

@Override
void execute(TestCodeSnippet snippet) throws Exception {

void execute(File tempDir, TestCodeSnippet snippet) throws Exception {
// noop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.junit.DelegatingTestRunner;
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.junit.RunnerProvider;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.gradle.internal.impldep.org.junit.runner.RunWith;
import org.gradle.internal.impldep.org.junit.runner.Runner;

import java.util.LinkedList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets

import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.junit.SnippetRunner
import org.junit.runner.Runner
import org.gradle.internal.impldep.org.junit.runner.Runner

class DefaultCodeSnippetTests implements CodeSnippetTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.SnippetFixture;
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.TestCodeSnippet;

import java.io.File;

public interface SnippetExecuter {

SnippetFixture getFixture();

void execute(TestCodeSnippet snippet) throws Exception;
void execute(File tempDir, TestCodeSnippet snippet) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.junit;

import org.junit.runner.Runner;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerScheduler;
import org.gradle.internal.impldep.org.junit.runner.Runner;
import org.gradle.internal.impldep.org.junit.runners.Suite;
import org.gradle.internal.impldep.org.junit.runners.model.InitializationError;
import org.gradle.internal.impldep.org.junit.runners.model.RunnerScheduler;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.junit;

import org.junit.runner.Runner;
import org.gradle.internal.impldep.org.junit.runner.Runner;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.junit;

import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.TestCodeSnippet;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.gradle.internal.impldep.org.junit.runner.Description;
import org.gradle.internal.impldep.org.junit.runner.Runner;
import org.gradle.internal.impldep.org.junit.runner.notification.Failure;
import org.gradle.internal.impldep.org.junit.runner.notification.RunNotifier;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;

public class SnippetRunner extends Runner {

@TempDir
private Path tempDir;

private final Description description;
private final TestCodeSnippet snippet;

Expand All @@ -32,7 +38,7 @@ public void run(RunNotifier notifier) {

try {
notifier.fireTestStarted(description);
snippet.getExecuter().execute(snippet);
snippet.getExecuter().execute(tempDir.toFile(), snippet);
} catch (Throwable t) {
Throwable transform;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@

package com.github.jengelman.gradle.plugins.shadow.relocation

import junit.framework.TestCase
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.*


/**
* Modified from org.apache.maven.plugins.shade.relocation.SimpleRelocatorParameterTest.java
*
* Modifications
* @author John Engelman
*/
class SimpleRelocatorParameterTest extends TestCase {

@Override
protected void setUp() {
super.setUp()
}
class SimpleRelocatorParameterTest {

@Test
void testThatNullPatternInConstructorShouldNotThrowNullPointerException() {
constructThenFailOnNullPointerException(null, "")
}

@Test
void testThatNullShadedPatternInConstructorShouldNotThrowNullPointerException() {
constructThenFailOnNullPointerException("", null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
package com.github.jengelman.gradle.plugins.shadow.relocation

import com.github.jengelman.gradle.plugins.shadow.ShadowStats
import junit.framework.TestCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.*

/**
* Test for {@link SimpleRelocator}.
Expand All @@ -29,19 +32,20 @@ import junit.framework.TestCase
* @version $Id: SimpleRelocatorTest.java 1342979 2012-05-26 22:05:45Z bimargulies $
*
* Modified from org.apache.maven.plugins.shade.relocation.SimpleRelocatorTest.java
*
*
* Modifications
* @author John Engelman
*/
class SimpleRelocatorTest extends TestCase {
class SimpleRelocatorTest {

ShadowStats stats
private static ShadowStats stats

@Override
protected void setUp() {
@BeforeEach
void setUp() {
stats = new ShadowStats()
}

@Test
void testCanRelocatePath() {
SimpleRelocator relocator

Expand Down Expand Up @@ -94,7 +98,8 @@ class SimpleRelocatorTest extends TestCase {
assertEquals(true, relocator.canRelocatePath("org/f")) // equal to path pattern
assertEquals(true, relocator.canRelocatePath("/org/f")) // equal to path pattern with /
}


@Test
void testCanRelocatePathWithRegex() {
SimpleRelocator relocator

Expand Down Expand Up @@ -128,6 +133,7 @@ class SimpleRelocatorTest extends TestCase {
assertEquals(false, relocator.canRelocatePath("org/foo/R.class"))
}

@Test
void testCanRelocateClass() {
SimpleRelocator relocator

Expand Down Expand Up @@ -156,6 +162,7 @@ class SimpleRelocatorTest extends TestCase {
assertEquals(false, relocator.canRelocateClass("org.foo.recurse.sub.Class"))
}

@Test
void testCanRelocateRawString() {
SimpleRelocator relocator

Expand All @@ -167,12 +174,14 @@ class SimpleRelocatorTest extends TestCase {
}

//MSHADE-119, make sure that the easy part of this works.
@Test
void testCanRelocateAbsClassPath() {
SimpleRelocator relocator = new SimpleRelocator("org.apache.velocity", "org.apache.momentum", null, null)
assertEquals("/org/apache/momentum/mass.properties", relocator.relocatePath(pathContext("/org/apache/velocity/mass.properties")))

}

@Test
void testRelocatePath() {
SimpleRelocator relocator

Expand All @@ -183,6 +192,7 @@ class SimpleRelocatorTest extends TestCase {
assertEquals("private/stuff/bar/Class.class", relocator.relocatePath(pathContext("org/foo/bar/Class.class")))
}

@Test
void testRelocateClass() {
SimpleRelocator relocator

Expand All @@ -193,6 +203,7 @@ class SimpleRelocatorTest extends TestCase {
assertEquals("private.stuff.bar.Class", relocator.relocateClass(classContext("org.foo.bar.Class")))
}

@Test
void testRelocateRawString() {
SimpleRelocator relocator

Expand All @@ -202,12 +213,12 @@ class SimpleRelocatorTest extends TestCase {
relocator = new SimpleRelocator("^META-INF/org.foo.xml\$", "META-INF/hidden.org.foo.xml", null, null, true)
assertEquals("META-INF/hidden.org.foo.xml", relocator.relocatePath(pathContext("META-INF/org.foo.xml")))
}
protected RelocatePathContext pathContext(String path) {

protected static RelocatePathContext pathContext(String path) {
return RelocatePathContext.builder().path(path).stats(stats).build()
}

protected RelocateClassContext classContext(String className) {
protected static RelocateClassContext classContext(String className) {
return RelocateClassContext.builder().className(className).stats(stats).build()
}
}
Loading