From 60a94ab1ecb78e4699793db54f8ac58b243fda60 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Fri, 13 Sep 2024 21:48:20 +0200 Subject: [PATCH] Bug 14: Added tests for class MapStorage --- .../api/storage/map/MapStorage.java | 3 + .../api/storage/map/MapStorageTest.java | 223 ++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 src/test/java/com/pixelgamelibrary/api/storage/map/MapStorageTest.java diff --git a/src/main/java/com/pixelgamelibrary/api/storage/map/MapStorage.java b/src/main/java/com/pixelgamelibrary/api/storage/map/MapStorage.java index 1a92bcf..bc9a4e0 100644 --- a/src/main/java/com/pixelgamelibrary/api/storage/map/MapStorage.java +++ b/src/main/java/com/pixelgamelibrary/api/storage/map/MapStorage.java @@ -348,6 +348,9 @@ public class MapStorage implements Storage { @Override public boolean isdir(String name) { + if(name.equals(SLASH)) { + return true; + } // Check if the path is a directory return filetype(name) == MapFileType.DIRECTORY; } diff --git a/src/test/java/com/pixelgamelibrary/api/storage/map/MapStorageTest.java b/src/test/java/com/pixelgamelibrary/api/storage/map/MapStorageTest.java new file mode 100644 index 0000000..889db6e --- /dev/null +++ b/src/test/java/com/pixelgamelibrary/api/storage/map/MapStorageTest.java @@ -0,0 +1,223 @@ +package com.pixelgamelibrary.api.storage.map; + +import com.pixelgamelibrary.api.Pixel; +import com.pixelgamelibrary.api.Platform; +import com.pixelgamelibrary.api.interfaces.AppI; +import com.pixelgamelibrary.api.interfaces.AssetI; +import com.pixelgamelibrary.api.interfaces.AudioI; +import com.pixelgamelibrary.api.interfaces.GraphicsI; +import com.pixelgamelibrary.api.interfaces.InputI; +import com.pixelgamelibrary.api.interfaces.NetI; +import com.pixelgamelibrary.api.interfaces.PixelBackend; +import com.pixelgamelibrary.api.interfaces.StorageI; +import com.pixelgamelibrary.api.interfaces.UtilsI; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +// Tests for MapStorage class +public class MapStorageTest { + + private SimpleMap mockMap; + private MapStorage mapStorage; + + @org.junit.jupiter.api.BeforeAll public static void setupStart() { + + PixelBackend dummyPixelBackend = new PixelBackend() { + @Override + public AppI app() { + return new AppI() { + @Override + public Platform getPlatform() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void exit() { + } + + @Override + public void log(String msg) { + + } + + @Override + public void error(String msg) { + + } + + @Override + public void debug(String msg) { + + } + + }; + } + + @Override + public GraphicsI graphics() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public AudioI audio() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public InputI input() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public NetI net() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public AssetI asset() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public StorageI storage() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public UtilsI utils() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + }; + Pixel.initBackend(dummyPixelBackend); + } + @BeforeEach + public void setup() { + mockMap = Mockito.mock(SimpleMap.class); // Mocking the SimpleMap class + mapStorage = new MapStorage(mockMap); // Initialize MapStorage with the mocked map + } + + @Test + public void testMkdirSuccess() { + when(mockMap.contains("/")).thenReturn(true); // Simulate no directory exists + when(mockMap.contains("/newDir")).thenReturn(false); // Simulate no directory exists + + String result = mapStorage.mkdir("/newDir"); + + assertEquals("", result); // Success should return an empty string + verify(mockMap).putString("/newDir", "DIRECTORY::::::::"); + } + + @Test + public void testMkdirAlreadyExists() { + when(mockMap.contains("/")).thenReturn(true); // Root irectory already exists + when(mockMap.contains("/newDir")).thenReturn(true); // Directory already exists + + String result = mapStorage.mkdir("/newDir"); + + assertEquals("Cannot create new directory, because path already exists: /newDir", result); + } + + @Test + public void testCdChangeDirectorySuccess() { + when(mockMap.contains("/")).thenReturn(true); + when(mockMap.contains("/newDir")).thenReturn(true); + when(mockMap.getString("/")).thenReturn("DIRECTORY::::::::"); + when(mockMap.getString("/newDir")).thenReturn("DIRECTORY::::::::"); + + String result = mapStorage.cd("/newDir"); + + assertEquals("", result); // Success should return an empty string + } + + @Test + public void testCdPathDoesNotExist() { + when(mockMap.contains("/nonExistent")).thenReturn(false); + + String result = mapStorage.cd("/nonExistent"); + + assertEquals("Path does not exist: /nonExistent", result); + } + + @Test + public void testTouchCreateFileSuccess() { + when(mockMap.contains("/")).thenReturn(true); // Root exists + when(mockMap.getString("/")).thenReturn("DIRECTORY::::::::"); + + String result = mapStorage.touch("/newFile.txt", "Test content"); + + assertEquals("", result); // Success should return an empty string + verify(mockMap).putString("/newFile.txt", "FILE::::::::Test content"); + } + + @Test + public void testTouchFileAlreadyExists() { + when(mockMap.contains("/")).thenReturn(true); // File already exists + when(mockMap.contains("/newFile.txt")).thenReturn(true); // File already exists + + String result = mapStorage.touch("/newFile.txt", "Test content"); + + assertEquals("Cannot create new file, because path already exists: /newFile.txt", result); + } + + @Test + public void testReadTextFile() { + when(mockMap.contains("/file.txt")).thenReturn(true); + when(mockMap.getString("/file.txt")).thenReturn("FILE::::::::Hello World"); + + String content = mapStorage.readtext("/file.txt"); + + assertEquals("Hello World", content); + } + + @Test + public void testReadTextFileDoesNotExist() { + when(mockMap.contains("/file.txt")).thenReturn(false); + + String content = mapStorage.readtext("/file.txt"); + + assertNull(content); + } + + @Test + public void testRmFile() { + when(mockMap.contains("/file.txt")).thenReturn(true); + + boolean result = mapStorage.rm("/file.txt"); + + assertTrue(result); // File successfully removed + verify(mockMap).remove("/file.txt"); + } + + @Test + public void testRmFileDoesNotExist() { + when(mockMap.contains("/file.txt")).thenReturn(false); + + boolean result = mapStorage.rm("/file.txt"); + + assertFalse(result); // File does not exist, so removal fails + } + + @Test + public void testDepth() { + assertEquals(0, mapStorage.depth("/")); + assertEquals(1, mapStorage.depth("/dir")); + assertEquals(2, mapStorage.depth("/dir/subdir")); + } + + @Test + public void testLs() { + when(mockMap.keyList()).thenReturn(List.of("/dir/file1", "/dir/file2", "/dir/subdir/file3")); + + List files = mapStorage.ls("/dir"); + + assertEquals(2, files.size()); + assertTrue(files.contains("/dir/file1")); + assertTrue(files.contains("/dir/file2")); + } +}