Skip to content

Commit

Permalink
test: add powermock-module-junit4 test dependency
Browse files Browse the repository at this point in the history
and class ZettelkastenAppRefactored extends ZettelkastenApp
and add getInitialParamZettel to Settings
  • Loading branch information
RalfBarkow committed Jan 19, 2025
1 parent 6a2d5b0 commit 885a87b
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 35 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.danielluedecke</groupId>
<artifactId>Zettelkasten</artifactId>
<version>3.2024.11</version>
<version>3.2025.01</version>
<name>Zettelkasten</name>
<properties>
<exec.mainClass>de.danielluedecke.zettelkasten.ZettelkastenApp</exec.mainClass>
Expand Down Expand Up @@ -298,6 +298,13 @@
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.unibe</groupId>
<artifactId>jexample</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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();
}
}

0 comments on commit 885a87b

Please sign in to comment.