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.After;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 import static org.junit.Assert.*;
35
36
37
38 public class DefaultFileProcessorTest {
39
40 private File targetDir;
41
42 private DefaultFileProcessor fileProcessor;
43
44 @Before
45 public void setup() throws IOException {
46 targetDir = TestFileUtils.createTempDir(getClass().getSimpleName());
47 fileProcessor = new DefaultFileProcessor();
48 }
49
50 @After
51 public void teardown() throws Exception {
52 TestFileUtils.deleteFile(targetDir);
53 fileProcessor = null;
54 }
55
56 @Test
57 public 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 public 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 public 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("empty file was not copied", target.exists() && target.length() == 0L);
90 target.delete();
91 }
92
93 @Test
94 public 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("file was not created", target.isFile());
106 assertEquals("file was not fully copied", 4L, target.length());
107 assertEquals("listener not called", 4, progressed.intValue());
108 target.delete();
109 }
110
111 @Test
112 public 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 public 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 }