package org.stagemonitor.requestmonitor;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.stagemonitor.core.metrics.metrics2.MetricName.name;

import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.stagemonitor.core.Stagemonitor;
import org.stagemonitor.core.metrics.annotations.MeteredInstrumenter;
import org.stagemonitor.core.metrics.annotations.TimedInstrumenter;
import org.stagemonitor.core.metrics.metrics2.Metric2Registry;
import org.stagemonitor.core.metrics.metrics2.MetricName;
import org.stagemonitor.requestmonitor.profiler.CallStackElement;
import org.stagemonitor.requestmonitor.profiler.Profiler;

public class MultipleAnnotationsAndProfilerTest {

	private TestObject testObject = new TestObject();

	private static class TestObject {
		@Metered
		@Timed
		public void testMethod() {
		}
	}

	@BeforeClass
	public static void attachProfiler() {
		Stagemonitor.init();
	}

	@Before
	@After
	public void clearMetricRegistry() {
		MeteredInstrumenter.init();
		TimedInstrumenter.init();
		Stagemonitor.getMetric2Registry().removeMatching(MetricFilter.ALL);
	}

	@AfterClass
	public static void resetStagemonitor() {
		Stagemonitor.reset();
		SharedMetricRegistries.clear();
	}

	@Test
	public void testMeterTimer() {
		CallStackElement total = Profiler.activateProfiling("total");
		testObject.testMethod();
		Profiler.stop();
		final String signature = total.getChildren().get(0).getSignature();
		assertTrue(signature, signature.contains("org.stagemonitor.requestmonitor.MultipleAnnotationsAndProfilerTest$TestObject.testMethod"));
		assertOneMeterExists(name("rate").tag("signature", "MultipleAnnotationsAndProfilerTest$TestObject#testMethod").build());
		assertOneTimerExists(name("timer").tag("signature", "MultipleAnnotationsAndProfilerTest$TestObject#testMethod").build());
	}

	private void assertOneMeterExists(MetricName name) {
		final Metric2Registry metricRegistry = Stagemonitor.getMetric2Registry();
		assertNotNull(metricRegistry.getMeters().keySet().toString(), metricRegistry.getMeters().get(name));
	}

	private void assertOneTimerExists(MetricName name) {
		final Metric2Registry metricRegistry = Stagemonitor.getMetric2Registry();
		assertNotNull(metricRegistry.getTimers().keySet().toString(), metricRegistry.getTimers().get(name));
	}

}

@@ -561,146 +561,8 @@
 or;%0A
-import org.stagemonitor.core.metrics.annotations.MeteredInstrumenter;%0Aimport org.stagemonitor.core.metrics.annotations.TimedInstrumenter;%0A
 impo
@@ -620,16 +620,16 @@
 gistry;%0A
+
 import o
@@ -1136,66 +1136,8 @@
 ) %7B%0A
-%09%09MeteredInstrumenter.init();%0A%09%09TimedInstrumenter.init();%0A
 %09%09St
@@ -1737,43 +1737,8 @@
 %22, %22
-MultipleAnnotationsAndProfilerTest$
 Test
@@ -1826,43 +1826,8 @@
 %22, %22
-MultipleAnnotationsAndProfilerTest$
 Test

Fix compile error in test