Search in sources :

Example 1 with StackFrame

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

the class ErrorLogHelperTest method sanityCheck.

private void sanityCheck(Exception exception) {
    assertNotNull(exception);
    assertNotNull(exception.getType());
    assertNotNull(exception.getFrames());
    assertFalse(exception.getFrames().isEmpty());
    for (StackFrame frame : exception.getFrames()) {
        assertNotNull(frame);
        assertNotNull(frame.getClassName());
        assertNotNull(frame.getMethodName());
    }
}
Also used : StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame)

Example 2 with StackFrame

use of com.microsoft.appcenter.crashes.ingestion.models.StackFrame 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());
        }
    }
}
Also used : Context(android.content.Context) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) Device(com.microsoft.appcenter.ingestion.models.Device) IOException(java.io.IOException) ActivityManager(android.app.ActivityManager) Date(java.util.Date) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) Thread(com.microsoft.appcenter.crashes.ingestion.models.Thread) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Build(android.os.Build) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) Matchers.anyLong(org.mockito.Matchers.anyLong) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with StackFrame

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

the class ErrorLogHelper method getModelStackFrame.

@NonNull
private static StackFrame getModelStackFrame(StackTraceElement stackTraceElement) {
    StackFrame stackFrame = new StackFrame();
    stackFrame.setClassName(stackTraceElement.getClassName());
    stackFrame.setMethodName(stackTraceElement.getMethodName());
    stackFrame.setLineNumber(stackTraceElement.getLineNumber());
    stackFrame.setFileName(stackTraceElement.getFileName());
    return stackFrame;
}
Also used : StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) NonNull(android.support.annotation.NonNull)

Example 4 with StackFrame

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

the class CrashesTest method trackExceptionForWrapperSdk.

@Test
public void trackExceptionForWrapperSdk() {
    StackFrame frame = new StackFrame();
    frame.setClassName("1");
    frame.setFileName("2");
    frame.setLineNumber(3);
    frame.setMethodName("4");
    final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
    exception.setType("5");
    exception.setMessage("6");
    exception.setFrames(singletonList(frame));
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    WrapperSdkExceptionManager.trackException(exception);
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    WrapperSdkExceptionManager.trackException(exception);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException());
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 0;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put("valid" + i, "valid");
            }
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 5;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof HandledErrorLog) {
                HandledErrorLog errorLog = (HandledErrorLog) item;
                if (exception.equals((errorLog.getException()))) {
                    if (errorLog.getProperties().size() == 1) {
                        Map.Entry<String, String> entry = errorLog.getProperties().entrySet().iterator().next();
                        String truncatedMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH, '*');
                        return entry.getKey().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH;
                    }
                }
            }
            return false;
        }
    }), eq(crashes.getGroupName()));
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Channel(com.microsoft.appcenter.channel.Channel) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) JSONException(org.json.JSONException) NativeException(com.microsoft.appcenter.crashes.model.NativeException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) ArgumentMatcher(org.mockito.ArgumentMatcher) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StackFrame (com.microsoft.appcenter.crashes.ingestion.models.StackFrame)4 Context (android.content.Context)2 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)2 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ActivityManager (android.app.ActivityManager)1 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)1 Build (android.os.Build)1 NonNull (android.support.annotation.NonNull)1 SessionContext (com.microsoft.appcenter.SessionContext)1 Channel (com.microsoft.appcenter.channel.Channel)1 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)1 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)1 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)1 Thread (com.microsoft.appcenter.crashes.ingestion.models.Thread)1 NativeException (com.microsoft.appcenter.crashes.model.NativeException)1 Device (com.microsoft.appcenter.ingestion.models.Device)1 Log (com.microsoft.appcenter.ingestion.models.Log)1