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