@SuppressWarnings("resource")
@Test
public void fakeReporters() throws Throwable {
ClassPathXmlApplicationContext ctx = null;
FakeReporter one = null;
FakeReporter two = null;
try {
final MetricRegistry registry = SharedMetricRegistries.getOrCreate("reporterTestRegistry");
ctx = new ClassPathXmlApplicationContext("classpath:fake-reporter-test.xml");
ctx.start();
Thread.sleep(1000);
one = ctx.getBean("fakeReporterOne", FakeReporter.class);
Assert.assertFalse(AopUtils.isAopProxy(one.getRegistry()));
Assert.assertSame(registry, one.getRegistry());
Assert.assertEquals("milliseconds", one.getDurationUnit());
Assert.assertEquals("second", one.getRateUnit());
Assert.assertEquals(100000000, one.getPeriod());
Assert.assertThat(one.getCalls(), allOf(greaterThanOrEqualTo(9), lessThanOrEqualTo(11)));
Assert.assertEquals("[MetricFilter regex=foo]", one.getFilter().toString());
Assert.assertTrue(one.isRunning());
two = ctx.getBean("fakeReporterTwo", FakeReporter.class);
Assert.assertFalse(AopUtils.isAopProxy(one.getRegistry()));
Assert.assertSame(registry, two.getRegistry());
Assert.assertEquals("nanoseconds", two.getDurationUnit());
Assert.assertEquals("hour", two.getRateUnit());
Assert.assertEquals(100000000, two.getPeriod());
Assert.assertThat(two.getCalls(), allOf(greaterThanOrEqualTo(9), lessThanOrEqualTo(11)));
Assert.assertEquals(ctx.getBean(BarFilter.class), two.getFilter());
Assert.assertTrue(one.isRunning());
// Make certain reporters aren't candidates for autowiring
ReporterCollaborator collab = ctx.getBean(ReporterCollaborator.class);
Assert.assertNotNull(collab.metricRegistry);
Assert.assertNull(collab.fakeReporter);
}
finally {
if (ctx != null) {
ctx.stop();
ctx.close();
}
}
if (one != null) {
Assert.assertFalse(one.isRunning());
}
if (two != null) {
Assert.assertFalse(two.isRunning());
}
}