use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.
the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashWithOnlyJavaThrowable.
@Test
public void saveWrapperSdkCrashWithOnlyJavaThrowable() throws JSONException, IOException {
LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
Crashes.getInstance().setLogSerializer(logSerializer);
Throwable throwable = new Throwable();
WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null);
verifyStatic(never());
StorageHelper.InternalStorage.writeObject(any(File.class), isNull(byte[].class));
verifyStatic();
StorageHelper.InternalStorage.writeObject(any(File.class), eq(throwable));
/* We can't do it twice in the same process. */
WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null);
verifyStatic();
StorageHelper.InternalStorage.writeObject(any(File.class), eq(throwable));
}
use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.
the class ErrorLogHelperTest method createErrorLog.
@Test
public void createErrorLog() throws java.lang.Exception {
/* Dummy coverage of utils class. */
new ErrorLogHelper();
/* Mock base. */
Context mockContext = mock(Context.class);
when(Process.myPid()).thenReturn(123);
Date logTimestamp = new Date(1000L);
whenNew(Date.class).withNoArguments().thenReturn(logTimestamp);
whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() {
@Override
public Date answer(InvocationOnMock invocation) throws Throwable {
return new Date((Long) invocation.getArguments()[0]);
}
});
/* Mock device. */
Device mockDevice = mock(Device.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
/* Mock process name. */
ActivityManager activityManager = mock(ActivityManager.class);
RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
runningAppProcessInfo.pid = 123;
runningAppProcessInfo.processName = "right.process";
when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
/* Mock architecture. */
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23);
TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
/* Test. */
long launchTimeStamp = 2000;
ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), launchTimeStamp);
assertNotNull(errorLog);
assertNotNull(errorLog.getId());
assertEquals(logTimestamp, errorLog.getTimestamp());
assertEquals(mockDevice, errorLog.getDevice());
assertEquals(Integer.valueOf(123), errorLog.getProcessId());
assertEquals("right.process", errorLog.getProcessName());
assertNull(errorLog.getParentProcessId());
assertNull(errorLog.getParentProcessName());
assertEquals("armeabi-v7a", errorLog.getArchitecture());
assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
assertEquals(Boolean.TRUE, errorLog.getFatal());
assertEquals(launchTimeStamp, errorLog.getAppLaunchTimestamp().getTime());
/* Check first exception. */
Exception topException = errorLog.getException();
sanityCheck(topException);
assertEquals(RuntimeException.class.getName(), topException.getType());
assertNotNull(topException.getMessage());
assertNotNull(topException.getInnerExceptions());
assertEquals(1, topException.getInnerExceptions().size());
/* Check second exception. */
Exception middleException = topException.getInnerExceptions().get(0);
sanityCheck(middleException);
assertEquals(IOException.class.getName(), middleException.getType());
assertNotNull(middleException.getInnerExceptions());
assertEquals(1, middleException.getInnerExceptions().size());
/* Check third exception. */
Exception rootCauseException = middleException.getInnerExceptions().get(0);
sanityCheck(rootCauseException);
assertEquals(TestCrashException.class.getName(), rootCauseException.getType());
assertNotNull(rootCauseException.getMessage());
assertNull(rootCauseException.getInnerExceptions());
/* Check threads. */
assertNotNull(errorLog.getThreads());
assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size());
for (Thread thread : errorLog.getThreads()) {
assertNotNull(thread);
assertTrue(thread.getId() > 0);
assertNotNull(thread.getName());
assertNotNull(thread.getFrames());
for (StackFrame frame : thread.getFrames()) {
assertNotNull(frame);
assertNotNull(frame.getClassName());
assertNotNull(frame.getMethodName());
}
}
}
use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.
the class ErrorLogHelper method getModelExceptionFromThrowable.
@NonNull
public static Exception getModelExceptionFromThrowable(@NonNull Throwable t) {
Exception topException = null;
Exception parentException = null;
for (Throwable cause = t; cause != null; cause = cause.getCause()) {
Exception exception = new Exception();
exception.setType(cause.getClass().getName());
exception.setMessage(cause.getMessage());
exception.setFrames(getModelFramesFromStackTrace(cause));
if (topException == null) {
topException = exception;
} else {
parentException.setInnerExceptions(Collections.singletonList(exception));
}
parentException = exception;
}
return topException;
}
use of com.microsoft.appcenter.crashes.ingestion.models.Exception 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.crashes.ingestion.models.Exception 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;
}
}));
}
Aggregations