From 58642099470c120d9ebdd6faede18ab49ceaef02 Mon Sep 17 00:00:00 2001 From: Hammy Date: Wed, 4 Aug 2021 06:12:29 +0100 Subject: [PATCH] Add simple tests for ClipboardUtils.java --- .../java/me/goudham/ClipboardUtilsTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/java/me/goudham/ClipboardUtilsTest.java diff --git a/src/test/java/me/goudham/ClipboardUtilsTest.java b/src/test/java/me/goudham/ClipboardUtilsTest.java new file mode 100644 index 0000000..81617a2 --- /dev/null +++ b/src/test/java/me/goudham/ClipboardUtilsTest.java @@ -0,0 +1,59 @@ +package me.goudham; + +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.slf4j.Logger; + +import static me.goudham.domain.Contents.TEXT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class ClipboardUtilsTest { + + @Mock + Transferable transferableMock; + + @Mock + Logger logger; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + ClipboardUtils.logger = logger; + } + + @Test + void successfullyGetStringContent() { + String expectedString = "testString"; + Transferable expectedTransferable = new StringSelection(expectedString); + + String actualStringContent = ClipboardUtils.getStringContent(expectedTransferable); + + assertThat(actualStringContent, is(expectedString)); + } + + @Test + void failToGetStringContent() throws IOException, UnsupportedFlavorException { + String expectedString = null; + String expectedExceptionMessage = "Exception Thrown When Receiving String Content"; + Throwable expectedException = new UnsupportedFlavorException(DataFlavor.stringFlavor); + + when(transferableMock.isDataFlavorSupported(TEXT.getDataFlavor())).thenReturn(true); + when(transferableMock.getTransferData(TEXT.getDataFlavor())).thenThrow(expectedException); + + String actualStringContent = ClipboardUtils.getStringContent(transferableMock); + + verify(logger, times(1)).info(expectedExceptionMessage, expectedException); + assertThat(actualStringContent, is(expectedString)); + } +} \ No newline at end of file