Search in sources :

Example 41 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesAndroidTest method getLastSessionCrashReportStackOverflowException.

@Test
public void getLastSessionCrashReportStackOverflowException() throws Exception {
    /* Null before start. */
    Crashes.unsetInstance();
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
    /* Crash on 1st process. */
    Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
    Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
    startFresh(null);
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
    final Error exception = generateStackOverflowError();
    assertTrue(exception.getStackTrace().length > ErrorLogHelper.FRAME_LIMIT);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            throw exception;
        }
    };
    thread.start();
    thread.join();
    /* Get last session crash on 2nd process. */
    startFresh(null);
    ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
    assertNotNull(errorReport);
    assertNotNull(errorReport.getStackTrace());
    assertTrue(Crashes.hasCrashedInLastSession().get());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) Test(org.junit.Test)

Example 42 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesAndroidTest method getLastSessionCrashReportNative.

@Test
public void getLastSessionCrashReportNative() throws Exception {
    /* Null before start. */
    Crashes.unsetInstance();
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
    assertNull(Crashes.getMinidumpDirectory().get());
    /* Simulate we have minidump from previous run. */
    File minidumpDirectory = getNewMinidumpDirectory();
    File sNewMinidumpDirectory = new File(minidumpDirectory, UUID.randomUUID().toString());
    FileManager.mkdir(sNewMinidumpDirectory.getPath());
    /* Simulate we have a minidump. */
    File newMinidumpDirectory = ErrorLogHelper.getNewMinidumpSubfolder();
    File minidumpFile = new File(newMinidumpDirectory, "minidump.dmp");
    FileManager.write(minidumpFile, "{\"DEVICE_INFO\":\"{mock minidump}\"}");
    /* Start crashes now. */
    startFresh(null);
    /* We can access directory now. */
    assertEquals(newMinidumpDirectory.getAbsolutePath(), Crashes.getMinidumpDirectory().get());
    ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
    assertNotNull(errorReport);
    assertTrue(Crashes.hasCrashedInLastSession().get());
    assertNotNull(errorReport.getStackTrace());
    /* File has been deleted. */
    assertFalse(minidumpFile.exists());
    /* After restart, it's processed. */
    Crashes.unsetInstance();
    startFresh(null);
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) File(java.io.File) Test(org.junit.Test)

Example 43 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class WrapperSdkExceptionManagerAndroidTest method buildHandledErrorReport.

@Test
public void buildHandledErrorReport() {
    /* If we start the Crashes sdk. */
    long beforeStartTime = System.currentTimeMillis();
    startFresh();
    long afterStartTime = System.currentTimeMillis();
    /* When we build an error report for an handled error. */
    long beforeBuildTime = System.currentTimeMillis();
    String errorReportId = UUID.randomUUID().toString();
    ErrorReport errorReport = WrapperSdkExceptionManager.buildHandledErrorReport(sApplication, errorReportId);
    long afterBuildTime = System.currentTimeMillis();
    /* Then it contains the following properties. */
    assertNotNull(errorReport);
    assertEquals(errorReportId, errorReport.getId());
    assertNotNull(errorReport.getDevice());
    assertNotNull(errorReport.getAppErrorTime());
    assertNotNull(errorReport.getAppStartTime());
    assertNull(errorReport.getStackTrace());
    assertNull(errorReport.getThreadName());
    /* Check start time is consistent. */
    assertTrue(errorReport.getAppStartTime().getTime() >= beforeStartTime);
    assertTrue(errorReport.getAppStartTime().getTime() <= afterStartTime);
    /* Check error time is consistent. */
    assertTrue(errorReport.getAppErrorTime().getTime() >= beforeBuildTime);
    assertTrue(errorReport.getAppErrorTime().getTime() <= afterBuildTime);
    /* Check device info is cached. */
    ErrorReport errorReport2 = WrapperSdkExceptionManager.buildHandledErrorReport(sApplication, UUID.randomUUID().toString());
    assertSame(errorReport.getDevice(), errorReport2.getDevice());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) Test(org.junit.Test)

Example 44 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method checkStacktraceWithLegacyThrowableFile.

@Test
public void checkStacktraceWithLegacyThrowableFile() throws Exception {
    String expectedStacktrace = "type: message\n" + " ClassName.MethodName(FileName:1)";
    /* Mock FileManager call. */
    when(FileManager.read(any(File.class))).thenReturn(expectedStacktrace);
    /* Create throwable file. */
    UUID logId = UUID.randomUUID();
    String throwableFileName = logId + ErrorLogHelper.THROWABLE_FILE_EXTENSION;
    File errorStorageDirectory = mTemporaryFolder.newFolder("error");
    ErrorLogHelper.setErrorLogDirectory(errorStorageDirectory);
    File throwableFile = new File(errorStorageDirectory, throwableFileName);
    assertTrue(throwableFile.createNewFile());
    /* Write file content. */
    BufferedWriter writer = new BufferedWriter(new FileWriter(throwableFile));
    writer.write(expectedStacktrace);
    writer.close();
    /* Mock log. */
    ManagedErrorLog mockLog = new ManagedErrorLog();
    mockLog.setId(logId);
    mockLog.setErrorThreadName("Thread name");
    mockLog.setAppLaunchTimestamp(new Date());
    mockLog.setTimestamp(new Date());
    mockLog.setDevice(new Device());
    /* Build error report. */
    Crashes crashes = Mockito.spy(Crashes.getInstance());
    ErrorReport report = crashes.buildErrorReport(mockLog);
    /* Verify. */
    assertEquals(expectedStacktrace, report.getStackTrace());
    verify(crashes, never()).buildStackTrace(any(com.microsoft.appcenter.crashes.ingestion.models.Exception.class));
    verifyStatic();
    FileManager.read(any(File.class));
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) FileWriter(java.io.FileWriter) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) Date(java.util.Date) JSONException(org.json.JSONException) IOException(java.io.IOException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) BufferedWriter(java.io.BufferedWriter) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) UUID(java.util.UUID) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 45 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method buildErrorReport.

@Test
public void buildErrorReport() {
    /* Mock and set exception data. */
    com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
    when(mockException.getType()).thenReturn("type");
    when(mockException.getMessage()).thenReturn("message");
    mErrorLog.setException(mockException);
    mErrorLog.setDevice(mock(Device.class));
    ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
    mockStatic(ErrorLogHelper.class);
    File throwableFile = mock(File.class);
    when(throwableFile.length()).thenReturn(1L);
    when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE)).thenReturn(errorReport);
    when(FileManager.read(any(File.class))).thenReturn(STACK_TRACE);
    Crashes crashes = Crashes.getInstance();
    ErrorReport report = crashes.buildErrorReport(mErrorLog);
    assertErrorEquals(mErrorLog, report);
    verifyStatic();
    ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
    /* Verify the caching. */
    assertEquals(report, crashes.buildErrorReport(mErrorLog));
    verifyStatic();
    ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
    mErrorLog.setId(UUID.randomUUID());
    report = crashes.buildErrorReport(mErrorLog);
    assertNotNull(report);
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) Device(com.microsoft.appcenter.ingestion.models.Device) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)54 Test (org.junit.Test)40 File (java.io.File)36 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 Context (android.content.Context)26 Channel (com.microsoft.appcenter.channel.Channel)25 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)22 UUID (java.util.UUID)21 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)18 Device (com.microsoft.appcenter.ingestion.models.Device)12 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)12 SessionContext (com.microsoft.appcenter.utils.context.SessionContext)12 SessionContext (com.microsoft.appcenter.SessionContext)11 Log (com.microsoft.appcenter.ingestion.models.Log)11 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)10 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)10 JSONException (org.json.JSONException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9