WrapperSdkExceptionManager { public static UUID saveWrapperException(Thread thread, Throwable throwable, com.microsoft.appcenter.crashes.ingestion.models.Exception modelException, String rawSerializedException) { try { UUID errorId = Crashes.getInstance().saveUncaughtException(thread, throwable, modelException); if (errorId != null && rawSerializedException != null) { sWrapperExceptionDataContainer.put(errorId.toString(), rawSerializedException); File dataFile = getFile(errorId); FileManager.write(dataFile, rawSerializedException); AppCenterLog.debug(LOG_TAG, "Saved raw wrapper exception data into " + dataFile); } return errorId; } catch (Exception e) { AppCenterLog.error(LOG_TAG, "Failed to save wrapper exception data to file", e); return null; } } @VisibleForTesting  WrapperSdkExceptionManager(); static UUID saveWrapperException(Thread thread, Throwable throwable, com.microsoft.appcenter.crashes.ingestion.models.Exception modelException, String rawSerializedException); static void deleteWrapperExceptionData(UUID errorId); static String loadWrapperExceptionData(UUID errorId); static String trackException(com.microsoft.appcenter.crashes.ingestion.models.Exception modelException, Map<String, String> properties, Iterable<ErrorAttachmentLog> attachments); static void setAutomaticProcessing(@SuppressWarnings("SameParameterValue") boolean automaticProcessing); static AppCenterFuture<Collection<ErrorReport>> getUnprocessedErrorReports(); static AppCenterFuture<Boolean> sendCrashReportsOrAwaitUserConfirmation(Collection<String> filteredReportIds); static ErrorReport buildHandledErrorReport(Context context, String errorReportId); static void sendErrorAttachments(String errorReportId, Iterable<ErrorAttachmentLog> attachments);  }
@Test public void saveWrapperSdkCrash() throws JSONException, IOException { 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(); FileManager.write(any(File.class), eq(data)); data = "e"; WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data); verifyStatic(never()); FileManager.write(any(File.class), eq(data)); }
@Test @PrepareForTest(android.util.Log.class) public void saveWrapperSdkCrashWithJavaThrowable() throws JSONException, IOException { LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock"); 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(STACK_TRACE)); 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(STACK_TRACE)); }
@Test @PrepareForTest({android.util.Log.class}) 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(); mockStatic(android.util.Log.class); Mockito.when(getStackTraceString(any(Throwable.class))).thenReturn(STACK_TRACE); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null); verifyStatic(never()); FileManager.write(any(File.class), isNull(String.class)); verifyStatic(); FileManager.write(any(File.class), eq(STACK_TRACE)); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null); verifyStatic(); FileManager.write(any(File.class), eq(STACK_TRACE)); }
@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(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof IOException; } })); data = "e"; 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; } })); }
@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); 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 JSONException; } })); data = "e"; 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 JSONException; } })); }
@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; } })); data = "e"; 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; } })); }
@Test public void saveWrapperSdkCrashFailsWithIOExceptionAfterLog() throws IOException, JSONException { String data = "d"; doThrow(new IOException()).when(FileManager.class); FileManager.write(any(File.class), eq(data)); 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(), data); verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof IOException; } })); data = "e"; 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; } })); }
@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); }