-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/test/java/mpo/dayon/common/network/TransferableFilesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package mpo.dayon.common.network; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.awt.datatransfer.DataFlavor; | ||
import java.awt.datatransfer.UnsupportedFlavorException; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class TransferableFilesTest { | ||
|
||
private TransferableFiles transferableFiles; | ||
@BeforeEach | ||
void setup() { | ||
transferableFiles = new TransferableFiles(new ArrayList<>()); | ||
} | ||
|
||
@Test | ||
void getTransferDataAsFileList() throws UnsupportedFlavorException { | ||
final File file = new File(String.valueOf(UUID.randomUUID())); | ||
transferableFiles = new TransferableFiles(Collections.singletonList(file)); | ||
final List<File> files = (List<File>) transferableFiles.getTransferData(DataFlavor.javaFileListFlavor); | ||
assertEquals(file, files.get(0)); | ||
} | ||
|
||
@Test | ||
void getTransferDataAsUri() throws ClassNotFoundException, UnsupportedFlavorException { | ||
final File file = new File(String.valueOf(UUID.randomUUID())); | ||
transferableFiles = new TransferableFiles(Collections.singletonList(file)); | ||
final String uri = (String) transferableFiles.getTransferData(new DataFlavor("text/uri-list;class=java.lang.String")); | ||
assertEquals("x-special/nautilus-clipboard", uri.substring(0, 28)); | ||
assertTrue(uri.contains(file.getName())); | ||
} | ||
|
||
@Test | ||
void getTransferDataAsGnomeCopiedFiles() throws ClassNotFoundException, UnsupportedFlavorException { | ||
final File file = new File(String.valueOf(UUID.randomUUID())); | ||
transferableFiles = new TransferableFiles(Collections.singletonList(file)); | ||
final ByteArrayInputStream stream = (ByteArrayInputStream) transferableFiles.getTransferData(new DataFlavor("x-special/gnome-copied-files;class=java.io.InputStream")); | ||
int len = stream.available(); | ||
byte[] bytes = new byte[len]; | ||
stream.read(bytes, 0, len); | ||
String string = new String(bytes, StandardCharsets.UTF_8); | ||
assertEquals("copy", string.substring(0, 4)); | ||
assertTrue(string.contains(file.getName())); | ||
} | ||
|
||
@Test | ||
void getTransferDataAsStringShouldThrow() { | ||
final File file = new File(String.valueOf(UUID.randomUUID())); | ||
transferableFiles = new TransferableFiles(Collections.singletonList(file)); | ||
assertThrows(UnsupportedFlavorException.class, () -> transferableFiles.getTransferData(DataFlavor.stringFlavor)); | ||
} | ||
|
||
@Test | ||
void getTransferDataFlavors() { | ||
final DataFlavor[] transferDataFlavors = transferableFiles.getTransferDataFlavors(); | ||
assertEquals(3, transferDataFlavors.length); | ||
} | ||
|
||
@Test | ||
void isDataFlavorSupported() throws ClassNotFoundException { | ||
assertTrue(transferableFiles.isDataFlavorSupported(DataFlavor.javaFileListFlavor)); | ||
assertTrue(transferableFiles.isDataFlavorSupported(new DataFlavor("text/uri-list;class=java.lang.String"))); | ||
assertTrue(transferableFiles.isDataFlavorSupported(new DataFlavor("x-special/gnome-copied-files;class=java.io.InputStream"))); | ||
assertFalse(transferableFiles.isDataFlavorSupported(DataFlavor.stringFlavor)); | ||
assertFalse(transferableFiles.isDataFlavorSupported(DataFlavor.imageFlavor)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/mpo/dayon/common/network/message/FileMetaDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mpo.dayon.common.network.message; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static java.lang.String.format; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class FileMetaDataTest { | ||
|
||
private final String basePath = "tmp/"; | ||
private final String fileName = format("%sfoo/bar.txt", basePath); | ||
private final long fileSize = 10; | ||
|
||
@Test | ||
void getFileName() { | ||
// given | ||
FileMetaData fileMetaData = new FileMetaData(fileName, fileSize, basePath); | ||
|
||
// when then | ||
assertEquals("foo/bar.txt", fileMetaData.getFileName()); | ||
assertEquals(fileSize, fileMetaData.getFileSize()); | ||
} | ||
|
||
@Test | ||
void equals() { | ||
// given | ||
FileMetaData fileMetaData = new FileMetaData(fileName, fileSize, basePath); | ||
|
||
// when then | ||
assertEquals(fileMetaData, new FileMetaData(fileName, fileSize, basePath)); | ||
} | ||
} |