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

Convert several tests to use JUnit 5 #115

Merged
merged 1 commit into from
Oct 23, 2019
Merged
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
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<!-- No API's intended to be used, none should be called from outside. -->
<maven.javadoc.skip>true</maven.javadoc.skip>
<hpi.compatibleSinceVersion>5.0</hpi.compatibleSinceVersion>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<junit.params.version>5.5.2</junit.params.version>
<junit.vintage.version>5.5.2</junit.vintage.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -102,6 +106,31 @@
</exclusions>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.params.version}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,20 @@
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;

@RunWith(Parameterized.class)
public class PlatformDetailsTaskLsbReleaseTest {

private final String lsbReleaseFileName;
private final String expectedName;
private final String expectedVersion;
private final String expectedArch;

public PlatformDetailsTaskLsbReleaseTest(
String lsbReleaseFileName, String expectedName, String expectedVersion, String expectedArch) {
this.lsbReleaseFileName = lsbReleaseFileName;
this.expectedName = expectedName;
this.expectedVersion = expectedVersion;
this.expectedArch = expectedArch;
}

/**
* Generate test parameters for Linux lsb_release-a sample files stored as resources.
*
* @return parameter values to be tested
*/
@Parameters(name = "{1}-{2}-{3}")
public static Collection<Object[]> generateReleaseFileNames() {
public static Stream<Object[]> generateReleaseFileNames() {
String packageName = PlatformDetailsTaskLsbReleaseTest.class.getPackage().getName();
Reflections reflections = new Reflections(packageName, new ResourcesScanner());
Set<String> fileNames = reflections.getResources(Pattern.compile(".*lsb_release-a"));
Expand All @@ -51,11 +35,14 @@ public static Collection<Object[]> generateReleaseFileNames() {
Object[] oneTest = {trimmedName, oneExpectedName, oneExpectedVersion, oneExpectedArch};
data.add(oneTest);
}
return data;
return data.stream();
}

@Test
public void testComputeLabelsForOsRelease() throws Exception {
@ParameterizedTest
@MethodSource("generateReleaseFileNames")
public void testComputeLabelsForOsRelease(
String lsbReleaseFileName, String expectedName, String expectedVersion, String expectedArch)
throws Exception {
PlatformDetailsTask details = new PlatformDetailsTask();
URL resource = getClass().getResource(lsbReleaseFileName);
File lsbReleaseFile = new File(resource.toURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,21 @@
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;

@RunWith(Parameterized.class)
public class PlatformDetailsTaskReleaseTest {

private final String releaseFileName;
private final String expectedName;
private final String expectedVersion;
private final String expectedArch;

public PlatformDetailsTaskReleaseTest(
String releaseFileName, String expectedName, String expectedVersion, String expectedArch) {
this.releaseFileName = releaseFileName;
this.expectedName = expectedName;
this.expectedVersion = expectedVersion;
this.expectedArch = expectedArch;
}

/**
* Generate test parameters for Linux os-release, redhat-release and SuSE-release sample files
* stored as resources.
*
* @return parameter values to be tested
*/
@Parameters(name = "{1}-{2}-{3}-{0}")
public static Collection<Object[]> generateReleaseFileNames() {
public static Stream<Object[]> generateReleaseFileNames() {
String packageName = PlatformDetailsTaskReleaseTest.class.getPackage().getName();
Reflections reflections = new Reflections(packageName, new ResourcesScanner());
Set<String> fileNames = reflections.getResources(Pattern.compile(".*-release"));
Expand All @@ -52,11 +36,14 @@ public static Collection<Object[]> generateReleaseFileNames() {
Object[] oneTest = {trimmedName, oneExpectedName, oneExpectedVersion, oneExpectedArch};
data.add(oneTest);
}
return data;
return data.stream();
}

@Test
public void testComputeLabelsForRelease() throws Exception {
@ParameterizedTest
@MethodSource("generateReleaseFileNames")
public void testComputeLabelsForRelease(
String releaseFileName, String expectedName, String expectedVersion, String expectedArch)
throws Exception {
PlatformDetailsTask details = new PlatformDetailsTask();
URL resource = getClass().getResource(releaseFileName);
File releaseFile = new File(resource.toURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,20 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@RunWith(Parameterized.class)
public class PlatformDetailsTaskStaticStringTest {

private final String name;
private final String arch;
private final String version;
private final String expectedName;
private final String expectedArch;
private final String expectedVersion;

public PlatformDetailsTaskStaticStringTest(String name, String arch, String version) {
this.name = name;
this.arch = arch;
this.version = version;
this.expectedName = computeExpectedName(name);
this.expectedArch = computeExpectedArch(name, arch);
this.expectedVersion = computeVersion(version);
}

/**
* Generate test parameters for cases which can be tested with static string conversions. Linux
* platform labeling can't be tested with static string conversions. Refer to other tests for
* Linux platform labeling tests. Expected values are computed in the test contructor.
* Linux platform labeling tests.
*
* @return parameter values to be tested
*/
@Parameters(name = "{0}-{1}-{2}")
public static Collection<Object[]> generateTestParameters() {
public static Stream<Object[]> generateTestParameters() {
Collection<Object[]> data =
Arrays.asList(
new Object[][] {
Expand Down Expand Up @@ -75,7 +56,7 @@ public static Collection<Object[]> generateTestParameters() {
/* Don't add data for this platform if linux - linux decodes the distribution as a label */
String myName = System.getProperty("os.name");
if (myName.equalsIgnoreCase("linux")) {
return data;
return data.stream();
}

/* Check this platform is in the test data */
Expand All @@ -85,7 +66,7 @@ public static Collection<Object[]> generateTestParameters() {
if (testData[0].equals(myName)
&& testData[1].equals(myArch)
&& testData[2].equals(myVersion)) {
return data;
return data.stream();
}
}

Expand All @@ -94,11 +75,15 @@ public static Collection<Object[]> generateTestParameters() {
List<Object[]> augmentedData = new ArrayList<>();
augmentedData.add(myTestData);
augmentedData.addAll(data);
return augmentedData;
return augmentedData.stream();
}

@Test
public void testComputeLabels() throws Exception {
@ParameterizedTest
@MethodSource("generateTestParameters")
public void testComputeLabels(String name, String arch, String version) throws Exception {
String expectedName = computeExpectedName(name);
String expectedArch = computeExpectedArch(name, arch);
String expectedVersion = computeVersion(name, version);
PlatformDetailsTask details = new PlatformDetailsTask();
PlatformDetails result = details.computeLabels(arch, name, version);
assertThat(result.getArchitecture(), is(expectedArch));
Expand Down Expand Up @@ -129,7 +114,7 @@ private String computeExpectedName(String name) {
return name.toLowerCase();
}

private String computeVersion(String version) {
private String computeVersion(String name, String version) {
if (!name.startsWith("Windows")) {
return version;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.jvnet.hudson.plugins.platformlabeler;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PlatformDetailsTaskTest {

Expand All @@ -25,7 +25,7 @@ public class PlatformDetailsTaskTest {
private static final String SPECIAL_CASE_ARCH =
SYSTEM_OS_ARCH.contains("amd") ? "x86" : SYSTEM_OS_ARCH;

@Before
@BeforeEach
public void createPlatformDetailsTask() {
platformDetailsTask = new PlatformDetailsTask();
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public void testComputeLabelsLinuxWithNullLsbRelease() throws Exception {
@Test
public void testCheckWindows32Bit() {
/* Always testing this case, no SPECIAL_CASE_ARCH needed */
assertThat(platformDetailsTask.checkWindows32Bit("x86", "AMD64", null), is("amd64"));
assertThat(platformDetailsTask.checkWindows32Bit("x86", "AMD64", ""), is("amd64"));
}

@Test
Expand Down