001package org.apache.maven.wagon;
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 org.codehaus.plexus.util.FileUtils;
023
024import java.io.File;
025import java.io.FileWriter;
026import java.io.IOException;
027import java.io.Writer;
028
029/**
030 * @author <a href="michal@apache.org>Michal Maczka</a>
031 *
032 */
033public class FileTestUtils
034{
035
036    public static final File createUniqueFile( final String dirname, final String name )
037        throws IOException
038    {
039
040        final File dir = createDir( dirname );
041
042        final File retValue = new File( dir, name );
043
044        return retValue;
045
046    }
047
048
049    public static final File createUniqueDir( final String name )
050        throws IOException
051    {
052
053        String filename = name + System.currentTimeMillis();
054
055        return createDir( filename );
056
057    }
058
059
060    public static final File createDir( final String name )
061        throws IOException
062    {
063
064        final File baseDirectory = getTestOutputDir();
065
066        final File retValue = new File( baseDirectory, name );
067       
068        if ( retValue.exists() )
069        {
070            FileUtils.cleanDirectory( retValue );
071            return retValue;
072        }
073        
074        retValue.mkdirs();
075
076        if ( !retValue.exists() )
077        {
078            throw new IOException( "Unable to create the directory for testdata " + retValue.getPath() );
079        }
080
081        return retValue;
082    }
083
084    public static final File getTestOutputDir()
085    {
086        final String tempDir = System.getProperty( "java.io.tmpdir" );
087
088        final String baseDir = System.getProperty( "basedir", tempDir );
089
090        final File base = new File( baseDir ).getAbsoluteFile();
091
092        final String pathname = base + File.separator + "target" + File.separator + "test-output";
093
094        final File retValue = new File( pathname );
095
096        retValue.mkdirs();
097
098        return retValue;
099    }
100
101    public static File generateFile( String file, String content )
102        throws IOException
103    {
104        File f = new File( file );
105
106        f.getParentFile().mkdirs();
107
108        Writer writer = new FileWriter( f );
109
110        try
111        {
112            writer.write( content );
113        }
114        finally
115        {
116            writer.close();
117        }
118
119        return f;
120    }
121}