WalletTool: fix --date option

Also adds tests for the `create` action.
This commit is contained in:
Sean Gilligan 2023-05-24 08:53:09 -07:00 committed by Andreas Schildbach
parent d65aefd93e
commit b8bfbcc658
2 changed files with 31 additions and 1 deletions

View file

@ -86,6 +86,7 @@ import java.text.ParseException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
@ -1213,7 +1214,7 @@ public class WalletTool implements Callable<Integer> {
if (unixtime != null)
return Optional.of(Instant.ofEpochSecond(unixtime));
else if (date != null)
return Optional.of(Instant.from(date));
return Optional.of(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
else
return Optional.empty();
}

View file

@ -17,8 +17,11 @@
package org.bitcoinj.wallettool;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import picocli.CommandLine;
import java.io.File;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -26,6 +29,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
* Basic functional/integration tests of {@code wallet-tool}
*/
public class WalletToolTest {
@TempDir
File tempDir;
@Test
void canConstruct() {
@ -55,6 +60,30 @@ public class WalletToolTest {
assertEquals(0, exitCode);
}
@Test
void createNoFileSpecified() {
int exitCode = execute("create");
// TODO: currently a stacktrace, give user-friendly error
assertEquals(1, exitCode);
}
@Test
void createMinimal(@TempDir File tempDir) {
String walletFile = tempDir.getPath() + "/wallet";
int exitCode = execute("create", "--wallet", walletFile);
assertEquals(0, exitCode);
}
@Test
void createWithDate(@TempDir File tempDir) {
String walletFile = tempDir.getPath() + "/wallet";
String date = "2023-05-01";
int exitCode = execute("create", "--wallet", walletFile, "--date", date);
assertEquals(0, exitCode);
}
/**
* Run the wallet-tool via {@link CommandLine#execute(String...)}
* @param args command-line arguments