Skip to content

Commit

Permalink
Merge pull request #4 from jamezp/issue1
Browse files Browse the repository at this point in the history
[1] Migrate tests from JUnit 4 to JUnit 5.
  • Loading branch information
jamezp authored Nov 18, 2024
2 parents 067831c + afcca9c commit 2f56ff6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 116 deletions.
51 changes: 15 additions & 36 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,21 @@
<version.org.jboss.logging.jboss-logging-tools>3.0.2.Final</version.org.jboss.logging.jboss-logging-tools>
<!-- Test dependencies -->
<version.org.jboss.modules.jboss-modules>2.1.5.Final</version.org.jboss.modules.jboss-modules>
<version.junit.junit>4.13.2</version.junit.junit>
<version.org.junit>5.11.3</version.org.junit>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${version.org.junit}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- This dependency is only needed at compile time and requires no runtime dependencies -->
<dependency>
Expand Down Expand Up @@ -91,9 +103,8 @@

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit.junit}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -111,7 +122,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${version.jar.plugin}</version>
<configuration>
<archive>
<manifestEntries>
Expand All @@ -126,40 +136,10 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${version.javadoc.plugin}</version>
<configuration>
<doclint>none</doclint>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire.plugin}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${version.surefire.plugin}</version>
</dependency>
</dependencies>
<configuration>
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
<enableAssertions>true</enableAssertions>
<systemPropertyVariables>
<test.level>${test.level}</test.level>
</systemPropertyVariables>
<argLine>${surefire.system.args}</argLine>
</configuration>

</plugin>
<!-- Checkstyle -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${version.checkstyle.plugin}</version>
<executions>
<execution>
<id>check-style</id>
Expand Down Expand Up @@ -191,7 +171,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${version.enforcer.plugin}</version>
<executions>
<execution>
<id>require-java11</id>
Expand Down
122 changes: 63 additions & 59 deletions src/test/java/org/wildfly/core/launcher/CommandBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

package org.wildfly.core.launcher;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -13,14 +17,14 @@
import java.util.Iterator;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.wildfly.core.launcher.Arguments.Argument;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class CommandBuilderTest {
class CommandBuilderTest {

private static final Path WILDFLY_HOME;
private static final Path WILDFLY_BOOTABLE_JAR;
Expand All @@ -40,7 +44,7 @@ public class CommandBuilderTest {
}

@Test
public void testJBossModulesBuilder() {
void jBossModulesBuilder() {
// Set up a standalone command builder
final JBossModulesCommandBuilder commandBuilder = JBossModulesCommandBuilder.of(WILDFLY_HOME, "org.jboss.as.launcher.test")
.addJavaOption("-Djava.security.manager")
Expand All @@ -52,11 +56,11 @@ public void testJBossModulesBuilder() {
// Get all the commands
List<String> commands = commandBuilder.buildArguments();

Assert.assertTrue("Missing -secmgr option", commands.contains("-secmgr"));
assertTrue(commands.contains("-secmgr"), "Missing -secmgr option");

Assert.assertTrue("Missing jboss-modules.jar", commands.stream().anyMatch(entry -> entry.matches("-javaagent:.*jboss-modules.jar$")));
Assert.assertTrue("Missing test-agent1.jar", commands.contains("-javaagent:test-agent1.jar"));
Assert.assertTrue("Missing --server=test", commands.contains("--server=test"));
assertTrue(commands.stream().anyMatch(entry -> entry.matches("-javaagent:.*jboss-modules.jar$")), "Missing jboss-modules.jar");
assertTrue(commands.contains("-javaagent:test-agent1.jar"), "Missing test-agent1.jar");
assertTrue(commands.contains("--server=test"), "Missing --server=test");

// If we're using Java 9+ ensure the modular JDK options were added
testModularJvmArguments(commands, 1);
Expand All @@ -68,14 +72,14 @@ public void testJBossModulesBuilder() {
count++;
}
}
Assert.assertEquals("There should be only one java.net.preferIPv4Stack system property", 1, count);
assertEquals(1, count, "There should be only one java.net.preferIPv4Stack system property");

// The value saved should be the last value added
Assert.assertTrue("java.net.preferIPv4Stack should be set to false", commandBuilder.getJavaOptions().contains("-Djava.net.preferIPv4Stack=false"));
assertTrue(commandBuilder.getJavaOptions().contains("-Djava.net.preferIPv4Stack=false"), "java.net.preferIPv4Stack should be set to false");
}

@Test
public void testStandaloneBuilder() {
void standaloneBuilder() {
// Set up a standalone command builder
final StandaloneCommandBuilder commandBuilder = StandaloneCommandBuilder.of(WILDFLY_HOME)
.setAdminOnly()
Expand All @@ -91,20 +95,20 @@ public void testStandaloneBuilder() {
// Get all the commands
List<String> commands = commandBuilder.buildArguments();

Assert.assertTrue("--admin-only is missing", commands.contains("--admin-only"));
assertTrue(commands.contains("--admin-only"), "--admin-only is missing");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("-b=0.0.0.0"));
assertTrue(commands.contains("-b=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("-bmanagement=0.0.0.0"));
assertTrue(commands.contains("-bmanagement=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing debug argument", commands.contains(String.format(StandaloneCommandBuilder.DEBUG_FORMAT, "y", 5005)));
assertTrue(commands.contains(String.format(StandaloneCommandBuilder.DEBUG_FORMAT, "y", 5005)), "Missing debug argument");

Assert.assertTrue("Missing server configuration file override", commands.contains("-c=standalone-full.xml"));
assertTrue(commands.contains("-c=standalone-full.xml"), "Missing server configuration file override");

Assert.assertTrue("Missing -secmgr option", commands.contains("-secmgr"));
assertTrue(commands.contains("-secmgr"), "Missing -secmgr option");

Assert.assertTrue("Missing jboss-modules.jar", commands.stream().anyMatch(entry -> entry.matches("-javaagent:.*jboss-modules.jar$")));
Assert.assertTrue("Missing test-agent1.jar", commands.contains("-javaagent:test-agent1.jar"));
assertTrue(commands.stream().anyMatch(entry -> entry.matches("-javaagent:.*jboss-modules.jar$")), "Missing jboss-modules.jar");
assertTrue(commands.contains("-javaagent:test-agent1.jar"), "Missing test-agent1.jar");

// If we're using Java 9+ ensure the modular JDK options were added
testModularJvmArguments(commands, 1);
Expand All @@ -116,19 +120,19 @@ public void testStandaloneBuilder() {
count++;
}
}
Assert.assertEquals("There should be only one java.net.preferIPv4Stack system property", 1, count);
assertEquals(1, count, "There should be only one java.net.preferIPv4Stack system property");

// The value saved should be the last value added
Assert.assertTrue("java.net.preferIPv4Stack should be set to false", commandBuilder.getJavaOptions().contains("-Djava.net.preferIPv4Stack=false"));
assertTrue(commandBuilder.getJavaOptions().contains("-Djava.net.preferIPv4Stack=false"), "java.net.preferIPv4Stack should be set to false");

// Rename the binding address
commandBuilder.setBindAddressHint(null);
commands = commandBuilder.buildArguments();
Assert.assertFalse("Binding address should have been removed", commands.contains("-b=0.0.0.0"));
assertFalse(commands.contains("-b=0.0.0.0"), "Binding address should have been removed");
}

@Test
public void testBootableJarBuilder() {
void bootableJarBuilder() {
// Set up a bootable command builder
final BootableJarCommandBuilder commandBuilder = BootableJarCommandBuilder.of(WILDFLY_BOOTABLE_JAR)
.setInstallDir(Paths.get("foo"))
Expand All @@ -145,15 +149,15 @@ public void testBootableJarBuilder() {
// Get all the commands
List<String> commands = commandBuilder.buildArguments();

Assert.assertTrue("--install-dir is missing", commands.contains("--install-dir=bar"));
assertTrue(commands.contains("--install-dir=bar"), "--install-dir is missing");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("-b=0.0.0.0"));
assertTrue(commands.contains("-b=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("-bmanagement=0.0.0.0"));
assertTrue(commands.contains("-bmanagement=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing debug argument", commands.contains(String.format(StandaloneCommandBuilder.DEBUG_FORMAT, "y", 5005)));
assertTrue(commands.contains(String.format(StandaloneCommandBuilder.DEBUG_FORMAT, "y", 5005)), "Missing debug argument");

Assert.assertTrue("--yaml is missing", commands.contains("--yaml=" + Path.of("dummy.yml").toFile().getAbsolutePath()));
assertTrue(commands.contains("--yaml=" + Path.of("dummy.yml").toFile().getAbsolutePath()), "--yaml is missing");

// If we're using Java 12+. the enhanced security manager option must be set.
testEnhancedSecurityManager(commands, 1);
Expand All @@ -166,7 +170,7 @@ public void testBootableJarBuilder() {
count++;
}
}
Assert.assertEquals("There should be only one java.net.preferIPv4Stack system property", 1, count);
assertEquals(1, count, "There should be only one java.net.preferIPv4Stack system property");

// Install dir should be added once.
count = 0L;
Expand All @@ -175,7 +179,7 @@ public void testBootableJarBuilder() {
count++;
}
}
Assert.assertEquals("There should be only one --install-dir", 1, count);
assertEquals(1, count, "There should be only one --install-dir");

// Install dir should be added once.
count = 0L;
Expand All @@ -184,16 +188,16 @@ public void testBootableJarBuilder() {
count++;
}
}
Assert.assertEquals("There should be only one --yaml", 1, count);
assertEquals(1, count, "There should be only one --yaml");

// Rename the binding address
commandBuilder.setBindAddressHint(null);
commands = commandBuilder.buildArguments();
Assert.assertFalse("Binding address should have been removed", commands.contains("-b=0.0.0.0"));
assertFalse(commands.contains("-b=0.0.0.0"), "Binding address should have been removed");
}

@Test
public void testDomainBuilder() {
void domainBuilder() {
// Set up a standalone command builder
final DomainCommandBuilder commandBuilder = DomainCommandBuilder.of(WILDFLY_HOME)
.setAdminOnly()
Expand All @@ -207,29 +211,29 @@ public void testDomainBuilder() {
// Get all the commands
List<String> commands = commandBuilder.buildArguments();

Assert.assertTrue("--admin-only is missing", commands.contains("--admin-only"));
assertTrue(commands.contains("--admin-only"), "--admin-only is missing");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("-b=0.0.0.0"));
assertTrue(commands.contains("-b=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("--primary-address=0.0.0.0"));
assertTrue(commands.contains("--primary-address=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing -b=0.0.0.0", commands.contains("-bmanagement=0.0.0.0"));
assertTrue(commands.contains("-bmanagement=0.0.0.0"), "Missing -b=0.0.0.0");

Assert.assertTrue("Missing server configuration file override", commands.contains("-c=domain.xml"));
assertTrue(commands.contains("-c=domain.xml"), "Missing server configuration file override");

Assert.assertTrue("Missing -secmgr option", commands.contains("-secmgr"));
assertTrue(commands.contains("-secmgr"), "Missing -secmgr option");

// If we're using Java 9+ ensure the modular JDK options were added
testModularJvmArguments(commands, 2);

// Rename the binding address
commandBuilder.setBindAddressHint(null);
commands = commandBuilder.buildArguments();
Assert.assertFalse("Binding address should have been removed", commands.contains("-b=0.0.0.0"));
assertFalse(commands.contains("-b=0.0.0.0"), "Binding address should have been removed");
}

@Test
public void testCliBuilder() {
void cliBuilder() {
// Set up a standalone command builder
final CliCommandBuilder commandBuilder = CliCommandBuilder.asModularLauncher(WILDFLY_HOME)
.addJavaOption("-Djava.net.preferIPv4Stack=true")
Expand All @@ -248,14 +252,14 @@ public void testCliBuilder() {
count++;
}
}
Assert.assertEquals("There should be only one java.net.preferIPv4Stack system property", 1, count);
assertEquals(1, count, "There should be only one java.net.preferIPv4Stack system property");

// The value saved should be the last value added
Assert.assertTrue("java.net.preferIPv4Stack should be set to false", commandBuilder.getJavaOptions().contains("-Djava.net.preferIPv4Stack=false"));
assertTrue(commandBuilder.getJavaOptions().contains("-Djava.net.preferIPv4Stack=false"), "java.net.preferIPv4Stack should be set to false");
}

@Test
public void testArguments() {
void arguments() {
final Arguments arguments = new Arguments();
arguments.add("-Dkey=value");
arguments.add("-X");
Expand All @@ -266,34 +270,34 @@ public void testArguments() {

// Validate the arguments
Iterator<Argument> iter = arguments.getArguments("key").iterator();
Assert.assertTrue("Missing 'key' entry", iter.hasNext());
Assert.assertEquals("value", arguments.get("key"));
Assert.assertEquals("-Dkey=value", iter.next().asCommandLineArgument());
assertTrue(iter.hasNext(), "Missing 'key' entry");
assertEquals("value", arguments.get("key"));
assertEquals("-Dkey=value", iter.next().asCommandLineArgument());

// -X should have been added twice
Assert.assertEquals(2, arguments.getArguments("-X").size());
assertEquals(2, arguments.getArguments("-X").size());

// Using set should only add the value once
Assert.assertEquals("Should not be more than one 'single-key' argument", 1, arguments.getArguments("single-key").size());
assertEquals(1, arguments.getArguments("single-key").size(), "Should not be more than one 'single-key' argument");

// Convert the arguments to a list and ensure each entry has been added in the format expected
final List<String> stringArgs = arguments.asList();
Assert.assertEquals(7, stringArgs.size());
Assert.assertTrue("Missing -Dkey=value", stringArgs.contains("-Dkey=value"));
Assert.assertTrue("Missing -X", stringArgs.contains("-X"));
Assert.assertTrue("Missing single-key=single-value", stringArgs.contains("single-key=single-value"));
Assert.assertTrue("Missing -Dprop1=value1", stringArgs.contains("-Dprop1=value1"));
Assert.assertTrue("Missing -Dprop2=value2", stringArgs.contains("-Dprop2=value2"));
Assert.assertTrue("Missing -Dprop3=value3", stringArgs.contains("-Dprop3=value3"));
assertEquals(7, stringArgs.size());
assertTrue(stringArgs.contains("-Dkey=value"), "Missing -Dkey=value");
assertTrue(stringArgs.contains("-X"), "Missing -X");
assertTrue(stringArgs.contains("single-key=single-value"), "Missing single-key=single-value");
assertTrue(stringArgs.contains("-Dprop1=value1"), "Missing -Dprop1=value1");
assertTrue(stringArgs.contains("-Dprop2=value2"), "Missing -Dprop2=value2");
assertTrue(stringArgs.contains("-Dprop3=value3"), "Missing -Dprop3=value3");
}

private void testEnhancedSecurityManager(final Collection<String> command, final int expectedCount) {
// If we're using Java 12+ ensure enhanced security manager option was added
if (Jvm.current().enhancedSecurityManagerAvailable()) {
assertArgumentExists(command, "-Djava.security.manager=allow", expectedCount);
} else {
Assert.assertFalse("Did not expect \"-Djava.security.manager=allow\" to be in the command list",
command.contains("-Djava.security.manager=allow"));
assertFalse(command.contains("-Djava.security.manager=allow"),
"Did not expect \"-Djava.security.manager=allow\" to be in the command list");
}
}

Expand Down Expand Up @@ -337,7 +341,7 @@ private static void assertArgumentExists(final Collection<String> args, final St
count++;
}
}
Assert.assertEquals(String.format("Expected %d %s arguments, found %d", expectedCount, arg, count), expectedCount, count);
assertEquals(expectedCount, count, String.format("Expected %d %s arguments, found %d", expectedCount, arg, count));
}

}
Loading

0 comments on commit 2f56ff6

Please sign in to comment.