/*
 * Copyright 2012 Ryan W Tenney (http://ryan.10e.us)
 *            and Martello Technologies (http://martellotech.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ryantenney.metrics.spring;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.Slf4jReporter;
import com.codahale.metrics.ganglia.GangliaReporter;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.ryantenney.metrics.spring.reporter.FakeReporter;

public class ReporterTest {

	@Test
	public void fakeReporters() throws Throwable {
		ClassPathXmlApplicationContext ctx = null;
		FakeReporter one = null;
		FakeReporter two = null;
		try {
			MetricRegistry registry = new MetricRegistry();
			SharedMetricRegistries.add("reporterTestRegistry", registry);

			ctx = new ClassPathXmlApplicationContext("classpath:fake-reporter-test.xml");
			ctx.start();

			Thread.sleep(1000);

			one = ctx.getBean("fakeReporterOne", FakeReporter.class);
			Assert.assertEquals(registry, one.getRegistry());
			Assert.assertEquals("milliseconds", one.getDurationUnit());
			Assert.assertEquals("second", one.getRateUnit());
			Assert.assertEquals(100000000, one.getPeriod());
			Assert.assertEquals(10, one.getCalls());
			Assert.assertEquals("[MetricFilter regex=foo]", one.getFilter().toString());
			Assert.assertTrue(one.isRunning());

			two = ctx.getBean("fakeReporterTwo", FakeReporter.class);
			Assert.assertEquals(registry, two.getRegistry());
			Assert.assertEquals("nanoseconds", two.getDurationUnit());
			Assert.assertEquals("hour", two.getRateUnit());
			Assert.assertEquals(100000000, two.getPeriod());
			Assert.assertEquals(10, one.getCalls());
			Assert.assertEquals(ctx.getBean(BarFilter.class), two.getFilter());
			Assert.assertTrue(one.isRunning());

		}
		finally {
			if (ctx != null) {
				ctx.stop();
				ctx.close();
			}
		}

		if (one != null) {
			Assert.assertFalse(one.isRunning());
		}

		if (two != null) {
			Assert.assertFalse(two.isRunning());
		}
	}

	@Test
	public void realReporters() throws Throwable {
		ClassPathXmlApplicationContext ctx = null;
		try {
			ctx = new ClassPathXmlApplicationContext("classpath:reporter-test.xml");
			// intentionally avoids calling ctx.start()

			Assert.assertNotNull(ctx.getBean(ConsoleReporter.class));
			Assert.assertNotNull(ctx.getBean(JmxReporter.class));
			Assert.assertNotNull(ctx.getBean(Slf4jReporter.class));
			Assert.assertNotNull(ctx.getBean(GraphiteReporter.class));
			Assert.assertNotNull(ctx.getBean(GangliaReporter.class));
		}
		finally {
			if (ctx != null) {
				ctx.close();
			}
		}
	}

	public static PrintStream testPrintStream() {
		return new PrintStream(new ByteArrayOutputStream());
	}

	public static class BarFilter implements MetricFilter {

		@Override
		public boolean matches(String name, Metric metric) {
			return false;
		}

	}

}

@@ -832,16 +832,65 @@
 t.Test;%0A
+import org.springframework.aop.support.AopUtils;%0A
 import o
@@ -956,16 +956,16 @@
 ontext;%0A
-
 %0Aimport 
@@ -1623,16 +1623,22 @@
 ry %7B%0A%09%09%09
+final 
 MetricRe
@@ -1659,33 +1659,8 @@
 y = 
-new MetricRegistry();%0A%09%09%09
 Shar
@@ -1682,11 +1682,19 @@
 ies.
-add
+getOrCreate
 (%22re
@@ -1716,18 +1716,8 @@
 try%22
-, registry
 );%0A%0A
@@ -1907,38 +1907,99 @@
 %09%09%09Assert.assert
-Equals
+False(AopUtils.isAopProxy(one.getRegistry()));%0A%09%09%09Assert.assertSame
 (registry, one.g
@@ -2422,22 +2422,83 @@
 t.assert
-Equals
+False(AopUtils.isAopProxy(one.getRegistry()));%0A%09%09%09Assert.assertSame
 (registr

Fix reporter test, apparently.