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  
49  import static org.junit.jupiter.api.Assertions.assertEquals;
50  import static org.junit.jupiter.api.Assertions.assertFalse;
51  import static org.junit.jupiter.api.Assertions.assertTrue;
52  
53  public class TestFileManager
54  {
55  
56      public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" );
57  
58      private List<File> filesToDelete = new ArrayList<>();
59  
60      private final String baseFilename;
61  
62      private final String fileSuffix;
63  
64      private StackTraceElement callerInfo;
65  
66      private Thread cleanupWarning;
67  
68      private boolean warnAboutCleanup = false;
69  
70      public TestFileManager( String baseFilename, String fileSuffix )
71      {
72          this.baseFilename = baseFilename;
73          this.fileSuffix = fileSuffix;
74  
75          initializeCleanupMonitoring();
76      }
77  
78      private void initializeCleanupMonitoring()
79      {
80          callerInfo = new NullPointerException().getStackTrace()[2];
81  
82          Runnable warning = this::maybeWarnAboutCleanUp;
83  
84          cleanupWarning = new Thread( warning );
85  
86          Runtime.getRuntime().addShutdownHook( cleanupWarning );
87      }
88  
89      private void maybeWarnAboutCleanUp()
90      {
91          if ( warnAboutCleanup )
92          {
93              System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" );
94          }
95      }
96  
97      public void markForDeletion( File toDelete )
98      {
99          filesToDelete.add( toDelete );
100         warnAboutCleanup = true;
101     }
102 
103     public synchronized File createTempDir()
104     {
105         try
106         {
107             Thread.sleep( 20 );
108         }
109         catch ( InterruptedException e )
110         {
111             // ignore
112         }
113 
114         File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() );
115 
116         dir.mkdirs();
117         markForDeletion( dir );
118 
119         return dir;
120     }
121 
122     public synchronized File createTempFile()
123         throws IOException
124     {
125         File tempFile = File.createTempFile( baseFilename, fileSuffix );
126         tempFile.deleteOnExit();
127         markForDeletion( tempFile );
128 
129         return tempFile;
130     }
131 
132     public void cleanUp()
133         throws IOException
134     {
135         for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
136         {
137             File file = (File) it.next();
138 
139             if ( file.exists() )
140             {
141                 if ( file.isDirectory() )
142                 {
143                     FileUtils.deleteDirectory( file );
144                 }
145                 else
146                 {
147                     file.delete();
148                 }
149             }
150 
151             it.remove();
152         }
153 
154         warnAboutCleanup = false;
155     }
156 
157     public void assertFileExistence( File dir, String filename, boolean shouldExist )
158     {
159         File file = new File( dir, filename );
160 
161         if ( shouldExist )
162         {
163             assertTrue( file.exists() );
164         }
165         else
166         {
167             assertFalse( file.exists() );
168         }
169     }
170 
171     public void assertFileContents( File dir, String filename, String contentsTest, String encoding )
172         throws IOException
173     {
174         assertFileExistence( dir, filename, true );
175 
176         File file = new File( dir, filename );
177 
178         String contents = FileUtils.fileRead( file, encoding );
179 
180         assertEquals( contentsTest, contents );
181     }
182 
183     public File createFile( File dir, String filename, String contents, String encoding )
184         throws IOException
185     {
186         File file = new File( dir, filename );
187 
188         file.getParentFile().mkdirs();
189 
190         FileUtils.fileWrite( file.getPath(), encoding, contents );
191 
192         markForDeletion( file );
193 
194         return file;
195     }
196 
197     public String getFileContents( File file, String encoding )
198         throws IOException
199     {
200         return FileUtils.fileRead( file, encoding );
201     }
202 
203     protected void finalize()
204         throws Throwable
205     {
206         maybeWarnAboutCleanUp();
207 
208         super.finalize();
209     }
210 
211     public File createFile( String filename, String content, String encoding )
212         throws IOException
213     {
214         File dir = createTempDir();
215         return createFile( dir, filename, content, encoding );
216     }
217 
218 }