Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Dec 27, 2023
1 parent 8bb8305 commit 9b612ba
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/test/java/mpo/dayon/common/network/TransferableFilesTest.java
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));
}
}
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));
}
}

0 comments on commit 9b612ba

Please sign in to comment.