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 package org.apache.hc.core5.http.io.entity;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35
36 import org.apache.hc.core5.http.ContentType;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39
40
41
42
43 public class TestPathEntity {
44
45 @Test
46 public void testBasics() throws Exception {
47 final Path tmpPath = Files.createTempFile("testfile", ".txt");
48
49 tmpPath.toFile().deleteOnExit();
50 try (final PathEntity httpEntity = new PathEntity(tmpPath, ContentType.TEXT_PLAIN)) {
51 Assertions.assertEquals(Files.size(tmpPath), httpEntity.getContentLength());
52 try (final InputStream content = httpEntity.getContent()) {
53 Assertions.assertNotNull(content);
54 }
55 Assertions.assertTrue(httpEntity.isRepeatable());
56 Assertions.assertFalse(httpEntity.isStreaming());
57
58 Assertions.assertTrue(Files.deleteIfExists(tmpPath), "Failed to delete " + tmpPath);
59 }
60 }
61
62 @Test
63 public void testNullConstructor() throws Exception {
64 Assertions.assertThrows(NullPointerException.class, () -> new PathEntity(null, ContentType.TEXT_PLAIN));
65 }
66
67 @Test
68 public void testWriteTo() throws Exception {
69 final Path tmpPath = Files.createTempFile("testfile", ".txt");
70
71 tmpPath.toFile().deleteOnExit();
72 try (final OutputStream outStream = Files.newOutputStream(tmpPath)) {
73 outStream.write(0);
74 outStream.write(1);
75 outStream.write(2);
76 outStream.write(3);
77 }
78
79 try (final PathEntity httpEntity = new PathEntity(tmpPath, ContentType.TEXT_PLAIN)) {
80 final ByteArrayOutputStream out = new ByteArrayOutputStream();
81 httpEntity.writeTo(out);
82 final byte[] bytes = out.toByteArray();
83 Assertions.assertNotNull(bytes);
84 Assertions.assertEquals(Files.size(tmpPath), bytes.length);
85 for (int i = 0; i < 4; i++) {
86 Assertions.assertEquals(i, bytes[i]);
87 }
88
89 Assertions.assertTrue(Files.deleteIfExists(tmpPath), "Failed to delete " + tmpPath);
90 Assertions.assertThrows(NullPointerException.class, () -> httpEntity.writeTo(null));
91 }
92 }
93 }