Search in sources :

Example 31 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project AppCenter-SDK-Android by Microsoft.

the class HandledErrorTest method trackExceptionWithEverythingFromWrapper.

@Test
public void trackExceptionWithEverythingFromWrapper() {
    /* If we start crashes. */
    startCrashes();
    /* And set a userId. */
    UserIdContext.getInstance().setUserId("omega");
    /* When we track error. */
    ErrorAttachmentLog textAttachment = ErrorAttachmentLog.attachmentWithText("text", null);
    ErrorAttachmentLog binaryAttachment = ErrorAttachmentLog.attachmentWithBinary("hello".getBytes(), "hello.so", "application/octet-stream");
    HashMap<String, String> properties = new HashMap<String, String>() {

        {
            put("a", "b");
        }
    };
    Exception modelException = new Exception();
    String errorId = WrapperSdkExceptionManager.trackException(modelException, properties, Arrays.asList(textAttachment, binaryAttachment));
    assertNotNull(errorId);
    /* Then we send the handled error. */
    ArgumentCaptor<Log> logs = ArgumentCaptor.forClass(Log.class);
    verify(mChannel, times(3)).enqueue(logs.capture(), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    assertNotNull(logs.getAllValues());
    assertEquals(3, logs.getAllValues().size());
    assertTrue(logs.getAllValues().get(0) instanceof HandledErrorLog);
    HandledErrorLog handledErrorLog = (HandledErrorLog) logs.getAllValues().get(0);
    assertEquals(modelException, handledErrorLog.getException());
    assertEquals(properties, handledErrorLog.getProperties());
    assertEquals(errorId, String.valueOf(handledErrorLog.getId()));
    /* Then the attachments. */
    assertSame(textAttachment, logs.getAllValues().get(1));
    assertSame(binaryAttachment, logs.getAllValues().get(2));
    /* We send userId only in the error log. */
    assertEquals("omega", handledErrorLog.getUserId());
    assertNull(logs.getAllValues().get(1).getUserId());
    assertNull(logs.getAllValues().get(2).getUserId());
}
Also used : HashMap(java.util.HashMap) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) Test(org.junit.Test)

Example 32 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project AppCenter-SDK-Android by Microsoft.

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithJSONException.

@Test
public void saveWrapperSdkCrashFailsWithJSONException() throws JSONException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenThrow(new JSONException("mock"));
    Crashes.getInstance().setLogSerializer(logSerializer);
    String data = "d";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof JSONException;
        }
    }));
    /* Second call is ignored. */
    data = "e";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    /* No more error. */
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof JSONException;
        }
    }));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) JSONException(org.json.JSONException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project AppCenter-SDK-Android by Microsoft.

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrash.

@Test
public void saveWrapperSdkCrash() throws JSONException, IOException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    String data = "d";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    FileManager.write(any(File.class), eq(data));
    /* We can't do it twice in the same process. */
    data = "e";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic(never());
    FileManager.write(any(File.class), eq(data));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project AppCenter-SDK-Android by Microsoft.

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithIOExceptionAfterLog.

@Test
public void saveWrapperSdkCrashFailsWithIOExceptionAfterLog() throws IOException, JSONException {
    String data = "d";
    doThrow(new IOException()).when(FileManager.class);
    FileManager.write(any(File.class), eq(data));
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
    /* Second call is ignored. */
    data = "e";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    /* No more error. */
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) IOException(java.io.IOException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.

the class HandledErrorTest method trackExceptionWithOneAttachmentFromWrapper.

@Test
public void trackExceptionWithOneAttachmentFromWrapper() {
    /* If we start crashes. */
    startCrashes();
    /* When we track error with an attachment. */
    ErrorAttachmentLog textAttachment = ErrorAttachmentLog.attachmentWithText("text", null);
    Exception exception = new Exception();
    String errorId = WrapperSdkExceptionManager.trackException(exception, null, Collections.singleton(textAttachment));
    assertNotNull(errorId);
    /* Then we send the handled error. */
    ArgumentCaptor<Log> log = ArgumentCaptor.forClass(Log.class);
    verify(mChannel, times(2)).enqueue(log.capture(), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    assertNotNull(log.getAllValues());
    assertEquals(2, log.getAllValues().size());
    assertTrue(log.getAllValues().get(0) instanceof HandledErrorLog);
    HandledErrorLog handledErrorLog = (HandledErrorLog) log.getAllValues().get(0);
    assertEquals(exception, handledErrorLog.getException());
    assertEquals(errorId, String.valueOf(handledErrorLog.getId()));
    /* Then the attachment. */
    assertSame(textAttachment, log.getAllValues().get(1));
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) Test(org.junit.Test)

Aggregations

Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)49 Test (org.junit.Test)41 IOException (java.io.IOException)38 JSONException (org.json.JSONException)38 Matchers.anyString (org.mockito.Matchers.anyString)33 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 Log.getStackTraceString (android.util.Log.getStackTraceString)27 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)24 File (java.io.File)23 Log (com.microsoft.appcenter.ingestion.models.Log)12 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)12 ArgumentMatcher (org.mockito.ArgumentMatcher)12 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)11 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)9 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)9 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)9 Date (java.util.Date)8 Context (android.content.Context)6 StackFrame (com.microsoft.appcenter.crashes.ingestion.models.StackFrame)6