How to use delete method of org.junit.rules.TemporaryFolder class

Best junit code snippet using org.junit.rules.TemporaryFolder.delete

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1public class org.junit.rules.TemporaryFolder extends org.junit.rules.ExternalResource {
2  public org.junit.rules.TemporaryFolder();
3  public org.junit.rules.TemporaryFolder(java.io.File);
4  protected org.junit.rules.TemporaryFolder(org.junit.rules.TemporaryFolder$Builder);
5  public static org.junit.rules.TemporaryFolder$Builder builder();
6  protected void before() throws java.lang.Throwable;
7  protected void after();
8  public void create() throws java.io.IOException;
9  public java.io.File newFile(java.lang.String) throws java.io.IOException;
10  public java.io.File newFile() throws java.io.IOException;
11  public java.io.File newFolder(java.lang.String) throws java.io.IOException;
12  public java.io.File newFolder(java.lang.String...) throws java.io.IOException;
13  public java.io.File newFolder() throws java.io.IOException;
14  public java.io.File getRoot();
15  public void delete();
16}
17
Full Screen
copy
1package org.opentosca.toscana.core;
2
3import java.io.File;
4import java.io.IOException;
5
6import org.apache.commons.io.FileUtils;
7import org.junit.AfterClass;
8import org.junit.Before;
9import org.junit.BeforeClass;
10import org.junit.Rule;
11import org.junit.rules.DisableOnDebug;
12import org.junit.rules.TemporaryFolder;
13import org.junit.rules.TestRule;
14import org.junit.rules.Timeout;
15import org.junit.runner.RunWith;
16import org.junit.runners.JUnit4;
17
18/**
19 If files need to get written on disk during tests, use {@link #temporaryFolder}
20 */
21@RunWith(JUnit4.class)
22public abstract class BaseUnitTest extends BaseTest {
23
24    protected static File staticTmpDir;
25    private static final String STATIC_TMPDIR = "testsuite-tmpdir-static";
26    /**
27     Grants disk access. Is reset before every test method.
28     */
29    @Rule
30    public final TemporaryFolder temporaryFolder = new TemporaryFolder(PROJECT_ROOT);
31
32    /**
33     Timeout rule
34     <p>
35     This rule limits the runtime of a test (one method) to 30 Seconds.
36     This is to prevent continuos loops
37     <p>
38     This is equal to @Test(timeout = 30000)
39     */
40    @Rule
41    public final TestRule timeoutRule = new DisableOnDebug(Timeout.seconds(30));
42    protected File tmpdir;
43
44    @BeforeClass
45    public final static void offerStaticTmpDir() throws IOException {
46        staticTmpDir = new File(PROJECT_ROOT, STATIC_TMPDIR);
47        FileUtils.deleteDirectory(staticTmpDir);
48        staticTmpDir.mkdir();
49    }
50
51    @Before
52    public final void initTmpdir() throws IOException {
53        tmpdir = temporaryFolder.newFolder();
54    }
55
56    @AfterClass
57    public static void cleanUpDisk() throws IOException, InterruptedException {
58        File[] files = PROJECT_ROOT.listFiles((file1, s) -> s.matches("junit[0-9]+"));
59        for (File file : files) {
60            FileUtils.deleteDirectory(file);
61        }
62    }
63
64    @AfterClass
65    public final static void cleanupStaticTmpDir() throws IOException {
66        FileUtils.deleteDirectory(staticTmpDir);
67    }
68}
69
Full Screen
copy
1/*
2 * Copyright 2018 Nils Hoffmann &lt;[email protected]&gt;.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package de.isas.mztab2.test.utils;
17
18import java.io.File;
19import java.io.IOException;
20import java.nio.file.Files;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.logging.Level;
24import java.util.logging.Logger;
25import org.junit.rules.ExternalResource;
26import org.junit.rules.TemporaryFolder;
27
28/**
29 * <p>
30 * ExtractClassPathFiles class.</p>
31 *
32 * @author nilshoffmann
33 *
34 */
35public class ExtractClassPathFiles extends ExternalResource {
36
37    private final TemporaryFolder tf;
38    private final ClassPathFile[] classPathFiles;
39    private final List<File> files = new LinkedList<>();
40    private File baseFolder;
41
42    /**
43     * <p>
44     * Constructor for ExtractClassPathFiles.</p>
45     *
46     * @param classPathFiles an array of {@link ClassPathFile} objects to extract.
47     */
48    public ExtractClassPathFiles(ClassPathFile... classPathFiles) {
49        this(new TemporaryFolder(), classPathFiles);
50    }
51    
52    /**
53     * <p>
54     * Constructor for ExtractClassPathFiles.</p>
55     *
56     * @param tf a {@link org.junit.rules.TemporaryFolder} object.
57     * @param classPathFiles an array of {@link ClassPathFile} objects to extract.
58     */
59    public ExtractClassPathFiles(TemporaryFolder tf, ClassPathFile... classPathFiles) {
60        this.tf = tf;
61        this.classPathFiles = classPathFiles;
62    }
63
64    /**
65     * {@inheritDoc}
66     */
67    @Override
68    protected void before() throws Throwable {
69        try {
70            this.tf.create();
71        } catch (IOException ex) {
72            throw ex;
73        }
74        baseFolder = tf.getRoot();
75        int i = 0;
76        for (ClassPathFile resource : classPathFiles) {
77            File file = ZipResourceExtractor.extract(
78                resource.resourcePath(), baseFolder);
79            files.add(file);
80        }
81    }
82
83    /**
84     * {@inheritDoc}
85     */
86    @Override
87    protected void after() {
88        for (File f : files) {
89            if (f != null && f.exists()) {
90                try {
91                    Files.deleteIfExists(f.toPath());
92                } catch (IOException ioex) {
93                    Logger.getLogger(ExtractClassPathFiles.class.getName()).
94                        log(Level.SEVERE,
95                            "Caught an IOException while trying to delete file " + f.
96                                getAbsolutePath(), ioex);
97                }
98            }
99        }
100        tf.delete();
101    }
102
103    /**
104     * <p>
105     * Getter for the field <code>files</code>.</p>
106     *
107     * @return a {@link java.util.List} object.
108     */
109    public List<File> getFiles() {
110        return this.files;
111    }
112
113    /**
114     * <p>
115     * getBaseDir.</p>
116     *
117     * @return a {@link java.io.File} object.
118     */
119    public File getBaseDir() {
120        return baseFolder;
121    }
122}
123
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in TemporaryFolder

Trigger delete code on LambdaTest Cloud Grid

Execute automation tests with delete on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)