de.unkrig.commons.junit4.Files Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of de-unkrig-commons Show documentation
Show all versions of de-unkrig-commons Show documentation
A versatile Java(TM) library that implements many useful container and utility classes.
/*
* de.unkrig.patch - An enhanced version of the UNIX PATCH utility
*
* Copyright (c) 2013, Arno Unkrig
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package de.unkrig.commons.junit4;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import de.unkrig.commons.file.org.apache.commons.compress.archivers.ArchiveFormat;
import de.unkrig.commons.file.org.apache.commons.compress.archivers.ArchiveFormatFactory;
import de.unkrig.commons.file.org.apache.commons.compress.compressors.CompressionFormat;
import de.unkrig.commons.file.org.apache.commons.compress.compressors.CompressionFormatFactory;
import de.unkrig.commons.lang.protocol.ConsumerWhichThrows;
import de.unkrig.commons.nullanalysis.Nullable;
/**
* A helper class for creating, reading and comparing directory trees (and contained ZIP files). Extremely useful for
* testing code that processes or transforms directory trees and/or ZIP files.
*/
public
class Files {
private final Object desc;
/** Initializes this object from the given object array. */
public
Files(Object[] desc) { this.desc = desc; }
/** Initializes this object from the given file or directory. */
public
Files(File file) throws IOException, ArchiveException, CompressorException { this.desc = Files.load(file); }
/** @return The contents of the given file */
private static Object
load(File file) throws IOException, ArchiveException, CompressorException {
// Directory?
if (file.isDirectory()) return Files.loadDir(file);
// Archive file?
ArchiveFormat af = ArchiveFormatFactory.forFileName(file.getName());
if (af != null) return Files.loadArchiveFile(file, af);
// Compressed file?
CompressionFormat cf = CompressionFormatFactory.forFileName(file.getName());
if (cf != null) return Files.loadCompressedFile(file, cf);
// Plain file.
return Files.loadPlainFile(file);
}
/** @return Pairs of name and contents (iff {@code is} is an archive, or the plain text in the file */
private static Object
load(InputStream is) throws IOException, ArchiveException, CompressorException {
if (!is.markSupported()) is = new BufferedInputStream(is);
// Archive?
ArchiveFormat af = ArchiveFormatFactory.forContents(is);
if (af != null) return Files.loadArchive(af.archiveInputStream(is));
// Compressed contents?
CompressionFormat cf = CompressionFormatFactory.forContents(is);
if (cf != null) return Files.load(cf.compressorInputStream(is));
// Load plain contents.
return Files.loadReader(new InputStreamReader(is));
}
/** @return The entries of the given archive file */
private static Object[]
loadArchiveFile(File archiveFile, ArchiveFormat af) throws IOException, ArchiveException, CompressorException {
ArchiveInputStream ais = af.open(archiveFile);
try {
Object[] entries = Files.loadArchive(ais);
ais.close();
return entries;
} finally {
try { ais.close(); } catch (Exception e) {}
}
}
/** @return The contents of the compressed file */
private static Object
loadCompressedFile(File file, CompressionFormat cf)
throws IOException, ArchiveException, CompressorException {
InputStream is = new FileInputStream(file);
try {
CompressorInputStream cis = cf.compressorInputStream(is);
Object contents = Files.load(cis);
cis.close();
return contents;
} finally {
try { is.close(); } catch (Exception e) {}
}
}
/** @return Pairs of name an contents */
private static Object[]
loadArchive(ArchiveInputStream ais) throws IOException, ArchiveException, CompressorException {
List