1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 import org.eclipse.aether.internal.test.util.TestFileUtils;
29 import org.eclipse.aether.spi.io.FileProcessor.ProgressListener;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34 import static org.junit.jupiter.api.Assertions.*;
35
36
37
38 public class DefaultFileProcessorTest {
39
40 private File targetDir;
41
42 private DefaultFileProcessor fileProcessor;
43
44 @BeforeEach
45 void setup() throws IOException {
46 targetDir = TestFileUtils.createTempDir(getClass().getSimpleName());
47 fileProcessor = new DefaultFileProcessor();
48 }
49
50 @AfterEach
51 void teardown() throws Exception {
52 TestFileUtils.deleteFile(targetDir);
53 fileProcessor = null;
54 }
55
56 @Test
57 void testCopy() throws IOException {
58 String data = "testCopy\nasdf";
59 File file = TestFileUtils.createTempFile(data);
60 File target = new File(targetDir, "testCopy.txt");
61
62 fileProcessor.copy(file, target);
63
64 assertEquals(data, TestFileUtils.readString(file));
65
66 file.delete();
67 }
68
69 @Test
70 void testOverwrite() throws IOException {
71 String data = "testCopy\nasdf";
72 File file = TestFileUtils.createTempFile(data);
73
74 for (int i = 0; i < 5; i++) {
75 File target = new File(targetDir, "testCopy.txt");
76 fileProcessor.copy(file, target);
77 assertEquals(data, TestFileUtils.readString(file));
78 }
79
80 file.delete();
81 }
82
83 @Test
84 void testCopyEmptyFile() throws IOException {
85 File file = TestFileUtils.createTempFile("");
86 File target = new File(targetDir, "testCopyEmptyFile");
87 target.delete();
88 fileProcessor.copy(file, target);
89 assertTrue(target.exists() && target.length() == 0L, "empty file was not copied");
90 target.delete();
91 }
92
93 @Test
94 void testProgressingChannel() throws IOException {
95 File file = TestFileUtils.createTempFile("test");
96 File target = new File(targetDir, "testProgressingChannel");
97 target.delete();
98 final AtomicInteger progressed = new AtomicInteger();
99 ProgressListener listener = new ProgressListener() {
100 public void progressed(ByteBuffer buffer) {
101 progressed.addAndGet(buffer.remaining());
102 }
103 };
104 fileProcessor.copy(file, target, listener);
105 assertTrue(target.isFile(), "file was not created");
106 assertEquals(4L, target.length(), "file was not fully copied");
107 assertEquals(4, progressed.intValue(), "listener not called");
108 target.delete();
109 }
110
111 @Test
112 void testWrite() throws IOException {
113 String data = "testCopy\nasdf";
114 File target = new File(targetDir, "testWrite.txt");
115
116 fileProcessor.write(target, data);
117
118 assertEquals(data, TestFileUtils.readString(target));
119
120 target.delete();
121 }
122
123
124
125
126 @Test
127 void testWriteStream() throws IOException {
128 String data = "testCopy\nasdf";
129 File target = new File(targetDir, "testWriteStream.txt");
130
131 fileProcessor.write(target, new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));
132
133 assertEquals(data, TestFileUtils.readString(target));
134
135 target.delete();
136 }
137
138 @Test
139 void testReadChecksumNonExistentFile() {
140 assertThrows(IOException.class, () -> fileProcessor.readChecksum(new File("non existent")));
141 }
142
143 @Test
144 void testReadChecksumEmptyFile() throws IOException {
145 File emptyFile = TestFileUtils.createTempFile("");
146 String read = fileProcessor.readChecksum(emptyFile);
147 assertEquals("", read);
148 }
149
150 @Test
151 void testReadChecksum() throws IOException {
152 String checksum = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
153 File checksumFile = TestFileUtils.createTempFile(checksum);
154 String read = fileProcessor.readChecksum(checksumFile);
155 assertEquals(checksum, read);
156 }
157
158 @Test
159 void testReadChecksumWhitespace() throws IOException {
160 String checksum = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
161 File checksumFile;
162 String read;
163
164 checksumFile = TestFileUtils.createTempFile("foobar(alg) = " + checksum);
165 read = fileProcessor.readChecksum(checksumFile);
166 assertEquals(checksum, read);
167
168 checksumFile = TestFileUtils.createTempFile(checksum + " foobar");
169 read = fileProcessor.readChecksum(checksumFile);
170 assertEquals(checksum, read);
171 }
172 }