use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method checkStacktraceWithLegacyThrowableFile.
@Test
public void checkStacktraceWithLegacyThrowableFile() throws Exception {
String expectedStacktrace = "type: message\n" + " ClassName.MethodName(FileName:1)";
/* Mock FileManager call. */
when(FileManager.read(any(File.class))).thenReturn(expectedStacktrace);
/* Create throwable file. */
UUID logId = UUID.randomUUID();
String throwableFileName = logId + ErrorLogHelper.THROWABLE_FILE_EXTENSION;
File errorStorageDirectory = mTemporaryFolder.newFolder("error");
ErrorLogHelper.setErrorLogDirectory(errorStorageDirectory);
File throwableFile = new File(errorStorageDirectory, throwableFileName);
assertTrue(throwableFile.createNewFile());
/* Write file content. */
BufferedWriter writer = new BufferedWriter(new FileWriter(throwableFile));
writer.write(expectedStacktrace);
writer.close();
/* Mock log. */
ManagedErrorLog mockLog = new ManagedErrorLog();
mockLog.setId(logId);
mockLog.setErrorThreadName("Thread name");
mockLog.setAppLaunchTimestamp(new Date());
mockLog.setTimestamp(new Date());
mockLog.setDevice(new Device());
/* Build error report. */
Crashes crashes = Mockito.spy(Crashes.getInstance());
ErrorReport report = crashes.buildErrorReport(mockLog);
/* Verify. */
assertEquals(expectedStacktrace, report.getStackTrace());
verify(crashes, never()).buildStackTrace(any(com.microsoft.appcenter.crashes.ingestion.models.Exception.class));
verifyStatic();
FileManager.read(any(File.class));
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method checkStacktraceWhenThrowableFileIsEmpty.
@Test
public void checkStacktraceWhenThrowableFileIsEmpty() throws Exception {
String expectedStacktrace = "type: message\n" + "\t at ClassName.MethodName(FileName:1)";
/* Create empty throwable file. */
UUID logId = UUID.randomUUID();
String throwableFileName = logId + ErrorLogHelper.THROWABLE_FILE_EXTENSION;
File errorStorageDirectory = mTemporaryFolder.newFolder("error");
ErrorLogHelper.setErrorLogDirectory(errorStorageDirectory);
File throwableFile = new File(errorStorageDirectory, throwableFileName);
assertTrue(throwableFile.createNewFile());
/* Prepare first frame. */
final StackFrame stackFrame1 = new StackFrame();
stackFrame1.setClassName("ClassName");
stackFrame1.setMethodName("MethodName");
stackFrame1.setFileName("FileName");
stackFrame1.setLineNumber(1);
/* Mock exception value. */
com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
when(mockException.getType()).thenReturn("type");
when(mockException.getMessage()).thenReturn("message");
when(mockException.getFrames()).thenReturn(Arrays.asList(stackFrame1));
/* Mock log. */
ManagedErrorLog mockLog = new ManagedErrorLog();
mockLog.setId(logId);
mockLog.setErrorThreadName("Thread name");
mockLog.setAppLaunchTimestamp(new Date());
mockLog.setTimestamp(new Date());
mockLog.setDevice(new Device());
mockLog.setException(mockException);
/* Build error report. */
Crashes crashes = Crashes.getInstance();
/* Verify that stacktrace is null. */
ErrorReport report = crashes.buildErrorReport(mockLog);
assertEquals(expectedStacktrace, report.getStackTrace());
verifyStatic(never());
FileManager.read(any(File.class));
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method getChannelListener.
@Test
public void getChannelListener() throws JSONException {
/* Mock and set exception data. */
com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
when(mockException.getType()).thenReturn("type");
when(mockException.getMessage()).thenReturn("message");
mErrorLog.setException(mockException);
ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
mockStatic(ErrorLogHelper.class);
File errorLogFile = mock(File.class);
when(errorLogFile.length()).thenReturn(1L);
when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(errorLogFile);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE)).thenReturn(errorReport);
when(FileManager.read(any(File.class))).thenReturn("");
when(FileManager.read(any(File.class))).thenReturn(STACK_TRACE);
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
CrashesListener crashesListener = mock(CrashesListener.class);
when(crashesListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
Crashes.setListener(crashesListener);
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), mock(Channel.class), "", null, true);
/* The error report was created and cached but device is null here. */
verifyStatic();
ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
assertNull(errorReport.getDevice());
/* The channel sets a device. */
mErrorLog.setDevice(mock(Device.class));
ArgumentCaptor<ErrorReport> errorReportCaptor = ArgumentCaptor.forClass(ErrorReport.class);
Channel.GroupListener channelListener = crashes.getChannelListener();
/* Simulate onBeforeSending event. */
channelListener.onBeforeSending(mErrorLog);
verify(crashesListener).onBeforeSending(errorReportCaptor.capture());
assertErrorEquals(mErrorLog, errorReportCaptor.getValue());
/* Simulate onSuccess event. */
channelListener.onSuccess(mErrorLog);
verify(crashesListener).onSendingSucceeded(errorReportCaptor.capture());
assertErrorEquals(mErrorLog, errorReportCaptor.getValue());
/* No more error reports should be produced at the point. */
verifyStatic();
ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
/* Simulate onFailure event. */
channelListener.onFailure(mErrorLog, EXCEPTION);
verify(crashesListener).onSendingFailed(errorReportCaptor.capture(), eq(EXCEPTION));
assertErrorEquals(mErrorLog, errorReportCaptor.getValue());
/* onSuccess and onFailure invalidate the cache, so one more call is expected. */
verifyStatic();
ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class ErrorLogHelperTest method throwDeviceInfoExceptionWhenGetMinidumpSubfolderWithDeviceInfo.
@Test
public void throwDeviceInfoExceptionWhenGetMinidumpSubfolderWithDeviceInfo() throws java.lang.Exception {
/* Prepare data. */
Device mockDevice = mock(Device.class);
mockStatic(DeviceInfoHelper.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("crash", new java.lang.Exception()));
Context mockContext = mock(Context.class);
File mockFile = mock(File.class);
whenNew(File.class).withAnyArguments().thenReturn(mockFile);
/* Verify. */
ErrorLogHelper.getNewMinidumpSubfolderWithContextData(mockContext);
verify(mockFile).delete();
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class ErrorLogHelperTest method getStoredDeviceInfo.
@Test
public void getStoredDeviceInfo() throws IOException {
String deviceInfoString = "{\"sdkName\":\"appcenter.android\",\"sdkVersion\":\"2.5.4.2\",\"model\":\"Android SDK built for x86\",\"oemName\":\"Google\",\"osName\":\"Android\",\"osVersion\":\"9\",\"osBuild\":\"PSR1.180720.075\",\"osApiLevel\":28,\"locale\":\"en_US\",\"timeZoneOffset\":240,\"screenSize\":\"1080x1794\",\"appVersion\":\"2.5.4.2\",\"carrierName\":\"Android\",\"carrierCountry\":\"us\",\"appBuild\":\"59\",\"appNamespace\":\"com.microsoft.appcenter.sasquatch.project\"}";
File minidumpFolder = mTemporaryFolder.newFolder("minidump");
File deviceInfoFile = new File(minidumpFolder, ErrorLogHelper.DEVICE_INFO_FILE);
assertTrue(deviceInfoFile.createNewFile());
mockStatic(FileManager.class);
when(FileManager.read(eq(deviceInfoFile))).thenReturn(deviceInfoString);
Device storedDeviceInfo = ErrorLogHelper.getStoredDeviceInfo(minidumpFolder);
assertNotNull(storedDeviceInfo);
}
Aggregations