From 7a6bf2a12d93492ec6cb6bb9db6a1d2c2d836a0e Mon Sep 17 00:00:00 2001 From: PhilippvK Date: Mon, 23 Nov 2020 16:50:07 +0100 Subject: [PATCH 1/3] Add persistent storage for user preferences to client Partially resolves Issue #75 TODO: Also save language setting after it was added to the Launcher Dialog --- .../java/org/moparforia/client/Launcher.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/client/src/main/java/org/moparforia/client/Launcher.java b/client/src/main/java/org/moparforia/client/Launcher.java index 17d89a52..34ac0946 100644 --- a/client/src/main/java/org/moparforia/client/Launcher.java +++ b/client/src/main/java/org/moparforia/client/Launcher.java @@ -9,6 +9,9 @@ import java.io.IOException; import java.text.ParseException; import java.util.concurrent.Callable; +import java.util.prefs.Preferences; +import java.util.Date; +import java.text.SimpleDateFormat; @CommandLine.Command( @@ -41,6 +44,9 @@ public class Launcher implements Callable { @CommandLine.Option(names = {"--verbose", "-v"}, description = "Set if you want verbose information") private static boolean verbose = false; + @CommandLine.Option(names = {"--reset", "-r"}, description = "Reset user preferences at launch") + private static boolean reset = false; + public static boolean debug() { return verbose; } @@ -49,6 +55,42 @@ public static boolean isUsingCustomServer() { return true;//instance.serverBox.isSelected(); } + public void loadPreferences() { + Preferences prefs = Preferences.userRoot().node(this.getClass().getName()); + long last_time = prefs.getLong("timestamp", 0); + String last_version = prefs.get("version", "unknown"); // TODO: check version + String last_hostname = prefs.get("hostname", ""); + int last_port = prefs.getInt("port", 0); + if (!last_hostname.isEmpty() && last_port != 0) { + hostname = last_hostname; + port = last_port; + System.out.printf("Successfully loaded old user preferences written with v%s at %s\n", + last_version, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(last_time)); + } else { + System.err.println("No old user preferences found. Using the default settings..."); + } + } + + public void savePreferences() { + Preferences prefs = Preferences.userRoot().node(this.getClass().getName()); + System.out.println("this.getClass().getName()=="+this.getClass().getName()); + + prefs.putLong("timestamp", new Date().getTime()); + prefs.put("version", Launcher.class.getPackage().getImplementationVersion()); + prefs.put("hostname", hostname); + prefs.putInt("port", port); + System.out.println("User preferences have been saved successfully"); + } + + public void resetPreferences() { + Preferences prefs = Preferences.userRoot().node(this.getClass().getName()); + prefs.remove("timestamp"); + prefs.remove("version"); + prefs.remove("hostname"); + prefs.remove("port"); + System.out.println("Removed old preferences as requested by user"); + } + public static void main(String... args) { Launcher launcher = new Launcher(); new CommandLine(launcher) @@ -113,8 +155,13 @@ private String[] login(JFrame frame) { @Override public Void call() throws Exception{ JFrame frame = createFrame(); + // Remove preferences if requested + if (reset) { + resetPreferences(); + } if (hostname.isEmpty() || port == 0) { // Determine which of these was actually false + loadPreferences(); String temp_hostname = hostname.isEmpty() ? DEFAULT_SERVER : hostname; int temp_port = port == 0 ? DEFAULT_PORT : port; if (!showSettingDialog(frame, temp_hostname, temp_port)) { @@ -123,6 +170,7 @@ public Void call() throws Exception{ return null; } } + savePreferences(); launchGame(frame, hostname, port, lang, verbose); return null; From 8b9c74188c39dffd750ca704447daae24aa1121e Mon Sep 17 00:00:00 2001 From: PhilippvK Date: Mon, 23 Nov 2020 16:52:01 +0100 Subject: [PATCH 2/3] WIP: tests for persitstent user preferences Warning: currently incomplete (TODO: fix) --- .../client/UserPreferencesTest.java | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 client/src/test/java/org/moparforia/client/UserPreferencesTest.java diff --git a/client/src/test/java/org/moparforia/client/UserPreferencesTest.java b/client/src/test/java/org/moparforia/client/UserPreferencesTest.java new file mode 100644 index 00000000..b361f151 --- /dev/null +++ b/client/src/test/java/org/moparforia/client/UserPreferencesTest.java @@ -0,0 +1,148 @@ +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()); + + //doReturn(mock(JFrame.class)).when(launcher).launchGame(); + //doAnswer((invocaton) -> { + // launcher.setPort(invocaton.getArgument(2)); + // launcher.setHostname(invocaton.getArgument(1)); + // return true; + //}).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); + + // 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 + 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 + /*assertNotEquals(0, cmd.execute("--reset")); + try { + when(launcher.showSettingDialog(any(JFrame.class), anyString(), anyInt())).thenReturn(false); + } catch (ParseException e) { + //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix + } + 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"); + 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"); + + 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 + } + + } +} From f3a6f592947cbf61fbfc651b7059c078b2e9cb2f Mon Sep 17 00:00:00 2001 From: PhilippvK Date: Mon, 23 Nov 2020 23:19:13 +0100 Subject: [PATCH 3/3] out of ideas now... --- .../client/UserPreferencesTest.java | 53 ++++++++++++++----- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/client/src/test/java/org/moparforia/client/UserPreferencesTest.java b/client/src/test/java/org/moparforia/client/UserPreferencesTest.java index b361f151..32f11a01 100644 --- a/client/src/test/java/org/moparforia/client/UserPreferencesTest.java +++ b/client/src/test/java/org/moparforia/client/UserPreferencesTest.java @@ -40,15 +40,8 @@ void setUp() throws Exception { // Use real methods doCallRealMethod().when(launcher).call(); - //doCallRealMethod().when(launcher).setPort(anyInt()); - //doCallRealMethod().when(launcher).setHostname(anyString()); - - //doReturn(mock(JFrame.class)).when(launcher).launchGame(); - //doAnswer((invocaton) -> { - // launcher.setPort(invocaton.getArgument(2)); - // launcher.setHostname(invocaton.getArgument(1)); - // return true; - //}).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); + 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"); @@ -90,6 +83,16 @@ void testPreferencesValidCLI() { //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), @@ -98,12 +101,14 @@ void testPreferencesValidCLI() { anyBoolean()); // Dismiss settings dialog TODO: fix this - /*assertNotEquals(0, cmd.execute("--reset")); - try { - when(launcher.showSettingDialog(any(JFrame.class), anyString(), anyInt())).thenReturn(false); + /*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)); @@ -113,6 +118,16 @@ void testPreferencesValidCLI() { @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), @@ -122,7 +137,7 @@ void testSavePreferences() { 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", "")); + //assertEquals(Launcher.Language.EN_US, prefs.get("lang", ""));*/ } @Test @@ -133,6 +148,16 @@ void testLoadPreferences() { 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(), @@ -142,7 +167,7 @@ void testLoadPreferences() { ); } catch (ParseException e) { //fail("showSettingDialog should not have thrown an Exception"); // TODO: fix - } + }*/ } }