diff --git a/besu/build.gradle b/besu/build.gradle index fe5bb45c30d..0b31c4a43c2 100644 --- a/besu/build.gradle +++ b/besu/build.gradle @@ -99,10 +99,8 @@ dependencies { testImplementation 'org.awaitility:awaitility' testImplementation 'org.junit.jupiter:junit-jupiter' testImplementation 'org.mockito:mockito-core' - testImplementation 'org.mockito:mockito-junit-jupiter' testImplementation 'org.testcontainers:testcontainers' testImplementation 'tech.pegasys.discovery:discovery' - testRuntimeOnly 'org.junit.vintage:junit-vintage-engine' annotationProcessor 'com.google.dagger:dagger-compiler' } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 8f004d494e1..b65217c5b15 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -127,19 +127,18 @@ import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.toml.Toml; import org.apache.tuweni.toml.TomlParseResult; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class BesuCommandTest extends CommandTestAbstract { private static final String ENCLAVE_URI = "http://1.2.3.4:5555"; @@ -198,7 +197,7 @@ public class BesuCommandTest extends CommandTestAbstract { DEFAULT_API_CONFIGURATION = ImmutableApiConfiguration.builder().build(); } - @Before + @BeforeEach public void setup() { try { // optimistically tear down a potential previous loaded trusted setup @@ -210,7 +209,7 @@ public void setup() { MergeConfigOptions.setMergeEnabled(false); } - @After + @AfterEach public void tearDown() { MergeConfigOptions.setMergeEnabled(false); @@ -366,10 +365,10 @@ public void callingWithConfigOptionButInvalidValueTomlFileShouldDisplayHelp() th } @Test - public void overrideDefaultValuesIfKeyIsPresentInConfigFile() throws IOException { + public void overrideDefaultValuesIfKeyIsPresentInConfigFile(final @TempDir File dataFolder) + throws IOException { final URL configFile = this.getClass().getResource("/complete_config.toml"); final Path genesisFile = createFakeGenesisFile(GENESIS_VALID_JSON); - final File dataFolder = temp.newFolder(); final String updatedConfig = Resources.toString(configFile, UTF_8) .replace("/opt/besu/genesis.json", escapeTomlString(genesisFile.toString())) @@ -3745,7 +3744,7 @@ public void colorCanBeEnabledOrDisabledExplicitly() { }); } - @Ignore + @Disabled public void pruningIsEnabledIfSyncModeIsFast() { parseCommand("--sync-mode", "FAST"); @@ -3756,7 +3755,7 @@ public void pruningIsEnabledIfSyncModeIsFast() { assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } - @Ignore + @Disabled public void pruningIsDisabledIfSyncModeIsFull() { parseCommand("--sync-mode", "FULL"); @@ -3778,7 +3777,7 @@ public void pruningEnabledExplicitly() { assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } - @Ignore + @Disabled public void pruningDisabledExplicitly() { parseCommand("--pruning-enabled=false", "--sync-mode=FAST"); @@ -4373,14 +4372,11 @@ public void privacyWithPruningMustError() { assertThat(commandOutput.toString(UTF_8)).isEmpty(); } - @Rule public TemporaryFolder testFolder = new TemporaryFolder(); - @Test - public void errorIsRaisedIfStaticNodesAreNotAllowed() throws IOException { - final File staticNodesFile = testFolder.newFile("static-nodes.json"); - staticNodesFile.deleteOnExit(); - final File permissioningConfig = testFolder.newFile("permissioning"); - permissioningConfig.deleteOnExit(); + public void errorIsRaisedIfStaticNodesAreNotAllowed(final @TempDir Path testFolder) + throws IOException { + final Path staticNodesFile = testFolder.resolve("static-nodes.json"); + final Path permissioningConfig = testFolder.resolve("permissioning.json"); final EnodeURL staticNodeURI = EnodeURLImpl.builder() @@ -4399,17 +4395,16 @@ public void errorIsRaisedIfStaticNodesAreNotAllowed() throws IOException { .listeningPort(30304) .build(); + Files.write(staticNodesFile, ("[\"" + staticNodeURI.toString() + "\"]").getBytes(UTF_8)); Files.write( - staticNodesFile.toPath(), ("[\"" + staticNodeURI.toString() + "\"]").getBytes(UTF_8)); - Files.write( - permissioningConfig.toPath(), + permissioningConfig, ("nodes-allowlist=[\"" + allowedNode.toString() + "\"]").getBytes(UTF_8)); parseCommand( - "--data-path=" + testFolder.getRoot().getPath(), + "--data-path=" + testFolder, "--bootnodes", "--permissions-nodes-config-file-enabled=true", - "--permissions-nodes-config-file=" + permissioningConfig.getPath()); + "--permissions-nodes-config-file=" + permissioningConfig); assertThat(commandErrorOutput.toString(UTF_8)) .contains(staticNodeURI.toString(), "not in nodes-allowlist"); } @@ -4985,12 +4980,10 @@ public void pkiBlockCreationPasswordFileRequired() { "File containing password to unlock keystore is required when PKI Block Creation is enabled"); } - @Rule public TemporaryFolder pkiTempFolder = new TemporaryFolder(); - @Test - public void pkiBlockCreationFullConfig() throws Exception { + public void pkiBlockCreationFullConfig(final @TempDir Path pkiTempFolder) throws Exception { // Create temp file with password - final File pwdFile = pkiTempFolder.newFile("pwd"); + final File pwdFile = pkiTempFolder.resolve("pwd").toFile(); FileUtils.writeStringToFile(pwdFile, "foo", UTF_8); parseCommand( diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index 8597c0582df..e9d8d559641 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -112,22 +112,20 @@ import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; import org.awaitility.Awaitility; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import picocli.CommandLine; import picocli.CommandLine.Model.CommandSpec; import picocli.CommandLine.RunLast; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public abstract class CommandTestAbstract { private static final Logger TEST_LOGGER = LoggerFactory.getLogger(CommandTestAbstract.class); protected static final JsonObject VALID_GENESIS_QBFT_POST_LONDON = @@ -156,17 +154,30 @@ public abstract class CommandTestAbstract { protected static final RpcEndpointServiceImpl rpcEndpointServiceImpl = new RpcEndpointServiceImpl(); - @Mock protected RunnerBuilder mockRunnerBuilder; + @Mock(lenient = true) + protected RunnerBuilder mockRunnerBuilder; + @Mock protected Runner mockRunner; - @Mock protected BesuController.Builder mockControllerBuilderFactory; + @Mock(lenient = true) + protected BesuController.Builder mockControllerBuilderFactory; + + @Mock(lenient = true) + protected BesuControllerBuilder mockControllerBuilder; + + @Mock(lenient = true) + protected EthProtocolManager mockEthProtocolManager; - @Mock protected BesuControllerBuilder mockControllerBuilder; - @Mock protected EthProtocolManager mockEthProtocolManager; @Mock protected ProtocolSchedule mockProtocolSchedule; - @Mock protected ProtocolContext mockProtocolContext; + + @Mock(lenient = true) + protected ProtocolContext mockProtocolContext; + @Mock protected BlockBroadcaster mockBlockBroadcaster; - @Mock protected BesuController mockController; + + @Mock(lenient = true) + protected BesuController mockController; + @Mock protected RlpBlockExporter rlpBlockExporter; @Mock protected JsonBlockImporter jsonBlockImporter; @Mock protected RlpBlockImporter rlpBlockImporter; @@ -188,7 +199,8 @@ public abstract class CommandTestAbstract { @Mock protected Logger mockLogger; - @Mock protected BesuComponent mockBesuComponent; + @Mock(lenient = true) + protected BesuComponent mockBesuComponent; @Mock protected PkiBlockCreationConfigurationProvider mockPkiBlockCreationConfigProvider; @Mock protected PkiBlockCreationConfiguration mockPkiBlockCreationConfiguration; @@ -218,9 +230,7 @@ public abstract class CommandTestAbstract { @Captor protected ArgumentCaptor ethstatsOptionsArgumentCaptor; - @Rule public final TemporaryFolder temp = new TemporaryFolder(); - - @Before + @BeforeEach public void initMocks() throws Exception { // doReturn used because of generic BesuController doReturn(mockControllerBuilder) @@ -353,7 +363,7 @@ public void initMocks() throws Exception { when(mockBesuComponent.getBesuCommandLogger()).thenReturn(mockLogger); } - @Before + @BeforeEach public void setUpStreams() { // reset the global opentelemetry singleton GlobalOpenTelemetry.resetForTest(); @@ -364,7 +374,7 @@ public void setUpStreams() { } // Display outputs for debug purpose - @After + @AfterEach public void displayOutput() throws IOException { TEST_LOGGER.info("Standard output {}", commandOutput.toString(UTF_8)); TEST_LOGGER.info("Standard error {}", commandErrorOutput.toString(UTF_8)); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java index 62d35830c0b..5d661f92038 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java @@ -19,12 +19,12 @@ import org.hyperledger.besu.BesuInfo; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine.Model.CommandSpec; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class PasswordSubCommandTest extends CommandTestAbstract { @Test diff --git a/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java index 3da574e8e32..d2ca00990a0 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java @@ -35,14 +35,14 @@ import org.bouncycastle.asn1.sec.SECNamedCurves; import org.bouncycastle.asn1.x9.X9ECParameters; import org.bouncycastle.crypto.params.ECDomainParameters; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine.Model.CommandSpec; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class PublicKeySubCommandTest extends CommandTestAbstract { private static final String EXPECTED_PUBLIC_KEY_USAGE = @@ -126,13 +126,13 @@ public class PublicKeySubCommandTest extends CommandTestAbstract { private static final String ALGORITHM = SignatureAlgorithm.ALGORITHM; private static ECDomainParameters curve; - @BeforeClass + @BeforeAll public static void setUp() { final X9ECParameters params = SECNamedCurves.getByName(CURVE_NAME); curve = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); } - @Before + @BeforeEach public void before() { SignatureAlgorithmFactory.resetInstance(); } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/ValidateConfigSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/ValidateConfigSubCommandTest.java index dc8d0548e0c..ac6efbf405b 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/ValidateConfigSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/ValidateConfigSubCommandTest.java @@ -25,12 +25,12 @@ import java.nio.file.Path; import com.google.common.io.Resources; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine.Model.CommandSpec; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class ValidateConfigSubCommandTest extends CommandTestAbstract { private static final String EXPECTED_PUBLIC_KEY_USAGE = diff --git a/besu/src/test/java/org/hyperledger/besu/cli/operator/OperatorSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/operator/OperatorSubCommandTest.java index 414412ebb4a..ac1ec2e5777 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/operator/OperatorSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/operator/OperatorSubCommandTest.java @@ -52,13 +52,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.vertx.core.json.JsonObject; import org.apache.tuweni.bytes.Bytes32; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine.Model.CommandSpec; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class OperatorSubCommandTest extends CommandTestAbstract { private static final String EXPECTED_OPERATOR_USAGE = @@ -80,7 +80,7 @@ public class OperatorSubCommandTest extends CommandTestAbstract { private Path tmpOutputDirectoryPath; - @Before + @BeforeEach public void init() throws IOException { SignatureAlgorithmFactory.resetInstance(); tmpOutputDirectoryPath = createTempDirectory(format("output-%d", currentTimeMillis())); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/AbstractCLIOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/AbstractCLIOptionsTest.java index 09b4e3b1365..35dcaa4baee 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/AbstractCLIOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/AbstractCLIOptionsTest.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.function.Consumer; -import org.junit.Test; +import org.junit.jupiter.api.Test; public abstract class AbstractCLIOptionsTest> extends CommandTestAbstract { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/EthProtocolOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/EthProtocolOptionsTest.java index 9d1d696ab43..6f7aa637cdd 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/EthProtocolOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/EthProtocolOptionsTest.java @@ -21,11 +21,11 @@ import org.hyperledger.besu.cli.options.unstable.EthProtocolOptions; import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class EthProtocolOptionsTest extends AbstractCLIOptionsTest { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/MetricsCLIOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/MetricsCLIOptionsTest.java index 8044770a9b4..e88462216f4 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/MetricsCLIOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/MetricsCLIOptionsTest.java @@ -17,10 +17,10 @@ import org.hyperledger.besu.cli.options.unstable.MetricsCLIOptions; import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class MetricsCLIOptionsTest extends AbstractCLIOptionsTest { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/MiningOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/MiningOptionsTest.java index 0b7cc5e1224..404c021e3b9 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/MiningOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/MiningOptionsTest.java @@ -35,12 +35,12 @@ import java.util.Optional; import org.apache.tuweni.bytes.Bytes; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class MiningOptionsTest extends AbstractCLIOptionsTest { @Test diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/NetworkingOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/NetworkingOptionsTest.java index a2cbdc5942b..4b4601bb9ca 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/NetworkingOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/NetworkingOptionsTest.java @@ -23,11 +23,11 @@ import java.util.Arrays; import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class NetworkingOptionsTest extends AbstractCLIOptionsTest { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/SynchronizerOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/SynchronizerOptionsTest.java index 0e814a07e26..3b1a9e6839a 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/SynchronizerOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/SynchronizerOptionsTest.java @@ -23,10 +23,10 @@ import java.util.List; import com.google.common.collect.Range; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class SynchronizerOptionsTest extends AbstractCLIOptionsTest { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java index d7031eb9a4f..00d4efc3b9f 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java @@ -28,11 +28,11 @@ import java.time.Duration; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class TransactionPoolOptionsTest extends AbstractCLIOptionsTest { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/options/stable/DataStorageOptionsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/options/stable/DataStorageOptionsTest.java index 5b4e0228550..437053afd56 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/options/stable/DataStorageOptionsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/options/stable/DataStorageOptionsTest.java @@ -23,7 +23,7 @@ import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat; import org.hyperledger.besu.ethereum.worldstate.ImmutableDataStorageConfiguration; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class DataStorageOptionsTest extends AbstractCLIOptionsTest { diff --git a/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java index 822c7322718..3eca54df252 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java @@ -24,11 +24,12 @@ import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import picocli.CommandLine.Model.CommandSpec; public class RLPSubCommandTest extends CommandTestAbstract { @@ -158,10 +159,10 @@ public void encodeWithOutputFileMustWriteInThisFile() throws Exception { } @Test - public void encodeWithInputFilePathMustReadFromThisFile() throws Exception { - - final File tempJsonFile = temp.newFile("test.json"); - try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempJsonFile.toPath(), UTF_8)) { + public void encodeWithInputFilePathMustReadFromThisFile(final @TempDir Path dir) + throws Exception { + final Path tempJsonFile = Files.createTempFile(dir, "input", "json"); + try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempJsonFile, UTF_8)) { fileWriter.write( "[\"be068f726a13c8d46c44be6ce9d275600e1735a4\", \"5ff6f4b66a46a2b2310a6f3a93aaddc0d9a1c193\"]"); @@ -169,7 +170,10 @@ public void encodeWithInputFilePathMustReadFromThisFile() throws Exception { fileWriter.flush(); parseCommand( - RLP_SUBCOMMAND_NAME, RLP_ENCODE_SUBCOMMAND_NAME, "--from", tempJsonFile.getPath()); + RLP_SUBCOMMAND_NAME, + RLP_ENCODE_SUBCOMMAND_NAME, + "--from", + tempJsonFile.toFile().getAbsolutePath()); final String expectedRlpString = "0xf853a00000000000000000000000000000000000000000000000000000000000000000ea94be068f726a13c8d" @@ -180,9 +184,9 @@ public void encodeWithInputFilePathMustReadFromThisFile() throws Exception { } @Test - public void canEncodeToQbftExtraData() throws IOException { - final File tempJsonFile = temp.newFile("test.json"); - try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempJsonFile.toPath(), UTF_8)) { + public void canEncodeToQbftExtraData(final @TempDir Path dir) throws Exception { + final Path tempJsonFile = Files.createTempFile(dir, "qbft", "json"); + try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempJsonFile, UTF_8)) { fileWriter.write( "[\"be068f726a13c8d46c44be6ce9d275600e1735a4\", \"5ff6f4b66a46a2b2310a6f3a93aaddc0d9a1c193\"]"); @@ -193,7 +197,7 @@ public void canEncodeToQbftExtraData() throws IOException { RLP_SUBCOMMAND_NAME, RLP_ENCODE_SUBCOMMAND_NAME, "--from", - tempJsonFile.getPath(), + tempJsonFile.toFile().getAbsolutePath(), "--type", RLP_QBFT_TYPE); @@ -206,17 +210,19 @@ public void canEncodeToQbftExtraData() throws IOException { } @Test - public void encodeWithInvalidInputMustRaiseAnError() throws Exception { - - final File tempJsonFile = temp.newFile("invalid_test.json"); - try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempJsonFile.toPath(), UTF_8)) { + public void encodeWithInvalidInputMustRaiseAnError(final @TempDir Path dir) throws Exception { + final Path tempJsonFile = Files.createTempFile(dir, "invalid", "json"); + try (final BufferedWriter fileWriter = Files.newBufferedWriter(tempJsonFile, UTF_8)) { fileWriter.write("{\"property\":0}"); fileWriter.flush(); parseCommand( - RLP_SUBCOMMAND_NAME, RLP_ENCODE_SUBCOMMAND_NAME, "--from", tempJsonFile.getPath()); + RLP_SUBCOMMAND_NAME, + RLP_ENCODE_SUBCOMMAND_NAME, + "--from", + tempJsonFile.toFile().getAbsolutePath()); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)) @@ -226,11 +232,13 @@ public void encodeWithInvalidInputMustRaiseAnError() throws Exception { } @Test - public void encodeWithEmptyInputMustRaiseAnError() throws Exception { - - final File tempJsonFile = temp.newFile("empty.json"); - - parseCommand(RLP_SUBCOMMAND_NAME, RLP_ENCODE_SUBCOMMAND_NAME, "--from", tempJsonFile.getPath()); + public void encodeWithEmptyInputMustRaiseAnError(final @TempDir Path dir) throws Exception { + final Path emptyFile = Files.createTempFile(dir, "empty", "json"); + parseCommand( + RLP_SUBCOMMAND_NAME, + RLP_ENCODE_SUBCOMMAND_NAME, + "--from", + emptyFile.toFile().getAbsolutePath()); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)) @@ -251,7 +259,7 @@ public void encodeWithEmptyStdInputMustRaiseAnError() throws Exception { .startsWith("An error occurred while trying to read the JSON data."); } - @After + @AfterEach public void restoreStdin() { System.setIn(System.in); } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java index 5c62076e692..29f519f8911 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java @@ -36,17 +36,16 @@ import java.nio.file.Paths; import java.util.Optional; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine.Model.CommandSpec; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class BlocksSubCommandTest extends CommandTestAbstract { - @Rule public final TemporaryFolder folder = new TemporaryFolder(); + @TempDir public Path folder; private static final String EXPECTED_BLOCK_USAGE = "Usage: besu blocks [-hV] [COMMAND]" @@ -187,10 +186,13 @@ public void callingBlockImportSubCommandVersionMustDisplayVersion() { } @Test - public void callingBlockImportSubCommandWithPathMustImportBlocksWithThisPath() throws Exception { - final File fileToImport = temp.newFile("blocks.file"); + public void callingBlockImportSubCommandWithPathMustImportBlocksWithThisPath( + final @TempDir File fileToImport) throws Exception { parseCommand( - BLOCK_SUBCOMMAND_NAME, BLOCK_IMPORT_SUBCOMMAND_NAME, "--from", fileToImport.getPath()); + BLOCK_SUBCOMMAND_NAME, + BLOCK_IMPORT_SUBCOMMAND_NAME, + "--from", + fileToImport.getAbsolutePath().toString()); verify(rlpBlockImporter) .importBlockchain(pathArgumentCaptor.capture(), any(), anyBoolean(), anyLong(), anyLong()); @@ -202,8 +204,7 @@ public void callingBlockImportSubCommandWithPathMustImportBlocksWithThisPath() t } @Test - public void blocksImport_rlpFormat() throws Exception { - final File fileToImport = temp.newFile("blocks.file"); + public void blocksImport_rlpFormat(final @TempDir File fileToImport) throws Exception { parseCommand( BLOCK_SUBCOMMAND_NAME, BLOCK_IMPORT_SUBCOMMAND_NAME, @@ -222,10 +223,11 @@ public void blocksImport_rlpFormat() throws Exception { } @Test - public void blocksImport_rlpFormatMultiple() throws Exception { - final File fileToImport = temp.newFile("blocks.file"); - final File file2ToImport = temp.newFile("blocks2.file"); - final File file3ToImport = temp.newFile("blocks3.file"); + public void blocksImport_rlpFormatMultiple( + final @TempDir File fileToImport, + final @TempDir File file2ToImport, + final @TempDir File file3ToImport) + throws Exception { parseCommand( BLOCK_SUBCOMMAND_NAME, BLOCK_IMPORT_SUBCOMMAND_NAME, @@ -247,10 +249,10 @@ public void blocksImport_rlpFormatMultiple() throws Exception { } @Test - public void blocksImport_jsonFormat() throws Exception { + public void blocksImport_jsonFormat(final @TempDir Path dir) throws Exception { final String fileContent = "test"; - final File fileToImport = temp.newFile("blocks.file"); - final Writer fileWriter = Files.newBufferedWriter(fileToImport.toPath(), UTF_8); + final Path fileToImport = Files.createTempFile(dir, "tmp", "json"); + final Writer fileWriter = Files.newBufferedWriter(fileToImport, UTF_8); fileWriter.write(fileContent); fileWriter.close(); @@ -260,7 +262,7 @@ public void blocksImport_jsonFormat() throws Exception { "--format", "JSON", "--from", - fileToImport.getPath()); + fileToImport.toFile().getAbsolutePath()); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -274,9 +276,7 @@ public void blocksImport_jsonFormat() throws Exception { public void blocksExport_missingFileParam() throws IOException { createDbDirectory(true); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), - BLOCK_SUBCOMMAND_NAME, - BLOCK_EXPORT_SUBCOMMAND_NAME); + "--data-path=" + folder.getRoot(), BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME); final String expectedErrorOutputStart = "Missing required option: '--to='"; assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).startsWith(expectedErrorOutputStart); @@ -285,17 +285,17 @@ public void blocksExport_missingFileParam() throws IOException { } @Test - public void blocksExport_noDbDirectory() throws IOException { - final File outputFile = folder.newFile("blocks.bin"); + public void blocksExport_noDbDirectory(final @TempDir Path noDbDir) throws IOException { + final File outputFile = noDbDir.resolve("blocks.bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + noDbDir, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", - outputFile.getPath()); + outputFile.getAbsolutePath()); final String expectedErrorOutputStart = "Chain is empty. Unable to export blocks from specified data directory: " - + folder.getRoot().getAbsolutePath() + + noDbDir + File.separator + BesuController.DATABASE_PATH; assertThat(commandOutput.toString(UTF_8)).isEmpty(); @@ -307,16 +307,16 @@ public void blocksExport_noDbDirectory() throws IOException { @Test public void blocksExport_emptyDbDirectory() throws IOException { createDbDirectory(false); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createFile(folder.resolve("empty")).toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", outputFile.getPath()); final String expectedErrorOutputStart = "Chain is empty. Unable to export blocks from specified data directory: " - + folder.getRoot().getAbsolutePath() + + folder + File.separator + BesuController.DATABASE_PATH; assertThat(commandOutput.toString(UTF_8)).isEmpty(); @@ -328,13 +328,13 @@ public void blocksExport_emptyDbDirectory() throws IOException { @Test public void blocksExport_noStartOrEnd() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", - outputFile.getPath()); + outputFile.getAbsolutePath()); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -344,9 +344,9 @@ public void blocksExport_noStartOrEnd() throws IOException { @Test public void blocksExport_withStartAndNoEnd() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createFile(folder.resolve("blocks.bin")).toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", @@ -361,9 +361,9 @@ public void blocksExport_withStartAndNoEnd() throws IOException { @Test public void blocksExport_withEndAndNoStart() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", @@ -378,13 +378,13 @@ public void blocksExport_withEndAndNoStart() throws IOException { @Test public void blocksExport_withStartAndEnd() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", - outputFile.getPath(), + outputFile.getAbsolutePath(), "--start-block=1", "--end-block=10"); assertThat(commandOutput.toString(UTF_8)).isEmpty(); @@ -396,13 +396,13 @@ public void blocksExport_withStartAndEnd() throws IOException { @Test public void blocksExport_withOutOfOrderStartAndEnd() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", - outputFile.getPath(), + outputFile.getAbsolutePath(), "--start-block=10", "--end-block=1"); assertThat(commandErrorOutput.toString(UTF_8)) @@ -415,9 +415,9 @@ public void blocksExport_withOutOfOrderStartAndEnd() throws IOException { @Test public void blocksExport_withEmptyRange() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder, BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", @@ -434,9 +434,9 @@ public void blocksExport_withEmptyRange() throws IOException { @Test public void blocksExport_withInvalidStart() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder.getRoot(), BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", @@ -452,9 +452,9 @@ public void blocksExport_withInvalidStart() throws IOException { @Test public void blocksExport_withInvalidEnd() throws IOException { createDbDirectory(true); - final File outputFile = folder.newFile("blocks.bin"); + final File outputFile = Files.createTempFile(folder, "blocks", "bin").toFile(); parseCommand( - "--data-path=" + folder.getRoot().getAbsolutePath(), + "--data-path=" + folder.getRoot(), BLOCK_SUBCOMMAND_NAME, BLOCK_EXPORT_SUBCOMMAND_NAME, "--to", @@ -482,17 +482,18 @@ public void callingBlockExportSubCommandVersionMustDisplayVersion() { } private void createDbDirectory(final boolean createDataFiles) throws IOException { - final File dbDir = folder.newFolder(BesuController.DATABASE_PATH); + final Path dbDir = Files.createDirectory(folder.resolve(BesuController.DATABASE_PATH)); + if (createDataFiles) { - final Path dataFilePath = Paths.get(dbDir.getAbsolutePath(), "0000001.sst"); - final boolean success = new File(dataFilePath.toString()).createNewFile(); + final Path dataFilePath = Paths.get(dbDir.toFile().getAbsolutePath(), "0000001.sst"); + final boolean success = new File(dataFilePath.toFile().getAbsolutePath()).createNewFile(); assertThat(success).isTrue(); } } @Test - public void blocksImportWithNoSyncModeDoesNotRaiseNPE() throws IOException { - final File fileToImport = temp.newFile("blocks.file"); + public void blocksImportWithNoSyncModeDoesNotRaiseNPE(final @TempDir File fileToImport) + throws IOException { parseCommand( BLOCK_SUBCOMMAND_NAME, BLOCK_IMPORT_SUBCOMMAND_NAME, "--from", fileToImport.getPath()); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/storage/StorageSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/storage/StorageSubCommandTest.java index e3b67d8b430..3ca4f41aaf8 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/storage/StorageSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/storage/StorageSubCommandTest.java @@ -36,11 +36,11 @@ import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.Silent.class) +@ExtendWith(MockitoExtension.class) public class StorageSubCommandTest extends CommandTestAbstract { @Test