Search in sources :

Example 16 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class Crashes method queueException.

/**
     * Send an exception.
     *
     * @param throwable An exception.
     */
private synchronized void queueException(@NonNull final Throwable throwable) {
    if (isInactive())
        return;
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mContext, Thread.currentThread(), throwable, Thread.getAllStackTraces(), getInitializeTimestamp(), false);
    mChannel.enqueue(errorLog, ERROR_GROUP);
}
Also used : ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)

Example 17 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class CrashesAndroidTest method wrapperSdkOverrideLog.

@Test
public void wrapperSdkOverrideLog() throws InterruptedException {
    Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
    Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
    Channel channel = mock(Channel.class);
    Crashes.getInstance().onStarted(sContext, "", channel);
    Crashes.WrapperSdkListener wrapperSdkListener = mock(Crashes.WrapperSdkListener.class);
    Crashes.getInstance().setWrapperSdkListener(wrapperSdkListener);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ManagedErrorLog errorLog = (ManagedErrorLog) invocationOnMock.getArguments()[0];
            errorLog.setErrorThreadName("ReplacedErrorThreadName");
            WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
            return null;
        }
    }).when(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
    final RuntimeException exception = new RuntimeException("mock");
    final Thread thread = new Thread() {

        @Override
        public void run() {
            throw exception;
        }
    };
    thread.start();
    thread.join();
    verify(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
    Crashes.unsetInstance();
    Crashes.getInstance().onStarted(sContext, "", channel);
    waitForCrashesHandlerTasksToComplete();
    Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport errorReport) {
            assertNotNull(errorReport);
            assertEquals("ReplacedErrorThreadName", errorReport.getThreadName());
        }
    });
}
Also used : Channel(com.microsoft.azure.mobile.channel.Channel) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 18 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method trackException.

@Test
public void trackException() {
    /* Track exception test. */
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    Crashes.trackException(EXCEPTION);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof ManagedErrorLog && EXCEPTION.getMessage().equals(((ManagedErrorLog) item).getException().getMessage());
        }
    }), eq(crashes.getGroupName()));
    ManagedErrorLog mockLog = mock(ManagedErrorLog.class);
    when(mockLog.getFatal()).thenReturn(false);
    CrashesListener mockListener = mock(CrashesListener.class);
    crashes.setInstanceListener(mockListener);
    /* Crashes callback test for trackException. */
    crashes.getChannelListener().onBeforeSending(mockLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(mockLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(mockLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
    ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
    crashes.getChannelListener().onBeforeSending(attachmentLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(attachmentLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method saveWrapperSdkErrorLogIOException.

@Test
public void saveWrapperSdkErrorLogIOException() throws IOException, JSONException {
    mockStatic(MobileCenterLog.class);
    ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setId(UUIDUtils.randomUUID());
    mockStatic(StorageHelper.InternalStorage.class);
    doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
    StorageHelper.InternalStorage.write(any(File.class), anyString());
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.serializeLog(errorLog)).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
    verifyStatic();
    MobileCenterLog.error(anyString(), anyString(), any(IOException.class));
}
Also used : ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) StorageHelper(com.microsoft.azure.mobile.utils.storage.StorageHelper) IOException(java.io.IOException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method setWrapperSdkListener.

@Test
public void setWrapperSdkListener() {
    mockStatic(ErrorLogHelper.class);
    ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setId(UUIDUtils.randomUUID());
    when(ErrorLogHelper.createErrorLog(any(Context.class), any(Thread.class), any(Throwable.class), anyMapOf(Thread.class, StackTraceElement[].class), anyLong(), anyBoolean())).thenReturn(errorLog);
    Crashes.getInstance().setLogSerializer(mock(LogSerializer.class));
    Crashes.WrapperSdkListener wrapperSdkListener = mock(Crashes.WrapperSdkListener.class);
    Crashes.getInstance().setWrapperSdkListener(wrapperSdkListener);
    Crashes.getInstance().saveUncaughtException(Thread.currentThread(), new TestCrashException());
    verify(wrapperSdkListener).onCrashCaptured(errorLog);
}
Also used : Context(android.content.Context) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)20 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 Context (android.content.Context)9 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)9 File (java.io.File)7 Channel (com.microsoft.azure.mobile.channel.Channel)6 JSONException (org.json.JSONException)6 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)5 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)5 IOException (java.io.IOException)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)4 Log (com.microsoft.azure.mobile.ingestion.models.Log)4 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)4 UUID (java.util.UUID)4 ActivityManager (android.app.ActivityManager)3 Build (android.os.Build)3 Device (com.microsoft.azure.mobile.ingestion.models.Device)3 Answer (org.mockito.stubbing.Answer)3