View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.nio.ByteBuffer;
27  import java.util.concurrent.atomic.AtomicInteger;
28  
29  import org.eclipse.aether.internal.impl.DefaultFileProcessor;
30  import org.eclipse.aether.internal.test.util.TestFileUtils;
31  import org.eclipse.aether.spi.io.FileProcessor.ProgressListener;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  /**
37   */
38  public class DefaultFileProcessorTest
39  {
40  
41      private File targetDir;
42  
43      private DefaultFileProcessor fileProcessor;
44  
45      @Before
46      public void setup()
47          throws IOException
48      {
49          targetDir = TestFileUtils.createTempDir( getClass().getSimpleName() );
50          fileProcessor = new DefaultFileProcessor();
51      }
52  
53      @After
54      public void teardown()
55          throws Exception
56      {
57          TestFileUtils.deleteFile( targetDir );
58          fileProcessor = null;
59      }
60  
61      @Test
62      public void testCopy()
63          throws IOException
64      {
65          String data = "testCopy\nasdf";
66          File file = TestFileUtils.createTempFile( data );
67          File target = new File( targetDir, "testCopy.txt" );
68  
69          fileProcessor.copy( file, target );
70  
71          assertEquals( data, TestFileUtils.readString( file ) );
72  
73          file.delete();
74      }
75  
76      @Test
77      public void testOverwrite()
78          throws IOException
79      {
80          String data = "testCopy\nasdf";
81          File file = TestFileUtils.createTempFile( data );
82  
83          for ( int i = 0; i < 5; i++ )
84          {
85              File target = new File( targetDir, "testCopy.txt" );
86              fileProcessor.copy( file, target );
87              assertEquals( data, TestFileUtils.readString( file ) );
88          }
89  
90          file.delete();
91      }
92  
93      @Test
94      public void testCopyEmptyFile()
95          throws IOException
96      {
97          File file = TestFileUtils.createTempFile( "" );
98          File target = new File( targetDir, "testCopyEmptyFile" );
99          target.delete();
100         fileProcessor.copy( file, target );
101         assertTrue( "empty file was not copied", target.exists() && target.length() == 0L );
102         target.delete();
103     }
104 
105     @Test
106     public void testProgressingChannel()
107         throws IOException
108     {
109         File file = TestFileUtils.createTempFile( "test" );
110         File target = new File( targetDir, "testProgressingChannel" );
111         target.delete();
112         final AtomicInteger progressed = new AtomicInteger();
113         ProgressListener listener = new ProgressListener()
114         {
115             public void progressed( ByteBuffer buffer )
116             {
117                 progressed.addAndGet( buffer.remaining() );
118             }
119         };
120         fileProcessor.copy( file, target, listener );
121         assertTrue( "file was not created", target.isFile() );
122         assertEquals( "file was not fully copied", 4L, target.length() );
123         assertEquals( "listener not called", 4, progressed.intValue() );
124         target.delete();
125     }
126 
127 }