View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.tools.easymock;
20  
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import junit.framework.Assert;
32  
33  import org.codehaus.plexus.util.FileUtils;
34  import org.codehaus.plexus.util.IOUtil;
35  
36  public class TestFileManager
37  {
38  
39      public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" );
40  
41      private List filesToDelete = new ArrayList();
42  
43      private final String baseFilename;
44  
45      private final String fileSuffix;
46  
47      private StackTraceElement callerInfo;
48  
49      private Thread cleanupWarning;
50  
51      private boolean warnAboutCleanup = false;
52  
53      public TestFileManager( String baseFilename, String fileSuffix )
54      {
55          this.baseFilename = baseFilename;
56          this.fileSuffix = fileSuffix;
57  
58          initializeCleanupMonitoring();
59      }
60  
61      private void initializeCleanupMonitoring()
62      {
63          callerInfo = new NullPointerException().getStackTrace()[2];
64  
65          Runnable warning = new Runnable()
66          {
67  
68              public void run()
69              {
70                  maybeWarnAboutCleanUp();
71              }
72  
73          };
74  
75          cleanupWarning = new Thread( warning );
76  
77          Runtime.getRuntime().addShutdownHook( cleanupWarning );
78      }
79  
80      private void maybeWarnAboutCleanUp()
81      {
82          if ( warnAboutCleanup )
83          {
84              System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" );
85          }
86      }
87  
88      public void markForDeletion( File toDelete )
89      {
90          filesToDelete.add( toDelete );
91          warnAboutCleanup = true;
92      }
93  
94      public synchronized File createTempDir()
95      {
96          try
97          {
98              Thread.sleep( 20 );
99          }
100         catch ( InterruptedException e )
101         {
102         }
103 
104         File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() );
105 
106         dir.mkdirs();
107         markForDeletion( dir );
108 
109         return dir;
110     }
111 
112     public synchronized File createTempFile()
113         throws IOException
114     {
115         File tempFile = File.createTempFile( baseFilename, fileSuffix );
116         tempFile.deleteOnExit();
117         markForDeletion( tempFile );
118 
119         return tempFile;
120     }
121 
122     public void cleanUp()
123         throws IOException
124     {
125         for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
126         {
127             File file = (File) it.next();
128 
129             if ( file.exists() )
130             {
131                 if ( file.isDirectory() )
132                 {
133                     FileUtils.deleteDirectory( file );
134                 }
135                 else
136                 {
137                     file.delete();
138                 }
139             }
140 
141             it.remove();
142         }
143 
144         warnAboutCleanup = false;
145     }
146 
147     public void assertFileExistence( File dir, String filename, boolean shouldExist )
148     {
149         File file = new File( dir, filename );
150 
151         if ( shouldExist )
152         {
153             Assert.assertTrue( file.exists() );
154         }
155         else
156         {
157             Assert.assertFalse( file.exists() );
158         }
159     }
160 
161     public void assertFileContents( File dir, String filename, String contentsTest )
162         throws IOException
163     {
164         assertFileExistence( dir, filename, true );
165 
166         File file = new File( dir, filename );
167 
168         FileReader reader = null;
169         StringWriter writer = new StringWriter();
170 
171         try
172         {
173             reader = new FileReader( file );
174 
175             IOUtil.copy( reader, writer );
176         }
177         finally
178         {
179             IOUtil.close( reader );
180         }
181 
182         Assert.assertEquals( contentsTest, writer.toString() );
183     }
184 
185     public File createFile( File dir, String filename, String contents )
186         throws IOException
187     {
188         File file = new File( dir, filename );
189 
190         file.getParentFile().mkdirs();
191 
192         FileWriter writer = null;
193 
194         try
195         {
196             writer = new FileWriter( file );
197 
198             IOUtil.copy( new StringReader( contents ), writer );
199         }
200         finally
201         {
202             IOUtil.close( writer );
203         }
204 
205         markForDeletion( file );
206 
207         return file;
208     }
209 
210     public String getFileContents( File file )
211         throws IOException
212     {
213         String result = null;
214 
215         FileReader reader = null;
216         try
217         {
218             reader = new FileReader( file );
219 
220             StringWriter writer = new StringWriter();
221 
222             IOUtil.copy( reader, writer );
223 
224             result = writer.toString();
225         }
226         finally
227         {
228             IOUtil.close( reader );
229         }
230 
231         return result;
232     }
233 
234     protected void finalize()
235         throws Throwable
236     {
237         maybeWarnAboutCleanUp();
238 
239         super.finalize();
240     }
241 
242     public File createFile( String filename, String content )
243         throws IOException
244     {
245         File dir = createTempDir();
246         return createFile( dir, filename, content );
247     }
248 
249 }