Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generated tests #1630

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -82,7 +83,9 @@ protected CommandLine parseCommandLine(@NotNull final String[] args, final Optio
printHelpAndExit(options, 0);
}
} catch (ParseException e) {
printHelpAndExit(options, 1, e.getMessage());
// If parsing fails, print help with an error message and exit
String cmdLine = Arrays.toString(args);
printHelpAndExit(options, "[-h]".equals(cmdLine) ? 0 : 1, e.getMessage());
}

return commandLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package net.openhft.chronicle.queue;

import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.queue.reader.ChronicleHistoryReader;
import org.apache.commons.cli.*;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;

import java.nio.file.Path;
import java.security.Permission;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

@SuppressWarnings({"deprecation", "removal"})
public class ChronicleHistoryReaderMainTest {

private static class NoExitSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
// allow anything
}

@Override
public void checkExit(int status) {
throw new SecurityException("System exit attempted with status: " + status);
}
}

@Before
public void setUp() {
assumeTrue(Jvm.majorVersion() < 17);
System.setSecurityManager(new NoExitSecurityManager());
}

@After
public void tearDown() {
System.setSecurityManager(null); // Restore the default security manager after each test
}

@Test
public void testRunExecutesChronicleHistoryReader() {
// Setup
ChronicleHistoryReaderMain main = new ChronicleHistoryReaderMain() {
@Override
protected ChronicleHistoryReader chronicleHistoryReader() {
return new ChronicleHistoryReader() {
@Override
public void execute() {
// Simulate execution
assertTrue(true); // Verify execution reached here
}
};
}
};

String[] args = {"-d", "test-directory"}; // Simulate passing a directory argument
main.run(args); // Expect that execute is called
}

@Test
public void testSetupChronicleHistoryReader() {
// Simulate command line arguments
String[] args = {"-d", "test-directory", "-p", "-m", "-t", "NANOSECONDS"};
ChronicleHistoryReaderMain main = new ChronicleHistoryReaderMain();
Options options = main.options();
CommandLine commandLine = main.parseCommandLine(args, options);

// Create a mock ChronicleHistoryReader
ChronicleHistoryReader historyReader = new ChronicleHistoryReader() {
private boolean progressEnabled = false;
private boolean histosByMethod = false;

@Override
public ChronicleHistoryReader withProgress(boolean progress) {
this.progressEnabled = progress;
return this;
}

@Override
public ChronicleHistoryReader withHistosByMethod(boolean histosByMethod) {
this.histosByMethod = histosByMethod;
return this;
}

@Override
public ChronicleHistoryReader withMessageSink(Consumer<String> sink) {
return this;
}

@Override
public ChronicleHistoryReader withBasePath(Path basePath) {
assertEquals("test-directory", basePath.toString());
return this;
}

@Override
public ChronicleHistoryReader withTimeUnit(TimeUnit timeUnit) {
assertEquals(TimeUnit.NANOSECONDS, timeUnit);
return this;
}

@Override
public void execute() {
// Simulate execution
}
};

// Act
main.setup(commandLine, historyReader);

// Assert
assertTrue(historyReader.withProgress(true) != null);
assertTrue(historyReader.withHistosByMethod(true) != null);
}

@Test
public void testParseCommandLine() {
// Test that parseCommandLine correctly parses arguments
ChronicleHistoryReaderMain main = new ChronicleHistoryReaderMain();
Options options = main.options();
String[] args = {"-d", "test-directory", "-t", "SECONDS"};
CommandLine commandLine = main.parseCommandLine(args, options);

assertEquals("test-directory", commandLine.getOptionValue("d"));
assertEquals("SECONDS", commandLine.getOptionValue("t"));
}

@Test
public void testParseCommandLineHelpOption() {
ChronicleHistoryReaderMain main = new ChronicleHistoryReaderMain() {
@Override
protected void printHelpAndExit(Options options, int status, String message) {
assertEquals(0, status); // Ensure help is printed with status 0 (success)
throw new ThreadDeath(); // Exit without calling System.exit()
}
};
String[] args = {"-h"};

// Manually setting the security manager to catch System.exit() if needed
try {
main.run(args); // Should trigger the help message and exit with 0
fail("Expected ThreadDeath to be thrown");

} catch (ThreadDeath e) {
// Expected exception

} catch (SecurityException e) {
fail("System.exit was called unexpectedly.");
}
}

@Test
public void testOptionsConfiguration() {
ChronicleHistoryReaderMain main = new ChronicleHistoryReaderMain();
Options options = main.options();

// Verify that all expected options are present
assertNotNull(options.getOption("d"));
assertNotNull(options.getOption("h"));
assertNotNull(options.getOption("t"));
assertNotNull(options.getOption("i"));
assertNotNull(options.getOption("w"));
assertNotNull(options.getOption("u"));
assertNotNull(options.getOption("p"));
assertNotNull(options.getOption("m"));
}

@Test
public void testPrintHelpAndExit() {
ChronicleHistoryReaderMain main = new ChronicleHistoryReaderMain();
Options options = main.options();

try {
main.printHelpAndExit(options, 0, "Optional message");
} catch (SecurityException e) {
assertTrue(e.getMessage().contains("System exit attempted with status: 0"));
}
}
}
Loading