Bug 14: Added tests for storage command classes
This commit is contained in:
parent
552bbc6274
commit
07a2c60c75
@ -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 {
|
||||
|
0
nbproject/project.properties
Normal file
0
nbproject/project.properties
Normal file
@ -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() + ")")
|
||||
|
@ -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<String, StorageCommandResult> 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());
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
@ -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<String, StorageCommandResult> 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);
|
||||
}
|
||||
}
|
@ -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<String, String> 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<String, String> 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"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user