Skip to content

Commit eb49f3c

Browse files
committed
Add interface-based detection test
Add a test that explicitely validates that `@ManagedResource` and friends can be set on an interface. Issue: SPR-13138
1 parent a112557 commit eb49f3c

File tree

4 files changed

+127
-20
lines changed

4 files changed

+127
-20
lines changed

spring-context/src/test/java/org/springframework/jmx/export/annotation/AnnotationMetadataAssemblerTests.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5-
* use this file except in compliance with the License. You may obtain a copy of
6-
* the License at
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
77
*
88
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13-
* License for the specific language governing permissions and limitations under
14-
* the License.
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
1515
*/
1616

1717
package org.springframework.jmx.export.annotation;
@@ -58,6 +58,30 @@ public void testOperationOnGetter() throws Exception {
5858
assertNotNull(op);
5959
}
6060

61+
@Test
62+
public void testRegistrationOnInterface() throws Exception {
63+
Object bean = getContext().getBean("testInterfaceBean");
64+
ModelMBeanInfo inf = getAssembler().getMBeanInfo(bean, "bean:name=interfaceTestBean");
65+
assertNotNull(inf);
66+
assertEquals("My Managed Bean", inf.getDescription());
67+
68+
ModelMBeanOperationInfo op = inf.getOperation("foo");
69+
assertNotNull("foo operation not exposed", op);
70+
assertEquals("invoke foo", op.getDescription());
71+
72+
assertNull("doNotExpose operation should not be exposed", inf.getOperation("doNotExpose"));
73+
74+
ModelMBeanAttributeInfo attr = inf.getAttribute("Bar");
75+
assertNotNull("bar attribute not exposed", attr);
76+
assertEquals("Bar description", attr.getDescription());
77+
78+
ModelMBeanAttributeInfo attr2 = inf.getAttribute("CacheEntries");
79+
assertNotNull("cacheEntries attribute not exposed", attr2);
80+
assertEquals("Metric Type should be COUNTER", "COUNTER",
81+
attr2.getDescriptor().getFieldValue("metricType"));
82+
}
83+
84+
6185
@Override
6286
protected JmxAttributeSource getAttributeSource() {
6387
return new AnnotationJmxAttributeSource();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jmx.export.annotation;
18+
19+
import org.springframework.jmx.support.MetricType;
20+
21+
/**
22+
* @author Stephane Nicoll
23+
*/
24+
@ManagedResource(objectName = "bean:name=interfaceTestBean", description = "My Managed Bean")
25+
public interface AnotherAnnotationTestBean {
26+
27+
@ManagedOperation(description = "invoke foo")
28+
void foo();
29+
30+
@ManagedAttribute(description = "Bar description")
31+
String getBar();
32+
33+
void setBar(String bar);
34+
35+
@ManagedMetric(description = "a metric", metricType = MetricType.COUNTER)
36+
int getCacheEntries();
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jmx.export.annotation;
18+
19+
/**
20+
* @author Stephane Nicoll
21+
*/
22+
class AnotherAnnotationTestBeanImpl implements AnotherAnnotationTestBean {
23+
24+
private String bar;
25+
26+
@Override
27+
public void foo() {
28+
}
29+
30+
public void doNotExpose() {
31+
32+
}
33+
34+
@Override
35+
public String getBar() {
36+
return this.bar;
37+
}
38+
39+
@Override
40+
public void setBar(String bar) {
41+
this.bar = bar;
42+
}
43+
44+
@Override
45+
public int getCacheEntries() {
46+
return 42;
47+
}
48+
49+
}

spring-context/src/test/resources/org/springframework/jmx/export/annotation/annotations.xml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,22 @@
2020
</bean>
2121

2222
<bean id="testBean" class="org.springframework.jmx.export.annotation.AnnotationTestSubBean">
23-
<property name="name">
24-
<value>TEST</value>
25-
</property>
26-
<property name="age">
27-
<value>100</value>
28-
</property>
23+
<property name="name" value="TEST"/>
24+
<property name="age" value="100"/>
25+
</bean>
26+
27+
<bean id="testInterfaceBean" class="org.springframework.jmx.export.annotation.AnotherAnnotationTestBeanImpl">
28+
<property name="bar" value="Init value"/>
2929
</bean>
3030

3131
<bean id="attributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
3232

3333
<bean id="metadataNamingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
34-
<property name="attributeSource">
35-
<ref bean="attributeSource"/>
36-
</property>
34+
<property name="attributeSource" ref="attributeSource"/>
3735
</bean>
3836

3937
<bean id="metadataAssembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
40-
<property name="attributeSource">
41-
<ref bean="attributeSource"/>
42-
</property>
38+
<property name="attributeSource" ref="attributeSource"/>
4339
</bean>
4440

4541
</beans>

0 commit comments

Comments
 (0)