### Question:
CompositeMetricsCollector extends SystemMetricsCollector<CompositeMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(CompositeMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); boolean result = false; SimpleArrayMap<Class<? extends SystemMetrics>, SystemMetrics> snapshotMetrics = snapshot.getMetrics(); for (int i = 0, size = snapshotMetrics.size(); i < size; i++) { Class<? extends SystemMetrics> metricsClass = snapshotMetrics.keyAt(i); SystemMetricsCollector collector = mMetricsCollectorMap.get(metricsClass); boolean snapshotResult = false; if (collector != null) { SystemMetrics metric = snapshot.getMetric(metricsClass); snapshotResult = collector.getSnapshot(metric); } snapshot.setIsValid(metricsClass, snapshotResult); result |= snapshotResult; } return result; } protected  CompositeMetricsCollector(Builder builder); T getMetricsCollector(
      Class<S> metricsClass); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(CompositeMetrics snapshot); @Override CompositeMetrics createMetrics();  }### Answer:
@Test public void testNullSnapshot() { mExpectedException.expect(IllegalArgumentException.class); mCollector.getSnapshot(null); }
@Test public void allSnapshotsSucceed() throws Exception { mACollector.succeeds = true; mACollector.currentValue = 100; mBCollector.succeeds = true; mBCollector.currentValue = 120; assertThat(mCollector.getSnapshot(mMetrics)).isTrue(); assertThat(mMetrics.getMetrics().size()).isEqualTo(2); assertThat(mMetrics.getMetric(A.class).value).isEqualTo(100); assertThat(mMetrics.isValid(A.class)).isEqualTo(true); assertThat(mMetrics.getMetric(B.class).value).isEqualTo(120); assertThat(mMetrics.isValid(B.class)).isEqualTo(true); }
@Test public void partialFailures() throws Exception { mACollector.succeeds = true; mACollector.currentValue = 100; mBCollector.succeeds = false; assertThat(mCollector.getSnapshot(mMetrics)).isTrue(); assertThat(mMetrics.getMetrics().size()).isEqualTo(2); assertThat(mMetrics.getMetric(A.class).value).isEqualTo(100); assertThat(mMetrics.isValid(A.class)).isEqualTo(true); assertThat(mMetrics.isValid(B.class)).isEqualTo(false); }
@Test public void allSnapshotsFail() throws Exception { mACollector.succeeds = false; mBCollector.succeeds = false; assertThat(mCollector.getSnapshot(mMetrics)).isFalse(); assertThat(mMetrics.getMetrics().size()).isEqualTo(2); assertThat(mMetrics.isValid(A.class)).isEqualTo(false); assertThat(mMetrics.isValid(B.class)).isEqualTo(false); }
@Test public void subsetSnapshots() throws Exception { mACollector.currentValue = 100; mACollector.succeeds = true; CompositeMetrics m = new CompositeMetrics().putMetric(A.class, new A()); mCollector.getSnapshot(m); assertThat(mCollector.getSnapshot(m)).isTrue(); assertThat(m.getMetrics().size()).isEqualTo(1); assertThat(m.getMetric(A.class).value).isEqualTo(100); assertThat(m.isValid(A.class)).isEqualTo(true); assertThat(m.getMetric(B.class)).isNull(); }
@Test public void supersetSnapshots() throws Exception { mACollector.currentValue = 100; mACollector.succeeds = true; CompositeMetrics m = new CompositeMetrics().putMetric(A.class, new A()).putMetric(C.class, new C()); mCollector.getSnapshot(m); assertThat(mCollector.getSnapshot(m)).isTrue(); assertThat(m.getMetrics().size()).isEqualTo(2); assertThat(m.getMetric(A.class).value).isEqualTo(100); assertThat(m.isValid(A.class)).isEqualTo(true); assertThat(m.getMetric(B.class)).isNull(); assertThat(m.isValid(C.class)).isFalse(); }