All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.coderskitchen.junitrules.filesetup.FilePrepareStatement Maven / Gradle / Ivy

Go to download

This archive represents a set of JUnit Rules that aren't available in the basic setup.

There is a newer version: 1.0.2
Show newest version
/**
 * Copyright 2013 Peter Daum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.coderskitchen.junitrules.filesetup;

import org.junit.runners.model.Statement;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * This Statement wraps the original statement.
 * It grants creation and deletion of a requested directory and file structure
 * to be used by the Statement base.
 * 

* The directory and file structure is provided by an HashMap>. * * @author peter */ public class FilePrepareStatement extends Statement { private final HashMap> directoryStructure; private Statement base; /** * Constructor * * @param base The wrapped statement * @param directoryStructure directory structure to be created */ public FilePrepareStatement(final Statement base, HashMap> directoryStructure) { this.base = base; this.directoryStructure = directoryStructure; } @Override public void evaluate() throws Throwable { boolean setupSuccessfully = false; try { setupStructure(); setupSuccessfully = true; base.evaluate(); } finally { if (setupSuccessfully) { tearDownStructure(); } } } private void setupStructure() throws IOException { Set directories = directoryStructure.keySet(); for (String currentDir : directories) { File directory = new File(currentDir); if (!directory.exists() && !directory.mkdirs()) { throw new IOException("Directory '" + directory.getAbsolutePath() + "' does not exist and creation failed!"); } HashSet files = directoryStructure.get(currentDir); for (String currentFile : files) { File toCreate = new File(directory, currentFile); try { if (!toCreate.createNewFile()) { throw new IOException("Touch of file '" + toCreate.getAbsolutePath() + "' failed!"); } } catch (Throwable t) { IOException ex = new IOException("Touch of file '" + toCreate.getAbsolutePath() + "' failed!"); ex.setStackTrace(t.getStackTrace()); throw ex; } } } } private void tearDownStructure() throws IOException { Set directories = directoryStructure.keySet(); for (String directory : directories) { recursiveDelete(new File(directory)); } } private void recursiveDelete(File directory) throws IOException { File[] listed = directory.listFiles(); for (File subFile : listed) { if (!subFile.exists()) continue; if (subFile.isDirectory()) { recursiveDelete(subFile); } if (!subFile.delete()) { throw new IOException("File / Directory '" + subFile.getAbsolutePath() + "' couldn't be deleted!"); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy