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.ByteArrayInputStream;
025import java.io.File;
026import java.io.IOException;
027import java.nio.ByteBuffer;
028import java.nio.charset.StandardCharsets;
029import java.util.concurrent.atomic.AtomicInteger;
030
031import org.eclipse.aether.internal.impl.DefaultFileProcessor;
032import org.eclipse.aether.internal.test.util.TestFileUtils;
033import org.eclipse.aether.spi.io.FileProcessor.ProgressListener;
034import org.junit.After;
035import org.junit.Before;
036import org.junit.Test;
037
038/**
039 */
040public class DefaultFileProcessorTest
041{
042
043    private File targetDir;
044
045    private DefaultFileProcessor fileProcessor;
046
047    @Before
048    public void setup()
049        throws IOException
050    {
051        targetDir = TestFileUtils.createTempDir( getClass().getSimpleName() );
052        fileProcessor = new DefaultFileProcessor();
053    }
054
055    @After
056    public void teardown()
057        throws Exception
058    {
059        TestFileUtils.deleteFile( targetDir );
060        fileProcessor = null;
061    }
062
063    @Test
064    public void testCopy()
065        throws IOException
066    {
067        String data = "testCopy\nasdf";
068        File file = TestFileUtils.createTempFile( data );
069        File target = new File( targetDir, "testCopy.txt" );
070
071        fileProcessor.copy( file, target );
072
073        assertEquals( data, TestFileUtils.readString( file ) );
074
075        file.delete();
076    }
077
078    @Test
079    public void testOverwrite()
080        throws IOException
081    {
082        String data = "testCopy\nasdf";
083        File file = TestFileUtils.createTempFile( data );
084
085        for ( int i = 0; i < 5; i++ )
086        {
087            File target = new File( targetDir, "testCopy.txt" );
088            fileProcessor.copy( file, target );
089            assertEquals( data, TestFileUtils.readString( file ) );
090        }
091
092        file.delete();
093    }
094
095    @Test
096    public void testCopyEmptyFile()
097        throws IOException
098    {
099        File file = TestFileUtils.createTempFile( "" );
100        File target = new File( targetDir, "testCopyEmptyFile" );
101        target.delete();
102        fileProcessor.copy( file, target );
103        assertTrue( "empty file was not copied", target.exists() && target.length() == 0L );
104        target.delete();
105    }
106
107    @Test
108    public void testProgressingChannel()
109        throws IOException
110    {
111        File file = TestFileUtils.createTempFile( "test" );
112        File target = new File( targetDir, "testProgressingChannel" );
113        target.delete();
114        final AtomicInteger progressed = new AtomicInteger();
115        ProgressListener listener = new ProgressListener()
116        {
117            public void progressed( ByteBuffer buffer )
118            {
119                progressed.addAndGet( buffer.remaining() );
120            }
121        };
122        fileProcessor.copy( file, target, listener );
123        assertTrue( "file was not created", target.isFile() );
124        assertEquals( "file was not fully copied", 4L, target.length() );
125        assertEquals( "listener not called", 4, progressed.intValue() );
126        target.delete();
127    }
128
129    @Test
130    public void testWrite()
131            throws IOException
132    {
133        String data = "testCopy\nasdf";
134        File target = new File( targetDir, "testWrite.txt" );
135
136        fileProcessor.write( target, data );
137
138        assertEquals( data, TestFileUtils.readString( target ) );
139
140        target.delete();
141    }
142
143    /**
144     * Used ONLY when FileProcessor present, never otherwise.
145     */
146    @Test
147    public void testWriteStream()
148            throws IOException
149    {
150        String data = "testCopy\nasdf";
151        File target = new File( targetDir, "testWriteStream.txt" );
152
153        fileProcessor.write( target, new ByteArrayInputStream( data.getBytes( StandardCharsets.UTF_8 ) ) );
154
155        assertEquals( data, TestFileUtils.readString( target ) );
156
157        target.delete();
158    }
159
160}