diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 03ee096f..e9aafa36 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 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/cli/Cli.java b/src/main/java/it/mulders/mcs/cli/Cli.java index ed2a38ed..b8100124 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.CopyToClipboardConfig; 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 CopyToClipboardConfig createCopyToClipboardConfiguration(boolean copyToClipboard) { + return new CopyToClipboardConfig( + 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..de76e842 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.CopyToClipboardConfig; 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 CopyToClipboardConfig 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 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 fb98f4cf..255683b9 100644 --- a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java @@ -2,27 +2,54 @@ 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.CopyToClipboardConfig; +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 CopyToClipboardConfig copyToClipboardConfig) { if (response.numFound() != 1) { throw new IllegalArgumentException("Search response with more than one result not expected here"); } 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()))); + + if (copyToClipboardConfig.copyToClipboardEnabled()) { + copyToClipboard(stream, new SystemClipboard(), coordinates); + } else { + printCopyToClipboardHint(stream, copyToClipboardConfig); + } + stream.println(); } + private static void copyToClipboard(PrintStream stream, Clipboard clipboard, String coordinates) { + clipboard.copy(coordinates); + stream.println("Snippet copied to clipboard."); + } + + private static void printCopyToClipboardHint(PrintStream stream, + CopyToClipboardConfig copyToClipboardConfig) { + stream.printf("(To directly copy this snippet to the clipboard, run mcs with the %s or %s flag.)%n", + copyToClipboardConfig.shortFlagName(), + copyToClipboardConfig.longFlagName()); + } + private String first(final String... values) { for (var value : values) { if (value != null) { 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..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,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; 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 CopyToClipboardConfig copyToClipboardConfig) { 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, 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 7af1b2fe..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,12 +2,14 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +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) { + public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream, + 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 fa456f77..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,11 +2,13 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; import java.io.PrintStream; public interface OutputPrinter { void print(final SearchQuery query, final SearchResponse.Response response, - final PrintStream stream); + final PrintStream stream, + 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 c36b6415..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,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig; 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 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/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/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/CopyToClipboardPrinter.java b/src/main/java/it/mulders/mcs/search/printer/clipboard/CopyToClipboardPrinter.java new file mode 100644 index 00000000..a7f71f63 --- /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 CopyToClipboardConfig copyToClipboardConfig); +} 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..07a6c79b --- /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.Toolkit; +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/cli/CliTest.java b/src/test/java/it/mulders/mcs/cli/CliTest.java index ced6e1c0..e073a82b 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.CopyToClipboardConfig; 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 CopyToClipboardConfig(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..80a62ff0 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.CopyToClipboardConfig; 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 CopyToClipboardConfig dontCopyToClipboard = new CopyToClipboardConfig( + "-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(CopyToClipboardConfig.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(CopyToClipboardConfig.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(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 5f0fdbb6..8591eca3 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,9 @@ 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.CopyToClipboardConfig; +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; @@ -51,6 +54,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( @@ -69,9 +73,40 @@ 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 CopyToClipboardConfig( + "-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_print_clipboard_hint(CoordinatePrinter printer, String expected) { + var dontCopyToClipboard = new CopyToClipboardConfig( + "-cpt", "--coordinate-printer-test", false); + printer.print(QUERY, RESPONSE, new PrintStream(buffer), dontCopyToClipboard); + var output = buffer.toString(); + + 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 CopyToClipboardConfig( + "-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("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 384f84e2..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,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +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; @@ -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 CopyToClipboardConfig("-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..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,6 +2,7 @@ import it.mulders.mcs.search.SearchQuery; import it.mulders.mcs.search.SearchResponse; +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; @@ -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 CopyToClipboardConfig dontCopyToClipboard = new CopyToClipboardConfig( + "-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 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