Search in sources :

Example 21 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 22 with Exception

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithIOException.

@Test
public void saveWrapperSdkCrashFailsWithIOException() throws IOException, JSONException {
    doThrow(new IOException()).when(FileManager.class);
    FileManager.write(any(File.class), anyString());
    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();
    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) IOException(java.io.IOException) 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 23 with Exception

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

the class WrapperSdkExceptionManagerTest method saveWrapperExceptionWhenSDKDisabled.

@Test
public void saveWrapperExceptionWhenSDKDisabled() throws JSONException {
    when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(false);
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    Crashes.getInstance().setLogSerializer(logSerializer);
    String data = "d";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verify(logSerializer, never()).serializeLog(any(Log.class));
    verifyNoMoreInteractions(ErrorLogHelper.class);
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) 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 24 with Exception

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsToCreateThrowablePlaceholder.

@Test
public void saveWrapperSdkCrashFailsToCreateThrowablePlaceholder() throws java.lang.Exception {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    File throwableFile = mock(File.class);
    whenNew(File.class).withParameterTypes(String.class, String.class).withArguments(anyString(), argThat(new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return String.valueOf(argument).endsWith(ErrorLogHelper.THROWABLE_FILE_EXTENSION);
        }
    })).thenReturn(throwableFile);
    when(throwableFile.createNewFile()).thenReturn(false);
    String data = "d";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic(times(3));
    AppCenterLog.debug(anyString(), anyString());
    /* Second call is ignored. */
    data = "e";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    /* No more error. */
    verifyStatic(times(3));
    AppCenterLog.debug(anyString(), anyString());
}
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 25 with Exception

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashWithJavaThrowable.

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

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