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.surefire.api.util;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.List;
24  import java.util.Random;
25  
26  import junit.framework.TestCase;
27  import org.junit.Test;
28  
29  import static org.assertj.core.api.Assertions.assertThat;
30  import static org.powermock.reflect.Whitebox.getInternalState;
31  
32  /**
33   * Unit test for the surefire temp file manager.
34   *
35   * @author Markus Spann
36   */
37  public class TempFileManagerTest extends TestCase {
38  
39      @Test
40      public void testDefaultInstance() {
41          TempFileManager tfm = TempFileManager.instance();
42          assertSame(tfm, TempFileManager.instance((File) null));
43          assertSame(tfm, TempFileManager.instance((String) null));
44          assertSame(tfm, TempFileManager.instance(new File(System.getProperty("java.io.tmpdir"))));
45  
46          assertThat(tfm.getTempDir()).isEqualTo(new File(System.getProperty("java.io.tmpdir")));
47          assertThat(tfm.toString()).contains("tempDir=" + tfm.getTempDir().getPath());
48          assertThat(tfm.toString()).contains("baseName=" + getInternalState(tfm, "baseName"));
49      }
50  
51      @Test
52      public void testSubdirInstance() {
53          String subDirName = TempFileManagerTest.class.getSimpleName() + new Random().nextLong();
54          TempFileManager tfm = TempFileManager.instance(subDirName);
55          assertEquals(tfm.getTempDir(), new File(System.getProperty("java.io.tmpdir"), subDirName));
56          assertFalse(tfm.getTempDir() + " should not exist", tfm.getTempDir().exists());
57      }
58  
59      @Test
60      public void testCreateTempFileAndDelete() {
61          String subDirName = TempFileManagerTest.class.getSimpleName() + new Random().nextLong();
62          TempFileManager tfm = TempFileManager.instance(subDirName);
63          String prefix = "prefix";
64          String suffix = "suffix";
65          File tempFile = tfm.createTempFile(prefix, suffix);
66          assertThat(tempFile).exists();
67          assertThat(tempFile).isWritable();
68          assertTrue(tempFile.getParentFile().equals(tfm.getTempDir()));
69          assertThat(tempFile.getName()).startsWith(prefix);
70          assertThat(tempFile.getName()).endsWith(suffix);
71          assertThat(tempFile.getName()).contains((String) getInternalState(tfm, "baseName"));
72  
73          List<File> tempFiles = getInternalState(tfm, "tempFiles");
74          assertThat(tempFiles).contains(tempFile);
75          assertThat(tempFiles).size().isEqualTo(1);
76  
77          tfm.deleteAll();
78          assertThat(tempFile).doesNotExist();
79          assertThat(tfm.getTempDir()).doesNotExist();
80      }
81  
82      @Test
83      public void testFileCreateTempFile() throws IOException {
84          File tempFile = File.createTempFile("TempFileManager", ".tmp");
85          assertTrue(tempFile.exists());
86          assertTrue(tempFile.delete());
87          assertFalse(tempFile.exists());
88      }
89  
90      @Test
91      public void testDeleteOnExit() {
92          TempFileManager tfm = TempFileManager.instance();
93  
94          assertFalse(tfm.isDeleteOnExit());
95          tfm.setDeleteOnExit(true);
96          assertTrue(tfm.isDeleteOnExit());
97      }
98  }