View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      * Used ONLY when FileProcessor present, never otherwise.
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 }