Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardladenthin committed Mar 20, 2024
1 parent 84d2979 commit c70d718
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/net/ladenthin/bitcoinaddressfinder/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
// @formatter:on
package net.ladenthin.bitcoinaddressfinder.cli;

import com.google.common.annotations.VisibleForTesting;
import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import net.ladenthin.bitcoinaddressfinder.AddressFilesToLMDB;
import net.ladenthin.bitcoinaddressfinder.Finder;
import net.ladenthin.bitcoinaddressfinder.Interruptable;
Expand All @@ -40,7 +40,8 @@
// VM option: -Dorg.slf4j.simpleLogger.defaultLogLevel=trace
public class Main implements Runnable, Interruptable {

private final static Logger logger = LoggerFactory.getLogger(Main.class);
@VisibleForTesting
public static Logger logger = LoggerFactory.getLogger(Main.class);

private final List<Interruptable> interruptables = new ArrayList<>();

Expand Down Expand Up @@ -109,6 +110,23 @@ public void run() {
throw new UnsupportedOperationException("Command: " + configuration.command.name() + " currently not supported." );
}
logger.info("Main#run end.");

if(false) {
try {
Thread.sleep(2000L);
} catch (InterruptedException ex) {

}
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
for (Thread thread : threadSet) {
System.out.println("##################################################");
System.out.println("#thread: " + thread);
StackTraceElement[] stackTrace = thread.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
System.out.println(stackTraceElement);
}
}
}
}

private void addSchutdownHook() {
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/net/ladenthin/bitcoinaddressfinder/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import net.ladenthin.bitcoinaddressfinder.cli.Main;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import org.mockito.ArgumentCaptor;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.slf4j.Logger;

public class MainTest {

Expand Down Expand Up @@ -64,4 +74,23 @@ public void testOpenCLInfo_configurationGiven_noExceptionThrown() throws IOExcep
mainFind_SecretsFile.run();
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="invalidArgument">
@Test
public void main_noArgumentGiven_errorLogged() throws IOException, InterruptedException {
// arrange
Logger logger = mock(Logger.class);
when(logger.isTraceEnabled()).thenReturn(true);
Main.logger = logger;

// act
Main.main(new String[0]);

// assert
ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
List<String> arguments = logCaptor.getAllValues();
verify(logger, times(1)).error(logCaptor.capture());
assertThat(arguments.get(0), is(equalTo("Invalid arguments. Pass path to configuration as first argument.")));
}
// </editor-fold>
}

0 comments on commit c70d718

Please sign in to comment.