-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add powermock-module-junit4 test dependency
and class ZettelkastenAppRefactored extends ZettelkastenApp and add getInitialParamZettel to Settings
- Loading branch information
1 parent
6a2d5b0
commit 885a87b
Showing
6 changed files
with
242 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactored.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppPowerMockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/test/java/de/danielluedecke/zettelkasten/ZettelkastenAppRefactoredTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters