@ -36,17 +36,16 @@ import java.nio.file.Path;
import java.nio.file.Paths ;
import java.nio.file.Paths ;
import java.util.Optional ;
import java.util.Optional ;
import org.junit.Rule ;
import org.junit.jupiter.api.Test ;
import org.junit.Test ;
import org.junit.jupiter.api.extension.ExtendWith ;
import org.junit.rules.TemporaryFolder ;
import org.junit.jupiter.api.io.TempDir ;
import org.junit.runner.RunWith ;
import org.mockito.junit.jupiter.MockitoExtension ;
import org.mockito.junit.MockitoJUnitRunner ;
import picocli.CommandLine.Model.CommandSpec ;
import picocli.CommandLine.Model.CommandSpec ;
@RunWith ( MockitoJUnitRunner . class )
@ExtendWith ( MockitoExtension . class )
public class BlocksSubCommandTest extends CommandTestAbstract {
public class BlocksSubCommandTest extends CommandTestAbstract {
@Rule public final TemporaryFolder folder = new TemporaryFolder ( ) ;
@TempDir public Path folder ;
private static final String EXPECTED_BLOCK_USAGE =
private static final String EXPECTED_BLOCK_USAGE =
"Usage: besu blocks [-hV] [COMMAND]"
"Usage: besu blocks [-hV] [COMMAND]"
@ -187,10 +186,13 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
}
@Test
@Test
public void callingBlockImportSubCommandWithPathMustImportBlocksWithThisPath ( ) throws Exception {
public void callingBlockImportSubCommandWithPathMustImportBlocksWithThisPath (
final File fileToImport = temp . newFile ( "blocks.file" ) ;
final @TempDir File fileToImport ) throws Exception {
parseCommand (
parseCommand (
BLOCK_SUBCOMMAND_NAME , BLOCK_IMPORT_SUBCOMMAND_NAME , "--from" , fileToImport . getPath ( ) ) ;
BLOCK_SUBCOMMAND_NAME ,
BLOCK_IMPORT_SUBCOMMAND_NAME ,
"--from" ,
fileToImport . getAbsolutePath ( ) . toString ( ) ) ;
verify ( rlpBlockImporter )
verify ( rlpBlockImporter )
. importBlockchain ( pathArgumentCaptor . capture ( ) , any ( ) , anyBoolean ( ) , anyLong ( ) , anyLong ( ) ) ;
. importBlockchain ( pathArgumentCaptor . capture ( ) , any ( ) , anyBoolean ( ) , anyLong ( ) , anyLong ( ) ) ;
@ -202,8 +204,7 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
}
@Test
@Test
public void blocksImport_rlpFormat ( ) throws Exception {
public void blocksImport_rlpFormat ( final @TempDir File fileToImport ) throws Exception {
final File fileToImport = temp . newFile ( "blocks.file" ) ;
parseCommand (
parseCommand (
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_IMPORT_SUBCOMMAND_NAME ,
BLOCK_IMPORT_SUBCOMMAND_NAME ,
@ -222,10 +223,11 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
}
@Test
@Test
public void blocksImport_rlpFormatMultiple ( ) throws Exception {
public void blocksImport_rlpFormatMultiple (
final File fileToImport = temp . newFile ( "blocks.file" ) ;
final @TempDir File fileToImport ,
final File file2ToImport = temp . newFile ( "blocks2.file" ) ;
final @TempDir File file2ToImport ,
final File file3ToImport = temp . newFile ( "blocks3.file" ) ;
final @TempDir File file3ToImport )
throws Exception {
parseCommand (
parseCommand (
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_IMPORT_SUBCOMMAND_NAME ,
BLOCK_IMPORT_SUBCOMMAND_NAME ,
@ -247,10 +249,10 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
}
@Test
@Test
public void blocksImport_jsonFormat ( ) throws Exception {
public void blocksImport_jsonFormat ( final @TempDir Path dir ) throws Exception {
final String fileContent = "test" ;
final String fileContent = "test" ;
final File fileToImport = temp . newFile ( "blocks.file ") ;
final Path fileToImport = Files . createTempFile ( dir , "tmp" , "json ") ;
final Writer fileWriter = Files . newBufferedWriter ( fileToImport . toPath ( ) , UTF_8 ) ;
final Writer fileWriter = Files . newBufferedWriter ( fileToImport , UTF_8 ) ;
fileWriter . write ( fileContent ) ;
fileWriter . write ( fileContent ) ;
fileWriter . close ( ) ;
fileWriter . close ( ) ;
@ -260,7 +262,7 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
"--format" ,
"--format" ,
"JSON" ,
"JSON" ,
"--from" ,
"--from" ,
fileToImport . getPath ( ) ) ;
fileToImport . toFile ( ) . getAbsolute Path ( ) ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandErrorOutput . 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 {
public void blocksExport_missingFileParam ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder . getRoot ( ) , BLOCK_SUBCOMMAND_NAME , BLOCK_EXPORT_SUBCOMMAND_NAME ) ;
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ) ;
final String expectedErrorOutputStart = "Missing required option: '--to=<FILE>'" ;
final String expectedErrorOutputStart = "Missing required option: '--to=<FILE>'" ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandErrorOutput . toString ( UTF_8 ) ) . startsWith ( expectedErrorOutputStart ) ;
assertThat ( commandErrorOutput . toString ( UTF_8 ) ) . startsWith ( expectedErrorOutputStart ) ;
@ -285,17 +285,17 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
}
@Test
@Test
public void blocksExport_noDbDirectory ( ) throws IOException {
public void blocksExport_noDbDirectory ( final @TempDir Path noDbDir ) throws IOException {
final File outputFile = folder . newFil e( "blocks.bin" ) ;
final File outputFile = noDbDir . resolv e( "blocks.bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + noDbDir ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
outputFile . getPath ( ) ) ;
outputFile . getAbsolute Path ( ) ) ;
final String expectedErrorOutputStart =
final String expectedErrorOutputStart =
"Chain is empty. Unable to export blocks from specified data directory: "
"Chain is empty. Unable to export blocks from specified data directory: "
+ folder . getRoot ( ) . getAbsolutePath ( )
+ noDbDir
+ File . separator
+ File . separator
+ BesuController . DATABASE_PATH ;
+ BesuController . DATABASE_PATH ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
@ -307,16 +307,16 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_emptyDbDirectory ( ) throws IOException {
public void blocksExport_emptyDbDirectory ( ) throws IOException {
createDbDirectory ( false ) ;
createDbDirectory ( false ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createFile ( folder . resolve ( "empty" ) ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
outputFile . getPath ( ) ) ;
outputFile . getPath ( ) ) ;
final String expectedErrorOutputStart =
final String expectedErrorOutputStart =
"Chain is empty. Unable to export blocks from specified data directory: "
"Chain is empty. Unable to export blocks from specified data directory: "
+ folder . getRoot ( ) . getAbsolutePath ( )
+ folder
+ File . separator
+ File . separator
+ BesuController . DATABASE_PATH ;
+ BesuController . DATABASE_PATH ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
@ -328,13 +328,13 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_noStartOrEnd ( ) throws IOException {
public void blocksExport_noStartOrEnd ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
outputFile . getPath ( ) ) ;
outputFile . getAbsolute Path ( ) ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandErrorOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandErrorOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
@ -344,9 +344,9 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withStartAndNoEnd ( ) throws IOException {
public void blocksExport_withStartAndNoEnd ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFil e( "blocks.bin" ) ;
final File outputFile = Files . createFile ( folder . resolv e( "blocks.bin" ) ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
@ -361,9 +361,9 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withEndAndNoStart ( ) throws IOException {
public void blocksExport_withEndAndNoStart ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
@ -378,13 +378,13 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withStartAndEnd ( ) throws IOException {
public void blocksExport_withStartAndEnd ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
outputFile . getPath ( ) ,
outputFile . getAbsolute Path ( ) ,
"--start-block=1" ,
"--start-block=1" ,
"--end-block=10" ) ;
"--end-block=10" ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
assertThat ( commandOutput . toString ( UTF_8 ) ) . isEmpty ( ) ;
@ -396,13 +396,13 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withOutOfOrderStartAndEnd ( ) throws IOException {
public void blocksExport_withOutOfOrderStartAndEnd ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
outputFile . getPath ( ) ,
outputFile . getAbsolute Path ( ) ,
"--start-block=10" ,
"--start-block=10" ,
"--end-block=1" ) ;
"--end-block=1" ) ;
assertThat ( commandErrorOutput . toString ( UTF_8 ) )
assertThat ( commandErrorOutput . toString ( UTF_8 ) )
@ -415,9 +415,9 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withEmptyRange ( ) throws IOException {
public void blocksExport_withEmptyRange ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
@ -434,9 +434,9 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withInvalidStart ( ) throws IOException {
public void blocksExport_withInvalidStart ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder . getRoot ( ) ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
@ -452,9 +452,9 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
@Test
@Test
public void blocksExport_withInvalidEnd ( ) throws IOException {
public void blocksExport_withInvalidEnd ( ) throws IOException {
createDbDirectory ( true ) ;
createDbDirectory ( true ) ;
final File outputFile = folder . newFile ( "blocks.bin" ) ;
final File outputFile = Files . createTempFile ( folder , "blocks" , "bin" ) . toFile ( ) ;
parseCommand (
parseCommand (
"--data-path=" + folder . getRoot ( ) . getAbsolutePath ( ) ,
"--data-path=" + folder . getRoot ( ) ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
BLOCK_EXPORT_SUBCOMMAND_NAME ,
"--to" ,
"--to" ,
@ -482,17 +482,18 @@ public class BlocksSubCommandTest extends CommandTestAbstract {
}
}
private void createDbDirectory ( final boolean createDataFiles ) throws IOException {
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 ) {
if ( createDataFiles ) {
final Path dataFilePath = Paths . get ( dbDir . getAbsolutePath ( ) , "0000001.sst" ) ;
final Path dataFilePath = Paths . get ( dbDir . toFile ( ) . getAbsolutePath ( ) , "0000001.sst" ) ;
final boolean success = new File ( dataFilePath . toString ( ) ) . createNewFile ( ) ;
final boolean success = new File ( dataFilePath . toFile ( ) . getAbsolutePath ( ) ) . createNewFile ( ) ;
assertThat ( success ) . isTrue ( ) ;
assertThat ( success ) . isTrue ( ) ;
}
}
}
}
@Test
@Test
public void blocksImportWithNoSyncModeDoesNotRaiseNPE ( ) throws IOException {
public void blocksImportWithNoSyncModeDoesNotRaiseNPE ( final @TempDir File fileToImport )
final File fileToImport = temp . newFile ( "blocks.file" ) ;
throws IOException {
parseCommand (
parseCommand (
BLOCK_SUBCOMMAND_NAME , BLOCK_IMPORT_SUBCOMMAND_NAME , "--from" , fileToImport . getPath ( ) ) ;
BLOCK_SUBCOMMAND_NAME , BLOCK_IMPORT_SUBCOMMAND_NAME , "--from" , fileToImport . getPath ( ) ) ;