private void checkSerialization(LogSerializer serializer, MockCommonSchemaLog log) throws JSONException { Log copy = serializer.deserializeLog(serializer.serializeLog(log), MockCommonSchemaLog.TYPE); assertEquals(log, copy); } }
private static void checkSerialization(Log log, LogSerializer serializer) throws JSONException { String payload = serializer.serializeLog(log); Log deSerializedLog = serializer.deserializeLog(payload, null); checkEquals(log, deSerializedLog); }
@Test public void noQueueNullLog() throws JSONException { Context mockContext = mock(Context.class); Channel mockChannel = mock(Channel.class); mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(null); crashes.setLogSerializer(logSerializer); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mockContext, mockChannel, "", null, true); verify(mockChannel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
@Test public void processPendingErrorsCorrupted() throws JSONException { mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); when(FileManager.read(any(File.class))).thenReturn(""); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mock(ManagedErrorLog.class)); crashes.setLogSerializer(logSerializer); CrashesListener listener = mock(CrashesListener.class); crashes.setInstanceListener(listener); Channel channel = mock(Channel.class); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mock(Context.class), channel, "", null, true); verifyZeroInteractions(listener); verify(channel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
@Test public void printErrorOnJSONException() throws JSONException { Context mockContext = mock(Context.class); Channel mockChannel = mock(Channel.class); JSONException jsonException = new JSONException("Fake JSON exception"); mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); when(FileManager.read(any(File.class))).thenReturn(""); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenThrow(jsonException); crashes.setLogSerializer(logSerializer); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mockContext, mockChannel, "", null, true); verify(mockChannel, never()).enqueue(any(Log.class), anyString(), anyInt()); verifyStatic(); AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException)); }
@Test public void handleUserConfirmationAlwaysSend() throws IOException, ClassNotFoundException, JSONException { /* Simulate the method is called from Worker Thread. */ mockStatic(Looper.class); when(Looper.myLooper()).thenReturn(mock(Looper.class)); when(Looper.getMainLooper()).thenReturn(null); mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class)); when(FileManager.readObject(any(File.class))).thenReturn(null); CrashesListener mockListener = mock(CrashesListener.class); when(mockListener.shouldProcess(any(ErrorReport.class))).thenReturn(true); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog); crashes.setLogSerializer(logSerializer); crashes.setInstanceListener(mockListener); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mock(Context.class), mock(Channel.class), "", null, true); Crashes.notifyUserConfirmation(Crashes.ALWAYS_SEND); verifyStatic(); SharedPreferencesManager.putBoolean(Crashes.PREF_KEY_ALWAYS_SEND, true); }
if (logfileContents != null) { try { ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logfileContents, null); UUID id = log.getId(); ErrorReport report = buildErrorReport(log);
@Test public void queuePendingCrashesShouldNotProcess() throws IOException, ClassNotFoundException, JSONException { Context mockContext = mock(Context.class); Channel mockChannel = mock(Channel.class); ErrorReport report = new ErrorReport(); mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class)); when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report); when(FileManager.read(any(File.class))).thenReturn(""); when(FileManager.readObject(any(File.class))).thenReturn(new RuntimeException()).thenReturn(new byte[]{}); CrashesListener mockListener = mock(CrashesListener.class); when(mockListener.shouldProcess(report)).thenReturn(false); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog); crashes.setLogSerializer(logSerializer); crashes.setInstanceListener(mockListener); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mockContext, mockChannel, "", null, true); verify(mockListener).shouldProcess(report); verify(mockListener, never()).shouldAwaitUserConfirmation(); verify(mockListener, never()).getErrorAttachments(report); verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()), anyInt()); }
when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(log);
@Test public void handleUserConfirmationDoNotSend() throws IOException, ClassNotFoundException, JSONException { mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class)); when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(new ErrorReport()); when(FileManager.read(any(File.class))).thenReturn(""); when(FileManager.readObject(any(File.class))).thenReturn(null); CrashesListener mockListener = mock(CrashesListener.class); when(mockListener.shouldProcess(any(ErrorReport.class))).thenReturn(true); when(mockListener.shouldAwaitUserConfirmation()).thenReturn(true); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog); crashes.setLogSerializer(logSerializer); crashes.setInstanceListener(mockListener); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mock(Context.class), mock(Channel.class), "", null, true); Crashes.notifyUserConfirmation(Crashes.DONT_SEND); verify(mockListener, never()).getErrorAttachments(any(ErrorReport.class)); verifyStatic(); ErrorLogHelper.removeStoredErrorLogFile(mErrorLog.getId()); verifyStatic(); ErrorLogHelper.removeStoredThrowableFile(mErrorLog.getId()); }
when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
@Test(expected = JSONException.class) public void deserializeWithInvalidNumber() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"," + "\"properties\":[{\"name\":\"test\",\"type\":\"number\",\"value\":false}]" + "}", null); }
@Test(expected = JSONException.class) public void deserializeWithoutProperties() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"" + "}", null); }
@Test(expected = JSONException.class) public void deserializeWithInvalidDate() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"," + "\"properties\":[{\"name\":\"test\",\"type\":\"dateTime\",\"value\":\"today\"}]" + "}", null); }
@Test(expected = JSONException.class) public void deserializeWithInvalidType() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"," + "\"properties\":[{\"name\":\"test\",\"type\":\"unknown\",\"value\":42}]" + "}", null); }
@Test public void customPropertiesLog() throws JSONException { CustomPropertiesLog log = new CustomPropertiesLog(); Map<String, Object> properties = new HashMap<>(); properties.put("t1", "test"); properties.put("t2", new Date(0)); properties.put("t3", 0); properties.put("t4", false); //noinspection ConstantConditions properties.put("t5", null); log.setProperties(properties); UUID sid = UUIDUtils.randomUUID(); log.setSid(sid); log.setTimestamp(new Date()); /* Verify serialize and deserialize. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); String payload = serializer.serializeLog(log); Log actualContainer = serializer.deserializeLog(payload, null); assertEquals(log, actualContainer); }
@Test public void failToDeserializeLastSessionCrashReport() throws JSONException, IOException { LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mock(ManagedErrorLog.class)); when(logSerializer.deserializeLog(anyString(), anyString())).thenThrow(jsonException);
@Test public void startServiceLog() throws JSONException { StartServiceLog log = new StartServiceLog(); List<String> services = new ArrayList<>(); services.add("FIRST"); services.add("SECOND"); log.setServices(services); UUID sid = UUIDUtils.randomUUID(); log.setSid(sid); log.setTimestamp(new Date()); /* Verify serialize and deserialize. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(StartServiceLog.TYPE, new StartServiceLogFactory()); String payload = serializer.serializeLog(log); Log actualContainer = serializer.deserializeLog(payload, null); assertEquals(log, actualContainer); }
.when(logSerializer).deserializeLog(anyString(), anyString()); persistence.setLogSerializer(logSerializer); try {
@Test public void logWithUserId() throws JSONException { MockLog expectedLog = AndroidTestUtils.generateMockLog(); expectedLog.setTimestamp(new Date()); expectedLog.setUserId("charlie"); /* Verify serialize and deserialize. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); String payload = serializer.serializeLog(expectedLog); Log actualLog = serializer.deserializeLog(payload, null); assertEquals(expectedLog, actualLog); assertEquals("charlie", actualLog.getUserId()); }