Coverage Report - org.apache.maven.shared.tools.easymock.TestFileManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TestFileManager
0%
0/78
0%
0/10
1,429
TestFileManager$1
0%
0/3
N/A
1,429
 
 1  
 package org.apache.maven.shared.tools.easymock;
 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 java.io.File;
 23  
 import java.io.FileReader;
 24  
 import java.io.FileWriter;
 25  
 import java.io.IOException;
 26  
 import java.io.StringReader;
 27  
 import java.io.StringWriter;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 
 32  
 import junit.framework.Assert;
 33  
 
 34  
 import org.codehaus.plexus.util.FileUtils;
 35  
 import org.codehaus.plexus.util.IOUtil;
 36  
 
 37  
 /**
 38  
  * @version $Id: TestFileManager.java 677117 2008-07-16 00:29:56Z vsiveton $
 39  
  */
 40  
 public class TestFileManager
 41  
 {
 42  
     /** Temp dir from "java.io.tmpdir" property */
 43  0
     public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" );
 44  
 
 45  0
     private List filesToDelete = new ArrayList();
 46  
 
 47  
     private final String baseFilename;
 48  
 
 49  
     private final String fileSuffix;
 50  
 
 51  
     private StackTraceElement callerInfo;
 52  
 
 53  
     private Thread cleanupWarning;
 54  
 
 55  0
     private boolean warnAboutCleanup = false;
 56  
 
 57  
     /**
 58  
      * Default constructor
 59  
      *
 60  
      * @param baseFilename
 61  
      * @param fileSuffix
 62  
      */
 63  
     public TestFileManager( String baseFilename, String fileSuffix )
 64  0
     {
 65  0
         this.baseFilename = baseFilename;
 66  0
         this.fileSuffix = fileSuffix;
 67  
 
 68  0
         initializeCleanupMonitoring();
 69  0
     }
 70  
 
 71  
     private void initializeCleanupMonitoring()
 72  
     {
 73  0
         callerInfo = new NullPointerException().getStackTrace()[2];
 74  
 
 75  0
         Runnable warning = new Runnable()
 76  
         {
 77  
             /** {@inheritDoc} */
 78  0
             public void run()
 79  
             {
 80  0
                 maybeWarnAboutCleanUp();
 81  0
             }
 82  
 
 83  
         };
 84  
 
 85  0
         cleanupWarning = new Thread( warning );
 86  
 
 87  0
         Runtime.getRuntime().addShutdownHook( cleanupWarning );
 88  0
     }
 89  
 
 90  
     protected void maybeWarnAboutCleanUp()
 91  
     {
 92  0
         if ( warnAboutCleanup )
 93  
         {
 94  0
             System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" );
 95  
         }
 96  0
     }
 97  
 
 98  
     /**
 99  
      * @param toDelete
 100  
      */
 101  
     public void markForDeletion( File toDelete )
 102  
     {
 103  0
         filesToDelete.add( toDelete );
 104  0
         warnAboutCleanup = true;
 105  0
     }
 106  
 
 107  
     /**
 108  
      * @return a temp dir
 109  
      */
 110  
     public synchronized File createTempDir()
 111  
     {
 112  
         try
 113  
         {
 114  0
             Thread.sleep( 20 );
 115  
         }
 116  0
         catch ( InterruptedException e )
 117  
         {
 118  
             // ignored
 119  0
         }
 120  
 
 121  0
         File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() );
 122  
 
 123  0
         dir.mkdirs();
 124  0
         markForDeletion( dir );
 125  
 
 126  0
         return dir;
 127  
     }
 128  
 
 129  
     /**
 130  
      * @return a temp file
 131  
      * @throws IOException if any
 132  
      * @todo maybe use {@link FileUtils#createTempFile(String, String, File)}
 133  
      */
 134  
     public synchronized File createTempFile()
 135  
         throws IOException
 136  
     {
 137  0
         File tempFile = File.createTempFile( baseFilename, fileSuffix );
 138  0
         tempFile.deleteOnExit();
 139  0
         markForDeletion( tempFile );
 140  
 
 141  0
         return tempFile;
 142  
     }
 143  
 
 144  
     /**
 145  
      * @throws IOException if any
 146  
      */
 147  
     public void cleanUp()
 148  
         throws IOException
 149  
     {
 150  0
         for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
 151  
         {
 152  0
             File file = (File) it.next();
 153  
 
 154  0
             if ( file.exists() )
 155  
             {
 156  0
                 if ( file.isDirectory() )
 157  
                 {
 158  0
                     FileUtils.deleteDirectory( file );
 159  
                 }
 160  
                 else
 161  
                 {
 162  0
                     file.delete();
 163  
                 }
 164  
             }
 165  
 
 166  0
             it.remove();
 167  0
         }
 168  
 
 169  0
         warnAboutCleanup = false;
 170  0
     }
 171  
 
 172  
     /**
 173  
      * @param dir
 174  
      * @param filename
 175  
      * @param shouldExist
 176  
      */
 177  
     public void assertFileExistence( File dir, String filename, boolean shouldExist )
 178  
     {
 179  0
         File file = new File( dir, filename );
 180  
 
 181  0
         if ( shouldExist )
 182  
         {
 183  0
             Assert.assertTrue( file.exists() );
 184  
         }
 185  
         else
 186  
         {
 187  0
             Assert.assertFalse( file.exists() );
 188  
         }
 189  0
     }
 190  
 
 191  
     /**
 192  
      * @param dir
 193  
      * @param filename
 194  
      * @param contentsTest
 195  
      * @throws IOException if any
 196  
      */
 197  
     public void assertFileContents( File dir, String filename, String contentsTest )
 198  
         throws IOException
 199  
     {
 200  0
         assertFileExistence( dir, filename, true );
 201  
 
 202  0
         File file = new File( dir, filename );
 203  
 
 204  0
         FileReader reader = null;
 205  0
         StringWriter writer = new StringWriter();
 206  
 
 207  
         try
 208  
         {
 209  0
             reader = new FileReader( file );
 210  
 
 211  0
             IOUtil.copy( reader, writer );
 212  
         }
 213  
         finally
 214  
         {
 215  0
             IOUtil.close( reader );
 216  0
         }
 217  
 
 218  0
         Assert.assertEquals( contentsTest, writer.toString() );
 219  0
     }
 220  
 
 221  
     /**
 222  
      * @param dir
 223  
      * @param filename
 224  
      * @param contents
 225  
      * @return
 226  
      * @throws IOException if any
 227  
      */
 228  
     public File createFile( File dir, String filename, String contents )
 229  
         throws IOException
 230  
     {
 231  0
         File file = new File( dir, filename );
 232  
 
 233  0
         file.getParentFile().mkdirs();
 234  
 
 235  0
         FileWriter writer = null;
 236  
 
 237  
         try
 238  
         {
 239  0
             writer = new FileWriter( file );
 240  
 
 241  0
             IOUtil.copy( new StringReader( contents ), writer );
 242  
         }
 243  
         finally
 244  
         {
 245  0
             IOUtil.close( writer );
 246  0
         }
 247  
 
 248  0
         markForDeletion( file );
 249  
 
 250  0
         return file;
 251  
     }
 252  
 
 253  
     /**
 254  
      * @param file
 255  
      * @return
 256  
      * @throws IOException if any
 257  
      */
 258  
     public String getFileContents( File file )
 259  
         throws IOException
 260  
     {
 261  0
         String result = null;
 262  
 
 263  0
         FileReader reader = null;
 264  
         try
 265  
         {
 266  0
             reader = new FileReader( file );
 267  
 
 268  0
             StringWriter writer = new StringWriter();
 269  
 
 270  0
             IOUtil.copy( reader, writer );
 271  
 
 272  0
             result = writer.toString();
 273  
         }
 274  
         finally
 275  
         {
 276  0
             IOUtil.close( reader );
 277  0
         }
 278  
 
 279  0
         return result;
 280  
     }
 281  
 
 282  
     /** {@inheritDoc} */
 283  
     protected void finalize()
 284  
         throws Throwable
 285  
     {
 286  0
         maybeWarnAboutCleanUp();
 287  
 
 288  0
         super.finalize();
 289  0
     }
 290  
 
 291  
     /**
 292  
      * @param filename
 293  
      * @param content
 294  
      * @return
 295  
      * @throws IOException if any
 296  
      */
 297  
     public File createFile( String filename, String content )
 298  
         throws IOException
 299  
     {
 300  0
         File dir = createTempDir();
 301  0
         return createFile( dir, filename, content );
 302  
     }
 303  
 }