/** * Init. */ private Crashes() { mFactories = new HashMap<>(); mFactories.put(ManagedErrorLog.TYPE, ManagedErrorLogFactory.getInstance()); mFactories.put(HandledErrorLog.TYPE, HandledErrorLogFactory.getInstance()); mFactories.put(ErrorAttachmentLog.TYPE, ErrorAttachmentLogFactory.getInstance()); mLogSerializer = new DefaultLogSerializer(); mLogSerializer.addLogFactory(ManagedErrorLog.TYPE, ManagedErrorLogFactory.getInstance()); mLogSerializer.addLogFactory(ErrorAttachmentLog.TYPE, ErrorAttachmentLogFactory.getInstance()); mCrashesListener = DEFAULT_ERROR_REPORTING_LISTENER; mUnprocessedErrorReports = new LinkedHashMap<>(); mErrorReportCache = new LinkedHashMap<>(); }
@Test public void toCommonSchemaLog() { LogFactory logFactory = mock(LogFactory.class); MockLog log = AndroidTestUtils.generateMockLog(); LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, logFactory); serializer.toCommonSchemaLog(log); verify(logFactory).toCommonSchemaLogs(log); } }
@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(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 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(expected = JSONException.class) public void deserializeUnknownType() throws JSONException { MockLog log = AndroidTestUtils.generateMockLog(); LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); String payload = serializer.serializeLog(log); android.util.Log.v(TAG, payload); new DefaultLogSerializer().deserializeLog(payload, null); }
@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); }
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer); try {
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer); try {
@Test public void oneLog() throws JSONException { LogContainer expectedContainer = AndroidTestUtils.generateMockLogContainer(); LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); String payload = serializer.serializeContainer(expectedContainer); android.util.Log.v(TAG, payload); LogContainer actualContainer = serializer.deserializeContainer(payload, null); assertEquals(expectedContainer, actualContainer); assertEquals(expectedContainer.hashCode(), actualContainer.hashCode()); }
@Test public void serialize() throws JSONException { LogContainer expectedContainer = new LogContainer(); List<Log> logs = new ArrayList<>(); { DistributionStartSessionLog log = new DistributionStartSessionLog(); log.setTimestamp(new Date()); logs.add(log); } expectedContainer.setLogs(logs); UUID sid = UUIDUtils.randomUUID(); for (Log log : logs) { log.setSid(sid); } /* Serialize and deserialize logs container. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(DistributionStartSessionLog.TYPE, new DistributionStartSessionLogFactory()); String payload = serializer.serializeContainer(expectedContainer); LogContainer actualContainer = serializer.deserializeContainer(payload, null); /* Verify that logs container successfully deserialized. */ Assert.assertEquals(expectedContainer, actualContainer); } }
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer); try {
@Test public void serialize() throws JSONException { LogContainer expectedContainer = new LogContainer(); List<Log> logs = new ArrayList<>(); { PushInstallationLog log = new PushInstallationLog(); log.setTimestamp(new Date()); log.setPushToken("TEST"); logs.add(log); } expectedContainer.setLogs(logs); UUID sid = UUIDUtils.randomUUID(); for (Log log : logs) { log.setSid(sid); } LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory()); String payload = serializer.serializeContainer(expectedContainer); LogContainer actualContainer = serializer.deserializeContainer(payload, null); Assert.assertEquals(expectedContainer, actualContainer); } }
@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()); }
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer); try {
private void putLog(int inputFlags, Integer persistedPriorityFlag) throws PersistenceException { /* Initialize database persistence. */ DatabasePersistence persistence = new DatabasePersistence(sContext); /* Set a mock log serializer. */ LogSerializer logSerializer = new DefaultLogSerializer(); logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer); try { /* Initial count is 0. */ assertEquals(0, persistence.countLogs("test-p1")); /* Generate a log and persist. */ Log log = AndroidTestUtils.generateMockLog(); persistence.putLog(log, "test-p1", inputFlags); /* Count logs. */ assertEquals(1, persistence.countLogs("test-p1")); /* Get a log from persistence. */ List<Log> outputLogs = new ArrayList<>(); persistence.getLogs("test-p1", Collections.<String>emptyList(), 1, outputLogs); assertEquals(1, outputLogs.size()); assertEquals(log, outputLogs.get(0)); assertEquals(1, persistence.countLogs("test-p1")); /* Check priority flag. */ ContentValues contentValues = getContentValues(persistence, "test-p1"); assertEquals(persistedPriorityFlag, contentValues.getAsInteger(DatabasePersistence.COLUMN_PRIORITY)); } finally { persistence.close(); } }
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer); try {
@Test public void handledErrorLog() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(HandledErrorLog.TYPE, HandledErrorLogFactory.getInstance());