From eecb5f0a8651efa1731142764754dd43605baae4 Mon Sep 17 00:00:00 2001 From: Hanno Embregts Date: Sun, 12 Feb 2023 16:07:56 +0100 Subject: [PATCH 1/4] feature: Copy snippet to clipboard in case of single result --- README.md | 2 +- .../mcs/search/printer/CoordinatePrinter.java | 14 ++++++- .../search/printer/clipboard/Clipboard.java | 7 ++++ .../printer/clipboard/SystemClipboard.java | 40 +++++++++++++++++++ .../search/printer/CoordinatePrinterTest.java | 13 +++++- .../printer/clipboard/ClipboardTest.java | 25 ++++++++++++ 6 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/main/java/it/mulders/mcs/search/printer/clipboard/Clipboard.java create mode 100644 src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java create mode 100644 src/test/java/it/mulders/mcs/search/printer/clipboard/ClipboardTest.java diff --git a/README.md b/README.md index 26002965..609db5c2 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ This setup does not touch your computer - as soon as you close your browser tab, ## Ideas for future development * [ ] Proper support for multiple classifiers at a particular coordinate. -* [ ] Immediately copy the pom.xml snippet to the clipboard. +* [x] Immediately copy the pom.xml snippet to the clipboard. * [x] Show the coordinates in a different form (Ivy, Gradle, SBT). diff --git a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java index fb98f4cf..cbd93651 100644 --- a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java @@ -2,6 +2,8 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.Clipboard; +import it.mulders.mcs.search.printer.clipboard.SystemClipboard; import java.io.PrintStream; @@ -18,8 +20,18 @@ default void print(final SearchQuery query, final SearchResponse.Response respon } var doc = response.docs()[0]; + String coordinates = provideCoordinates(doc.g(), doc.a(), first(doc.v(), doc.latestVersion())); + + stream.println(); + stream.println(coordinates); stream.println(); - stream.println(provideCoordinates(doc.g(), doc.a(), first(doc.v(), doc.latestVersion()))); + + copyToClipboard(stream, new SystemClipboard(), coordinates); + } + + private void copyToClipboard(PrintStream stream, Clipboard clipboard, String coordinates) { + clipboard.copy(coordinates); + stream.println("Snippet copied to clipboard."); stream.println(); } diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/Clipboard.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/Clipboard.java new file mode 100644 index 00000000..c87b65da --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/Clipboard.java @@ -0,0 +1,7 @@ +package it.mulders.mcs.search.printer.clipboard; + +public sealed interface Clipboard permits SystemClipboard { + void copy(String text); + String paste(); +} + diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java new file mode 100644 index 00000000..38aaaa97 --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java @@ -0,0 +1,40 @@ +package it.mulders.mcs.search.printer.clipboard; + +import java.awt.*; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; + +import static java.awt.datatransfer.DataFlavor.stringFlavor; + +public final class SystemClipboard implements Clipboard { + java.awt.datatransfer.Clipboard systemClipboard; + + public SystemClipboard() { + this.systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + } + + @Override + public void copy(String text) { + var clipboardData = new StringSelection(text); + systemClipboard.setContents(clipboardData, clipboardData); + } + + @Override + public String paste() { + Transferable clipboardData = systemClipboard.getContents(null); + String text = ""; + boolean hasTransferableText = clipboardData != null && clipboardData.isDataFlavorSupported(stringFlavor); + + if (hasTransferableText) { + try { + text = (String) clipboardData.getTransferData(stringFlavor); + } catch (IOException | UnsupportedFlavorException e) { + throw new RuntimeException(e); + } + } + + return text; + } +} \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java index e7b12383..35c9b422 100644 --- a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java @@ -2,6 +2,8 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.Clipboard; +import it.mulders.mcs.search.printer.clipboard.SystemClipboard; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -34,7 +36,7 @@ class CoordinatePrinterTest implements WithAssertions { """; private static final String GRADLE_GROOVY_OUTPUT = "implementation group: 'org.codehaus.plexus', name: 'plexus-utils', version: '3.4.1'"; - private static final String GRADLE_GROOVY_SHORT_OUTPUT = "'org.codehaus.plexus:plexus-utils:3.4.1'"; + private static final String GRADLE_GROOVY_SHORT_OUTPUT = "implementation 'org.codehaus.plexus:plexus-utils:3.4.1'"; private static final String GRADLE_KOTLIN_OUTPUT = "implementation(\"org.codehaus.plexus:plexus-utils:3.4.1\")"; private static final String SBT_OUTPUT = """ libraryDependencies += "org.codehaus.plexus" % "plexus-utils" % "3.4.1" @@ -51,6 +53,7 @@ class CoordinatePrinterTest implements WithAssertions { private static final String BUILDR_OUTPUT = "'org.codehaus.plexus:plexus-utils:jar:3.4.1'"; private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + private final Clipboard clipboard = new SystemClipboard(); private static Stream coordinatePrinters() { return Stream.of( @@ -74,4 +77,12 @@ void should_print_snippet(CoordinatePrinter printer, String expected) { assertThat(xml).containsIgnoringWhitespaces(expected); } + + @ParameterizedTest + @MethodSource("coordinatePrinters") + void should_copy_to_clipboard(CoordinatePrinter printer, String expected) { + printer.print(QUERY, RESPONSE, new PrintStream(buffer)); + + assertThat(clipboard.paste()).isEqualToIgnoringWhitespace(expected); + } } \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/printer/clipboard/ClipboardTest.java b/src/test/java/it/mulders/mcs/search/printer/clipboard/ClipboardTest.java new file mode 100644 index 00000000..cbb007e6 --- /dev/null +++ b/src/test/java/it/mulders/mcs/search/printer/clipboard/ClipboardTest.java @@ -0,0 +1,25 @@ +package it.mulders.mcs.search.printer.clipboard; + +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.Test; + +class ClipboardTest implements WithAssertions { + private final Clipboard clipboard = new SystemClipboard(); + + @Test + void paste_result_should_be_equal_to_copied_text() { + var singleLineText = "'org.codehaus.plexus:plexus-utils:jar:3.4.1'"; + clipboard.copy(singleLineText); + assertThat(clipboard.paste()).isEqualTo(singleLineText); + + var multiLineText = """ + + org.codehaus.plexus + plexus-utils + 3.4.1 + + """; + clipboard.copy(multiLineText); + assertThat(clipboard.paste()).isEqualTo(multiLineText); + } +} \ No newline at end of file From 027e768603c106b54572515616f9278174d6ee1b Mon Sep 17 00:00:00 2001 From: Hanno Embregts Date: Mon, 13 Feb 2023 14:14:57 +0100 Subject: [PATCH 2/4] chore(build): Introduce a dummy display to run clipboard tests on a headless JDK --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 11719927..ac28ae76 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,5 +29,7 @@ jobs: path: ~/.m2 key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-m2 + - name: Install xvfb for headless unit tests + run: sudo apt install xvfb - name: Build with Maven - run: mvn -B verify + run: xvfb-run mvn -B verify From e74cb75acc2d952a414043f77d8300f06f56732b Mon Sep 17 00:00:00 2001 From: Hanno Embregts Date: Mon, 27 Mar 2023 20:30:35 +0200 Subject: [PATCH 3/4] Introduce -c/--copy flag to control copy behaviour --- src/main/java/it/mulders/mcs/cli/Cli.java | 30 +++++++++++++++-- .../mcs/search/SearchCommandHandler.java | 10 +++--- .../mcs/search/printer/CoordinatePrinter.java | 25 +++++++++++--- .../printer/DelegatingOutputPrinter.java | 10 +++--- .../mcs/search/printer/NoOutputPrinter.java | 4 ++- .../mcs/search/printer/OutputPrinter.java | 4 ++- .../search/printer/TabularOutputPrinter.java | 4 ++- .../CopyToClipboardConfiguration.java | 4 +++ .../clipboard/CopyToClipboardPrinter.java | 11 +++++++ .../printer/clipboard/SystemClipboard.java | 2 +- src/test/java/it/mulders/mcs/cli/CliTest.java | 6 +++- .../mcs/search/SearchCommandHandlerTest.java | 29 +++++++++------- .../search/printer/CoordinatePrinterTest.java | 33 ++++++++++++++++--- .../printer/DelegatingOutputPrinterTest.java | 30 +++++++++-------- .../search/printer/NoOutputPrinterTest.java | 6 ++-- .../printer/TabularOutputPrinterTest.java | 15 +++++---- 16 files changed, 166 insertions(+), 57 deletions(-) create mode 100644 src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java create mode 100644 src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java diff --git a/src/main/java/it/mulders/mcs/cli/Cli.java b/src/main/java/it/mulders/mcs/cli/Cli.java index ed2a38ed..8446cbfd 100644 --- a/src/main/java/it/mulders/mcs/cli/Cli.java +++ b/src/main/java/it/mulders/mcs/cli/Cli.java @@ -4,6 +4,7 @@ import it.mulders.mcs.search.SearchCommandHandler; import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.printer.CoordinatePrinter; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import picocli.CommandLine; import java.util.concurrent.Callable; @@ -15,6 +16,8 @@ versionProvider = ClasspathVersionProvider.class ) public class Cli { + final static String COPY_SHORT_FLAG_NAME = "-c"; + final static String COPY_LONG_FLAG_NAME = "--copy"; @CommandLine.Option( names = { "-V", "--version" }, @@ -39,6 +42,13 @@ public ClassSearchCommand createClassSearchCommand() { return new ClassSearchCommand(); } + private CopyToClipboardConfiguration createCopyToClipboardConfiguration(boolean copyToClipboard) { + return new CopyToClipboardConfiguration( + COPY_SHORT_FLAG_NAME, + COPY_LONG_FLAG_NAME, + copyToClipboard); + } + @CommandLine.Command( name = "search", description = "Search artifacts in Maven Central by coordinates", @@ -55,6 +65,14 @@ public class SearchCommand implements Callable { ) private String[] query; + @CommandLine.Option( + names = { COPY_SHORT_FLAG_NAME, COPY_LONG_FLAG_NAME }, + negatable = true, + arity = "0", + description = "Copy dependency definition to the clipboard when a single result is found" + ) + private boolean copyToClipboard; + @CommandLine.Option( names = { "-l", "--limit" }, description = "Show results", @@ -83,7 +101,7 @@ public Integer call() { CoordinatePrinter coordinatePrinter = FormatType.providePrinter(responseFormat); var searchCommandHandler = new SearchCommandHandler(coordinatePrinter); - searchCommandHandler.search(searchQuery); + searchCommandHandler.search(searchQuery, createCopyToClipboardConfiguration(copyToClipboard)); return 0; } } @@ -102,6 +120,14 @@ public class ClassSearchCommand implements Callable { ) private String query; + @CommandLine.Option( + names = { COPY_SHORT_FLAG_NAME, COPY_LONG_FLAG_NAME }, + negatable = true, + arity = "0", + description = "Copy dependency definition to the clipboard when a single result is found" + ) + private boolean copyToClipboard; + @CommandLine.Option( names = { "-f", "--full-name" }, negatable = true, @@ -125,7 +151,7 @@ public Integer call() { .withLimit(limit) .build(); var searchCommandHandler = new SearchCommandHandler(); - searchCommandHandler.search(searchQuery); + searchCommandHandler.search(searchQuery, createCopyToClipboardConfiguration(copyToClipboard)); return 0; } } diff --git a/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java b/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java index f68a0d00..8a2b7e3b 100644 --- a/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java +++ b/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java @@ -3,6 +3,7 @@ import it.mulders.mcs.common.Result; import it.mulders.mcs.search.printer.DelegatingOutputPrinter; import it.mulders.mcs.search.printer.OutputPrinter; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import static it.mulders.mcs.search.Constants.MAX_LIMIT; @@ -24,10 +25,10 @@ public SearchCommandHandler(final OutputPrinter coordinateOutput) { this.outputPrinter = outputPrinter; } - public void search(final SearchQuery query) { + public void search(final SearchQuery query, final CopyToClipboardConfiguration configuration) { performSearch(query) .map(response -> performAdditionalSearch(query, response)) - .ifPresent(response -> printResponse(query, response)); + .ifPresent(response -> printResponse(query, response, configuration)); } private SearchResponse.Response performAdditionalSearch(final SearchQuery query, @@ -64,7 +65,8 @@ private Result performSearch(final SearchQuery query) { .map(SearchResponse::response); } - private void printResponse(final SearchQuery query, final SearchResponse.Response response) { - outputPrinter.print(query, response, System.out); + private void printResponse(final SearchQuery query, final SearchResponse.Response response, + final CopyToClipboardConfiguration configuration) { + outputPrinter.print(query, response, System.out, configuration); } } diff --git a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java index cbd93651..beb449e8 100644 --- a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java @@ -3,18 +3,21 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; import it.mulders.mcs.search.printer.clipboard.Clipboard; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardPrinter; import it.mulders.mcs.search.printer.clipboard.SystemClipboard; import java.io.PrintStream; -public sealed interface CoordinatePrinter extends OutputPrinter +public sealed interface CoordinatePrinter extends OutputPrinter, CopyToClipboardPrinter permits BuildrOutput, GradleGroovyOutput, GradleGroovyShortOutput, GradleKotlinOutput, GrapeOutput, IvyXmlOutput, LeiningenOutput, PomXmlOutput, SbtOutput { String provideCoordinates(final String group, final String artifact, final String version); @Override - default void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) { + default void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, + final CopyToClipboardConfiguration copyToClipboardConfiguration) { if (response.numFound() != 1) { throw new IllegalArgumentException("Search response with more than one result not expected here"); } @@ -26,13 +29,25 @@ default void print(final SearchQuery query, final SearchResponse.Response respon stream.println(coordinates); stream.println(); - copyToClipboard(stream, new SystemClipboard(), coordinates); + if (copyToClipboardConfiguration.copyOnClipboardEnabled()) { + copyToClipboard(stream, new SystemClipboard(), coordinates); + } else { + printCopyToClipboardHint(stream, copyToClipboardConfiguration); + } + + stream.println(); } - private void copyToClipboard(PrintStream stream, Clipboard clipboard, String coordinates) { + private static void copyToClipboard(PrintStream stream, Clipboard clipboard, String coordinates) { clipboard.copy(coordinates); stream.println("Snippet copied to clipboard."); - stream.println(); + } + + private static void printCopyToClipboardHint(PrintStream stream, + CopyToClipboardConfiguration copyToClipboardConfiguration) { + stream.printf("(To directly copy this snippet to the clipboard, run mcs with the %s or %s flag.)%n", + copyToClipboardConfiguration.shortFlagName(), + copyToClipboardConfiguration.longFlagName()); } private String first(final String... values) { diff --git a/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java index 70f20a88..16126780 100644 --- a/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java @@ -2,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import java.io.PrintStream; @@ -25,11 +26,12 @@ public DelegatingOutputPrinter(final OutputPrinter coordinateOutput) { } @Override - public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) { + public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, + final CopyToClipboardConfiguration copyToClipboardConfiguration) { switch (response.numFound()) { - case 0 -> noOutput.print(query, response, stream); - case 1 -> coordinateOutput.print(query, response, stream); - default -> tabularSearchOutput.print(query, response, stream); + case 0 -> noOutput.print(query, response, stream, null); + case 1 -> coordinateOutput.print(query, response, stream, copyToClipboardConfiguration); + default -> tabularSearchOutput.print(query, response, stream, null); } } } diff --git a/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java index 7af1b2fe..ccb73d40 100644 --- a/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java @@ -2,12 +2,14 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import java.io.PrintStream; public class NoOutputPrinter implements OutputPrinter { @Override - public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) { + public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, + final CopyToClipboardConfiguration configuration) { if (response.numFound() != 0) { throw new IllegalArgumentException("Search response with any result not expected here"); } diff --git a/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java index fa456f77..53c63e54 100644 --- a/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java @@ -2,11 +2,13 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import java.io.PrintStream; public interface OutputPrinter { void print(final SearchQuery query, final SearchResponse.Response response, - final PrintStream stream); + final PrintStream stream, + final CopyToClipboardConfiguration configuration); } diff --git a/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java index c36b6415..09413e5f 100644 --- a/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java @@ -2,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import picocli.CommandLine; import picocli.CommandLine.Help; import picocli.CommandLine.Help.Ansi; @@ -29,7 +30,8 @@ private String header(final SearchQuery query, final SearchResponse.Response res response.numFound(), additionalMessage); } - public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) { + public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, + final CopyToClipboardConfiguration configuration) { stream.println(CommandLine.Help.Ansi.AUTO.string(header(query, response))); var colorScheme = Help.defaultColorScheme(Ansi.AUTO); diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java new file mode 100644 index 00000000..418f534a --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java @@ -0,0 +1,4 @@ +package it.mulders.mcs.search.printer.clipboard; + +public record CopyToClipboardConfiguration(String shortFlagName, String longFlagName, boolean copyOnClipboardEnabled) { +} diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java new file mode 100644 index 00000000..ebcee0b1 --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java @@ -0,0 +1,11 @@ +package it.mulders.mcs.search.printer.clipboard; + +import it.mulders.mcs.search.SearchQuery; +import it.mulders.mcs.search.SearchResponse; + +import java.io.PrintStream; + +public interface CopyToClipboardPrinter { + void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, + final CopyToClipboardConfiguration copyToClipboardConfiguration); +} diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java index 38aaaa97..07a6c79b 100644 --- a/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/SystemClipboard.java @@ -1,6 +1,6 @@ package it.mulders.mcs.search.printer.clipboard; -import java.awt.*; +import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; diff --git a/src/test/java/it/mulders/mcs/cli/CliTest.java b/src/test/java/it/mulders/mcs/cli/CliTest.java index ced6e1c0..2b28605f 100644 --- a/src/test/java/it/mulders/mcs/cli/CliTest.java +++ b/src/test/java/it/mulders/mcs/cli/CliTest.java @@ -3,6 +3,7 @@ import it.mulders.mcs.search.Constants; import it.mulders.mcs.search.SearchCommandHandler; import it.mulders.mcs.search.SearchQuery; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -73,11 +74,14 @@ void accepts_full_name_parameter() { } private void verifySearchExecution(SearchQuery query, String... args) { + var dontCopyToClipboard = new CopyToClipboardConfiguration(Cli.COPY_SHORT_FLAG_NAME, Cli.COPY_LONG_FLAG_NAME, + false); + try (MockedConstruction mocked = Mockito.mockConstruction(SearchCommandHandler.class)) { program.execute(args); assertThat(mocked.constructed()).hasSize(1); SearchCommandHandler searchCommandHandler = mocked.constructed().get(0); - verify(searchCommandHandler).search(query); + verify(searchCommandHandler).search(query, dontCopyToClipboard); } } } \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java b/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java index b73ad753..21d669ea 100644 --- a/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java +++ b/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java @@ -2,6 +2,7 @@ import it.mulders.mcs.common.Result; import it.mulders.mcs.search.printer.OutputPrinter; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayNameGeneration; @@ -17,20 +18,22 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class SearchCommandHandlerTest implements WithAssertions { private final OutputPrinter outputPrinter = mock(OutputPrinter.class); + private final CopyToClipboardConfiguration dontCopyToClipboard = new CopyToClipboardConfiguration( + "-scht", "--search-command-handler-test", false); private final SearchResponse.Response wildcardResponse = new SearchResponse.Response( 0, 0, - new SearchResponse.Response.Doc[] {} + new SearchResponse.Response.Doc[]{} ); private final SearchResponse.Response twoPartCoordinateResponse = new SearchResponse.Response( 0, 0, - new SearchResponse.Response.Doc[] {} + new SearchResponse.Response.Doc[]{} ); private final SearchResponse.Response threePartCoordinateResponse = new SearchResponse.Response( 0, 0, - new SearchResponse.Response.Doc[] {} + new SearchResponse.Response.Doc[]{} ); private final SearchClient searchClient = new SearchClient() { @Override @@ -52,8 +55,9 @@ public Result search(final SearchQuery query) { class WildcardSearchTest { @Test void should_invoke_search_client() { - handler.search(SearchQuery.search("plexus-utils").build()); - verify(outputPrinter).print(any(WildcardSearchQuery.class), eq(wildcardResponse), any()); + handler.search(SearchQuery.search("plexus-utils").build(), dontCopyToClipboard); + verify(outputPrinter).print(any(WildcardSearchQuery.class), eq(wildcardResponse), any(), + any(CopyToClipboardConfiguration.class)); } } @@ -62,20 +66,23 @@ void should_invoke_search_client() { class CoordinateSearchTest { @Test void should_reject_search_terms_in_wrong_format() { - assertThatThrownBy(() -> handler.search(SearchQuery.search("foo:bar:baz:qux").build())) - .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> handler.search(SearchQuery.search("foo:bar:baz:qux").build(), + dontCopyToClipboard)).isInstanceOf(IllegalArgumentException.class); } @Test void should_invoke_search_client_with_groupId_and_artifactId() { - handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils").build()); - verify(outputPrinter).print(any(CoordinateQuery.class), eq(twoPartCoordinateResponse), any()); + handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils").build(), dontCopyToClipboard); + verify(outputPrinter).print(any(CoordinateQuery.class), eq(twoPartCoordinateResponse), any(), + any(CopyToClipboardConfiguration.class)); } @Test void should_invoke_search_client_with_groupId_and_artifactId_and_version() { - handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils:3.4.1").build()); - verify(outputPrinter).print(any(CoordinateQuery.class), eq(threePartCoordinateResponse), any()); + handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils:3.4.1").build(), + dontCopyToClipboard); + verify(outputPrinter).print(any(CoordinateQuery.class), eq(threePartCoordinateResponse), any(), + any(CopyToClipboardConfiguration.class)); } } } \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java index 4228c436..f90fd455 100644 --- a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java @@ -3,6 +3,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; import it.mulders.mcs.search.printer.clipboard.Clipboard; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import it.mulders.mcs.search.printer.clipboard.SystemClipboard; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.params.ParameterizedTest; @@ -72,17 +73,41 @@ private static Stream coordinatePrinters() { @ParameterizedTest @MethodSource("coordinatePrinters") void should_print_snippet(CoordinatePrinter printer, String expected) { - printer.print(QUERY, RESPONSE, new PrintStream(buffer)); + var dontCopyToClipboard = new CopyToClipboardConfiguration( + "-cpt", "--coordinate-printer-test", false); + printer.print(QUERY, RESPONSE, new PrintStream(buffer), dontCopyToClipboard); var xml = buffer.toString(); - assertThat(xml).isEqualToIgnoringWhitespace(expected); + assertThat(xml).containsIgnoringWhitespaces(expected); } @ParameterizedTest @MethodSource("coordinatePrinters") - void should_copy_to_clipboard(CoordinatePrinter printer, String expected) { - printer.print(QUERY, RESPONSE, new PrintStream(buffer)); + void should_print_clipboard_hint(CoordinatePrinter printer, String expected) { + var dontCopyToClipboard = new CopyToClipboardConfiguration( + "-cpt", "--coordinate-printer-test", false); + printer.print(QUERY, RESPONSE, new PrintStream(buffer), dontCopyToClipboard); + var output = buffer.toString(); + + assertThat(output).containsIgnoringWhitespaces(String.format( + "(To directly copy this snippet to the clipboard, run mcs with the %s or %s flag.)", + dontCopyToClipboard.shortFlagName(), dontCopyToClipboard.longFlagName())); + assertThat(output).doesNotContain("Snippet copied to clipboard."); + } + + @ParameterizedTest + @MethodSource("coordinatePrinters") + void should_copy_to_clipboard_and_not_print_clipboard_hint(CoordinatePrinter printer, String expected) { + var copyToClipboard = new CopyToClipboardConfiguration( + "-cpt", "--coordinate-printer-test", true); + printer.print(QUERY, RESPONSE, new PrintStream(buffer), copyToClipboard); + var output = buffer.toString(); assertThat(clipboard.paste()).isEqualToIgnoringWhitespace(expected); + + assertThat(output).contains("Snippet copied to clipboard."); + assertThat(output).doesNotContain(String.format( + "(To directly copy this snippet to the clipboard, run mcs with the -%s or --%s flag.)", + copyToClipboard.shortFlagName(), copyToClipboard.longFlagName())); } } \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java index 384f84e2..1ee28b77 100644 --- a/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java @@ -2,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import org.apache.commons.io.output.NullOutputStream; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayNameGeneration; @@ -29,26 +30,29 @@ class DelegatingOutputPrinterTest implements WithAssertions { @Test void no_results_delegate() { - printer.print(query, responseWithResult(0), outputStream); - verify(noOutput).print(eq(query), any(), eq(outputStream)); - verify(coordinateOutput, never()).print(any(), any(), any()); - verify(tabularSearchOutput, never()).print(any(), any(), any()); + printer.print(query, responseWithResult(0), outputStream, null); + verify(noOutput).print(eq(query), any(), eq(outputStream), any()); + verify(coordinateOutput, never()).print(any(), any(), any(), any()); + verify(tabularSearchOutput, never()).print(any(), any(), any(), any()); } @Test - void one_result_delegate() { - printer.print(query, responseWithResult(1), outputStream); - verify(noOutput, never()).print(any(), any(), any()); - verify(coordinateOutput).print(eq(query), any(), eq(outputStream)); - verify(tabularSearchOutput, never()).print(any(), any(), any()); + void single_result_delegate() { + var configuration = new CopyToClipboardConfiguration("-dopt", + "--delegating-output-printer-test", true); + + printer.print(query, responseWithResult(1), outputStream, configuration); + verify(noOutput, never()).print(any(), any(), any(), any()); + verify(coordinateOutput).print(eq(query), any(), eq(outputStream), eq(configuration)); + verify(tabularSearchOutput, never()).print(any(), any(), any(), any()); } @Test void multiple_results_delegate() { - printer.print(query, responseWithResult(2), outputStream); - verify(noOutput, never()).print(any(), any(), any()); - verify(coordinateOutput, never()).print(any(), any(), any()); - verify(tabularSearchOutput).print(eq(query), any(), eq(outputStream)); + printer.print(query, responseWithResult(2), outputStream, null); + verify(noOutput, never()).print(any(), any(), any(), any()); + verify(coordinateOutput, never()).print(any(), any(), any(), any()); + verify(tabularSearchOutput).print(eq(query), any(), eq(outputStream), any()); } private SearchResponse.Response responseWithResult(int count) { diff --git a/src/test/java/it/mulders/mcs/search/printer/NoOutputPrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/NoOutputPrinterTest.java index 6f282ea9..3818ad94 100644 --- a/src/test/java/it/mulders/mcs/search/printer/NoOutputPrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/NoOutputPrinterTest.java @@ -15,15 +15,15 @@ class NoOutputPrinterTest implements WithAssertions { @Test void should_fail_with_non_empty_result() { - assertThatThrownBy(() -> printer.print(null, responseWithResult(1), new PrintStream(outputStream))) + assertThatThrownBy(() -> printer.print(null, responseWithResult(1), new PrintStream(outputStream), null)) .isInstanceOf(IllegalArgumentException.class); - assertThatThrownBy(() -> printer.print(null, responseWithResult(2), new PrintStream(outputStream))) + assertThatThrownBy(() -> printer.print(null, responseWithResult(2), new PrintStream(outputStream), null)) .isInstanceOf(IllegalArgumentException.class); } @Test void should_print_message_with_empty_result() { - printer.print(null, responseWithResult(0), new PrintStream(outputStream)); + printer.print(null, responseWithResult(0), new PrintStream(outputStream), null); assertThat(outputStream.toString(StandardCharsets.UTF_8)).contains("No results found"); } diff --git a/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java index 8fbc1eaf..efe26e00 100644 --- a/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java @@ -2,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -17,6 +18,8 @@ class TabularOutputPrinterTest implements WithAssertions { private final TabularOutputPrinter output = new TabularOutputPrinter(); private final SearchQuery query = SearchQuery.search("org.codehaus.plexus:plexus-utils").build(); + private final CopyToClipboardConfiguration dontCopyToClipboard = new CopyToClipboardConfiguration( + "-topt", "--tabular-output-printer-test", false); @Test void should_print_gav() { @@ -36,7 +39,7 @@ void should_print_gav() { // Act - output.print(query, response, new PrintStream(buffer)); + output.print(query, response, new PrintStream(buffer), dontCopyToClipboard); // Assert @@ -62,7 +65,7 @@ void should_print_last_updated() { // Act - output.print(query, response, new PrintStream(buffer)); + output.print(query, response, new PrintStream(buffer), dontCopyToClipboard); // Assert @@ -90,7 +93,7 @@ void should_mention_number_of_results() { // Act - output.print(query, response, new PrintStream(buffer)); + output.print(query, response, new PrintStream(buffer), dontCopyToClipboard); // Assert @@ -126,7 +129,7 @@ void should_mention_when_number_of_results_is_larger_than_the_search_limit() { // Act var query = SearchQuery.search("org.codehaus.plexus:plexus-utils").withLimit(2).build(); - output.print(query, response, new PrintStream(buffer)); + output.print(query, response, new PrintStream(buffer), dontCopyToClipboard); // Assert @@ -162,7 +165,7 @@ void should_not_mention_when_number_of_results_is_equal_to_the_search_limit() { // Act var query = SearchQuery.search("org.codehaus.plexus:plexus-utils").withLimit(2).build(); - output.print(query, response, new PrintStream(buffer)); + output.print(query, response, new PrintStream(buffer), dontCopyToClipboard); // Assert @@ -189,7 +192,7 @@ void should_not_mention_when_number_of_results_is_smaller_than_the_search_limit( // Act var query = SearchQuery.search("org.codehaus.plexus:plexus-utils").withLimit(2).build(); - output.print(query, response, new PrintStream(buffer)); + output.print(query, response, new PrintStream(buffer), dontCopyToClipboard); // Assert From 778f50e6fbe6d7689848c3fd63954643bb3cd22d Mon Sep 17 00:00:00 2001 From: Hanno Embregts Date: Mon, 10 Apr 2023 19:38:26 +0200 Subject: [PATCH 4/4] fix: applied fixes for PR remarks --- src/main/java/it/mulders/mcs/cli/Cli.java | 6 +++--- .../mcs/search/SearchCommandHandler.java | 6 +++--- .../mcs/search/printer/CoordinatePrinter.java | 14 +++++++------- .../printer/DelegatingOutputPrinter.java | 6 +++--- .../mcs/search/printer/NoOutputPrinter.java | 4 ++-- .../mcs/search/printer/OutputPrinter.java | 4 ++-- .../search/printer/TabularOutputPrinter.java | 4 ++-- .../clipboard/CopyToClipboardConfig.java | 4 ++++ .../CopyToClipboardConfiguration.java | 4 ---- .../clipboard/CopyToClipboardPrinter.java | 2 +- src/test/java/it/mulders/mcs/cli/CliTest.java | 4 ++-- .../mcs/search/SearchCommandHandlerTest.java | 10 +++++----- .../search/printer/CoordinatePrinterTest.java | 19 +++++++++---------- .../printer/DelegatingOutputPrinterTest.java | 4 ++-- .../printer/TabularOutputPrinterTest.java | 4 ++-- 15 files changed, 47 insertions(+), 48 deletions(-) create mode 100644 src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfig.java delete mode 100644 src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java diff --git a/src/main/java/it/mulders/mcs/cli/Cli.java b/src/main/java/it/mulders/mcs/cli/Cli.java index 8446cbfd..b8100124 100644 --- a/src/main/java/it/mulders/mcs/cli/Cli.java +++ b/src/main/java/it/mulders/mcs/cli/Cli.java @@ -4,7 +4,7 @@ import it.mulders.mcs.search.SearchCommandHandler; import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.printer.CoordinatePrinter; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import picocli.CommandLine; import java.util.concurrent.Callable; @@ -42,8 +42,8 @@ public ClassSearchCommand createClassSearchCommand() { return new ClassSearchCommand(); } - private CopyToClipboardConfiguration createCopyToClipboardConfiguration(boolean copyToClipboard) { - return new CopyToClipboardConfiguration( + private CopyToClipboardConfig createCopyToClipboardConfiguration(boolean copyToClipboard) { + return new CopyToClipboardConfig( COPY_SHORT_FLAG_NAME, COPY_LONG_FLAG_NAME, copyToClipboard); diff --git a/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java b/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java index 8a2b7e3b..de76e842 100644 --- a/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java +++ b/src/main/java/it/mulders/mcs/search/SearchCommandHandler.java @@ -3,7 +3,7 @@ import it.mulders.mcs.common.Result; import it.mulders.mcs.search.printer.DelegatingOutputPrinter; import it.mulders.mcs.search.printer.OutputPrinter; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import static it.mulders.mcs.search.Constants.MAX_LIMIT; @@ -25,7 +25,7 @@ public SearchCommandHandler(final OutputPrinter coordinateOutput) { this.outputPrinter = outputPrinter; } - public void search(final SearchQuery query, final CopyToClipboardConfiguration configuration) { + public void search(final SearchQuery query, final CopyToClipboardConfig configuration) { performSearch(query) .map(response -> performAdditionalSearch(query, response)) .ifPresent(response -> printResponse(query, response, configuration)); @@ -66,7 +66,7 @@ private Result performSearch(final SearchQuery query) { } private void printResponse(final SearchQuery query, final SearchResponse.Response response, - final CopyToClipboardConfiguration configuration) { + final CopyToClipboardConfig configuration) { outputPrinter.print(query, response, System.out, configuration); } } diff --git a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java index beb449e8..255683b9 100644 --- a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java @@ -3,7 +3,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; import it.mulders.mcs.search.printer.clipboard.Clipboard; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import it.mulders.mcs.search.printer.clipboard.CopyToClipboardPrinter; import it.mulders.mcs.search.printer.clipboard.SystemClipboard; @@ -17,7 +17,7 @@ public sealed interface CoordinatePrinter extends OutputPrinter, CopyToClipboard @Override default void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, - final CopyToClipboardConfiguration copyToClipboardConfiguration) { + final CopyToClipboardConfig copyToClipboardConfig) { if (response.numFound() != 1) { throw new IllegalArgumentException("Search response with more than one result not expected here"); } @@ -29,10 +29,10 @@ default void print(final SearchQuery query, final SearchResponse.Response respon stream.println(coordinates); stream.println(); - if (copyToClipboardConfiguration.copyOnClipboardEnabled()) { + if (copyToClipboardConfig.copyToClipboardEnabled()) { copyToClipboard(stream, new SystemClipboard(), coordinates); } else { - printCopyToClipboardHint(stream, copyToClipboardConfiguration); + printCopyToClipboardHint(stream, copyToClipboardConfig); } stream.println(); @@ -44,10 +44,10 @@ private static void copyToClipboard(PrintStream stream, Clipboard clipboard, Str } private static void printCopyToClipboardHint(PrintStream stream, - CopyToClipboardConfiguration copyToClipboardConfiguration) { + CopyToClipboardConfig copyToClipboardConfig) { stream.printf("(To directly copy this snippet to the clipboard, run mcs with the %s or %s flag.)%n", - copyToClipboardConfiguration.shortFlagName(), - copyToClipboardConfiguration.longFlagName()); + copyToClipboardConfig.shortFlagName(), + copyToClipboardConfig.longFlagName()); } private String first(final String... values) { diff --git a/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java index 16126780..cba76634 100644 --- a/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/DelegatingOutputPrinter.java @@ -2,7 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import java.io.PrintStream; @@ -27,10 +27,10 @@ public DelegatingOutputPrinter(final OutputPrinter coordinateOutput) { @Override public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, - final CopyToClipboardConfiguration copyToClipboardConfiguration) { + final CopyToClipboardConfig copyToClipboardConfig) { switch (response.numFound()) { case 0 -> noOutput.print(query, response, stream, null); - case 1 -> coordinateOutput.print(query, response, stream, copyToClipboardConfiguration); + case 1 -> coordinateOutput.print(query, response, stream, copyToClipboardConfig); default -> tabularSearchOutput.print(query, response, stream, null); } } diff --git a/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java index ccb73d40..d74dd4dc 100644 --- a/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/NoOutputPrinter.java @@ -2,14 +2,14 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import java.io.PrintStream; public class NoOutputPrinter implements OutputPrinter { @Override public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, - final CopyToClipboardConfiguration configuration) { + final CopyToClipboardConfig configuration) { if (response.numFound() != 0) { throw new IllegalArgumentException("Search response with any result not expected here"); } diff --git a/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java index 53c63e54..605af116 100644 --- a/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/OutputPrinter.java @@ -2,7 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import java.io.PrintStream; @@ -10,5 +10,5 @@ public interface OutputPrinter { void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, - final CopyToClipboardConfiguration configuration); + final CopyToClipboardConfig configuration); } diff --git a/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java b/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java index 09413e5f..99b4d2f1 100644 --- a/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/TabularOutputPrinter.java @@ -2,7 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import picocli.CommandLine; import picocli.CommandLine.Help; import picocli.CommandLine.Help.Ansi; @@ -31,7 +31,7 @@ private String header(final SearchQuery query, final SearchResponse.Response res } public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, - final CopyToClipboardConfiguration configuration) { + final CopyToClipboardConfig configuration) { stream.println(CommandLine.Help.Ansi.AUTO.string(header(query, response))); var colorScheme = Help.defaultColorScheme(Ansi.AUTO); diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfig.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfig.java new file mode 100644 index 00000000..b708ffd0 --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfig.java @@ -0,0 +1,4 @@ +package it.mulders.mcs.search.printer.clipboard; + +public record CopyToClipboardConfig(String shortFlagName, String longFlagName, boolean copyToClipboardEnabled) { +} diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java deleted file mode 100644 index 418f534a..00000000 --- a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardConfiguration.java +++ /dev/null @@ -1,4 +0,0 @@ -package it.mulders.mcs.search.printer.clipboard; - -public record CopyToClipboardConfiguration(String shortFlagName, String longFlagName, boolean copyOnClipboardEnabled) { -} diff --git a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java index ebcee0b1..a7f71f63 100644 --- a/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java @@ -7,5 +7,5 @@ public interface CopyToClipboardPrinter { void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, - final CopyToClipboardConfiguration copyToClipboardConfiguration); + final CopyToClipboardConfig copyToClipboardConfig); } diff --git a/src/test/java/it/mulders/mcs/cli/CliTest.java b/src/test/java/it/mulders/mcs/cli/CliTest.java index 2b28605f..e073a82b 100644 --- a/src/test/java/it/mulders/mcs/cli/CliTest.java +++ b/src/test/java/it/mulders/mcs/cli/CliTest.java @@ -3,7 +3,7 @@ import it.mulders.mcs.search.Constants; import it.mulders.mcs.search.SearchCommandHandler; import it.mulders.mcs.search.SearchQuery; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -74,7 +74,7 @@ void accepts_full_name_parameter() { } private void verifySearchExecution(SearchQuery query, String... args) { - var dontCopyToClipboard = new CopyToClipboardConfiguration(Cli.COPY_SHORT_FLAG_NAME, Cli.COPY_LONG_FLAG_NAME, + var dontCopyToClipboard = new CopyToClipboardConfig(Cli.COPY_SHORT_FLAG_NAME, Cli.COPY_LONG_FLAG_NAME, false); try (MockedConstruction mocked = Mockito.mockConstruction(SearchCommandHandler.class)) { diff --git a/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java b/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java index 21d669ea..80a62ff0 100644 --- a/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java +++ b/src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java @@ -2,7 +2,7 @@ import it.mulders.mcs.common.Result; import it.mulders.mcs.search.printer.OutputPrinter; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayNameGeneration; @@ -18,7 +18,7 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class SearchCommandHandlerTest implements WithAssertions { private final OutputPrinter outputPrinter = mock(OutputPrinter.class); - private final CopyToClipboardConfiguration dontCopyToClipboard = new CopyToClipboardConfiguration( + private final CopyToClipboardConfig dontCopyToClipboard = new CopyToClipboardConfig( "-scht", "--search-command-handler-test", false); private final SearchResponse.Response wildcardResponse = new SearchResponse.Response( 0, @@ -57,7 +57,7 @@ class WildcardSearchTest { void should_invoke_search_client() { handler.search(SearchQuery.search("plexus-utils").build(), dontCopyToClipboard); verify(outputPrinter).print(any(WildcardSearchQuery.class), eq(wildcardResponse), any(), - any(CopyToClipboardConfiguration.class)); + any(CopyToClipboardConfig.class)); } } @@ -74,7 +74,7 @@ void should_reject_search_terms_in_wrong_format() { void should_invoke_search_client_with_groupId_and_artifactId() { handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils").build(), dontCopyToClipboard); verify(outputPrinter).print(any(CoordinateQuery.class), eq(twoPartCoordinateResponse), any(), - any(CopyToClipboardConfiguration.class)); + any(CopyToClipboardConfig.class)); } @Test @@ -82,7 +82,7 @@ void should_invoke_search_client_with_groupId_and_artifactId_and_version() { handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils:3.4.1").build(), dontCopyToClipboard); verify(outputPrinter).print(any(CoordinateQuery.class), eq(threePartCoordinateResponse), any(), - any(CopyToClipboardConfiguration.class)); + any(CopyToClipboardConfig.class)); } } } \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java index f90fd455..8591eca3 100644 --- a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java @@ -3,7 +3,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; import it.mulders.mcs.search.printer.clipboard.Clipboard; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import it.mulders.mcs.search.printer.clipboard.SystemClipboard; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.params.ParameterizedTest; @@ -73,7 +73,7 @@ private static Stream coordinatePrinters() { @ParameterizedTest @MethodSource("coordinatePrinters") void should_print_snippet(CoordinatePrinter printer, String expected) { - var dontCopyToClipboard = new CopyToClipboardConfiguration( + var dontCopyToClipboard = new CopyToClipboardConfig( "-cpt", "--coordinate-printer-test", false); printer.print(QUERY, RESPONSE, new PrintStream(buffer), dontCopyToClipboard); var xml = buffer.toString(); @@ -84,21 +84,22 @@ void should_print_snippet(CoordinatePrinter printer, String expected) { @ParameterizedTest @MethodSource("coordinatePrinters") void should_print_clipboard_hint(CoordinatePrinter printer, String expected) { - var dontCopyToClipboard = new CopyToClipboardConfiguration( + var dontCopyToClipboard = new CopyToClipboardConfig( "-cpt", "--coordinate-printer-test", false); printer.print(QUERY, RESPONSE, new PrintStream(buffer), dontCopyToClipboard); var output = buffer.toString(); - assertThat(output).containsIgnoringWhitespaces(String.format( - "(To directly copy this snippet to the clipboard, run mcs with the %s or %s flag.)", - dontCopyToClipboard.shortFlagName(), dontCopyToClipboard.longFlagName())); + assertThat(output).containsIgnoringWhitespaces("To directly copy this snippet to the clipboard"); + assertThat(output).containsIgnoringWhitespaces("-cpt"); + assertThat(output).containsIgnoringWhitespaces("--coordinate-printer-test"); + assertThat(output).doesNotContain("Snippet copied to clipboard."); } @ParameterizedTest @MethodSource("coordinatePrinters") void should_copy_to_clipboard_and_not_print_clipboard_hint(CoordinatePrinter printer, String expected) { - var copyToClipboard = new CopyToClipboardConfiguration( + var copyToClipboard = new CopyToClipboardConfig( "-cpt", "--coordinate-printer-test", true); printer.print(QUERY, RESPONSE, new PrintStream(buffer), copyToClipboard); var output = buffer.toString(); @@ -106,8 +107,6 @@ void should_copy_to_clipboard_and_not_print_clipboard_hint(CoordinatePrinter pri assertThat(clipboard.paste()).isEqualToIgnoringWhitespace(expected); assertThat(output).contains("Snippet copied to clipboard."); - assertThat(output).doesNotContain(String.format( - "(To directly copy this snippet to the clipboard, run mcs with the -%s or --%s flag.)", - copyToClipboard.shortFlagName(), copyToClipboard.longFlagName())); + assertThat(output).doesNotContain("To directly copy this snippet to the clipboard"); } } \ No newline at end of file diff --git a/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java index 1ee28b77..41155bbf 100644 --- a/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/DelegatingOutputPrinterTest.java @@ -2,7 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import org.apache.commons.io.output.NullOutputStream; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayNameGeneration; @@ -38,7 +38,7 @@ void no_results_delegate() { @Test void single_result_delegate() { - var configuration = new CopyToClipboardConfiguration("-dopt", + var configuration = new CopyToClipboardConfig("-dopt", "--delegating-output-printer-test", true); printer.print(query, responseWithResult(1), outputStream, configuration); diff --git a/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java index efe26e00..10d8659b 100644 --- a/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/TabularOutputPrinterTest.java @@ -2,7 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; -import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfiguration; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -18,7 +18,7 @@ class TabularOutputPrinterTest implements WithAssertions { private final TabularOutputPrinter output = new TabularOutputPrinter(); private final SearchQuery query = SearchQuery.search("org.codehaus.plexus:plexus-utils").build(); - private final CopyToClipboardConfiguration dontCopyToClipboard = new CopyToClipboardConfiguration( + private final CopyToClipboardConfig dontCopyToClipboard = new CopyToClipboardConfig( "-topt", "--tabular-output-printer-test", false); @Test