public final class

ParcelableFailure

extends java.lang.Object

 java.lang.Object

↳androidx.test.orchestrator.junit.ParcelableFailure

Gradle dependencies

compile group: 'androidx.test', name: 'runner', version: '1.5.0-alpha03'

  • groupId: androidx.test
  • artifactId: runner
  • version: 1.5.0-alpha03

Artifact androidx.test:runner:1.5.0-alpha03 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:runner com.android.support.test:runner

Androidx class mapping:

androidx.test.orchestrator.junit.ParcelableFailure android.support.test.orchestrator.junit.ParcelableFailure

Overview

Parcelable imitation of a JUnit Failure

Summary

Fields
public static final <any>CREATOR

Constructors
publicParcelableFailure(Failure failure)

publicParcelableFailure(ParcelableDescription description, java.lang.String message)

publicParcelableFailure(ParcelableDescription description, java.lang.Throwable t)

Methods
public intdescribeContents()

public ParcelableDescriptiongetDescription()

public java.lang.StringgetTrace()

public voidwriteToParcel(Parcel out, int flags)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final <any> CREATOR

Constructors

public ParcelableFailure(Failure failure)

public ParcelableFailure(ParcelableDescription description, java.lang.Throwable t)

public ParcelableFailure(ParcelableDescription description, java.lang.String message)

Methods

public int describeContents()

public void writeToParcel(Parcel out, int flags)

public java.lang.String getTrace()

public ParcelableDescription getDescription()

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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 or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.orchestrator.junit;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import androidx.test.services.events.internal.StackTrimmer;
import org.junit.runner.notification.Failure;

/** Parcelable imitation of a JUnit Failure */
public final class ParcelableFailure implements Parcelable {

  private static final String TAG = "ParcelableFailure";

  private static final int MAX_STREAM_LENGTH = 16 * 1024;

  private final ParcelableDescription description;
  private final String trace;

  public ParcelableFailure(Failure failure) {
    this.description = new ParcelableDescription(failure.getDescription());
    this.trace = StackTrimmer.getTrimmedStackTrace(failure);
  }

  private ParcelableFailure(Parcel in) {
    description = in.readParcelable(ParcelableDescription.class.getClassLoader());
    trace = in.readString();
  }

  public ParcelableFailure(ParcelableDescription description, Throwable t) {
    this(description, t.getMessage());
  }

  public ParcelableFailure(ParcelableDescription description, String message) {
    this.description = description;
    trace = trimToLength(message);
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel out, int flags) {
    out.writeParcelable(description, 0);
    out.writeString(trace);
  }

  public static final Creator<ParcelableFailure> CREATOR =
      new Creator<ParcelableFailure>() {
        @Override
        public ParcelableFailure createFromParcel(Parcel in) {
          return new ParcelableFailure(in);
        }

        @Override
        public ParcelableFailure[] newArray(int size) {
          return new ParcelableFailure[size];
        }
      };

  private static String trimToLength(String trace) {
    if (!trace.endsWith("\n")) {
      trace = trace + "\n";
    }
    if (trace.length() > MAX_STREAM_LENGTH) {
      Log.i(
          TAG,
          String.format(
              "Stack trace too long, trimmed to first %s characters.", MAX_STREAM_LENGTH - 1));
      return trace.substring(0, MAX_STREAM_LENGTH - 1) + "\n";
    } else {
      return trace;
    }
  }

  public String getTrace() {
    return trace;
  }

  public ParcelableDescription getDescription() {
    return description;
  }
}