From a9ee9c1bdc9d69dc41689eb48f94c3286c110b96 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Sat, 5 Feb 2022 00:51:16 +0100 Subject: [PATCH] Rework beacon to support more cli features --- .../main/java/io/xpipe/api/DataSource.java | 3 +- .../io/xpipe/api/impl/DataSourceImpl.java | 3 +- .../java/io/xpipe/api/test/XPipeConfig.java | 18 ++++---- .../java/io/xpipe/beacon/BeaconServer.java | 15 +++++-- .../beacon/exchange/EditExecuteExchange.java | 43 +++++++++++++++++++ .../exchange/EditPreparationExchange.java | 43 +++++++++++++++++++ beacon/src/main/java/module-info.java | 2 + core/build.gradle | 1 + .../core/source/DataSourceReference.java | 2 +- .../io/xpipe/core/test/DataSourceIdTest.java | 5 +-- .../core/test/DataSourceReferenceTest.java | 31 +++++++++++++ 11 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 beacon/src/main/java/io/xpipe/beacon/exchange/EditExecuteExchange.java create mode 100644 beacon/src/main/java/io/xpipe/beacon/exchange/EditPreparationExchange.java create mode 100644 core/src/test/java/io/xpipe/core/test/DataSourceReferenceTest.java diff --git a/api/src/main/java/io/xpipe/api/DataSource.java b/api/src/main/java/io/xpipe/api/DataSource.java index f80eccc47..4f230a20a 100644 --- a/api/src/main/java/io/xpipe/api/DataSource.java +++ b/api/src/main/java/io/xpipe/api/DataSource.java @@ -33,7 +33,8 @@ public interface DataSource { * @return a reference to the data source that can be used to access the underlying data source */ static DataSource get(DataSourceId id) { - return DataSourceImpl.get(id); + return null; + //return DataSourceImpl.get(id); } static DataSource wrap(InputStream in, String type, Map configOptions) { diff --git a/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java b/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java index c55dd0476..bcb709172 100644 --- a/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java +++ b/api/src/main/java/io/xpipe/api/impl/DataSourceImpl.java @@ -11,6 +11,7 @@ import io.xpipe.beacon.exchange.StoreResourceExchange; import io.xpipe.beacon.exchange.StoreStreamExchange; import io.xpipe.core.source.DataSourceConfig; import io.xpipe.core.source.DataSourceId; +import io.xpipe.core.source.DataSourceReference; import java.io.InputStream; import java.net.URL; @@ -18,7 +19,7 @@ import java.util.Map; public abstract class DataSourceImpl implements DataSource { - public static DataSource get(DataSourceId ds) { + public static DataSource get(DataSourceReference ds) { final DataSource[] source = new DataSource[1]; new XPipeApiConnector() { @Override diff --git a/api/src/test/java/io/xpipe/api/test/XPipeConfig.java b/api/src/test/java/io/xpipe/api/test/XPipeConfig.java index 5d5f045a4..0b634ae92 100644 --- a/api/src/test/java/io/xpipe/api/test/XPipeConfig.java +++ b/api/src/test/java/io/xpipe/api/test/XPipeConfig.java @@ -1,10 +1,10 @@ package io.xpipe.api.test; +import io.xpipe.beacon.BeaconClient; +import io.xpipe.beacon.BeaconServer; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; -import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL; - public class XPipeConfig implements BeforeAllCallback, ExtensionContext.Store.CloseableResource { private static boolean started = false; @@ -13,15 +13,17 @@ public class XPipeConfig implements BeforeAllCallback, ExtensionContext.Store.Cl public void beforeAll(ExtensionContext context) throws Exception { if (!started) { started = true; - // Your "before all tests" startup logic goes here - // The following line registers a callback hook when the root test context is shut down - context.getRoot().getStore(GLOBAL).put("any unique name", this); - //BeaconServer.start(); + if (BeaconServer.tryStart()) { + throw new AssertionError(); + } } } @Override - public void close() { - // Your "after all tests" logic goes here + public void close() throws Exception { + var client = new BeaconClient(); + if (BeaconServer.tryStop(client)) { + throw new AssertionError(); + } } } diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java b/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java index 4d91c72f9..79f24b56c 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconServer.java @@ -1,5 +1,7 @@ package io.xpipe.beacon; +import io.xpipe.beacon.exchange.StopExchange; + import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; @@ -42,6 +44,11 @@ public class BeaconServer { return false; } + public static boolean tryStop(BeaconClient client) throws Exception { + StopExchange.Response res = client.simpleExchange(StopExchange.Request.builder().build()); + return res.isSuccess(); + } + private static Optional getPortableLauncherExecutable() { var env = System.getenv("XPIPE_HOME"); Path file = null; @@ -49,9 +56,9 @@ public class BeaconServer { // Prepare for invalid XPIPE_HOME path value try { if (System.getProperty("os.name").startsWith("Windows")) { - file = Path.of(env, "xpipe-launcher.exe"); + file = Path.of(env, "xpipe_launcher.exe"); } else { - file = Path.of(env, "xpipe-launcher"); + file = Path.of(env, "xpipe_launcher"); } return Files.exists(file) ? Optional.of(file) : Optional.empty(); } catch (Exception ex) { @@ -68,9 +75,9 @@ public class BeaconServer { try { Path file = null; if (System.getProperty("os.name").startsWith("Windows")) { - file = Path.of(System.getenv("LOCALAPPDATA"), "X-Pipe", "xpipe-launcher.exe"); + file = Path.of(System.getenv("LOCALAPPDATA"), "X-Pipe", "xpipe_launcher.exe"); } else { - file = Path.of("/opt/xpipe/xpipe-launcher"); + file = Path.of("/opt/xpipe/xpipe_launcher"); } return Files.exists(file) ? Optional.of(file) : Optional.empty(); } catch (Exception ex) { diff --git a/beacon/src/main/java/io/xpipe/beacon/exchange/EditExecuteExchange.java b/beacon/src/main/java/io/xpipe/beacon/exchange/EditExecuteExchange.java new file mode 100644 index 000000000..4095d2737 --- /dev/null +++ b/beacon/src/main/java/io/xpipe/beacon/exchange/EditExecuteExchange.java @@ -0,0 +1,43 @@ +package io.xpipe.beacon.exchange; + +import io.xpipe.beacon.message.RequestMessage; +import io.xpipe.beacon.message.ResponseMessage; +import io.xpipe.core.source.DataSourceConfigInstance; +import io.xpipe.core.source.DataSourceReference; +import lombok.Builder; +import lombok.NonNull; +import lombok.Value; +import lombok.extern.jackson.Jacksonized; + +public class EditExecuteExchange implements MessageExchange { + + @Override + public String getId() { + return "editExecute"; + } + + @Override + public Class getRequestClass() { + return EditExecuteExchange.Request.class; + } + + @Override + public Class getResponseClass() { + return EditExecuteExchange.Response.class; + } + + @Jacksonized + @Builder + @Value + public static class Request implements RequestMessage { + @NonNull DataSourceReference ref; + @NonNull + DataSourceConfigInstance config; + } + + @Jacksonized + @Builder + @Value + public static class Response implements ResponseMessage { + } +} diff --git a/beacon/src/main/java/io/xpipe/beacon/exchange/EditPreparationExchange.java b/beacon/src/main/java/io/xpipe/beacon/exchange/EditPreparationExchange.java new file mode 100644 index 000000000..efe7010df --- /dev/null +++ b/beacon/src/main/java/io/xpipe/beacon/exchange/EditPreparationExchange.java @@ -0,0 +1,43 @@ +package io.xpipe.beacon.exchange; + +import io.xpipe.beacon.message.RequestMessage; +import io.xpipe.beacon.message.ResponseMessage; +import io.xpipe.core.source.DataSourceConfigInstance; +import io.xpipe.core.source.DataSourceReference; +import lombok.Builder; +import lombok.NonNull; +import lombok.Value; +import lombok.extern.jackson.Jacksonized; + +public class EditPreparationExchange implements MessageExchange { + + @Override + public String getId() { + return "editPreparation"; + } + + @Override + public Class getRequestClass() { + return EditPreparationExchange.Request.class; + } + + @Override + public Class getResponseClass() { + return EditPreparationExchange.Response.class; + } + + @Jacksonized + @Builder + @Value + public static class Request implements RequestMessage { + @NonNull + DataSourceReference ref; + } + + @Jacksonized + @Builder + @Value + public static class Response implements ResponseMessage { + DataSourceConfigInstance config; + } +} diff --git a/beacon/src/main/java/module-info.java b/beacon/src/main/java/module-info.java index 4a496f99b..b2c306b81 100644 --- a/beacon/src/main/java/module-info.java +++ b/beacon/src/main/java/module-info.java @@ -33,5 +33,7 @@ module io.xpipe.beacon { DialogExchange, InfoExchange, PreStoreExchange, + EditPreparationExchange, + EditExecuteExchange, VersionExchange; } \ No newline at end of file diff --git a/core/build.gradle b/core/build.gradle index a7ba8991e..0d7423123 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -8,6 +8,7 @@ plugins { apply from: "$rootDir/deps/java.gradle" apply from: "$rootDir/deps/jackson.gradle" apply from: "$rootDir/deps/lombok.gradle" +apply from: "$rootDir/deps/junit.gradle" apply from: 'publish.gradle' apply from: "$rootDir/deps/publish-base.gradle" diff --git a/core/src/main/java/io/xpipe/core/source/DataSourceReference.java b/core/src/main/java/io/xpipe/core/source/DataSourceReference.java index b717122e4..14c55b1a2 100644 --- a/core/src/main/java/io/xpipe/core/source/DataSourceReference.java +++ b/core/src/main/java/io/xpipe/core/source/DataSourceReference.java @@ -21,7 +21,7 @@ public interface DataSourceReference { return new Id(DataSourceId.fromString(s)); } - return new Name(s); + return new Name(s.trim()); } enum Type { diff --git a/core/src/test/java/io/xpipe/core/test/DataSourceIdTest.java b/core/src/test/java/io/xpipe/core/test/DataSourceIdTest.java index 47155950e..1bc50cc5d 100644 --- a/core/src/test/java/io/xpipe/core/test/DataSourceIdTest.java +++ b/core/src/test/java/io/xpipe/core/test/DataSourceIdTest.java @@ -10,9 +10,6 @@ public class DataSourceIdTest { @Test public void testCreateInvalidParameters() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - DataSourceId.create(null, "abc"); - }); Assertions.assertThrows(IllegalArgumentException.class, () -> { DataSourceId.create("a:bc", "abc"); }); @@ -46,7 +43,7 @@ public class DataSourceIdTest { } @ParameterizedTest - @ValueSource(strings = {"abc", "abc:", "ab::c", ":abc", " :ab", "::::", "", " "}) + @ValueSource(strings = {"abc", "abc:", "ab::c", "::abc", " ab", "::::", "", " "}) public void testFromStringInvalidParameters(String arg) { Assertions.assertThrows(IllegalArgumentException.class, () -> { DataSourceId.fromString(arg); diff --git a/core/src/test/java/io/xpipe/core/test/DataSourceReferenceTest.java b/core/src/test/java/io/xpipe/core/test/DataSourceReferenceTest.java new file mode 100644 index 000000000..4f8bd3638 --- /dev/null +++ b/core/src/test/java/io/xpipe/core/test/DataSourceReferenceTest.java @@ -0,0 +1,31 @@ +package io.xpipe.core.test; + +import io.xpipe.core.source.DataSourceId; +import io.xpipe.core.source.DataSourceReference; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class DataSourceReferenceTest { + + @Test + public void parseValidParameters() { + Assertions.assertEquals(DataSourceReference.parse(" ").getType(), DataSourceReference.Type.EMPTY); + Assertions.assertEquals(DataSourceReference.parse(null).getType(), DataSourceReference.Type.EMPTY); + + Assertions.assertEquals(DataSourceReference.parse("abc").getType(), DataSourceReference.Type.NAME); + Assertions.assertEquals(DataSourceReference.parse(" abc_ d e").getName(), "abc_ d e"); + + Assertions.assertEquals(DataSourceReference.parse("ab:c").getId(), DataSourceId.fromString(" AB: C ")); + Assertions.assertEquals(DataSourceReference.parse(" ab:c ").getId(), DataSourceId.fromString("ab:c ")); + } + + @ParameterizedTest + @ValueSource(strings = {"abc:", "ab::c", "::abc"}) + public void parseInvalidParameters(String arg) { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + DataSourceReference.parse(arg); + }); + } +}