This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from adammelliott/inject-fields-during-before-…
…initialization Fix metric injection into AOP-advised beans
- Loading branch information
Showing
5 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/test/java/com/ryantenney/metrics/spring/AopFieldInjectionInteractionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (C) 2012 Ryan W Tenney ([email protected]) | ||
* | ||
* 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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (C) 2012 Ryan W Tenney ([email protected]) | ||
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. | ||
--> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:aop="http://www.springframework.org/schema/aop" | ||
xmlns:metrics="http://www.ryantenney.com/schema/metrics" | ||
|
||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd | ||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd | ||
http://www.ryantenney.com/schema/metrics http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd"> | ||
|
||
<aop:aspectj-autoproxy /> | ||
|
||
<metrics:annotation-driven /> | ||
|
||
<bean class="com.ryantenney.metrics.spring.TestAspectWrapper" /> | ||
<bean class="com.ryantenney.metrics.spring.TestAspectTargetImpl" /> | ||
|
||
</beans> |