/**
 * Copyright (C) 2012 Ryan W Tenney (ryan@10e.us)
 *
 * 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 static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import com.ryantenney.metrics.annotation.InjectMetric;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:aop-field-injection-interaction.xml")
public class AopFieldInjectionInteractionTest {

	@Autowired private MetricRegistry metricRegistry;
	@Autowired private TestAspectTarget target;

	@Test
	public void testFieldInjectionShouldNotCauseErrorWhenTargetIsAopProxy() throws Exception {
		// verify that AOP interception is working
		assertThat(target.targetMethod(), is(5));

		assertThat(metricRegistry.getCounters(), hasKey("targetCounter"));
		assertThat(metricRegistry.getCounters().get("targetCounter").getCount(), is(1L));
	}

}

@Aspect
class TestAspectWrapper {

	@Around("execution(* com.ryantenney.metrics.spring.TestAspectTarget.targetMethod())")
	public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
		joinPoint.proceed();
		return 5;
	}

}

interface TestAspectTarget {

	public int targetMethod();

}

class TestAspectTargetImpl implements TestAspectTarget {

	@InjectMetric(name = "targetCounter", absolute = true) private Counter counter;

	@Override
	public int targetMethod() {
		this.counter.inc();
		return 3;
	}

}

@@ -1256,16 +1256,65 @@
 tMetric;
+%0Aimport com.ryantenney.metrics.annotation.Metric;
 %0A%0A@RunWi
@@ -1418,16 +1418,49 @@
 n.xml%22)%0A
+@SuppressWarnings(%22deprecation%22)%0A
 public c
@@ -1938,16 +1938,172 @@
 is(1L));
+%0A%0A%09%09assertThat(metricRegistry.getCounters(), hasKey(%22targetCounter2%22));%0A%09%09assertThat(metricRegistry.getCounters().get(%22targetCounter2%22).getCount(), is(1L));
 %0A%09%7D%0A%0A%7D%0A%0A
@@ -2464,16 +2464,50 @@
 rget %7B%0A%0A
+%09@SuppressWarnings(%22deprecation%22)%0A
 %09@Inject
@@ -2580,16 +2580,94 @@
 unter;%0A%0A
+%09@Metric(name = %22targetCounter2%22, absolute = true) private Counter counter2;%0A%0A
 %09@Overri
@@ -2698,16 +2698,16 @@
 hod() %7B%0A
-
 %09%09this.c
@@ -2716,24 +2716,47 @@
 nter.inc();%0A
+%09%09this.counter2.inc();%0A
 %09%09return 3;%0A

Update AopFieldInjectionInterationTest