MutationDetectors { public static <T> MutationDetector forValueWithCoder(T value, Coder<T> coder) throws CoderException { if (value == null) { return noopMutationDetector(); } else { return new CodedValueMutationDetector<>(value, coder); } } private  MutationDetectors(); static MutationDetector forValueWithCoder(T value, Coder<T> coder); static MutationDetector noopMutationDetector();  }
@Test public void testMutationBasedOnStructuralValue() throws Exception { AtomicInteger value = new AtomicInteger(); MutationDetector detector = MutationDetectors.forValueWithCoder(value, new ForSDKMutationDetectionTestCoder()); value.incrementAndGet(); detector.verifyUnmodified(); }
@Test public void testMutationWithEqualEncodings() throws Exception { class EncodingBadStructuralValueCoder extends AtomicCoder<List<Object>> { @Override public void encode(List<Object> value, OutputStream outStream) throws CoderException, IOException { outStream.write(new byte[] {1, 2, -3, 45}); } @Override public List<Object> decode(InputStream inStream) throws CoderException, IOException { int read = inStream.read(new byte[4]); return new ArrayList<>(); } @Override public Object structuralValue(List<Object> value) { return new Object(); } } List<Object> ls = new ArrayList<>(); ls.add(1); ls.add("foo"); MutationDetector detector = MutationDetectors.forValueWithCoder(ls, new EncodingBadStructuralValueCoder()); ls.add(new Byte[] {1, 2, -3, 45}); detector.verifyUnmodified(); }
@Test public void testMutatingList() throws Exception { List<Integer> value = Arrays.asList(1, 2, 3, 4); MutationDetector detector = MutationDetectors.forValueWithCoder(value, ListCoder.of(VarIntCoder.of())); value.set(0, 37); thrown.expect(IllegalMutationException.class); detector.verifyUnmodified(); }
@Test public void testUnmodifiedLinkedList() throws Exception { List<Integer> value = Lists.newLinkedList(Arrays.asList(1, 2, 3, 4)); MutationDetector detector = MutationDetectors.forValueWithCoder(value, ListCoder.of(VarIntCoder.of())); detector.verifyUnmodified(); }
@Test public void testImmutableList() throws Exception { List<Integer> value = Lists.newLinkedList(Arrays.asList(1, 2, 3, 4)); MutationDetector detector = MutationDetectors.forValueWithCoder(value, IterableCoder.of(VarIntCoder.of())); detector.verifyUnmodified(); }
@Test public void testImmutableSet() throws Exception { Set<Integer> value = Sets.newHashSet(Arrays.asList(1, 2, 3, 4)); MutationDetector detector = MutationDetectors.forValueWithCoder(value, IterableCoder.of(VarIntCoder.of())); detector.verifyUnmodified(); }
@Test public void testStructuralValue() throws Exception { Set<Integer> value = Sets.newHashSet(Arrays.asList(1, 2, 3, 4)); MutationDetector detector = MutationDetectors.forValueWithCoder(value, IterableCoder.of(VarIntCoder.of())); detector.verifyUnmodified(); }
@Test public void testImmutableIterable() throws Exception { Iterable<Integer> value = FluentIterable.from(Arrays.asList(1, 2, 3, 4)).cycle().limit(50); MutationDetector detector = MutationDetectors.forValueWithCoder(value, IterableCoder.of(VarIntCoder.of())); detector.verifyUnmodified(); }
@Test public void testMutatingArray() throws Exception { byte[] value = new byte[] {0x1, 0x2, 0x3, 0x4}; MutationDetector detector = MutationDetectors.forValueWithCoder(value, ByteArrayCoder.of()); value[0] = 0xa; thrown.expect(IllegalMutationException.class); detector.verifyUnmodified(); }
@Test public void testUnmodifiedArray() throws Exception { byte[] value = new byte[] {0x1, 0x2, 0x3, 0x4}; MutationDetector detector = MutationDetectors.forValueWithCoder(value, ByteArrayCoder.of()); detector.verifyUnmodified(); }
@Test public void testEquivalentListOfArrays() throws Exception { List<byte[]> value = Arrays.asList(new byte[] {0x1}, new byte[] {0x2, 0x3}, new byte[] {0x4}); MutationDetector detector = MutationDetectors.forValueWithCoder(value, ListCoder.of(ByteArrayCoder.of())); value.set(0, new byte[] {0x1}); detector.verifyUnmodified(); }