patterntesting.runtime.io.FileHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/**
* $Id: FileHelper.java,v 1.8 2011/08/01 22:05:43 oboehm Exp $
*
* Copyright (c) 2008 by Oliver Boehm
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 11.09.2008 by oliver ([email protected])
*/
package patterntesting.runtime.io;
import java.io.*;
import org.apache.commons.io.FilenameUtils;
/**
* The Class FileHelper.
*
* @author oliver
* @since 11.09.2008
* @version $Revision: 1.8 $
*/
public final class FileHelper {
/** No need to instantiate it (utility class). */
private FileHelper() {}
/**
* Gets the tmpdir.
*
* @return the tmpdir
*/
public static File getTmpdir() {
return new File(System.getProperty("java.io.tmpdir"));
}
/**
* Gets the tmpdir.
*
* @param file the file
*
* @return the tmpdir
*/
public static File getTmpdir(final File file) {
return getTmpdir(file.toString());
}
/**
* Gets the tmpdir.
*
* @param filename the filename
*
* @return the tmpdir
*/
public static File getTmpdir(final String filename) {
return new File(getTmpdir(), filename);
}
/**
* Creates a directory in the temp directory.
*
* @return the directory
* @throws IOException Signals that an I/O exception has occurred.
*/
public static File createTmpdir() throws IOException {
return createTmpdir("dir", "");
}
/**
* Creates a directory in the temp directory.
*
* @param prefix the prefix
* @param suffix the suffix
* @return the directory
* @throws IOException Signals that an I/O exception has occurred.
*/
public static File createTmpdir(final String prefix, final String suffix) throws IOException {
File tmpdir = getTmpdir(prefix + System.currentTimeMillis() + suffix);
if (!tmpdir.mkdirs()) {
throw new IOException("can't create dir " + tmpdir);
}
return tmpdir;
}
/**
* Gets the home dir.
*
* @return the home dir
*/
public static File getHomeDir() {
return new File(System.getProperty("user.home"));
}
/**
* Normalizes a file, analogous to {@link FilenameUtils#normalize(String)}.
* But before we normalize it we try to ask to OS for the canonical
* path for it.
*
* @param file the file
* @return the file
*/
public static File normalize(final File file) {
String filename = null;
try {
filename = file.getCanonicalPath();
} catch (IOException e) {
filename = file.getAbsolutePath();
}
filename = FilenameUtils.normalize(filename);
return new File(filename);
}
/**
* Comparing two files is not so easy as it seems to be. On unix systems
* or Mac OS-X you can see the same file via two different mount points or
* as two different hard links. If you are on *nix you can use the inode
* to compare but how to get the inode?
*
* @param f1 file 1
* @param f2 file 2
* @return true, if successful
* @since 1.1
*/
public static boolean equals(final File f1, final File f2) {
File one = normalize(f1);
File two = normalize(f2);
return one.equals(two);
}
}