-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathAttachedFileTest.java
More file actions
63 lines (53 loc) · 1.99 KB
/
AttachedFileTest.java
File metadata and controls
63 lines (53 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
public class AttachedFileTest {
@Test
public void testInputStreamConstructors() throws IOException {
byte[] data = "content".getBytes();
// Test: InputStream, fileName, fileSize
try (var is = new ByteArrayInputStream(data)) {
AttachedFile file = new AttachedFile(is, "test1.txt", data.length);
assertEquals("test1.txt", file.getFileName());
assertEquals(data.length, file.getFileSize());
assertTrue(file.getContentDisposition().startsWith("attachment"));
}
// Test: InputStream, fileName
try (var is = new ByteArrayInputStream(data)) {
AttachedFile file = new AttachedFile(is, "test2.txt");
assertEquals("test2.txt", file.getFileName());
assertEquals(-1, file.getFileSize());
assertTrue(file.getContentDisposition().startsWith("attachment"));
}
}
@Test
public void testPathConstructors() throws IOException {
Path tempFile = Files.createTempFile("attached-file", ".json");
Files.write(tempFile, "{}".getBytes());
try {
// Test: Path, fileName
AttachedFile f1 = new AttachedFile(tempFile, "custom.json");
assertEquals("custom.json", f1.getFileName());
assertEquals(2, f1.getFileSize());
assertTrue(f1.getContentDisposition().contains("attachment"));
f1.stream().close();
// Test: Path
AttachedFile f2 = new AttachedFile(tempFile);
assertEquals(tempFile.getFileName().toString(), f2.getFileName());
assertTrue(f2.getContentDisposition().contains("attachment"));
f2.stream().close();
} finally {
Files.deleteIfExists(tempFile);
}
}
}