Search in sources :

Example 1 with JsonWriter

use of com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter in project randomizedtesting by randomizedtesting.

the class AggregatedSuiteResultEvent method serialize.

private void serialize(JsonWriter w, SimpleDateFormat sdf, AggregatedTestResultEvent e) throws IOException {
    w.beginObject();
    w.name("slave").value(e.getSlave().id);
    w.name("startTimestamp").value(e.getStartTimestamp());
    w.name("startTimestampDate").value(sdf.format(new Date(e.getStartTimestamp())));
    w.name("executionTime").value(e.getExecutionTime());
    w.name("description");
    JsonHelpers.writeDescription(w, e.getDescription());
    w.name("status").value(e.getStatus().name());
    w.name("testFailures");
    w.beginArray();
    for (FailureMirror m : e.getFailures()) {
        serialize(w, m);
    }
    w.endArray();
    w.endObject();
}
Also used : FailureMirror(com.carrotsearch.ant.tasks.junit4.events.mirrors.FailureMirror) Date(java.util.Date)

Example 2 with JsonWriter

use of com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter in project randomizedtesting by randomizedtesting.

the class AggregatedSuiteResultEvent method serializeEvents.

private void serializeEvents(JsonWriter w, boolean outputStreams) throws IOException {
    final Charset charset = getSlave().getCharset();
    int lineBuffer = 160;
    final StringWriter out = new StringWriter();
    final StringWriter err = new StringWriter();
    WriterOutputStream stdout = new WriterOutputStream(out, charset, lineBuffer, false);
    WriterOutputStream stderr = new WriterOutputStream(err, charset, lineBuffer, false);
    for (IEvent evt : getEventStream()) {
        try {
            switch(evt.getType()) {
                case SUITE_FAILURE:
                case TEST_IGNORED_ASSUMPTION:
                case TEST_IGNORED:
                case TEST_STARTED:
                case TEST_FINISHED:
                case TEST_FAILURE:
                    flushBoth(w, out, err, stdout, stderr);
                    w.beginObject();
                    w.name("event").value(evt.getType().toString());
                    w.name("description");
                    JsonHelpers.writeDescription(w, ((IDescribable) evt).getDescription());
                    if (evt instanceof FailureEvent) {
                        w.name("failure");
                        ((FailureEvent) evt).serialize(w);
                    }
                    w.endObject();
                    break;
                case APPEND_STDOUT:
                    if (outputStreams) {
                        flush(APPEND_STDERR, w, stderr, err);
                        ((IStreamEvent) evt).copyTo(stdout);
                    }
                    break;
                case APPEND_STDERR:
                    if (outputStreams) {
                        flush(APPEND_STDOUT, w, stdout, out);
                        ((IStreamEvent) evt).copyTo(stderr);
                    }
                    break;
                default:
                    break;
            }
        } catch (IOException ex) {
        // Ignore.
        }
    }
    flushBoth(w, out, err, stdout, stderr);
}
Also used : StringWriter(java.io.StringWriter) IEvent(com.carrotsearch.ant.tasks.junit4.events.IEvent) IStreamEvent(com.carrotsearch.ant.tasks.junit4.events.IStreamEvent) FailureEvent(com.carrotsearch.ant.tasks.junit4.events.FailureEvent) Charset(java.nio.charset.Charset) IOException(java.io.IOException) WriterOutputStream(com.carrotsearch.randomizedtesting.WriterOutputStream)

Example 3 with JsonWriter

use of com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter in project randomizedtesting by randomizedtesting.

the class AggregatedSuiteResultEvent method serialize.

public void serialize(JsonWriter w, boolean outputStreams) throws IOException {
    w.beginObject();
    w.name("slave").value(getSlave().id);
    w.name("startTimestamp").value(getStartTimestamp());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ROOT);
    w.name("startTimestampDate").value(sdf.format(new Date(getStartTimestamp())));
    w.name("executionTime").value(getExecutionTime());
    w.name("description");
    JsonHelpers.writeDescription(w, getDescription());
    w.name("tests");
    w.beginArray();
    for (AggregatedTestResultEvent e : getTests()) {
        serialize(w, sdf, e);
    }
    w.endArray();
    w.name("suiteFailures");
    w.beginArray();
    for (FailureMirror m : getFailures()) {
        serialize(w, m);
    }
    w.endArray();
    w.name("executionEvents");
    w.beginArray();
    serializeEvents(w, outputStreams);
    w.endArray();
    w.endObject();
}
Also used : FailureMirror(com.carrotsearch.ant.tasks.junit4.events.mirrors.FailureMirror) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with JsonWriter

use of com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter in project randomizedtesting by randomizedtesting.

the class TestRemoteEventSerialization method checkRoundtrip.

private void checkRoundtrip(RemoteEvent event) throws IOException {
    StringWriter sw = new StringWriter();
    final boolean lenient = randomBoolean();
    JsonWriter jw = new JsonWriter(sw);
    jw.setIndent("  ");
    jw.setLenient(lenient);
    event.serialize(jw);
    jw.close();
    String serialized1 = sw.toString();
    JsonReader jr = new JsonReader(new StringReader(serialized1));
    jr.setLenient(lenient);
    RemoteEvent deserialized = event.getType().deserialize(jr);
    // If we serialize again, the contents should be identical.
    sw.getBuffer().setLength(0);
    jw = new JsonWriter(sw);
    jw.setIndent("  ");
    jw.setLenient(lenient);
    deserialized.serialize(jw);
    jw.close();
    String serialized2 = sw.toString();
    if (!serialized2.equals(serialized1)) {
        fail("Roundtrip serialization failed:\n1: " + serialized1 + "\n2: " + serialized2);
    }
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonReader(com.carrotsearch.ant.tasks.junit4.gson.stream.JsonReader) JsonWriter(com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter)

Example 5 with JsonWriter

use of com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter in project randomizedtesting by randomizedtesting.

the class JsonReport method setOuter.

/*
   * 
   */
@Override
public void setOuter(JUnit4 junit4) {
    this.junit4 = junit4;
    if (this.targetFile == null) {
        throw new BuildException("'file' attribute is required (target file name ending in .html, .json or .jsonp).");
    }
    if (method == OutputMethod.HTML) {
        if (Strings.isNullOrEmpty(jsonpMethod)) {
            setJsonpMethod("testData");
        } else if (!"testData".equals(jsonpMethod)) {
            throw new BuildException("JSONP method must be empty or equal 'testData' for HTML output.");
        }
    }
    if (method == OutputMethod.JSONP) {
        if (Strings.isNullOrEmpty(jsonpMethod)) {
            setJsonpMethod("testData");
        }
    }
    try {
        Files.createParentDirs(targetFile);
        File jsonFile = targetFile;
        if (method == OutputMethod.HTML) {
            jsonFile = new File(removeExtension(targetFile.getAbsolutePath()) + ".jsonp");
        }
        writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(jsonFile)), Charsets.UTF_8);
        if (!Strings.isNullOrEmpty(jsonpMethod)) {
            writer.write(jsonpMethod);
            writer.write("(");
        }
        jsonWriter = new JsonWriter(writer);
        jsonWriter.setHtmlSafe(false);
        jsonWriter.setIndent("  ");
        // Main holder.
        jsonWriter.beginObject();
        // junit4 object with properties.
        jsonWriter.name("junit4");
        jsonWriter.beginObject();
        jsonWriter.name("tests.seed");
        jsonWriter.value(junit4.getSeed());
        jsonWriter.name("project.name");
        jsonWriter.value(getProjectName());
        jsonWriter.endObject();
        // suites and an array of suites follows.
        jsonWriter.name("suites");
        jsonWriter.beginArray();
    } catch (IOException e) {
        throw new BuildException("Could not emit JSON report.", e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) JsonWriter(com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter)

Aggregations

FailureMirror (com.carrotsearch.ant.tasks.junit4.events.mirrors.FailureMirror)2 JsonWriter (com.carrotsearch.ant.tasks.junit4.gson.stream.JsonWriter)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Date (java.util.Date)2 FailureEvent (com.carrotsearch.ant.tasks.junit4.events.FailureEvent)1 IEvent (com.carrotsearch.ant.tasks.junit4.events.IEvent)1 IStreamEvent (com.carrotsearch.ant.tasks.junit4.events.IStreamEvent)1 JsonReader (com.carrotsearch.ant.tasks.junit4.gson.stream.JsonReader)1 WriterOutputStream (com.carrotsearch.randomizedtesting.WriterOutputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 Charset (java.nio.charset.Charset)1 SimpleDateFormat (java.text.SimpleDateFormat)1 BuildException (org.apache.tools.ant.BuildException)1