mockit.coverage.reporting.StaticFiles Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.coverage.reporting;
import java.io.*;
import java.security.*;
import javax.annotation.*;
final class StaticFiles
{
@Nonnull private final String outputDir;
private long lastModifiedTimeOfCoverageJar;
StaticFiles(@Nonnull String outputDir) { this.outputDir = outputDir; }
void copyToOutputDir(boolean forSourceFilePages) throws IOException
{
copyFile("coverage.css");
copyFile("coverage.js");
copyFile("logo.png");
copyFile("package.png");
copyFile("class.png");
copyFile("abstractClass.png");
copyFile("interface.png");
copyFile("annotation.png");
copyFile("exception.png");
copyFile("enum.png");
if (forSourceFilePages) {
copyFile("prettify.js");
}
}
private void copyFile(@Nonnull String fileName) throws IOException
{
File outputFile = new File(outputDir, fileName);
if (outputFile.exists() && outputFile.lastModified() > getLastModifiedTimeOfCoverageJar()) {
return;
}
//noinspection IOResourceOpenedButNotSafelyClosed
OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
InputStream input = new BufferedInputStream(StaticFiles.class.getResourceAsStream(fileName));
try {
int b;
while ((b = input.read()) != -1) {
output.write(b);
}
}
finally {
try {
input.close();
}
finally {
output.close();
}
}
}
private long getLastModifiedTimeOfCoverageJar()
{
if (lastModifiedTimeOfCoverageJar == 0) {
CodeSource codeSource = getClass().getProtectionDomain().getCodeSource();
if (codeSource == null) {
lastModifiedTimeOfCoverageJar = -1;
}
else {
String pathToThisJar = codeSource.getLocation().getPath();
lastModifiedTimeOfCoverageJar = new File(pathToThisJar).lastModified();
}
}
return lastModifiedTimeOfCoverageJar;
}
}