001package org.apache.maven.artifact.testutils;
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
022/*
023 * Licensed to the Apache Software Foundation (ASF) under one
024 * or more contributor license agreements.  See the NOTICE file
025 * distributed with this work for additional information
026 * regarding copyright ownership.  The ASF licenses this file
027 * to you under the Apache License, Version 2.0 (the
028 * "License"); you may not use this file except in compliance
029 * with the License.  You may obtain a copy of the License at
030 *
031 *  http://www.apache.org/licenses/LICENSE-2.0
032 *
033 * Unless required by applicable law or agreed to in writing,
034 * software distributed under the License is distributed on an
035 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
036 * KIND, either express or implied.  See the License for the
037 * specific language governing permissions and limitations
038 * under the License.
039 */
040
041import java.io.File;
042import java.io.IOException;
043import java.util.ArrayList;
044import java.util.Iterator;
045import java.util.List;
046
047import junit.framework.Assert;
048
049import org.codehaus.plexus.util.FileUtils;
050
051public class TestFileManager
052{
053
054    public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" );
055
056    private List<File> filesToDelete = new ArrayList<File>();
057
058    private final String baseFilename;
059
060    private final String fileSuffix;
061
062    private StackTraceElement callerInfo;
063
064    private Thread cleanupWarning;
065
066    private boolean warnAboutCleanup = false;
067
068    public TestFileManager( String baseFilename, String fileSuffix )
069    {
070        this.baseFilename = baseFilename;
071        this.fileSuffix = fileSuffix;
072
073        initializeCleanupMonitoring();
074    }
075
076    private void initializeCleanupMonitoring()
077    {
078        callerInfo = new NullPointerException().getStackTrace()[2];
079
080        Runnable warning = new Runnable()
081        {
082
083            public void run()
084            {
085                maybeWarnAboutCleanUp();
086            }
087
088        };
089
090        cleanupWarning = new Thread( warning );
091
092        Runtime.getRuntime().addShutdownHook( cleanupWarning );
093    }
094
095    private void maybeWarnAboutCleanUp()
096    {
097        if ( warnAboutCleanup )
098        {
099            System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" );
100        }
101    }
102
103    public void markForDeletion( File toDelete )
104    {
105        filesToDelete.add( toDelete );
106        warnAboutCleanup = true;
107    }
108
109    public synchronized File createTempDir()
110    {
111        try
112        {
113            Thread.sleep( 20 );
114        }
115        catch ( InterruptedException e )
116        {
117            // ignore
118        }
119
120        File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() );
121
122        dir.mkdirs();
123        markForDeletion( dir );
124
125        return dir;
126    }
127
128    public synchronized File createTempFile()
129        throws IOException
130    {
131        File tempFile = File.createTempFile( baseFilename, fileSuffix );
132        tempFile.deleteOnExit();
133        markForDeletion( tempFile );
134
135        return tempFile;
136    }
137
138    public void cleanUp()
139        throws IOException
140    {
141        for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
142        {
143            File file = (File) it.next();
144
145            if ( file.exists() )
146            {
147                if ( file.isDirectory() )
148                {
149                    FileUtils.deleteDirectory( file );
150                }
151                else
152                {
153                    file.delete();
154                }
155            }
156
157            it.remove();
158        }
159
160        warnAboutCleanup = false;
161    }
162
163    public void assertFileExistence( File dir, String filename, boolean shouldExist )
164    {
165        File file = new File( dir, filename );
166
167        if ( shouldExist )
168        {
169            Assert.assertTrue( file.exists() );
170        }
171        else
172        {
173            Assert.assertFalse( file.exists() );
174        }
175    }
176
177    public void assertFileContents( File dir, String filename, String contentsTest, String encoding )
178        throws IOException
179    {
180        assertFileExistence( dir, filename, true );
181
182        File file = new File( dir, filename );
183
184        String contents = FileUtils.fileRead( file, encoding );
185
186        Assert.assertEquals( contentsTest, contents );
187    }
188
189    public File createFile( File dir, String filename, String contents, String encoding )
190        throws IOException
191    {
192        File file = new File( dir, filename );
193
194        file.getParentFile().mkdirs();
195
196        FileUtils.fileWrite( file.getPath(), encoding, contents );
197
198        markForDeletion( file );
199
200        return file;
201    }
202
203    public String getFileContents( File file, String encoding )
204        throws IOException
205    {
206        return FileUtils.fileRead( file, encoding );
207    }
208
209    protected void finalize()
210        throws Throwable
211    {
212        maybeWarnAboutCleanUp();
213
214        super.finalize();
215    }
216
217    public File createFile( String filename, String content, String encoding )
218        throws IOException
219    {
220        File dir = createTempDir();
221        return createFile( dir, filename, content, encoding );
222    }
223
224}