001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.io.File;
025import java.io.IOException;
026import java.nio.ByteBuffer;
027import java.util.concurrent.atomic.AtomicInteger;
028
029import org.eclipse.aether.internal.impl.DefaultFileProcessor;
030import org.eclipse.aether.internal.test.util.TestFileUtils;
031import org.eclipse.aether.spi.io.FileProcessor.ProgressListener;
032import org.junit.After;
033import org.junit.Before;
034import org.junit.Test;
035
036/**
037 */
038public class DefaultFileProcessorTest
039{
040
041    private File targetDir;
042
043    private DefaultFileProcessor fileProcessor;
044
045    @Before
046    public void setup()
047        throws IOException
048    {
049        targetDir = TestFileUtils.createTempDir( getClass().getSimpleName() );
050        fileProcessor = new DefaultFileProcessor();
051    }
052
053    @After
054    public void teardown()
055        throws Exception
056    {
057        TestFileUtils.deleteFile( targetDir );
058        fileProcessor = null;
059    }
060
061    @Test
062    public void testCopy()
063        throws IOException
064    {
065        String data = "testCopy\nasdf";
066        File file = TestFileUtils.createTempFile( data );
067        File target = new File( targetDir, "testCopy.txt" );
068
069        fileProcessor.copy( file, target );
070
071        assertEquals( data, TestFileUtils.readString( file ) );
072
073        file.delete();
074    }
075
076    @Test
077    public void testOverwrite()
078        throws IOException
079    {
080        String data = "testCopy\nasdf";
081        File file = TestFileUtils.createTempFile( data );
082
083        for ( int i = 0; i < 5; i++ )
084        {
085            File target = new File( targetDir, "testCopy.txt" );
086            fileProcessor.copy( file, target );
087            assertEquals( data, TestFileUtils.readString( file ) );
088        }
089
090        file.delete();
091    }
092
093    @Test
094    public void testCopyEmptyFile()
095        throws IOException
096    {
097        File file = TestFileUtils.createTempFile( "" );
098        File target = new File( targetDir, "testCopyEmptyFile" );
099        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}