1   package org.apache.maven.artifact.testutils;
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 org.codehaus.plexus.util.FileUtils;
23  import org.codehaus.plexus.util.IOUtil;
24  
25  import java.io.File;
26  import java.io.FileReader;
27  import java.io.FileWriter;
28  import java.io.IOException;
29  import java.io.StringReader;
30  import java.io.StringWriter;
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import junit.framework.Assert;
36  
37  public class TestFileManager
38  {
39  
40      public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" );
41  
42      private List filesToDelete = new ArrayList();
43  
44      private final String baseFilename;
45  
46      private final String fileSuffix;
47  
48      private StackTraceElement callerInfo;
49  
50      private Thread cleanupWarning;
51  
52      private boolean warnAboutCleanup = false;
53  
54      public TestFileManager( String baseFilename, String fileSuffix )
55      {
56          this.baseFilename = baseFilename;
57          this.fileSuffix = fileSuffix;
58  
59          initializeCleanupMonitoring();
60      }
61  
62      private void initializeCleanupMonitoring()
63      {
64          callerInfo = new NullPointerException().getStackTrace()[2];
65  
66          Runnable warning = new Runnable()
67          {
68  
69              public void run()
70              {
71                  maybeWarnAboutCleanUp();
72              }
73  
74          };
75  
76          cleanupWarning = new Thread( warning );
77  
78          Runtime.getRuntime().addShutdownHook( cleanupWarning );
79      }
80  
81      private void maybeWarnAboutCleanUp()
82      {
83          if ( warnAboutCleanup )
84          {
85              System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" );
86          }
87      }
88  
89      public void markForDeletion( File toDelete )
90      {
91          filesToDelete.add( toDelete );
92          warnAboutCleanup = true;
93      }
94  
95      public synchronized File createTempDir()
96      {
97          try
98          {
99              Thread.sleep( 20 );
100         }
101         catch ( InterruptedException e )
102         {
103         }
104 
105         File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() );
106 
107         dir.mkdirs();
108         markForDeletion( dir );
109 
110         return dir;
111     }
112 
113     public synchronized File createTempFile()
114         throws IOException
115     {
116         File tempFile = File.createTempFile( baseFilename, fileSuffix );
117         tempFile.deleteOnExit();
118         markForDeletion( tempFile );
119 
120         return tempFile;
121     }
122 
123     public void cleanUp()
124         throws IOException
125     {
126         for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
127         {
128             File file = ( File ) it.next();
129 
130             if ( file.exists() )
131             {
132                 if ( file.isDirectory() )
133                 {
134                     FileUtils.deleteDirectory( file );
135                 }
136                 else
137                 {
138                     file.delete();
139                 }
140             }
141 
142             it.remove();
143         }
144 
145         warnAboutCleanup = false;
146     }
147 
148     public void assertFileExistence( File dir, String filename, boolean shouldExist )
149     {
150         File file = new File( dir, filename );
151 
152         if ( shouldExist )
153         {
154             Assert.assertTrue( file.exists() );
155         }
156         else
157         {
158             Assert.assertFalse( file.exists() );
159         }
160     }
161 
162     public void assertFileContents( File dir, String filename, String contentsTest )
163         throws IOException
164     {
165         assertFileExistence( dir, filename, true );
166 
167         File file = new File( dir, filename );
168 
169         FileReader reader = null;
170         StringWriter writer = new StringWriter();
171 
172         try
173         {
174             reader = new FileReader( file );
175 
176             IOUtil.copy( reader, writer );
177         }
178         finally
179         {
180             IOUtil.close( reader );
181         }
182 
183         Assert.assertEquals( contentsTest, writer.toString() );
184     }
185 
186     public File createFile( File dir, String filename, String contents )
187         throws IOException
188     {
189         File file = new File( dir, filename );
190 
191         file.getParentFile().mkdirs();
192 
193         FileWriter writer = null;
194 
195         try
196         {
197             writer = new FileWriter( file );
198 
199             IOUtil.copy( new StringReader( contents ), writer );
200         }
201         finally
202         {
203             IOUtil.close( writer );
204         }
205 
206         markForDeletion( file );
207 
208         return file;
209     }
210 
211     public String getFileContents( File file )
212         throws IOException
213     {
214         String result = null;
215 
216         FileReader reader = null;
217         try
218         {
219             reader = new FileReader( file );
220 
221             StringWriter writer = new StringWriter();
222 
223             IOUtil.copy( reader, writer );
224 
225             result = writer.toString();
226         }
227         finally
228         {
229             IOUtil.close( reader );
230         }
231 
232         return result;
233     }
234 
235     protected void finalize()
236         throws Throwable
237     {
238         maybeWarnAboutCleanUp();
239 
240         super.finalize();
241     }
242 
243     public File createFile( String filename, String content )
244         throws IOException
245     {
246         File dir = createTempDir();
247         return createFile( dir, filename, content );
248     }
249 
250 }