MetricConsumerProvider { public MetricConsumer newInstance() { if (factories.length == 1) { return factories[0].newInstance(); } MetricConsumer[] consumers = new MetricConsumer[factories.length]; for (int i = 0; i < factories.length; ++i) { consumers[i] = factories[i].newInstance(); } return new ForwardingMetricConsumer(consumers); } @Inject  MetricConsumerProvider(ComponentRegistry<MetricConsumerFactory> factoryRegistry,
                                  MetricsPresentationConfig presentationConfig,
                                  StateMonitor stateMonitor); MetricConsumer newInstance();  }
@Test public void requireThatSingleConsumerIsNotDelegated() { MetricConsumer consumer = Mockito.mock(MetricConsumer.class); MetricConsumerProvider provider = MetricConsumerProviders.newSingletonFactories(consumer); assertSame(consumer, provider.newInstance()); }
@Test public void requireThatMultipleConsumersAreDelegated() { MetricConsumer foo = Mockito.mock(MetricConsumer.class); MetricConsumer bar = Mockito.mock(MetricConsumer.class); MetricConsumerProvider provider = MetricConsumerProviders.newSingletonFactories(foo, bar); MetricConsumer consumer = provider.newInstance(); assertNotSame(foo, consumer); assertNotSame(bar, consumer); consumer.add("foo", 6, null); Mockito.verify(foo, Mockito.times(1)).add("foo", 6, null); Mockito.verify(bar, Mockito.times(1)).add("foo", 6, null); }
@Test public void requireThatDefaultConsumerFactoryIsStateMetric() { MetricConsumer consumer = MetricConsumerProviders.newDefaultInstance().newInstance(); assertEquals("StateMetricConsumer", consumer.getClass().getSimpleName()); }