Search in sources :

Example 1 with SuppressForbidden

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;
    }
}
Also used : Path(java.nio.file.Path) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with SuppressForbidden

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());
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Example 3 with SuppressForbidden

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);
}
Also used : JvmOutputEvent(com.carrotsearch.ant.tasks.junit4.events.aggregated.JvmOutputEvent) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Example 4 with SuppressForbidden

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.");
    }
}
Also used : PrintStream(java.io.PrintStream) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Example 5 with SuppressForbidden

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();
            }
        };
    }
}
Also used : CharSink(com.google.common.io.CharSink) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Aggregations

SuppressForbidden (com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)7 IOException (java.io.IOException)4 BuildException (org.apache.tools.ant.BuildException)3 PrintStream (java.io.PrintStream)2 Path (java.nio.file.Path)2 AppendStdErrEvent (com.carrotsearch.ant.tasks.junit4.events.AppendStdErrEvent)1 AppendStdOutEvent (com.carrotsearch.ant.tasks.junit4.events.AppendStdOutEvent)1 JvmOutputEvent (com.carrotsearch.ant.tasks.junit4.events.aggregated.JvmOutputEvent)1 CharSink (com.google.common.io.CharSink)1 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Execute (org.apache.tools.ant.taskdefs.Execute)1 Variable (org.apache.tools.ant.types.Environment.Variable)1 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1 Description (org.junit.runner.Description)1