use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.
the class RandomizedTest method globalTempDir.
/**
* Global temporary directory created for the duration of this class's lifespan. If
* multiple class loaders are used, there may be more global temp dirs, but it
* shouldn't really be the case in practice.
*/
public static Path globalTempDir() throws IOException {
checkContext();
synchronized (RandomizedTest.class) {
if (globalTempDir == null) {
String tempDirPath = System.getProperty("java.io.tmpdir");
if (tempDirPath == null)
throw new Error("No property java.io.tmpdir?");
Path tempDir = Paths.get(tempDirPath);
if (!Files.isDirectory(tempDir) || !Files.isWritable(tempDir)) {
throw new Error("Temporary folder not accessible: " + tempDir.toAbsolutePath());
}
SimpleDateFormat tsFormat = new SimpleDateFormat("'tests-'yyyyMMddHHmmss'-'SSS", Locale.ROOT);
String dirName = tsFormat.format(new Date());
final Path tmpFolder = tempDir.resolve(dirName);
// I assume mkdir is filesystem-atomic and only succeeds if the
// directory didn't previously exist?
Files.createDirectories(tmpFolder);
globalTempDir = tmpFolder;
Runtime.getRuntime().addShutdownHook(new Thread() {
@SuppressForbidden("Legitimate use of syserr.")
public void run() {
try {
rmDir(globalTempDir);
} catch (IOException e) {
// Not much else to do but to log and quit.
System.err.println("Could not delete temporary folder: " + globalTempDir.toAbsolutePath() + ". Cause: ");
e.printStackTrace(System.err);
}
}
});
}
return globalTempDir;
}
}
use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.
the class ReproduceInfoPrinter method testFailure.
@Override
@SuppressForbidden("Legitimate use of syserr.")
public void testFailure(Failure failure) throws Exception {
// Ignore assumptions.
if (failure.getException() instanceof AssumptionViolatedException) {
return;
}
final Description d = failure.getDescription();
final StringBuilder b = new StringBuilder();
b.append("FAILURE : ").append(d.getDisplayName()).append("\n");
b.append("Message : " + failure.getMessage() + "\n");
b.append("Reproduce: ");
new ReproduceErrorMessageBuilder(b).appendAllOpts(failure.getDescription());
b.append("\n");
b.append("Throwable:\n");
if (failure.getException() != null) {
TraceFormatting traces = new TraceFormatting();
try {
traces = RandomizedContext.current().getRunner().getTraceFormatting();
} catch (IllegalStateException e) {
// Ignore if no context.
}
traces.formatThrowable(b, failure.getException());
}
System.err.println(b.toString());
}
use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.
the class JUnit4 method checkJvmOutput.
@SuppressForbidden("legitimate sysout.")
private void checkJvmOutput(EventBus aggregatedBus, Path file, ForkedJvmInfo forked, String fileName) throws IOException {
if (Files.size(file) > 0) {
String message = "JVM J" + forked.id + ": " + fileName + " was not empty, see: " + file;
if (jvmOutputAction.contains(JvmOutputAction.WARN)) {
log(message, Project.MSG_WARN);
}
if (jvmOutputAction.contains(JvmOutputAction.LISTENERS)) {
aggregatedBus.post(new JvmOutputEvent(forked, file.toFile()));
}
if (jvmOutputAction.contains(JvmOutputAction.PIPE)) {
log(">>> JVM J" + forked.id + ": " + fileName + " (verbatim) ----", Project.MSG_INFO);
try {
// If file > 10 mb, stream directly. Otherwise use the logger.
if (Files.size(file) < 10 * (1024 * 1024)) {
// Append to logger.
log(new String(Files.readAllBytes(file), forked.getCharset()), Project.MSG_INFO);
} else {
// Stream directly.
CharStreams.copy(Files.newBufferedReader(file, forked.getCharset()), System.out);
}
} catch (IOException e) {
log("Couldn't pipe file " + file + ": " + e.toString(), Project.MSG_INFO);
}
log("<<< JVM J" + forked.id + ": EOF ----", Project.MSG_INFO);
}
if (jvmOutputAction.contains(JvmOutputAction.IGNORE)) {
Files.delete(file);
}
if (jvmOutputAction.contains(JvmOutputAction.FAIL)) {
throw new BuildException(message);
}
return;
}
Files.delete(file);
}
use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.
the class SlaveMain method warn.
/**
* Warning emitter. Uses whatever alternative non-event communication channel is.
*/
@SuppressForbidden("legitimate sysstreams.")
public static void warn(String message, Throwable t) {
PrintStream w = (warnings == null ? System.err : warnings);
try {
w.print("WARN: ");
w.print(message);
if (t != null) {
w.print(" -> ");
try {
t.printStackTrace(w);
} catch (OutOfMemoryError e) {
// Ignore, OOM.
w.print(t.getClass().getName());
w.print(": ");
w.print(t.getMessage());
w.println(" (stack unavailable; OOM)");
}
} else {
w.println();
}
w.flush();
} catch (OutOfMemoryError t2) {
w.println("ERROR: Couldn't even serialize a warning (out of memory).");
} catch (Throwable t2) {
// Can't do anything, really. Probably an OOM?
w.println("ERROR: Couldn't even serialize a warning.");
}
}
use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.
the class TextReport method setOuter.
/**
* Initialization by container task {@link JUnit4}.
*/
@Override
@SuppressForbidden("legitimate sysstreams.")
public void setOuter(JUnit4 task) {
this.seed = task.getSeed();
if (outputFile != null) {
try {
Files.createParentDirs(outputFile);
final CharSink charSink;
if (append) {
charSink = Files.asCharSink(outputFile, Charsets.UTF_8, FileWriteMode.APPEND);
} else {
charSink = Files.asCharSink(outputFile, Charsets.UTF_8);
}
this.output = charSink.openBufferedStream();
} catch (IOException e) {
throw new BuildException(e);
}
} else {
if (!UNICODE_ENCODINGS.contains(Charset.defaultCharset().name())) {
task.log("Your default console's encoding may not display certain" + " unicode glyphs: " + Charset.defaultCharset().name(), Project.MSG_INFO);
}
output = new LineFlushingWriter(new OutputStreamWriter(System.out, Charset.defaultCharset())) {
@Override
public // Don't close the underlying stream, just flush.
void close() throws IOException {
flush();
}
};
}
}
Aggregations