Search in sources :

Example 31 with LogSerializer

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

the class CrashesTest method failToDeserializeLastSessionCrashReport.

@Test
public void failToDeserializeLastSessionCrashReport() throws JSONException, IOException, ClassNotFoundException {
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
    mockStatic(ErrorLogHelper.class);
    File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    assertFalse(Crashes.hasCrashedInLastSession().get());
    JSONException jsonException = new JSONException("Fake JSON exception");
    when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
    /*
         * Last session error is only fetched upon initialization: enabled and channel ready.
         * Here the service is disabled by default until started, we are waiting channel to be ready, simulate that.
         */
    assertFalse(Crashes.isEnabled().get());
    assertNull(Crashes.getLastSessionCrashReport().get());
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mock(Channel.class));
    assertTrue(Crashes.isEnabled().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
    assertNull(Crashes.getLastSessionCrashReport().get());
    /*
         * De-serializing fails twice: processing the log from last time as part of the bulk processing.
         * And loading that same file for exposing it in getLastErrorReport.
         */
    verifyStatic(times(2));
    AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.appcenter.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with LogSerializer

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

the class WrapperSdkExceptionManagerTest method saveWrapperExceptionWhenSDKDisabled.

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

use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-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);
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[] { 'd' });
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof JSONException;
        }
    }));
    /* Second call is ignored. */
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[] { 'e' });
    /* 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) 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 LogSerializer

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithIOException.

@Test
public void saveWrapperSdkCrashFailsWithIOException() throws IOException, JSONException {
    doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
    StorageHelper.InternalStorage.write(any(File.class), anyString());
    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(), new byte[] { 'd' });
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
    /* Second call is ignored. */
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[] { 'e' });
    /* 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) StorageHelper(com.microsoft.appcenter.utils.storage.StorageHelper) 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 LogSerializer

use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-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);
    byte[] data = new byte[] { '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 = new byte[] { '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) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) 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)

Aggregations

LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)44 Test (org.junit.Test)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 Log (com.microsoft.appcenter.ingestion.models.Log)26 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)23 File (java.io.File)22 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)19 Context (android.content.Context)16 ArrayList (java.util.ArrayList)16 JSONException (org.json.JSONException)16 UUID (java.util.UUID)15 SessionContext (com.microsoft.appcenter.SessionContext)14 Channel (com.microsoft.appcenter.channel.Channel)14 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)14 MediumTest (android.support.test.filters.MediumTest)10 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)10 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)10 IOException (java.io.IOException)9 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)8 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)8