forked from WorldStarHipHopX/playforia
-
-
Notifications
You must be signed in to change notification settings - Fork 37
WIP: Persistent preferences #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PhilippvK
wants to merge
3
commits into
master
Choose a base branch
from
poc_persistent_preferences
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
173 changes: 173 additions & 0 deletions
173
client/src/test/java/org/moparforia/client/UserPreferencesTest.java
This file contains hidden or 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,173 @@ | ||
| package org.moparforia.client; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import picocli.CommandLine; | ||
|
|
||
| import javax.swing.*; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.text.ParseException; | ||
| import java.util.prefs.Preferences; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| /** | ||
| * Tests that loading/saving/resetting userPreferences works as expected | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| class UserPreferencesTest { | ||
|
|
||
| private Launcher launcher; | ||
|
|
||
| private CommandLine cmd; | ||
| private StringWriter stdOut; | ||
| private StringWriter stdErr; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| // Mock game | ||
| launcher = mock(Launcher.class, withSettings() | ||
| .lenient() | ||
| .withoutAnnotations()); | ||
|
|
||
| // Use real methods | ||
| doCallRealMethod().when(launcher).call(); | ||
| doCallRealMethod().when(launcher).setPort(anyInt()); | ||
| doCallRealMethod().when(launcher).setHostname(anyString()); | ||
|
|
||
| // Get rid of old preferences before test | ||
| Preferences prefs = Preferences.userRoot().node("org.moparforia.client.Launcher"); | ||
| prefs.remove("timestamp"); | ||
| prefs.remove("version"); | ||
| prefs.remove("hostname"); | ||
| prefs.remove("port"); | ||
| //prefs.remove("lang"); | ||
|
|
||
| cmd = new CommandLine(launcher).setCaseInsensitiveEnumValuesAllowed(true); | ||
|
|
||
| stdOut = new StringWriter(); | ||
| stdErr = new StringWriter(); | ||
|
|
||
| cmd.setOut(new PrintWriter(stdOut)); | ||
| cmd.setErr(new PrintWriter(stdErr)); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| clearInvocations(launcher); | ||
| } | ||
|
|
||
| @Test | ||
| void testPreferencesInvalidCLI() { | ||
| assertNotEquals(0, cmd.execute("-r", "test")); | ||
| assertNotEquals(0, cmd.execute("--reset", "test")); | ||
| assertNotEquals(0, cmd.execute("-r=test")); | ||
| assertNotEquals(0, cmd.execute("--reset=test")); | ||
| } | ||
|
|
||
| @Test | ||
| void testPreferencesValidCLI() { | ||
| // Prepare Test | ||
| Preferences prefs = Preferences.userRoot().node("org.moparforia.client.Launcher"); | ||
| prefs.put("version", "1.2.3"); | ||
| prefs.put("hostname", "localhost"); | ||
| prefs.putInt("port", 4321); | ||
| //prefs.putInt("lang", "en"); | ||
|
|
||
| // Normally launch Game | ||
| try { | ||
| doAnswer((invocaton) -> { | ||
| launcher.setHostname(invocaton.getArgument(1)); | ||
| launcher.setPort(invocaton.getArgument(2)); | ||
| return true; | ||
| }).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); | ||
|
|
||
| } catch (ParseException e) { | ||
| //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix | ||
| } | ||
| assertEquals(0, cmd.execute("-r")); | ||
| verify(launcher).launchGame(any(), | ||
| eq(Launcher.DEFAULT_SERVER), | ||
| eq(Launcher.DEFAULT_PORT), | ||
| eq(Launcher.Language.EN_US), | ||
| anyBoolean()); | ||
|
|
||
| // Dismiss settings dialog TODO: fix this | ||
| /*try { | ||
| doAnswer((invocaton) -> { | ||
| return false; | ||
| }).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); | ||
| } catch (ParseException e) { | ||
| //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix | ||
| } | ||
| assertNotEquals(0, cmd.execute("--reset")); | ||
| assertEquals("unknown", prefs.get("version", "unknown")); | ||
| assertEquals("", prefs.get("hostname", "")); | ||
| assertEquals(0, prefs.getInt("port", 0)); | ||
| assertEquals("", prefs.get("lang", ""));*/ | ||
| } | ||
|
|
||
| @Test | ||
| void testSavePreferences() { | ||
| Preferences prefs = Preferences.userRoot().node("org.moparforia.client.Launcher"); | ||
| /*try { | ||
| doAnswer((invocaton) -> { | ||
| launcher.setHostname(invocaton.getArgument(1)); | ||
| launcher.setPort(invocaton.getArgument(2)); | ||
| return true; | ||
| }).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); | ||
|
|
||
| } catch (ParseException e) { | ||
| //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix | ||
| } | ||
| assertEquals(0, cmd.execute()); | ||
| verify(launcher).launchGame(any(), | ||
| eq(Launcher.DEFAULT_SERVER), | ||
| eq(Launcher.DEFAULT_PORT), | ||
| eq(Launcher.Language.EN_US), | ||
| anyBoolean()); | ||
| assertEquals(Launcher.class.getPackage().getImplementationVersion(), prefs.get("version", "unknown")); | ||
| assertEquals(Launcher.DEFAULT_SERVER, prefs.get("hostname", "")); | ||
| assertEquals(Launcher.DEFAULT_PORT, prefs.getInt("port", 0)); | ||
| //assertEquals(Launcher.Language.EN_US, prefs.get("lang", ""));*/ | ||
| } | ||
|
|
||
| @Test | ||
| void testLoadPreferences() { | ||
| Preferences prefs = Preferences.userRoot().node("org.moparforia.client.Launcher"); | ||
| prefs.put("version", "1.2.3"); | ||
| prefs.put("hostname", "localhost"); | ||
| prefs.putInt("port", 4321); | ||
| //prefs.putInt("lang", "en"); | ||
|
|
||
| /*try { | ||
| doAnswer((invocaton) -> { | ||
| launcher.setHostname(invocaton.getArgument(1)); | ||
| launcher.setPort(invocaton.getArgument(2)); | ||
| return true; | ||
| }).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); | ||
|
|
||
| } catch (ParseException e) { | ||
| //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix | ||
| } | ||
| assertEquals(0, cmd.execute()); | ||
| try { | ||
| verify(launcher).showSettingDialog(any(), | ||
| eq("localhost"), | ||
| eq(4321) | ||
| //anyString(), | ||
| ); | ||
| } catch (ParseException e) { | ||
| //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix | ||
| }*/ | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move all this Preferences logic into a separate class and add getters/setters for individual properties, this way we can handle all the logic for one property being unknown there. It will be also easier to test, since we could be mocking individual methods in that new class instead of the entire properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getters could return Optional and the individual getters could work like this: