CustomDimension { public static boolean setDimension(@NonNull TrackMe trackMe, int dimensionId, @Nullable String dimensionValue) { if (dimensionId < 1) { Timber.tag(TAG).e("dimensionId should be great than 0 (arg: %d)", dimensionId); return false; } if (dimensionValue != null && dimensionValue.length() > 255) { dimensionValue = dimensionValue.substring(0, 255); Timber.tag(TAG).w("dimensionValue was truncated to 255 chars."); } if (dimensionValue != null && dimensionValue.length() == 0) { dimensionValue = null; } trackMe.set(formatDimensionId(dimensionId), dimensionValue); return true; } CustomDimension(int id, String value); int getId(); String getValue(); static boolean setDimension(@NonNull TrackMe trackMe, int dimensionId, @Nullable String dimensionValue); static boolean setDimension(TrackMe trackMe, CustomDimension dimension); @Nullable static String getDimension(TrackMe trackMe, int dimensionId);  }
@Test public void testSetCustomDimensions() throws Exception { TrackMe trackMe = new TrackMe(); CustomDimension.setDimension(trackMe, 0, "foo"); CustomDimension.setDimension(trackMe, 1, "foo"); CustomDimension.setDimension(trackMe, 2, "bar"); CustomDimension.setDimension(trackMe, 3, "empty"); CustomDimension.setDimension(trackMe, 3, null); CustomDimension.setDimension(trackMe, 4, ""); assertEquals("foo", trackMe.get("dimension1")); assertEquals("bar", trackMe.get("dimension2")); assertNull(trackMe.get("dimension0")); assertNull(trackMe.get("dimension3")); assertNull(trackMe.get("dimension4")); }
@Test public void testSet_truncate() throws Exception { TrackMe trackMe = new TrackMe(); CustomDimension.setDimension(trackMe, 1, new String(new char[1000])); assertEquals(255, trackMe.get("dimension1").length()); }
@Test public void testSet_badId() throws Exception { TrackMe trackMe = new TrackMe(); CustomDimension.setDimension(trackMe, 0, UUID.randomUUID().toString()); assertTrue(trackMe.isEmpty()); }
@Test public void testSet_removal() throws Exception { TrackMe trackMe = new TrackMe(); CustomDimension.setDimension(trackMe, 1, UUID.randomUUID().toString()); assertFalse(trackMe.isEmpty()); CustomDimension.setDimension(trackMe, 1, null); assertTrue(trackMe.isEmpty()); }
@Test public void testSet_empty() throws Exception { TrackMe trackMe = new TrackMe(); CustomDimension.setDimension(trackMe, 1, UUID.randomUUID().toString()); assertFalse(trackMe.isEmpty()); CustomDimension.setDimension(trackMe, 1, ""); assertTrue(trackMe.isEmpty()); }