From 07a2c60c75fdeb1e3f7c81f1f5dcaeddc7b622fc Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Fri, 13 Sep 2024 21:23:57 +0200 Subject: [PATCH] Bug 14: Added tests for storage command classes --- build.gradle | 6 + nbproject/project.properties | 0 .../storage/command/StorageCommandLine.java | 6 +- .../command/BaseStorageCommandTest.java | 55 +++++++ .../StorageCommandLineScannerTest.java | 60 +++++++ .../command/StorageCommandLineTest.java | 152 ++++++++++++++++++ .../command/StorageCommandResultTest.java | 80 +++++++++ .../storage/command/StorageCommandTest.java | 61 +++++++ .../api/storage/map/SimpleJavaMapTest.java | 92 +++++++++++ 9 files changed, 509 insertions(+), 3 deletions(-) create mode 100644 nbproject/project.properties create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/command/BaseStorageCommandTest.java create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineScannerTest.java create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineTest.java create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandResultTest.java create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandTest.java create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/map/SimpleJavaMapTest.java diff --git a/build.gradle b/build.gradle index 06a8c03..fd7f5bc 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,9 @@ dependencies { compileOnly "org.projectlombok:lombok:$lombokVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.3" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.3" + testImplementation 'org.mockito:mockito-core:5.5.0' + testImplementation 'org.mockito:mockito-junit-jupiter:3.6.0' + } sourceCompatibility = '11' @@ -38,6 +41,9 @@ tasks.withType(JavaCompile) { test { useJUnitPlatform() + testLogging { + events "passed", "skipped", "failed" + } } eclipse { diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/com/pixelgamelibrary/api/storage/command/StorageCommandLine.java b/src/main/java/com/pixelgamelibrary/api/storage/command/StorageCommandLine.java index 8bf7d0b..cafe8e7 100644 --- a/src/main/java/com/pixelgamelibrary/api/storage/command/StorageCommandLine.java +++ b/src/main/java/com/pixelgamelibrary/api/storage/command/StorageCommandLine.java @@ -42,7 +42,7 @@ public class StorageCommandLine { private String hostname; // Hostname for the command line private Storage storage; // Storage object for interacting with files private boolean exited = false; // Indicates if the command line session has been exited - private long startNanoTime = System.nanoTime(); // Start time of the session in nanoseconds + long startNanoTime = System.nanoTime(); // Start time of the session in nanoseconds /** * Returns the command line prompt string. @@ -60,7 +60,7 @@ public class StorageCommandLine { * @param argumentIndex the index of the argument to extract * @return the extracted argument */ - private String extractArgument(String arguments, int argumentIndex) { + String extractArgument(String arguments, int argumentIndex) { if(arguments.isEmpty()) { return arguments; } @@ -98,7 +98,7 @@ public class StorageCommandLine { addCommand("hostname", arguments -> provideOutput(result -> result.setOutput(hostname))); addCommand("test", arguments-> provideOutput(result-> result.setOutput((extractArgument(arguments, 0))))); addCommand("uname", arguments -> provideOutput(result -> result.setOutput( - "LinuxBashCommandLinePartialEmulation" + "LinuxBashCommandLinePartialEmulation " + ((extractArgument(arguments, 0).equals("-a")) ? (hostname + " 0.0.0 (" + new Date().toString() + ")") diff --git a/src/test/java/com/pixelgamelibrary/api/storage/command/BaseStorageCommandTest.java b/src/test/java/com/pixelgamelibrary/api/storage/command/BaseStorageCommandTest.java new file mode 100644 index 0000000..0ec8e0d --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/command/BaseStorageCommandTest.java @@ -0,0 +1,55 @@ +package com.pixelgamelibrary.api.storage.command; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.function.Function; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class BaseStorageCommandTest { + + private StorageCommandLine mockCommandLine; + private StorageCommandResult mockCommandResult; + private Function mockFunction; + private BaseStorageCommand command; + + @BeforeEach + void setUp() { + mockCommandLine = mock(StorageCommandLine.class); + mockCommandResult = mock(StorageCommandResult.class); + mockFunction = mock(Function.class); + + // When the function is applied, return the mock result + when(mockFunction.apply(anyString())).thenReturn(mockCommandResult); + + // Create an instance of BaseStorageCommand with mocks + command = new BaseStorageCommand(mockCommandLine, "testCommand", mockFunction); + } + + @Test + void testGetName() { + assertEquals("testCommand", command.getName()); + } + + @Test + void testGetStorageCommandLine() { + assertEquals(mockCommandLine, command.getStorageCommandLine()); + } + + @Test + void testExecute() { + StorageCommandResult result = command.execute("testCommand arg1"); + assertEquals(mockCommandResult, result); + verify(mockFunction).apply("testCommand arg1"); + } + + @Test + void testSetStorageCommandLine() { + StorageCommandLine newCommandLine = mock(StorageCommandLine.class); + command.setStorageCommandLine(newCommandLine); + assertEquals(newCommandLine, command.getStorageCommandLine()); + } +} diff --git a/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineScannerTest.java b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineScannerTest.java new file mode 100644 index 0000000..1a7e4f8 --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineScannerTest.java @@ -0,0 +1,60 @@ +package com.pixelgamelibrary.api.storage.command; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class StorageCommandLineScannerTest { + + @Mock + private StorageCommandLine mockStorageCommandLine; // Mocking StorageCommandLine + + @Mock + private CommandLineScanner mockCommandLineScanner; // Mocking CommandLineScanner + + private StorageCommandLineScanner storageCommandLineScanner; // Manually creating the instance + + @BeforeEach + void setUp() { + // Initialize mock objects before each test + MockitoAnnotations.openMocks(this); + } + + @Test + void testExecuteSimpleCommand() { + // Arrange + when(mockStorageCommandLine.getCommandLineStart()).thenReturn(">"); + when(mockCommandLineScanner.nextLine()).thenReturn("someCommand"); + StorageCommandResult mockResult = new StorageCommandResult("Command Executed"); + when(mockStorageCommandLine.execute("someCommand")).thenReturn(mockResult); + when(mockStorageCommandLine.isExited()).thenReturn(true); + + // Manually create the instance of StorageCommandLineScanner with mocks + storageCommandLineScanner = new StorageCommandLineScanner(mockStorageCommandLine, mockCommandLineScanner); + + // Assert: Verify that the expected methods were called + verify(mockStorageCommandLine).getCommandLineStart(); + verify(mockStorageCommandLine).execute("someCommand"); + } + + @Test + void testErrorOutput() { + // Arrange + when(mockStorageCommandLine.getCommandLineStart()).thenReturn(">"); + when(mockCommandLineScanner.nextLine()).thenReturn("invalidCommand"); + StorageCommandResult errorResult = new StorageCommandResult("Error occurred", true); + when(mockStorageCommandLine.execute("invalidCommand")).thenReturn(errorResult); + when(mockStorageCommandLine.isExited()).thenReturn(true); + + // Manually create the instance of StorageCommandLineScanner with mocks + storageCommandLineScanner = new StorageCommandLineScanner(mockStorageCommandLine, mockCommandLineScanner); + + // Assert: Verify the error output + verify(mockStorageCommandLine).execute("invalidCommand"); + assertTrue(errorResult.isError(), "The result should indicate an error"); + } +} diff --git a/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineTest.java b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineTest.java new file mode 100644 index 0000000..165b609 --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandLineTest.java @@ -0,0 +1,152 @@ +package com.pixelgamelibrary.api.storage.command; + +import com.pixelgamelibrary.api.storage.Storage; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class StorageCommandLineTest { + + private StorageCommandLine commandLine; + private Storage mockStorage; + + @BeforeEach + void setUp() { + mockStorage = mock(Storage.class); + when(mockStorage.pwd()).thenReturn("/mock/path"); + when(mockStorage.ls()).thenReturn(Arrays.asList("file1.txt", "file2.txt")); + commandLine = new StorageCommandLine("user", "hostname", mockStorage); + } + + @Test + void testGetCommandLineStart() { + assertEquals("user@hostname:/mock/path$ ", commandLine.getCommandLineStart()); + } + + @Test + void testExtractArgument() { + String result = commandLine.extractArgument("cmd arg1 arg2", 1); + assertEquals("arg2", result); + } + + @Test + void testExecuteDateCommand() { + StorageCommandResult result = commandLine.execute("date"); + assertEquals(new Date().toString(), result.getOutput().trim()); + } + + @Test + void testExecuteWhoamiCommand() { + StorageCommandResult result = commandLine.execute("whoami"); + assertEquals("user", result.getOutput().trim()); + } + + @Test + void testExecuteUptimeCommand() { + // Assuming uptime command result should be similar to "HH:MM up X minutes, 1 user" + String expectedOutput = new Date().toString().substring(11, 19) + " up " + + (System.nanoTime() - commandLine.startNanoTime) / 1000000000L / 60L + + " minutes, 1 user"; + StorageCommandResult result = commandLine.execute("uptime"); + assertEquals(expectedOutput, result.getOutput().trim()); + } + + @Test + void testExecuteHostnameCommand() { + StorageCommandResult result = commandLine.execute("hostname"); + assertEquals("hostname", result.getOutput().trim()); + } + + @Test + void testExecuteTestCommand() { + StorageCommandResult result = commandLine.execute("test arg1"); + assertEquals("arg1", result.getOutput().trim()); + } + + @Test + void testExecuteUnameCommand() { + StorageCommandResult result = commandLine.execute("uname -a"); + assertEquals("LinuxBashCommandLinePartialEmulation hostname 0.0.0 (" + + new Date().toString() + ")", result.getOutput().trim()); + } + + @Test + void testExecuteLsCommand() { + StorageCommandResult result = commandLine.execute("ls"); + assertEquals("file1.txt\nfile2.txt", result.getOutput().trim()); + } + + @Test + void testExecutePwdCommand() { + StorageCommandResult result = commandLine.execute("pwd"); + assertEquals("/mock/path", result.getOutput().trim()); + } + + @Test + void testExecuteDepthCommand() { + when(mockStorage.depth()).thenReturn(4); + StorageCommandResult result = commandLine.execute("depth"); + assertEquals(4, Integer.valueOf(result.getOutput().trim())); + } + + @Test + void testExecuteMkdirCommand() { + when(mockStorage.mkdirmore(any())).thenReturn(""); + StorageCommandResult result = commandLine.execute("mkdir newDir"); + assertEquals("New directory was successfully created", result.getOutput().trim()); + } + + @Test + void testExecuteCdCommand() { + when(mockStorage.cd(any())).thenReturn(""); + StorageCommandResult result = commandLine.execute("cd newDir"); + assertEquals("Changing working directory was successfully created", result.getOutput().trim()); + } + + @Test + void testExecuteTouchCommand() { + when(mockStorage.touch(any())).thenReturn(""); + StorageCommandResult result = commandLine.execute("touch newFile.txt"); + assertEquals("New file was successfully created", result.getOutput().trim()); + } + + @Test + void testExecuteReadtextCommand() { + when(mockStorage.readtext(any())).thenReturn("file content"); + StorageCommandResult result = commandLine.execute("readtext file.txt"); + assertEquals("Text file was successfully loaded\n\nfile content", result.getOutput().trim()); + } + + @Test + void testExecuteSavetextCommand() { + when(mockStorage.savetext(any(), any())).thenReturn(""); + StorageCommandResult result = commandLine.execute("savetext file.txt content"); + assertEquals("Text file was successfully saved", result.getOutput().trim()); + } + + @Test + void testExecuteDebugCommand() { + when(mockStorage.debug()).thenReturn("debug info"); + StorageCommandResult result = commandLine.execute("debug"); + assertEquals("debug info", result.getOutput().trim()); + } + + @Test + void testExecuteExitCommand() { + StorageCommandResult result = commandLine.execute("exit"); + assertTrue(commandLine.isExited()); + assertEquals("Exited", result.getOutput().trim()); + } + + @Test + void testExecuteUnsupportedCommand() { + StorageCommandResult result = commandLine.execute("unsupported"); + assertEquals("Unsupported command: unsupported", result.getOutput().trim()); + } +} diff --git a/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandResultTest.java b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandResultTest.java new file mode 100644 index 0000000..5702905 --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandResultTest.java @@ -0,0 +1,80 @@ +package com.pixelgamelibrary.api.storage.command; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class StorageCommandResultTest { + + @Test + void testDefaultConstructor() { + // Test default constructor which initializes an empty result + StorageCommandResult result = new StorageCommandResult(); + + assertNotNull(result.getOutput(), "Output should not be null"); + assertEquals("", result.getOutput(), "Default output should be empty"); + assertFalse(result.isError(), "Default result should not indicate an error"); + } + + @Test + void testConstructorWithOutput() { + // Test constructor that initializes with an output string + String expectedOutput = "Test Output"; + StorageCommandResult result = new StorageCommandResult(expectedOutput); + + assertEquals(expectedOutput, result.getOutput(), "Output should match the provided string"); + assertFalse(result.isError(), "Result should not indicate an error by default"); + } + + @Test + void testConstructorWithOutputAndErrorFlag() { + // Test constructor that initializes with output and error flag + String expectedOutput = "Error Output"; + StorageCommandResult result = new StorageCommandResult(expectedOutput, true); + + assertEquals(expectedOutput, result.getOutput(), "Output should match the provided string"); + assertTrue(result.isError(), "Error flag should be set to true"); + } + + @Test + void testSetOutputString() { + // Test setting the output using a string + StorageCommandResult result = new StorageCommandResult(); + String expectedOutput = "New Output"; + result.setOutput(expectedOutput); + + assertEquals(expectedOutput, result.getOutput(), "Output should be updated correctly"); + } + + @Test + void testSetOutputInt() { + // Test setting the output using an integer + StorageCommandResult result = new StorageCommandResult(); + int expectedOutput = 12345; + result.setOutput(expectedOutput); + + assertEquals(String.valueOf(expectedOutput), result.getOutput(), "Output should match the integer converted to string"); + } + + @Test + void testSetErrorOutput() { + // Test setting an error output and marking the result as an error + StorageCommandResult result = new StorageCommandResult(); + String errorOutput = "Error occurred"; + result.setErrorOutput(errorOutput); + + assertEquals(errorOutput, result.getOutput(), "Error output should be set correctly"); + assertTrue(result.isError(), "Error flag should be set to true after setting error output"); + } + + @Test + void testSetErrorFlag() { + // Test setting the error flag directly + StorageCommandResult result = new StorageCommandResult(); + result.setError(true); + + assertTrue(result.isError(), "Error flag should be set to true"); + + result.setError(false); + assertFalse(result.isError(), "Error flag should be set to false"); + } +} diff --git a/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandTest.java b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandTest.java new file mode 100644 index 0000000..f5ae93c --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/command/StorageCommandTest.java @@ -0,0 +1,61 @@ +package com.pixelgamelibrary.api.storage.command; + +import java.util.function.Function; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class StorageCommandTest { + + private StorageCommand mockCommand; + private StorageCommandLine mockCommandLine; + private StorageCommandResult mockCommandResult; + private Function mockFunction; + + @BeforeEach + void setUp() { + mockCommandLine = mock(StorageCommandLine.class); + mockCommandResult = mock(StorageCommandResult.class); + mockFunction = mock(Function.class); + + // Mock the behavior of execute method + when(mockFunction.apply(anyString())).thenReturn(mockCommandResult); + + // Create an instance of BaseStorageCommand with mocks + mockCommand = new BaseStorageCommand(mockCommandLine, "testCommand", mockFunction); + } + + @Test + void testGetName() { + assertEquals("testCommand", mockCommand.getName()); + } + + @Test + void testGetStorageCommandLine() { + assertEquals(mockCommandLine, mockCommand.getStorageCommandLine()); + } + + @Test + void testSetStorageCommandLine() { + StorageCommandLine newCommandLine = mock(StorageCommandLine.class); + mockCommand.setStorageCommandLine(newCommandLine); + assertEquals(newCommandLine, mockCommand.getStorageCommandLine()); + } + + @Test + void testExecute() { + StorageCommandResult result = mockCommand.execute("testCommand arg1"); + assertEquals(mockCommandResult, result); + verify(mockFunction).apply("testCommand arg1"); + } + + @Test + void testEmptyNewResult() { + StorageCommandResult result = StorageCommand.emptyNewResult(); + assertNotNull(result); + assertTrue(result instanceof StorageCommandResult); + } +} diff --git a/src/test/java/com/pixelgamelibrary/api/storage/map/SimpleJavaMapTest.java b/src/test/java/com/pixelgamelibrary/api/storage/map/SimpleJavaMapTest.java new file mode 100644 index 0000000..9507ae7 --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/map/SimpleJavaMapTest.java @@ -0,0 +1,92 @@ +package com.pixelgamelibrary.api.storage.map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +public class SimpleJavaMapTest { + + private SimpleJavaMap simpleMap; + + @BeforeEach + void setUp() { + simpleMap = new SimpleJavaMap(); + } + + @Test + void testPutAndGetString() { + simpleMap.putString("key1", "value1"); + assertEquals("value1", simpleMap.getString("key1")); + } + + @Test + void testPutAndGetStringWithDefault() { + simpleMap.putString("key1", "value1"); + assertEquals("value1", simpleMap.getString("key1", "default")); + assertEquals("default", simpleMap.getString("key2", "default")); + } + + @Test + void testPutMultiple() { + Map map = new HashMap<>(); + map.put("key1", "value1"); + map.put("key2", "value2"); + simpleMap.put(map); + + assertEquals("value1", simpleMap.getString("key1")); + assertEquals("value2", simpleMap.getString("key2")); + } + + @Test + void testGetReadOnlyMap() { + simpleMap.putString("key1", "value1"); + Map readOnlyMap = simpleMap.getReadOnlyMap(); + assertEquals("value1", readOnlyMap.get("key1")); + + // Attempting to modify the read-only map should throw UnsupportedOperationException + assertThrows(UnsupportedOperationException.class, () -> readOnlyMap.put("key2", "value2")); + } + + @Test + void testContains() { + simpleMap.putString("key1", "value1"); + assertTrue(simpleMap.contains("key1")); + assertFalse(simpleMap.contains("key2")); + } + + @Test + void testClear() { + simpleMap.putString("key1", "value1"); + simpleMap.clear(); + assertFalse(simpleMap.contains("key1")); + assertNull(simpleMap.getString("key1")); + } + + @Test + void testRemove() { + simpleMap.putString("key1", "value1"); + simpleMap.remove("key1"); + assertFalse(simpleMap.contains("key1")); + assertNull(simpleMap.getString("key1")); + } + + @Test + void testFlush() { + // No-op test since flush does nothing + simpleMap.flush(); + // No assertions needed, just checking no exception is thrown + } + + @Test + void testKeyList() { + simpleMap.putString("key1", "value1"); + simpleMap.putString("key2", "value2"); + assertTrue(simpleMap.keyList().contains("key1")); + assertTrue(simpleMap.keyList().contains("key2")); + assertFalse(simpleMap.keyList().contains("key3")); + } +}