[MINOR] CLI Subcommands migrate to junit 5 (#6282)

* updated cli and chainimport/export tests to junit 5

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* subcommands and besucommand test to junit5

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* removed duplicate line

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* removed vintage junit dep

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Co-authored-by: Fabio Di Fabio <fabio.difabio@consensys.net>
pull/6309/head
Sally MacFarlane 11 months ago committed by GitHub
parent 4cbe7b0693
commit c9a41fa3e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      besu/build.gradle
  2. 57
      besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
  3. 48
      besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java
  4. 8
      besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java
  5. 16
      besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java
  6. 8
      besu/src/test/java/org/hyperledger/besu/cli/ValidateConfigSubCommandTest.java
  7. 12
      besu/src/test/java/org/hyperledger/besu/cli/operator/OperatorSubCommandTest.java
  8. 2
      besu/src/test/java/org/hyperledger/besu/cli/options/AbstractCLIOptionsTest.java
  9. 8
      besu/src/test/java/org/hyperledger/besu/cli/options/EthProtocolOptionsTest.java
  10. 6
      besu/src/test/java/org/hyperledger/besu/cli/options/MetricsCLIOptionsTest.java
  11. 8
      besu/src/test/java/org/hyperledger/besu/cli/options/MiningOptionsTest.java
  12. 8
      besu/src/test/java/org/hyperledger/besu/cli/options/NetworkingOptionsTest.java
  13. 6
      besu/src/test/java/org/hyperledger/besu/cli/options/SynchronizerOptionsTest.java
  14. 8
      besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java
  15. 2
      besu/src/test/java/org/hyperledger/besu/cli/options/stable/DataStorageOptionsTest.java
  16. 54
      besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java
  17. 111
      besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java
  18. 8
      besu/src/test/java/org/hyperledger/besu/cli/subcommands/storage/StorageSubCommandTest.java

@ -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'
}

@ -127,19 +127,18 @@ import org.apache.commons.text.StringEscapeUtils;
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 class BesuCommandTest extends CommandTestAbstract {
MergeConfigOptions.setMergeEnabled(false);
}
@After
@AfterEach
public void tearDown() {
MergeConfigOptions.setMergeEnabled(false);
@ -366,10 +365,10 @@ public class BesuCommandTest extends CommandTestAbstract {
}
@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 class BesuCommandTest extends CommandTestAbstract {
});
}
@Ignore
@Disabled
public void pruningIsEnabledIfSyncModeIsFast() {
parseCommand("--sync-mode", "FAST");
@ -3756,7 +3755,7 @@ public class BesuCommandTest extends CommandTestAbstract {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
@Ignore
@Disabled
public void pruningIsDisabledIfSyncModeIsFull() {
parseCommand("--sync-mode", "FULL");
@ -3778,7 +3777,7 @@ public class BesuCommandTest extends CommandTestAbstract {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
@Ignore
@Disabled
public void pruningDisabledExplicitly() {
parseCommand("--pruning-enabled=false", "--sync-mode=FAST");
@ -4373,14 +4372,11 @@ public class BesuCommandTest extends CommandTestAbstract {
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 class BesuCommandTest extends CommandTestAbstract {
.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 class BesuCommandTest extends CommandTestAbstract {
"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(

@ -112,22 +112,20 @@ import io.vertx.core.json.JsonObject;
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<EthstatsOptions> 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 abstract class CommandTestAbstract {
when(mockBesuComponent.getBesuCommandLogger()).thenReturn(mockLogger);
}
@Before
@BeforeEach
public void setUpStreams() {
// reset the global opentelemetry singleton
GlobalOpenTelemetry.resetForTest();
@ -364,7 +374,7 @@ public abstract class CommandTestAbstract {
}
// 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));

@ -19,12 +19,12 @@ import static org.assertj.core.api.Assertions.assertThat;
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

@ -35,14 +35,14 @@ import org.apache.tuweni.bytes.Bytes32;
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();
}

@ -25,12 +25,12 @@ import java.nio.file.Files;
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 =

@ -52,13 +52,13 @@ import com.fasterxml.jackson.databind.JsonNode;
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()));

@ -26,7 +26,7 @@ import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public abstract class AbstractCLIOptionsTest<D, T extends CLIOptions<D>>
extends CommandTestAbstract {

@ -21,11 +21,11 @@ import static org.mockito.Mockito.verifyNoInteractions;
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<EthProtocolConfiguration, EthProtocolOptions> {

@ -17,10 +17,10 @@ package org.hyperledger.besu.cli.options;
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<MetricsConfiguration.Builder, MetricsCLIOptions> {

@ -35,12 +35,12 @@ import java.nio.file.Path;
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<MiningParameters, MiningOptions> {
@Test

@ -23,11 +23,11 @@ import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration;
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<NetworkingConfiguration, NetworkingOptions> {

@ -23,10 +23,10 @@ import java.util.Arrays;
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<SynchronizerConfiguration.Builder, SynchronizerOptions> {

@ -28,11 +28,11 @@ import org.hyperledger.besu.util.number.Percentage;
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<TransactionPoolConfiguration, TransactionPoolOptions> {

@ -23,7 +23,7 @@ import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
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<DataStorageConfiguration, DataStorageOptions> {

@ -24,11 +24,12 @@ import org.hyperledger.besu.cli.CommandTestAbstract;
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 class RLPSubCommandTest extends CommandTestAbstract {
}
@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 class RLPSubCommandTest extends CommandTestAbstract {
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 class RLPSubCommandTest extends CommandTestAbstract {
}
@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 class RLPSubCommandTest extends CommandTestAbstract {
RLP_SUBCOMMAND_NAME,
RLP_ENCODE_SUBCOMMAND_NAME,
"--from",
tempJsonFile.getPath(),
tempJsonFile.toFile().getAbsolutePath(),
"--type",
RLP_QBFT_TYPE);
@ -206,17 +210,19 @@ public class RLPSubCommandTest extends CommandTestAbstract {
}
@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 class RLPSubCommandTest extends CommandTestAbstract {
}
@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 class RLPSubCommandTest extends CommandTestAbstract {
.startsWith("An error occurred while trying to read the JSON data.");
}
@After
@AfterEach
public void restoreStdin() {
System.setIn(System.in);
}

@ -36,17 +36,16 @@ import java.nio.file.Path;
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 class BlocksSubCommandTest extends CommandTestAbstract {
}
@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 class BlocksSubCommandTest extends CommandTestAbstract {
}
@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 class BlocksSubCommandTest extends CommandTestAbstract {
}
@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 class BlocksSubCommandTest extends CommandTestAbstract {
}
@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 class BlocksSubCommandTest extends CommandTestAbstract {
"--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 class BlocksSubCommandTest extends CommandTestAbstract {
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=<FILE>'";
assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).startsWith(expectedErrorOutputStart);
@ -285,17 +285,17 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
@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 class BlocksSubCommandTest extends CommandTestAbstract {
}
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());

@ -36,11 +36,11 @@ import org.hyperledger.besu.services.kvstore.SegmentedKeyValueStorageAdapter;
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

Loading…
Cancel
Save