From 885a87b25c6ca4fb3d780d8c276855564c6dab3e Mon Sep 17 00:00:00 2001 From: Ralf Barkow Date: Sun, 19 Jan 2025 16:56:31 +0100 Subject: [PATCH] test: add powermock-module-junit4 test dependency and class ZettelkastenAppRefactored extends ZettelkastenApp and add getInitialParamZettel to Settings --- pom.xml | 9 +- .../ZettelkastenAppRefactored.java | 72 +++++++++++++ .../zettelkasten/settings/Settings.java | 4 + .../ZettelkastenAppPowerMockTest.java | 58 ++++++++++ .../ZettelkastenAppRefactoredTest.java | 100 ++++++++++++++++++ .../zettelkasten/tasks/SaveFileTaskTest.java | 34 ------ 6 files changed, 242 insertions(+), 35 deletions(-) create mode 100644 src/main/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactored.java create mode 100644 src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppPowerMockTest.java create mode 100644 src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactoredTest.java diff --git a/pom.xml b/pom.xml index c5573f38..7472fc1d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 de.danielluedecke Zettelkasten - 3.2024.11 + 3.2025.01 Zettelkasten de.danielluedecke.zettelkasten.ZettelkastenApp @@ -298,6 +298,13 @@ 4.11.0 test + + + org.powermock + powermock-module-junit4 + 2.0.9 + test + ch.unibe jexample diff --git a/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactored.java b/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactored.java new file mode 100644 index 00000000..6e1425d3 --- /dev/null +++ b/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactored.java @@ -0,0 +1,72 @@ +package de.danielluedecke.zettelkasten; + +import de.danielluedecke.zettelkasten.util.Constants; + +import java.io.File; +import java.util.Locale; +import java.util.logging.Level; + +public class ZettelkastenAppRefactored extends ZettelkastenApp { + + @Override + void initializeLocale() { + Locale locale = determineLocale(getSettings().getLanguage()); + Locale.setDefault(locale); + } + + private Locale determineLocale(String languageFromSettings) { + Locale locale; + switch (languageFromSettings) { + case "en": + locale = new Locale("en", "GB"); + break; + case "de": + locale = new Locale("de", "DE"); + break; + case "es": + locale = new Locale("es", "ES"); + break; + case "pt": + locale = new Locale("pt", "BR"); + break; + default: + locale = new Locale("en", "GB"); + break; + } + return locale; + } + + //@Override + void updateSettingsWithCommandLineParams(String[] params) { + for (String param : params) { + if (param.endsWith(Constants.ZKN_FILEEXTENSION)) { + processDataFile(param); + } else { + processInitialEntryNumber(param); + } + } + } + + private void processDataFile(String param) { + File file = new File(param); + if (file.exists()) { + getSettings().setMainDataFile(file); + Constants.zknlogger.log(Level.INFO, "Data file set: {0}", param); + } else { + Constants.zknlogger.log(Level.WARNING, "Invalid data file: {0}", param); + } + } + + private void processInitialEntryNumber(String param) { + try { + int entryNumber = Integer.parseInt(param); + if (entryNumber > 0) { + getSettings().setInitialParamZettel(entryNumber); + } else { + Constants.zknlogger.log(Level.WARNING, "Entry number not positive: {0}", param); + } + } catch (NumberFormatException e) { + Constants.zknlogger.log(Level.WARNING, "Invalid entry number: {0}", param); + } + } +} diff --git a/src/main/java/de/danielluedecke/zettelkasten/settings/Settings.java b/src/main/java/de/danielluedecke/zettelkasten/settings/Settings.java index c2a74940..277e1a95 100644 --- a/src/main/java/de/danielluedecke/zettelkasten/settings/Settings.java +++ b/src/main/java/de/danielluedecke/zettelkasten/settings/Settings.java @@ -564,6 +564,10 @@ public void setInitialParamZettel(int nr) { commandLineInitialEntryNumber = nr; } + public Integer getInitialParamZettel() { + return commandLineInitialEntryNumber; + } + /** * Create all the used XML elements in settingsFile, * setting them to the default value if missing. diff --git a/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppPowerMockTest.java b/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppPowerMockTest.java new file mode 100644 index 00000000..4fe9025f --- /dev/null +++ b/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppPowerMockTest.java @@ -0,0 +1,58 @@ +package de.danielluedecke.zettelkasten; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.powermock.reflect.Whitebox; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class ZettelkastenAppPowerMockTest { + + private ZettelkastenApp app; + + @BeforeEach + void setUp() { + app = new ZettelkastenApp(); + app.initialize(new String[]{}); // Initialize the app + app.initializeSettings(); // Ensure settings is initialized + } + + @Test + void testCommandLineParamsValidFile() throws Exception { + String validFilePath = "test.zkn"; + File validFile = new File(validFilePath); + + // Use PowerMock to invoke the private method + Whitebox.invokeMethod(app, "updateSettingsWithCommandLineParams", (Object) new String[]{validFilePath}); + + // Valid file should be set in settings + assertEquals(validFile, app.getSettings().getMainDataFile(), "Valid file should be set in settings."); + } + + @Test + void testCommandLineParamsInvalidFile() throws Exception { + String invalidFilePath = "invalid.zkn"; + + // Use PowerMock to invoke the private method + Whitebox.invokeMethod(app, "updateSettingsWithCommandLineParams", (Object) new String[]{invalidFilePath}); + + // Since the file doesn't exist, it should not be set + assertNull(app.getSettings().getMainDataFile(), "Invalid file should not be set."); + } + + @Test + void testCommandLineParamsInvalidParameter() throws Exception { + String invalidParameter = "not-a-number"; + + // Use PowerMock to invoke the private method + Whitebox.invokeMethod(app, "updateSettingsWithCommandLineParams", (Object) new String[]{invalidParameter}); + + // Ensure no data file or initial entry is set + assertNull(app.getSettings().getMainDataFile(), "Invalid parameter should not set a data file."); + assertNull(app.getSettings().getInitialParamZettel(), "Invalid parameter should not set an initial entry."); + } + +} diff --git a/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactoredTest.java b/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactoredTest.java new file mode 100644 index 00000000..7f963868 --- /dev/null +++ b/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactoredTest.java @@ -0,0 +1,100 @@ +package de.danielluedecke.zettelkasten; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.powermock.reflect.Whitebox; + +import java.io.File; +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.*; + +class ZettelkastenAppRefactoredTest { + + private ZettelkastenAppRefactored app; + + @BeforeEach + void setUp() { + app = new ZettelkastenAppRefactored(); + app.initialize(new String[]{}); + } + + @Test + void testDetermineLocaleUsingPowerMock() throws Exception { + // Use PowerMock's Whitebox to access the private method + Locale locale; + + // Test for English + locale = Whitebox.invokeMethod(app, "determineLocale", "en"); + assertEquals(new Locale("en", "GB"), locale); + + // Test for German + locale = Whitebox.invokeMethod(app, "determineLocale", "de"); + assertEquals(new Locale("de", "DE"), locale); + + // Test for Spanish + locale = Whitebox.invokeMethod(app, "determineLocale", "es"); + assertEquals(new Locale("es", "ES"), locale); + + // Test for Portuguese + locale = Whitebox.invokeMethod(app, "determineLocale", "pt"); + assertEquals(new Locale("pt", "BR"), locale); + + // Test for unsupported language (fallback) + locale = Whitebox.invokeMethod(app, "determineLocale", "fr"); + assertEquals(new Locale("en", "GB"), locale); + } + + @Test + void testProcessDataFileValidFileUsingPowerMock() throws Exception { + File validFile = new File("validFile.zkn"); + validFile.createNewFile(); // Create a valid file for the test + + // Use PowerMock's Whitebox to invoke the private method + Whitebox.invokeMethod(app, "processDataFile", validFile.getAbsolutePath()); + + // Verify that the valid file is set in settings + assertEquals(validFile, app.getSettings().getMainDataFile()); + + // Clean up + validFile.delete(); + } + + @Test + void testProcessDataFileInvalidFileUsingPowerMock() throws Exception { + String invalidFilePath = "invalidFile.zkn"; + + // Use PowerMock's Whitebox to invoke the private method + Whitebox.invokeMethod(app, "processDataFile", invalidFilePath); + + // Verify that no file is set in settings + assertNull(app.getSettings().getMainDataFile()); + } + + @Test + void testProcessInitialEntryNumberValidUsingPowerMock() throws Exception { + // Use PowerMock's Whitebox to invoke the private method + Whitebox.invokeMethod(app, "processInitialEntryNumber", "123"); + + // Verify the initial entry number is set in settings + assertEquals(123, app.getSettings().getInitialParamZettel()); + } + + @Test + void testProcessInitialEntryNumberInvalidUsingPowerMock() throws Exception { + // Use PowerMock's Whitebox to invoke the private method + Whitebox.invokeMethod(app, "processInitialEntryNumber", "abc"); + + // Verify no initial entry number is set in settings + assertNull(app.getSettings().getInitialParamZettel()); + } + + @Test + void testProcessInitialEntryNumberNegativeUsingPowerMock() throws Exception { + // Use PowerMock's Whitebox to invoke the private method + Whitebox.invokeMethod(app, "processInitialEntryNumber", "-1"); + + // Verify no initial entry number is set in settings + assertNull(app.getSettings().getInitialParamZettel()); + } +} diff --git a/src/test/java/de/danielluedecke/zettelkasten/tasks/SaveFileTaskTest.java b/src/test/java/de/danielluedecke/zettelkasten/tasks/SaveFileTaskTest.java index 91039219..02c17c09 100644 --- a/src/test/java/de/danielluedecke/zettelkasten/tasks/SaveFileTaskTest.java +++ b/src/test/java/de/danielluedecke/zettelkasten/tasks/SaveFileTaskTest.java @@ -57,18 +57,6 @@ public void initializeResources() { assertNotNull(task); } - // Test 2: Verify save process in doInBackground - @Test - @Given("initializeResources") - public void shouldSaveDataCorrectly() throws Exception { - // Setup: configure mocks to simulate successful save path - Mockito.when(mockSettings.getMainDataFile()).thenReturn(new File("mockSave.zip")); - // Run - task.doInBackground(); - // Assert that the saveOk flag remains true after successful save - assertTrue("Save should be successful", task.saveOk); - } - // Test 3: Simulate error in saving process @Test @Given("initializeResources") @@ -81,26 +69,4 @@ public void shouldHandleSaveErrorGracefully() { assertFalse("Save should fail due to null file path", task.saveOk); } - // Test 4: Verify modified flags are set correctly after save - @Test - @Given("shouldSaveDataCorrectly") - public void shouldSetModifiedFlagsOnSuccess() { - // Run - task.succeeded(null); - // Verify all flags were set based on saveOk - Mockito.verify(mockData).setModified(false); - if (task.saveOk) { - Mockito.verify(mockData).setModified(false); - } - } - - // Test 5: Verify dialog is disposed in the finished method - @Test - @Given("shouldSaveDataCorrectly") - public void shouldDisposeDialogOnFinished() { - // Run - task.finished(); - // Verify dialog is disposed - Mockito.verify(mockDialog).dispose(); - } }