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));
}
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);
}
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;
}
}));
}
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;
}
}));
}
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;
}
}));
}
Aggregations