001/*
002 * Copyright 2018 Nils Hoffmann <nils.hoffmann@isas.de>.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.isas.mztab2.test.utils;
017
018import java.io.File;
019import java.io.IOException;
020import java.nio.file.Files;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.logging.Level;
024import java.util.logging.Logger;
025import org.junit.rules.ExternalResource;
026import org.junit.rules.TemporaryFolder;
027
028/**
029 * <p>
030 * ExtractClassPathFiles class.</p>
031 *
032 * @author nilshoffmann
033 *
034 */
035public class ExtractClassPathFiles extends ExternalResource {
036
037    private final TemporaryFolder tf;
038    private final ClassPathFile[] classPathFiles;
039    private final List<File> files = new LinkedList<>();
040    private File baseFolder;
041
042    /**
043     * <p>
044     * Constructor for ExtractClassPathFiles.</p>
045     *
046     * @param classPathFiles an array of {@link ClassPathFile} objects to extract.
047     */
048    public ExtractClassPathFiles(ClassPathFile... classPathFiles) {
049        this(new TemporaryFolder(), classPathFiles);
050    }
051    
052    /**
053     * <p>
054     * Constructor for ExtractClassPathFiles.</p>
055     *
056     * @param tf a {@link org.junit.rules.TemporaryFolder} object.
057     * @param classPathFiles an array of {@link ClassPathFile} objects to extract.
058     */
059    public ExtractClassPathFiles(TemporaryFolder tf, ClassPathFile... classPathFiles) {
060        this.tf = tf;
061        this.classPathFiles = classPathFiles;
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    protected void before() throws Throwable {
069        try {
070            this.tf.create();
071        } catch (IOException ex) {
072            throw ex;
073        }
074        baseFolder = tf.getRoot();
075        int i = 0;
076        for (ClassPathFile resource : classPathFiles) {
077            File file = ZipResourceExtractor.extract(
078                resource.resourcePath(), baseFolder);
079            files.add(file);
080        }
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    protected void after() {
088        for (File f : files) {
089            if (f != null && f.exists()) {
090                try {
091                    Files.deleteIfExists(f.toPath());
092                } catch (IOException ioex) {
093                    Logger.getLogger(ExtractClassPathFiles.class.getName()).
094                        log(Level.SEVERE,
095                            "Caught an IOException while trying to delete file " + f.
096                                getAbsolutePath(), ioex);
097                }
098            }
099        }
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}