Skip to content

Commit

Permalink
refactor (org.eclipse.jkube.kubernetes.gradle.plugin): Migrate tests …
Browse files Browse the repository at this point in the history
…from JUnit4 to JUnit5 (eclipse-jkube#1581)

Signed-off-by: Anurag Rajawat <[email protected]>
  • Loading branch information
anurag-rajawat committed Nov 2, 2022
1 parent 3724101 commit 5dd3896
Show file tree
Hide file tree
Showing 21 changed files with 512 additions and 482 deletions.
10 changes: 8 additions & 2 deletions gradle-plugin/kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@

<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -43,10 +44,9 @@
import org.gradle.api.internal.provider.DefaultProvider;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.internal.deprecation.DeprecatableConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.stubbing.Answer;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -58,19 +58,19 @@
import static org.mockito.Mockito.when;

@SuppressWarnings("unused")
public class GradleUtilTest {
class GradleUtilTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();
@TempDir
Path folder;

private Project project;

private JavaPluginConvention javaPlugin;

private List<Configuration> projectConfigurations;

@Before
public void setUp() throws IOException {
@BeforeEach
void setUp() throws IOException {
project = mock(Project.class, RETURNS_DEEP_STUBS);
javaPlugin = mock(JavaPluginConvention.class, RETURNS_DEEP_STUBS);
when(javaPlugin.getSourceSets().stream()).thenReturn(Stream.empty());
Expand All @@ -83,13 +83,13 @@ public void setUp() throws IOException {

when(project.getBuildscript().getConfigurations().stream()).thenAnswer(i -> Stream.empty());
when(project.getProperties()).thenReturn(Collections.emptyMap());
when(project.getBuildDir()).thenReturn(folder.newFolder("build"));
when(project.getBuildDir()).thenReturn(Files.createDirectory(folder.resolve("build")).toFile());
when(project.getPlugins()).thenReturn(new DefaultPluginContainer(null, null, null));
when(project.getConvention().getPlugin(JavaPluginConvention.class)).thenReturn(javaPlugin);
}

@Test
public void extractProperties_withComplexMap_shouldReturnValidProperties() {
void extractProperties_withComplexMap_shouldReturnValidProperties() {
// Given
final Map<String, Object> complexProperties = new HashMap<>();
when(project.getProperties()).thenAnswer(i -> complexProperties);
Expand All @@ -106,7 +106,7 @@ public void extractProperties_withComplexMap_shouldReturnValidProperties() {
}

@Test
public void extractProperties_shouldContainSystemProperties() {
void extractProperties_shouldContainSystemProperties() {
// Given
System.setProperty("foo.property", "somevalue");
when(project.getProperties()).thenReturn(Collections.emptyMap());
Expand All @@ -121,7 +121,7 @@ public void extractProperties_shouldContainSystemProperties() {
}

@Test
public void extractProperties_whenBothSystemAndGradlePropertyProvided_thenSystemPropertyShouldHaveMorePrecedence() {
void extractProperties_whenBothSystemAndGradlePropertyProvided_thenSystemPropertyShouldHaveMorePrecedence() {
// Given
final Map<String, Object> gradleProperties = new HashMap<>();
gradleProperties.put("foo.property", "gradlevalue");
Expand All @@ -138,7 +138,7 @@ public void extractProperties_whenBothSystemAndGradlePropertyProvided_thenSystem
}

@Test
public void extractDependencies_withMultipleAndDuplicateDependencies_shouldReturnValidDependencies() {
void extractDependencies_withMultipleAndDuplicateDependencies_shouldReturnValidDependencies() {
// Given
final Function<String[], Configuration> mockConfiguration = configurationDependencyMock();
projectConfigurations.add(mockConfiguration.apply(new String[] { "api", "com.example", "artifact", null }));
Expand All @@ -164,7 +164,7 @@ public void extractDependencies_withMultipleAndDuplicateDependencies_shouldRetur
}

@Test
public void extractPlugins_withMultipleAndBuildScriptDuplicateDependencies_shouldReturnValidPlugins() {
void extractPlugins_withMultipleAndBuildScriptDuplicateDependencies_shouldReturnValidPlugins() {
// Given
final ConfigurationContainer cc = mock(ConfigurationContainer.class);
when(project.getBuildscript().getConfigurations()).thenReturn(cc);
Expand Down Expand Up @@ -196,7 +196,7 @@ public void extractPlugins_withMultipleAndBuildScriptDuplicateDependencies_shoul
* into an ArrayList.
*/
@Test
public void convertGradleProject_withConcurrentConfigurationModifications_shouldReturnValidProject() {
void convertGradleProject_withConcurrentConfigurationModifications_shouldReturnValidProject() {
// Given
final Configuration mockConfiguration = mock(Configuration.class, RETURNS_DEEP_STUBS);
projectConfigurations.add(mockConfiguration);
Expand All @@ -216,18 +216,18 @@ public void convertGradleProject_withConcurrentConfigurationModifications_should
}

@Test
public void findClassesOutputDirectory_withNotFoundSourceSet_shouldReturnDefault() {
void findClassesOutputDirectory_withNotFoundSourceSet_shouldReturnDefault() {
// Given
when(javaPlugin.getSourceSets().getByName("main")).thenThrow(new UnknownDomainObjectException("Not found"));
// When
final JavaProject result = convertGradleProject(project);
// Then
assertThat(result.getOutputDirectory())
.isEqualTo(folder.getRoot().toPath().resolve("build").resolve("classes").resolve("java").resolve("main").toFile());
.isEqualTo(folder.resolve("build").resolve("classes").resolve("java").resolve("main").toFile());
}

@Test
public void findClassesOutputDirectory_withValidSourceSet_shouldReturnFromSourceSet() {
void findClassesOutputDirectory_withValidSourceSet_shouldReturnFromSourceSet() {
// Given
when(javaPlugin.getSourceSets().getByName("main").getJava().getDestinationDirectory().getAsFile())
.thenReturn(new DefaultProvider<>(() -> new File("classes")));
Expand All @@ -238,23 +238,23 @@ public void findClassesOutputDirectory_withValidSourceSet_shouldReturnFromSource
}

@Test
public void findClassesOutputDirectory_withNoSourceSets_shouldReturnDefault() {
void findClassesOutputDirectory_withNoSourceSets_shouldReturnDefault() {
// Given
when(javaPlugin.getSourceSets()).thenReturn(null);
// When
final JavaProject result = convertGradleProject(project);
// Then
assertThat(result.getOutputDirectory())
.isEqualTo(folder.getRoot().toPath().resolve("build").resolve("classes").resolve("java").resolve("main").toFile());
.isEqualTo(folder.resolve("build").resolve("classes").resolve("java").resolve("main").toFile());
}

@Test
public void findArtifact_withExistentFile_shouldReturnValidArtifact() throws IOException {
void findArtifact_withExistentFile_shouldReturnValidArtifact() throws IOException {
// Given
final Configuration c = mock(Configuration.class, RETURNS_DEEP_STUBS);
when(c.getAllDependencies().stream()).thenAnswer(i -> Stream.empty());
when(c.getOutgoing().getArtifacts().getFiles().getFiles()).thenReturn(Stream.of(
folder.newFile("final-artifact.jar")
Files.createFile(folder.resolve("final-artifact.jar")).toFile()
).collect(Collectors.toSet()));
projectConfigurations.add(c);
// When
Expand All @@ -264,12 +264,12 @@ public void findArtifact_withExistentFile_shouldReturnValidArtifact() throws IOE
}

@Test
public void findArtifact_withMultipleExistentFiles_shouldReturnArtifactWithLargestSize() throws IOException {
void findArtifact_withMultipleExistentFiles_shouldReturnArtifactWithLargestSize() throws IOException {
// Given
final Configuration c = mock(Configuration.class, RETURNS_DEEP_STUBS);
File jar1 = folder.newFile("final-artifact.jar");
File jar1 = Files.createFile(folder.resolve("final-artifact.jar")).toFile();
Files.write(jar1.toPath(), "FatJar".getBytes());
File jar2 = folder.newFile("final-artifact-plain.jar");
File jar2 = Files.createFile(folder.resolve("final-artifact-plain.jar")).toFile();
when(c.getAllDependencies().stream()).thenAnswer(i -> Stream.empty());
when(c.getOutgoing().getArtifacts().getFiles().getFiles()).thenReturn(Stream.of(
jar2, jar1
Expand All @@ -282,12 +282,12 @@ public void findArtifact_withMultipleExistentFiles_shouldReturnArtifactWithLarge
}

@Test
public void findArtifact_withMultipleArchiveFiles_shouldReturnJavaArchiveOnly() throws IOException {
void findArtifact_withMultipleArchiveFiles_shouldReturnJavaArchiveOnly() throws IOException {
// Given
final Configuration c = mock(Configuration.class, RETURNS_DEEP_STUBS);
File jar1 = folder.newFile("final-artifact.jar");
File jar2 = folder.newFile("final-artifact.tar");
File jar3 = folder.newFile("final-artifact.zip");
File jar1 = Files.createFile(folder.resolve("final-artifact.jar")).toFile();
File jar2 = Files.createFile(folder.resolve("final-artifact.tar")).toFile();
File jar3 = Files.createFile(folder.resolve("final-artifact.zip")).toFile();
when(c.getAllDependencies().stream()).thenAnswer(i -> Stream.empty());
when(c.getOutgoing().getArtifacts().getFiles().getFiles()).thenReturn(Stream.of(jar1, jar2, jar3).collect(Collectors.toSet()));
projectConfigurations.add(c);
Expand All @@ -298,7 +298,7 @@ public void findArtifact_withMultipleArchiveFiles_shouldReturnJavaArchiveOnly()
}

@Test
public void canBeResolved_withDeprecatedAndResolutionAlternatives_shouldReturnFalse() {
void canBeResolved_withDeprecatedAndResolutionAlternatives_shouldReturnFalse() {
// Given
final DeprecatableConfiguration c = mock(DeprecatableConfiguration.class);
when(c.getResolutionAlternatives()).thenReturn(Collections.emptyList());
Expand All @@ -309,7 +309,7 @@ public void canBeResolved_withDeprecatedAndResolutionAlternatives_shouldReturnFa
}

@Test
public void canBeResolved_DeprecatedAndNullResolutionAlternativesAndResolvable_shouldReturnTrue() {
void canBeResolved_DeprecatedAndNullResolutionAlternativesAndResolvable_shouldReturnTrue() {
// Given
final DeprecatableConfiguration c = mock(DeprecatableConfiguration.class);
when(c.isCanBeResolved()).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

import groovy.lang.Closure;
import org.codehaus.groovy.runtime.GStringImpl;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.eclipse.jkube.gradle.plugin.GroovyUtil.closureTo;
import static org.eclipse.jkube.gradle.plugin.GroovyUtil.invokeOrParseClosureList;

@SuppressWarnings({ "unused", "serial" })
public class GroovyUtilTest {
@SuppressWarnings("unused")
class GroovyUtilTest {

/**
* <pre>
Expand All @@ -45,7 +45,7 @@ public class GroovyUtilTest {
* </pre>
*/
@Test
public void closureTo_withNestedClosure_shouldReturnStructuredClass() {
void closureTo_withNestedClosure_shouldReturnStructuredClass() {
// Given
final Closure<?> closure = closure(this,
"property", new GStringImpl(new Object[] { "lue" }, new String[] { "va" }),
Expand All @@ -67,7 +67,7 @@ public void closureTo_withNestedClosure_shouldReturnStructuredClass() {
*/
@SuppressWarnings("unchecked")
@Test
public void closureTo_withNestedClosureCollection_shouldReturnMap() {
void closureTo_withNestedClosureCollection_shouldReturnMap() {
// Given
final Closure<?> closure = closure(this,
"array", Arrays.asList("one", "two", closure(this, "nested", "closure")));
Expand Down Expand Up @@ -95,7 +95,7 @@ public void closureTo_withNestedClosureCollection_shouldReturnMap() {
* </pre>
*/
@Test
public void invokeOrParseClosureList_namedClosureListTo_withNamedListNestedClosure_shouldReturnOrderedList() {
void invokeOrParseClosureList_namedClosureListTo_withNamedListNestedClosure_shouldReturnOrderedList() {
// Given
final Closure<?> element1 = closure(this, "property", "value",
"nested", closure(this, "nestedProperty", "nestedValue"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
import java.util.Map;

import org.gradle.api.Task;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JKubePluginTest {
class JKubePluginTest {

@Test
public void getTaskPrecedence_withDefaults_shouldReturnEmpty() {
void getTaskPrecedence_withDefaults_shouldReturnEmpty() {
// Given
final JKubePlugin partial = mock(JKubePlugin.class);
when(partial.getTaskPrecedence()).thenCallRealMethod();
Expand Down
Loading

0 comments on commit 5dd3896

Please sign in to comment.