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