diff --git a/src/main/java/org/jabref/cli/Launcher.java b/src/main/java/org/jabref/cli/Launcher.java index 4cd5d9926f3..6549ba61b4f 100644 --- a/src/main/java/org/jabref/cli/Launcher.java +++ b/src/main/java/org/jabref/cli/Launcher.java @@ -11,7 +11,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.MainApplication; -import org.jabref.gui.remote.JabRefMessageHandler; import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.l10n.Localization; @@ -132,10 +131,6 @@ private static boolean handleMultipleAppInstances(String[] args, PreferencesServ } else { LOGGER.warn("Could not communicate with other running JabRef instance."); } - } else { - // We are alone, so we start the server - Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), remotePreferences.getPort(), - preferences); } } return true; diff --git a/src/main/java/org/jabref/gui/Globals.java b/src/main/java/org/jabref/gui/Globals.java index 1696a1dfba9..0e575e8a6bd 100644 --- a/src/main/java/org/jabref/gui/Globals.java +++ b/src/main/java/org/jabref/gui/Globals.java @@ -7,6 +7,7 @@ import org.jabref.architecture.AllowedToUseAwt; import org.jabref.gui.keyboard.KeyBindingRepository; +import org.jabref.gui.remote.CLIMessageHandler; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.undo.CountingUndoManager; import org.jabref.gui.util.DefaultFileUpdateMonitor; @@ -16,7 +17,8 @@ import org.jabref.logic.importer.ImportFormatReader; import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.protectedterms.ProtectedTermsLoader; -import org.jabref.logic.remote.server.RemoteListenerServerLifecycle; +import org.jabref.logic.remote.RemotePreferences; +import org.jabref.logic.remote.server.RemoteListenerServerManager; import org.jabref.logic.util.BuildInfo; import org.jabref.model.entry.BibEntryTypesManager; import org.jabref.model.strings.StringUtil; @@ -41,9 +43,7 @@ public class Globals { */ public static final BuildInfo BUILD_INFO = new BuildInfo(); - // Remote listener - public static final RemoteListenerServerLifecycle REMOTE_LISTENER = new RemoteListenerServerLifecycle(); - + public static final RemoteListenerServerManager REMOTE_LISTENER = new RemoteListenerServerManager(); /** * Manager for the state of the GUI. */ @@ -118,6 +118,10 @@ public static void startBackgroundTasks() { /* if (Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) { startTelemetryClient(); } */ + RemotePreferences remotePreferences = prefs.getRemotePreferences(); + if (remotePreferences.useRemoteServer()) { + Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(prefs), remotePreferences.getPort()); + } } private static void stopTelemetryClient() { diff --git a/src/main/java/org/jabref/gui/JabRefExecutorService.java b/src/main/java/org/jabref/gui/JabRefExecutorService.java index f60285e46ff..a9d5b5c2ee9 100644 --- a/src/main/java/org/jabref/gui/JabRefExecutorService.java +++ b/src/main/java/org/jabref/gui/JabRefExecutorService.java @@ -117,9 +117,9 @@ public void executeInterruptableTaskAndWait(Runnable runnable) { } } - public void manageRemoteThread(Thread thread) { + public void startRemoteThread(Thread thread) { if (this.remoteThread != null) { - throw new IllegalStateException("Remote thread is already attached"); + throw new IllegalStateException("Tele thread is already attached"); } else { this.remoteThread = thread; remoteThread.start(); @@ -141,7 +141,6 @@ public void submit(TimerTask timerTask, long millisecondsDelay) { * Shuts everything down. After termination, this method returns. */ public void shutdownEverything() { - // kill the remote thread stopRemoteThread(); gracefullyShutdown(this.executorService); diff --git a/src/main/java/org/jabref/gui/preferences/network/NetworkTabViewModel.java b/src/main/java/org/jabref/gui/preferences/network/NetworkTabViewModel.java index c3962e241e8..8af629de4cf 100644 --- a/src/main/java/org/jabref/gui/preferences/network/NetworkTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/network/NetworkTabViewModel.java @@ -21,7 +21,7 @@ import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.preferences.PreferenceTabViewModel; -import org.jabref.gui.remote.JabRefMessageHandler; +import org.jabref.gui.remote.CLIMessageHandler; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.logic.l10n.Localization; import org.jabref.logic.net.ProxyPreferences; @@ -73,7 +73,7 @@ public class NetworkTabViewModel implements PreferenceTabViewModel { private final TrustStoreManager trustStoreManager; - private AtomicBoolean sslCertificatesChanged = new AtomicBoolean(false); + private final AtomicBoolean sslCertificatesChanged = new AtomicBoolean(false); public NetworkTabViewModel(DialogService dialogService, PreferencesService preferences) { this.dialogService = dialogService; @@ -139,6 +139,7 @@ public NetworkTabViewModel(DialogService dialogService, PreferencesService prefe this.trustStoreManager = new TrustStoreManager(Path.of(sslPreferences.getTruststorePath())); } + @Override public void setValues() { remoteServerProperty.setValue(remotePreferences.useRemoteServer()); remotePortProperty.setValue(String.valueOf(remotePreferences.getPort())); @@ -174,6 +175,7 @@ private void setSSLValues() { }); } + @Override public void storeSettings() { storeRemoteSettings(); storeProxySettings(new ProxyPreferences( @@ -201,7 +203,7 @@ private void storeRemoteSettings() { if (remoteServerProperty.getValue()) { remotePreferences.setUseRemoteServer(true); - Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), remotePreferences.getPort(), preferences); + Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(preferences), remotePreferences.getPort()); } else { remotePreferences.setUseRemoteServer(false); Globals.REMOTE_LISTENER.stop(); @@ -256,6 +258,7 @@ public ValidationStatus proxyPasswordValidationStatus() { return proxyPasswordValidator.getValidationStatus(); } + @Override public boolean validateSettings() { CompositeValidator validator = new CompositeValidator(); diff --git a/src/main/java/org/jabref/gui/remote/JabRefMessageHandler.java b/src/main/java/org/jabref/gui/remote/CLIMessageHandler.java similarity index 74% rename from src/main/java/org/jabref/gui/remote/JabRefMessageHandler.java rename to src/main/java/org/jabref/gui/remote/CLIMessageHandler.java index a93e78f15a7..8a796e45be6 100644 --- a/src/main/java/org/jabref/gui/remote/JabRefMessageHandler.java +++ b/src/main/java/org/jabref/gui/remote/CLIMessageHandler.java @@ -7,18 +7,24 @@ import org.jabref.cli.ArgumentProcessor; import org.jabref.gui.JabRefGUI; import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.remote.server.MessageHandler; +import org.jabref.logic.remote.server.RemoteMessageHandler; import org.jabref.preferences.PreferencesService; import org.apache.commons.cli.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class JabRefMessageHandler implements MessageHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(JabRefMessageHandler.class); +public class CLIMessageHandler implements RemoteMessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(CLIMessageHandler.class); + + private final PreferencesService preferencesService; + + public CLIMessageHandler(PreferencesService preferencesService) { + this.preferencesService = preferencesService; + } @Override - public void handleCommandLineArguments(String[] message, PreferencesService preferencesService) { + public void handleCommandLineArguments(String[] message) { try { ArgumentProcessor argumentProcessor = new ArgumentProcessor(message, ArgumentProcessor.Mode.REMOTE_START, preferencesService); diff --git a/src/main/java/org/jabref/logic/remote/shared/Protocol.java b/src/main/java/org/jabref/logic/remote/Protocol.java similarity index 94% rename from src/main/java/org/jabref/logic/remote/shared/Protocol.java rename to src/main/java/org/jabref/logic/remote/Protocol.java index 6d93122fbe7..0f89e65f16c 100644 --- a/src/main/java/org/jabref/logic/remote/shared/Protocol.java +++ b/src/main/java/org/jabref/logic/remote/Protocol.java @@ -1,4 +1,4 @@ -package org.jabref.logic.remote.shared; +package org.jabref.logic.remote; import java.io.IOException; import java.io.ObjectInputStream; @@ -10,9 +10,6 @@ import javafx.util.Pair; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * @implNote The first byte of every message identifies its type as a {@link RemoteMessage}. * Every message is terminated with '\0'. @@ -21,8 +18,6 @@ public class Protocol implements AutoCloseable { public static final String IDENTIFIER = "jabref"; - private static final Logger LOGGER = LoggerFactory.getLogger(Protocol.class); - private final Socket socket; private final ObjectOutputStream out; private final ObjectInputStream in; diff --git a/src/main/java/org/jabref/logic/remote/shared/RemoteMessage.java b/src/main/java/org/jabref/logic/remote/RemoteMessage.java similarity index 92% rename from src/main/java/org/jabref/logic/remote/shared/RemoteMessage.java rename to src/main/java/org/jabref/logic/remote/RemoteMessage.java index 6f33cd2b3f6..7bb78a7a27d 100644 --- a/src/main/java/org/jabref/logic/remote/shared/RemoteMessage.java +++ b/src/main/java/org/jabref/logic/remote/RemoteMessage.java @@ -1,4 +1,4 @@ -package org.jabref.logic.remote.shared; +package org.jabref.logic.remote; public enum RemoteMessage { /** diff --git a/src/main/java/org/jabref/logic/remote/RemotePreferences.java b/src/main/java/org/jabref/logic/remote/RemotePreferences.java index 901bce68f53..5d06d28261b 100644 --- a/src/main/java/org/jabref/logic/remote/RemotePreferences.java +++ b/src/main/java/org/jabref/logic/remote/RemotePreferences.java @@ -13,8 +13,8 @@ */ public class RemotePreferences { - private IntegerProperty port; - private BooleanProperty useRemoteServer; + private final IntegerProperty port; + private final BooleanProperty useRemoteServer; public RemotePreferences(int port, boolean useRemoteServer) { this.port = new SimpleIntegerProperty(port); diff --git a/src/main/java/org/jabref/logic/remote/client/RemoteClient.java b/src/main/java/org/jabref/logic/remote/client/RemoteClient.java index 8090a73b9d5..915ffc5e598 100644 --- a/src/main/java/org/jabref/logic/remote/client/RemoteClient.java +++ b/src/main/java/org/jabref/logic/remote/client/RemoteClient.java @@ -7,9 +7,9 @@ import javafx.util.Pair; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.remote.Protocol; +import org.jabref.logic.remote.RemoteMessage; import org.jabref.logic.remote.RemotePreferences; -import org.jabref.logic.remote.shared.Protocol; -import org.jabref.logic.remote.shared.RemoteMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,7 +30,7 @@ public boolean ping() { protocol.sendMessage(RemoteMessage.PING); Pair response = protocol.receiveMessage(); - if (response.getKey() == RemoteMessage.PONG && Protocol.IDENTIFIER.equals(response.getValue())) { + if ((response.getKey() == RemoteMessage.PONG) && Protocol.IDENTIFIER.equals(response.getValue())) { return true; } else { String port = String.valueOf(this.port); diff --git a/src/main/java/org/jabref/logic/remote/server/MessageHandler.java b/src/main/java/org/jabref/logic/remote/server/MessageHandler.java deleted file mode 100644 index 86564a4d54b..00000000000 --- a/src/main/java/org/jabref/logic/remote/server/MessageHandler.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.jabref.logic.remote.server; - -import org.jabref.preferences.PreferencesService; - -@FunctionalInterface -public interface MessageHandler { - - void handleCommandLineArguments(String[] message, PreferencesService preferencesService); -} diff --git a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServer.java b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServer.java index f436fe5ae5c..2745d426454 100644 --- a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServer.java +++ b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServer.java @@ -7,10 +7,9 @@ import javafx.util.Pair; +import org.jabref.logic.remote.Protocol; +import org.jabref.logic.remote.RemoteMessage; import org.jabref.logic.remote.RemotePreferences; -import org.jabref.logic.remote.shared.Protocol; -import org.jabref.logic.remote.shared.RemoteMessage; -import org.jabref.preferences.PreferencesService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,14 +21,12 @@ public class RemoteListenerServer implements Runnable { private static final int TIMEOUT = 1000; - private final MessageHandler messageHandler; + private final RemoteMessageHandler messageHandler; private final ServerSocket serverSocket; - private final PreferencesService preferencesService; - public RemoteListenerServer(MessageHandler messageHandler, int port, PreferencesService preferencesService) throws IOException { + public RemoteListenerServer(RemoteMessageHandler messageHandler, int port) throws IOException { this.serverSocket = new ServerSocket(port, BACKLOG, RemotePreferences.getIpAddress()); this.messageHandler = messageHandler; - this.preferencesService = preferencesService; } @Override @@ -61,7 +58,7 @@ private void handleMessage(Protocol protocol, RemoteMessage type, Object argumen break; case SEND_COMMAND_LINE_ARGUMENTS: if (argument instanceof String[]) { - messageHandler.handleCommandLineArguments((String[]) argument, preferencesService); + messageHandler.handleCommandLineArguments((String[]) argument); protocol.sendMessage(RemoteMessage.OK); } else { throw new IOException("Argument for 'SEND_COMMAND_LINE_ARGUMENTS' is not of type String[]. Got " + argument); @@ -75,8 +72,8 @@ private void handleMessage(Protocol protocol, RemoteMessage type, Object argumen public void closeServerSocket() { try { serverSocket.close(); - } catch (IOException ignored) { - // Ignored + } catch (IOException exception) { + LOGGER.error("Could not close server socket", exception); } } } diff --git a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerLifecycle.java b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerLifecycle.java deleted file mode 100644 index e1ee48cbf75..00000000000 --- a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerLifecycle.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.jabref.logic.remote.server; - -import java.io.IOException; -import java.net.BindException; - -import org.jabref.gui.JabRefExecutorService; -import org.jabref.preferences.PreferencesService; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Manages the RemoteListenerServerThread through typical life cycle methods. - *

- * open -> start -> stop - * openAndStart -> stop - *

- * Observer: isOpen, isNotStartedBefore - */ -public class RemoteListenerServerLifecycle implements AutoCloseable { - - private static final Logger LOGGER = LoggerFactory.getLogger(RemoteListenerServerLifecycle.class); - - private RemoteListenerServerThread remoteListenerServerThread; - - public void stop() { - if (isOpen()) { - remoteListenerServerThread.interrupt(); - remoteListenerServerThread = null; - JabRefExecutorService.INSTANCE.stopRemoteThread(); - } - } - - /** - * Acquire any resources needed for the server. - */ - public void open(MessageHandler messageHandler, int port, PreferencesService preferencesService) { - if (isOpen()) { - return; - } - - RemoteListenerServerThread result; - try { - result = new RemoteListenerServerThread(messageHandler, port, preferencesService); - } catch (BindException e) { - LOGGER.warn("There was an error opening the configured network port {}. Please ensure there isn't another" + - " application already using that port.", port); - result = null; - } catch (IOException e) { - result = null; - } - remoteListenerServerThread = result; - } - - public boolean isOpen() { - return remoteListenerServerThread != null; - } - - public void start() { - if (isOpen() && isNotStartedBefore()) { - // threads can only be started when in state NEW - JabRefExecutorService.INSTANCE.manageRemoteThread(remoteListenerServerThread); - } - } - - public boolean isNotStartedBefore() { - // threads can only be started when in state NEW - return (remoteListenerServerThread == null) || (remoteListenerServerThread.getState() == Thread.State.NEW); - } - - public void openAndStart(MessageHandler messageHandler, int port, PreferencesService preferencesService) { - open(messageHandler, port, preferencesService); - start(); - } - - @Override - public void close() { - stop(); - } -} diff --git a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerManager.java b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerManager.java new file mode 100644 index 00000000000..2cc5483e150 --- /dev/null +++ b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerManager.java @@ -0,0 +1,78 @@ +package org.jabref.logic.remote.server; + +import java.io.IOException; +import java.net.BindException; + +import org.jabref.gui.JabRefExecutorService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manages the TeleServerThread through typical life cycle methods. + *

+ * open -> start -> stop + * openAndStart -> stop + *

+ * Observer: isOpen, isNotStartedBefore + */ +public class RemoteListenerServerManager implements AutoCloseable { + + private static final Logger LOGGER = LoggerFactory.getLogger(RemoteListenerServerManager.class); + + private RemoteListenerServerThread remoteServerThread; + + public void stop() { + if (isOpen()) { + remoteServerThread.interrupt(); + remoteServerThread = null; + JabRefExecutorService.INSTANCE.stopRemoteThread(); + } + } + + /** + * Acquire any resources needed for the server. + */ + public void open(RemoteMessageHandler messageHandler, int port) { + if (isOpen()) { + return; + } + + try { + remoteServerThread = new RemoteListenerServerThread(messageHandler, port); + } catch (BindException e) { + LOGGER.error("There was an error opening the configured network port {}. Please ensure there isn't another" + + " application already using that port.", port); + remoteServerThread = null; + } catch (IOException e) { + LOGGER.error("Unknown error while opening the network port.", e); + remoteServerThread = null; + } + } + + public boolean isOpen() { + return remoteServerThread != null; + } + + public void start() { + if (isOpen() && isNotStartedBefore()) { + // threads can only be started when in state NEW + JabRefExecutorService.INSTANCE.startRemoteThread(remoteServerThread); + } + } + + public boolean isNotStartedBefore() { + // threads can only be started when in state NEW + return (remoteServerThread == null) || (remoteServerThread.getState() == Thread.State.NEW); + } + + public void openAndStart(RemoteMessageHandler messageHandler, int port) { + open(messageHandler, port); + start(); + } + + @Override + public void close() { + stop(); + } +} diff --git a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerThread.java b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerThread.java index 99eea480c7e..431a45b0727 100644 --- a/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerThread.java +++ b/src/main/java/org/jabref/logic/remote/server/RemoteListenerServerThread.java @@ -2,13 +2,11 @@ import java.io.IOException; -import org.jabref.preferences.PreferencesService; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This thread wrapper is required to be able to interrupt the remote listener while listening on a port. + * This thread wrapper is required to be able to interrupt the remote listener server, e.g. when JabRef is closing down the server should shutdown as well. */ public class RemoteListenerServerThread extends Thread { @@ -16,17 +14,16 @@ public class RemoteListenerServerThread extends Thread { private final RemoteListenerServer server; - public RemoteListenerServerThread(MessageHandler messageHandler, int port, PreferencesService preferencesService) throws IOException { - this.server = new RemoteListenerServer(messageHandler, port, preferencesService); + public RemoteListenerServerThread(RemoteMessageHandler messageHandler, int port) throws IOException { + this.server = new RemoteListenerServer(messageHandler, port); this.setName("JabRef - Remote Listener Server on port " + port); } @Override public void interrupt() { - super.interrupt(); - LOGGER.debug("Interrupting " + this.getName()); this.server.closeServerSocket(); + super.interrupt(); } @Override diff --git a/src/main/java/org/jabref/logic/remote/server/RemoteMessageHandler.java b/src/main/java/org/jabref/logic/remote/server/RemoteMessageHandler.java new file mode 100644 index 00000000000..a90a35e953f --- /dev/null +++ b/src/main/java/org/jabref/logic/remote/server/RemoteMessageHandler.java @@ -0,0 +1,7 @@ +package org.jabref.logic.remote.server; + +@FunctionalInterface +public interface RemoteMessageHandler { + + void handleCommandLineArguments(String[] message); +} diff --git a/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java b/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java index 822bff67592..bb2cdc8dc90 100644 --- a/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java +++ b/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java @@ -3,9 +3,8 @@ import java.io.IOException; import org.jabref.logic.remote.client.RemoteClient; -import org.jabref.logic.remote.server.MessageHandler; -import org.jabref.logic.remote.server.RemoteListenerServerLifecycle; -import org.jabref.preferences.PreferencesService; +import org.jabref.logic.remote.server.RemoteListenerServerManager; +import org.jabref.logic.remote.server.RemoteMessageHandler; import org.jabref.support.DisabledOnCIServer; import org.junit.jupiter.api.AfterEach; @@ -24,18 +23,16 @@ class RemoteCommunicationTest { private RemoteClient client; - private RemoteListenerServerLifecycle serverLifeCycle; - private MessageHandler server; - private PreferencesService preferencesService; + private RemoteListenerServerManager serverLifeCycle; + private RemoteMessageHandler server; @BeforeEach void setUp() { final int port = 34567; - server = mock(MessageHandler.class); - preferencesService = mock(PreferencesService.class); - serverLifeCycle = new RemoteListenerServerLifecycle(); - serverLifeCycle.openAndStart(server, port, preferencesService); + server = mock(RemoteMessageHandler.class); + serverLifeCycle = new RemoteListenerServerManager(); + serverLifeCycle.openAndStart(server, port); client = new RemoteClient(port); } @@ -56,7 +53,7 @@ void commandLineArgumentSinglePassedToServer() { client.sendCommandLineArguments(message); - verify(server).handleCommandLineArguments(message, preferencesService); + verify(server).handleCommandLineArguments(message); } @Test @@ -65,7 +62,7 @@ void commandLineArgumentTwoPassedToServer() { client.sendCommandLineArguments(message); - verify(server).handleCommandLineArguments(message, preferencesService); + verify(server).handleCommandLineArguments(message); } @Test @@ -74,7 +71,7 @@ void commandLineArgumentMultiLinePassedToServer() { client.sendCommandLineArguments(message); - verify(server).handleCommandLineArguments(message, preferencesService); + verify(server).handleCommandLineArguments(message); } @Test @@ -84,6 +81,6 @@ void commandLineArgumentEncodingAndDecoding() { // will be encoded as "D%3A%5CT+EST%5C%E6%B5%8B%E8%AF%95te+st.bib" client.sendCommandLineArguments(message); - verify(server).handleCommandLineArguments(message, preferencesService); + verify(server).handleCommandLineArguments(message); } } diff --git a/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java b/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java index cd3f7905c13..8a76fe4c3ae 100644 --- a/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java +++ b/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java @@ -7,10 +7,9 @@ import java.nio.charset.StandardCharsets; import org.jabref.logic.remote.client.RemoteClient; -import org.jabref.logic.remote.server.MessageHandler; -import org.jabref.logic.remote.server.RemoteListenerServerLifecycle; +import org.jabref.logic.remote.server.RemoteListenerServerManager; +import org.jabref.logic.remote.server.RemoteMessageHandler; import org.jabref.logic.util.OS; -import org.jabref.preferences.PreferencesService; import org.jabref.support.DisabledOnCIServer; import org.junit.jupiter.api.BeforeEach; @@ -20,24 +19,18 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -/** - * Tests where the remote client and server setup is wrong. - */ @DisabledOnCIServer("Tests fails sporadically on CI server") class RemoteSetupTest { - private MessageHandler messageHandler; - private PreferencesService preferencesService; + private RemoteMessageHandler messageHandler; @BeforeEach void setUp() { - messageHandler = mock(MessageHandler.class); - preferencesService = mock(PreferencesService.class); + messageHandler = mock(RemoteMessageHandler.class); } @Test @@ -45,12 +38,12 @@ void testGoodCase() { final int port = 34567; final String[] message = new String[]{"MYMESSAGE"}; - try (RemoteListenerServerLifecycle server = new RemoteListenerServerLifecycle()) { + try (RemoteListenerServerManager server = new RemoteListenerServerManager()) { assertFalse(server.isOpen()); - server.openAndStart(messageHandler, port, preferencesService); + server.openAndStart(messageHandler, port); assertTrue(server.isOpen()); assertTrue(new RemoteClient(port).sendCommandLineArguments(message)); - verify(messageHandler).handleCommandLineArguments(message, preferencesService); + verify(messageHandler).handleCommandLineArguments(message); server.stop(); assertFalse(server.isOpen()); } @@ -61,13 +54,13 @@ void testGoodCaseWithAllLifecycleMethods() { final int port = 34567; final String[] message = new String[]{"MYMESSAGE"}; - try (RemoteListenerServerLifecycle server = new RemoteListenerServerLifecycle()) { + try (RemoteListenerServerManager server = new RemoteListenerServerManager()) { assertFalse(server.isOpen()); assertTrue(server.isNotStartedBefore()); server.stop(); assertFalse(server.isOpen()); assertTrue(server.isNotStartedBefore()); - server.open(messageHandler, port, preferencesService); + server.open(messageHandler, port); assertTrue(server.isOpen()); assertTrue(server.isNotStartedBefore()); server.start(); @@ -75,7 +68,7 @@ void testGoodCaseWithAllLifecycleMethods() { assertFalse(server.isNotStartedBefore()); assertTrue(new RemoteClient(port).sendCommandLineArguments(message)); - verify(messageHandler).handleCommandLineArguments(message, preferencesService); + verify(messageHandler).handleCommandLineArguments(message); server.stop(); assertFalse(server.isOpen()); assertTrue(server.isNotStartedBefore()); @@ -91,11 +84,11 @@ void testPortAlreadyInUse() throws IOException { try (ServerSocket socket = new ServerSocket(port)) { assertTrue(socket.isBound()); - try (RemoteListenerServerLifecycle server = new RemoteListenerServerLifecycle()) { + try (RemoteListenerServerManager server = new RemoteListenerServerManager()) { assertFalse(server.isOpen()); - server.openAndStart(messageHandler, port, preferencesService); + server.openAndStart(messageHandler, port); assertFalse(server.isOpen()); - verify(messageHandler, never()).handleCommandLineArguments(any(), eq(preferencesService)); + verify(messageHandler, never()).handleCommandLineArguments(any()); } } } @@ -138,8 +131,8 @@ void pingReturnsFalseForNoServerListening() throws IOException, InterruptedExcep void pingReturnsTrueWhenServerIsRunning() { final int port = 34567; - try (RemoteListenerServerLifecycle server = new RemoteListenerServerLifecycle()) { - server.openAndStart(messageHandler, port, preferencesService); + try (RemoteListenerServerManager server = new RemoteListenerServerManager()) { + server.openAndStart(messageHandler, port); assertTrue(new RemoteClient(port).ping()); }