ReportCollectionFactory { @SuppressWarnings("unchecked") public <T> T createReportCollection(Class<T> aClass) { requireNonNull(aClass, "class is null"); return (T) newProxyInstance(aClass.getClassLoader(), new Class[]{aClass}, new StatInvocationHandler(aClass, false, Optional.of(aClass.getSimpleName()), ImmutableMap.of())); } @Inject  ReportCollectionFactory(ReportExporter reportExporter); protected  ReportCollectionFactory(ReportExporter reportExporter, Ticker ticker); @SuppressWarnings("unchecked") T createReportCollection(Class<T> aClass); @SuppressWarnings("unchecked") T createReportCollection(Class<T> aClass, boolean applicationPrefix, @Nullable String namePrefix, Map<String, String> tags); @Deprecated @SuppressWarnings("unchecked") T createReportCollection(Class<T> aClass, String name);  }
@Test public void testKeyedDistribution() { KeyedDistribution keyedDistribution = reportCollectionFactory.createReportCollection(KeyedDistribution.class); SomeObject someObject = keyedDistribution.add("value", false); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<SomeObject> reportCaptor = ArgumentCaptor.forClass(SomeObject.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "KeyedDistribution.Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.of("foo", "value", "bar", "false")); assertSame(reportCaptor.getValue(), someObject); }
@Test public void testNullValue() { KeyedDistribution keyedDistribution = reportCollectionFactory.createReportCollection(KeyedDistribution.class); SomeObject someObject = keyedDistribution.add(null, false); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<SomeObject> reportCaptor = ArgumentCaptor.forClass(SomeObject.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "KeyedDistribution.Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.of("bar", "false")); assertSame(reportCaptor.getValue(), someObject); }
@Test public void testOptionalValue() { OptionalKeyedDistribution optionalKeyedDistribution = reportCollectionFactory.createReportCollection(OptionalKeyedDistribution.class); SomeObject someObject = optionalKeyedDistribution.add(Optional.empty(), Optional.of(false)); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<SomeObject> reportCaptor = ArgumentCaptor.forClass(SomeObject.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "OptionalKeyedDistribution.Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.of("bar", "false")); assertSame(reportCaptor.getValue(), someObject); }
@Test public void testPrefixedCollection() { KeyedDistribution keyedDistribution = reportCollectionFactory.createReportCollection( KeyedDistribution.class, true, "Prefix", ImmutableMap.of("a", "fooval", "b", "with\"quote", "c", "with,comma", "d", "with\\backslash") ); SomeObject someObject = keyedDistribution.add("value", false); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<SomeObject> reportCaptor = ArgumentCaptor.forClass(SomeObject.class); verify(reportExporter).export(reportCaptor.capture(), eq(true), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "Prefix.Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.builder() .put("a", "fooval") .put("b", "with\"quote") .put("c", "with,comma") .put("d", "with\\backslash") .put("foo", "value") .put("bar", "false") .build()); assertSame(reportCaptor.getValue(), someObject); }
@Test public void testPrefixAbsentCollection() { KeyedDistribution keyedDistribution = reportCollectionFactory.createReportCollection( KeyedDistribution.class, false, null, ImmutableMap.of("a", "fooval", "b", "with\"quote", "c", "with,comma", "d", "with\\backslash") ); SomeObject someObject = keyedDistribution.add("value", false); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<SomeObject> reportCaptor = ArgumentCaptor.forClass(SomeObject.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.builder() .put("a", "fooval") .put("b", "with\"quote") .put("c", "with,comma") .put("d", "with\\backslash") .put("foo", "value") .put("bar", "false") .build()); assertSame(reportCaptor.getValue(), someObject); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*KeyedDistribution\\.add\\(java\\.lang\\.String, boolean\\) @Key\\(\"foo\"\\) duplicates tag on entire report collection.*") public void testPrefixedCollectionWithConflictingTags() { reportCollectionFactory.createReportCollection( KeyedDistribution.class, true, "Prefix", ImmutableMap.of("foo", "fooval") ); }
@Test @SuppressWarnings("deprecation") public void testLegacyNamedCollection() { String name = new ObjectNameBuilder("com.example") .withProperty("a", "fooval") .withProperty("b", "with\"quote") .withProperty("c", "with,comma") .withProperty("d", "with\\backslash") .build(); KeyedDistribution keyedDistribution = reportCollectionFactory.createReportCollection(KeyedDistribution.class, name); SomeObject someObject = keyedDistribution.add("value", false); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<SomeObject> reportCaptor = ArgumentCaptor.forClass(SomeObject.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.builder() .put("a", "fooval") .put("b", "with\"quote") .put("c", "with,comma") .put("d", "with\\backslash") .put("foo", "value") .put("bar", "false") .build()); assertSame(reportCaptor.getValue(), someObject); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "com\\.proofpoint\\.reporting\\.TestReportCollectionFactory\\$MissingParameterName\\.add\\(java\\.lang\\.String, boolean\\) parameter 2 has no @com.proofpoint.reporting.Key annotation") public void testNoParameterNames() { reportCollectionFactory.createReportCollection(MissingParameterName.class); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "com\\.proofpoint\\.reporting\\.TestReportCollectionFactory\\$NotConstructable\\.add\\(java\\.lang\\.String, boolean\\) return type ConstructorNeedsArgument has no public no-arg constructor") public void testReturnTypeNotConstructable() { reportCollectionFactory.createReportCollection(NotConstructable.class); }
@Test public void testNoParameters() { TrackInstantiation.reset(); NoParameters noParameters = reportCollectionFactory.createReportCollection(NoParameters.class); TrackInstantiation.assertInstantiated(); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<TrackInstantiation> reportCaptor = ArgumentCaptor.forClass(TrackInstantiation.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "NoParameters.Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.of()); assertNotNull(reportCaptor.getValue()); assertNotNull(noParameters.add()); }
@Test public void testPrefixedNoParameters() { TrackInstantiation.reset(); NoParameters noParameters = reportCollectionFactory.createReportCollection( NoParameters.class, true, "Prefix", ImmutableMap.of("a", "fooval", "b", "with\"quote", "c", "with,comma", "d", "with\\backslash") ); TrackInstantiation.assertInstantiated(); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<TrackInstantiation> reportCaptor = ArgumentCaptor.forClass(TrackInstantiation.class); verify(reportExporter).export(reportCaptor.capture(), eq(true), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "Prefix.Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.builder() .put("a", "fooval") .put("b", "with\"quote") .put("c", "with,comma") .put("d", "with\\backslash") .build()); assertNotNull(reportCaptor.getValue()); assertNotNull(noParameters.add()); }
@Test public void testPrefixAbsentNoParameters() { TrackInstantiation.reset(); NoParameters noParameters = reportCollectionFactory.createReportCollection( NoParameters.class, false, null, ImmutableMap.of("a", "fooval", "b", "with\"quote", "c", "with,comma", "d", "with\\backslash") ); TrackInstantiation.assertInstantiated(); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<TrackInstantiation> reportCaptor = ArgumentCaptor.forClass(TrackInstantiation.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.builder() .put("a", "fooval") .put("b", "with\"quote") .put("c", "with,comma") .put("d", "with\\backslash") .build()); assertNotNull(reportCaptor.getValue()); assertNotNull(noParameters.add()); }
@Test @SuppressWarnings("deprecation") public void testLegacyNamedNoParameters() { TrackInstantiation.reset(); String name = new ObjectNameBuilder("com.example") .withProperty("a", "fooval") .withProperty("b", "with\"quote") .withProperty("c", "with,comma") .withProperty("d", "with\\backslash") .build(); NoParameters noParameters = reportCollectionFactory.createReportCollection(NoParameters.class, name); TrackInstantiation.assertInstantiated(); ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<TrackInstantiation> reportCaptor = ArgumentCaptor.forClass(TrackInstantiation.class); verify(reportExporter).export(reportCaptor.capture(), eq(false), stringCaptor.capture(), tagCaptor.capture()); assertEquals(stringCaptor.getValue(), "Add"); assertEquals(tagCaptor.getValue(), ImmutableMap.builder() .put("a", "fooval") .put("b", "with\"quote") .put("c", "with,comma") .put("d", "with\\backslash") .build()); assertNotNull(reportCaptor.getValue()); assertNotNull(noParameters.add()); }
@Test public void testEquivalence() { equivalenceTester() .addEquivalentGroup(reportCollectionFactory.createReportCollection(KeyedDistribution.class)) .addEquivalentGroup(reportCollectionFactory.createReportCollection(KeyedDistribution.class)) .check(); }