44 import java.util.Map;
45 import java.util.Set;
46
47 import javax.management.Attribute;
48 import javax.management.AttributeList;
49 import javax.management.MBeanAttributeInfo;
50 import javax.management.MBeanInfo;
51 import javax.management.MBeanServerConnection;
52 import javax.management.ObjectName;
53
54 import org.junit.Before;
55 import org.junit.Test;
56
57 import org.openjdk.jmc.rjmx.IConnectionHandle;
58 import org.openjdk.jmc.rjmx.subscription.MRI;
59 import org.openjdk.jmc.rjmx.subscription.MRI.Type;
60 import org.openjdk.jmc.rjmx.subscription.internal.AttributeValueToolkit;
61 import org.openjdk.jmc.rjmx.test.ServerHandleTestCase;
62
63 public class SyntheticAttributesTest extends ServerHandleTestCase {
64 private final static String NEW_VALUE = "new value"; //$NON-NLS-1$
65 protected IConnectionHandle localConnection;
66
67 @Test
68 public void testLookupDomain() throws Exception {
69 assertTrue("Could not find the test domain!", containsDomain("org.openjdk.jmc.test")); //$NON-NLS-1$ //$NON-NLS-2$
70 }
71
72 @Test
73 public void testFindNonSyntheticDomain() throws Exception {
74 assertTrue("Could not find the java.lang domain!", containsDomain("java.lang")); //$NON-NLS-1$ //$NON-NLS-2$
75 }
76
77 @Test
78 public void testFindMBean() throws Exception {
79 ObjectName mbean = getSyntheticAttributeDescriptor().getObjectName();
80 assertTrue("Could not find the test mbean!", containsMBean(mbean)); //$NON-NLS-1$
81 }
82
83 @Test
84 public void testFindNonSyntheticMBean() throws Exception {
85 ObjectName mbean = new ObjectName("java.lang:type=Runtime"); //$NON-NLS-1$
86 assertTrue("Could not find the Runtime mbean!", containsMBean(mbean)); //$NON-NLS-1$
87 }
88
89 @Test
90 public void testGetAttribute() throws Exception {
91 MRI descriptor = getSyntheticAttributeDescriptor();
92 Object value = getAttributeValue(descriptor);
93 assertNotNull("Could not retrieve the attribute value", value); //$NON-NLS-1$
94 }
95
96 @Test
97 public void testGetCompositeAttribute() throws Exception {
98 MRI descriptor = getCompositeAttributeDescriptor();
99 Object value = getAttributeValue(descriptor);
100 assertNotNull("Could not retrieve the attribute value", value); //$NON-NLS-1$
101 }
102
103 @Test
104 public void testGetAttributes() throws Exception {
105 MRI descriptor = getSyntheticAttributeDescriptor();
106 AttributeList list = getAttributeValues(descriptor.getObjectName(), new String[] {descriptor.getDataPath()});
107 Object value = list.get(0);
108 assertNotNull("Could not retrieve the attribute value", value); //$NON-NLS-1$
109 }
110
111 @Test
112 public void testGetCompositeAttributes() throws Exception {
113 MRI[] descriptors = getCombinedAttributeDescriptors();
114 ObjectName objectName = descriptors[0].getObjectName();
115 List<String> names = new ArrayList<>();
116 for (MRI ad : descriptors) {
117 names.add(ad.getDataPath());
118 }
119 AttributeList list = getAttributeValues(objectName, names.toArray(new String[descriptors.length]));
120 assertEquals("Could not retrieve all values", descriptors.length, list.size()); //$NON-NLS-1$
121 }
122
123 @Test
124 public void testGetCompositeAttributesThroughMBeanHelperService() throws Exception {
125 MRI[] descriptors = getCombinedAttributeDescriptors();
126 ObjectName objectName = descriptors[0].getObjectName();
127 List<String> names = new ArrayList<>();
128 for (MRI ad : descriptors) {
129 names.add(ad.getDataPath());
130 }
131 Collection<Object> list = getAttributeValuesThroughMBeanHelperService(objectName, names);
132 assertEquals("Could not retrieve all values", descriptors.length, list.size()); //$NON-NLS-1$
133 }
134
135 @Test
136 public void testGetMultipleAttributes() throws Exception {
137 MRI syntheticDescriptor = getExtendedSyntheticAttributeDescriptor();
138 MRI nonsyntheticDescriptor = getNonSyntheticDescriptor();
139 ObjectName mbean = syntheticDescriptor.getObjectName();
140 assertTrue("Not same MBean", mbean.equals(nonsyntheticDescriptor.getObjectName())); //$NON-NLS-1$
141 String[] attributes = new String[] {nonsyntheticDescriptor.getDataPath(), syntheticDescriptor.getDataPath()};
142 AttributeList values = getAttributeValues(mbean, attributes);
143 assertTrue("Not two values", values.size() == 2); //$NON-NLS-1$
144 for (Object attribute : values) {
145 assertNotNull(((Attribute) attribute).getValue());
146 }
147 }
148
149 @Test
150 public void testGetExtendedAttribute() throws Exception {
151 MRI descriptor = getExtendedSyntheticAttributeDescriptor();
152 Object value = getAttributeValue(descriptor);
153 assertNotNull("Could not retrieve the extended attribute value", value); //$NON-NLS-1$
154 }
155
156 @Test
157 public void testSetExtendedAttribute() throws Exception {
158 MRI descriptor = getExtendedSyntheticAttributeDescriptor();
159 String newValue = NEW_VALUE;
160 String oldValue = (String) getAttributeValue(descriptor);
161 setAttributeValue(descriptor, newValue);
162 assertEquals("Could not set the attribute value!", NEW_VALUE, getAttributeValue(descriptor)); //$NON-NLS-1$
163 setAttributeValue(descriptor, oldValue);
164 assertEquals("Could not restore old attribute value!", oldValue, getAttributeValue(descriptor)); //$NON-NLS-1$
165 }
166
167 @Test
168 public void testSetAttribute() throws Exception {
169 MRI descriptor = getSyntheticAttributeDescriptor();
170 String newValue = NEW_VALUE;
171 String oldValue = (String) getAttributeValue(descriptor);
172 setAttributeValue(descriptor, newValue);
173 assertEquals("Could not set the attribute value!", NEW_VALUE, getAttributeValue(descriptor)); //$NON-NLS-1$
174 setAttributeValue(descriptor, oldValue);
175 assertEquals("Could not restore old attribute value!", oldValue, getAttributeValue(descriptor)); //$NON-NLS-1$
176 }
177
178 @Test
179 public void testGetProperties() throws Exception {
180 @SuppressWarnings("unchecked")
181 Map<String, Object> values = (Map<String, Object>) getAttributeValue(
182 getPropertiesSyntheticAttributeDescriptor());
183 assertEquals("Gegga", values.get("denominator")); //$NON-NLS-1$ //$NON-NLS-2$
184 assertEquals("Moja", values.get("numerator")); //$NON-NLS-1$ //$NON-NLS-2$
185 assertEquals(100, values.get("someinteger")); //$NON-NLS-1$
186 assertEquals(0.01f, values.get("factor")); //$NON-NLS-1$
187 assertEquals(true, values.get("someboolean")); //$NON-NLS-1$
188 }
189
190 @Test
191 public void testGetNonSyntheticAttribute() throws Exception {
192 MRI descriptor = getNonSyntheticDescriptor();
193 Boolean value = (Boolean) getAttributeValue(descriptor);
194 assertNotNull(value);
195 }
196
197 @Test
198 public void testSetNonSyntheticAttribute() throws Exception {
199 MRI descriptor = getNonSyntheticDescriptor();
200 Boolean value = (Boolean) getAttributeValue(descriptor);
201 assertNotNull(value);
202 setAttributeValue(descriptor, Boolean.valueOf(!value.booleanValue()));
203 assertNotSame(value, getAttributeValue(descriptor));
204 setAttributeValue(descriptor, value);
205 assertEquals(value, getAttributeValue(descriptor));
206 }
207
208 @Test
209 public void testSetAttributes() throws Exception {
210 MRI synthethicDescriptor = getExtendedSyntheticAttributeDescriptor();
211 String newValue = NEW_VALUE;
212 String oldValue = (String) getAttributeValue(synthethicDescriptor);
213
214 MRI normalDescriptor = getNonSyntheticDescriptor();
215 Boolean value = (Boolean) getAttributeValue(normalDescriptor);
216 assertNotNull(value);
217
218 setAttributeValues(new MRI[] {synthethicDescriptor, normalDescriptor},
219 new Object[] {newValue, Boolean.valueOf(!value.booleanValue())});
220 assertEquals("Could not set the attribute value!", NEW_VALUE, getAttributeValue(synthethicDescriptor)); //$NON-NLS-1$
221 assertNotSame(value, getAttributeValue(normalDescriptor));
222
223 setAttributeValues(new MRI[] {synthethicDescriptor, normalDescriptor}, new Object[] {oldValue, value});
224 assertEquals("Could not restore old attribute value!", oldValue, getAttributeValue(synthethicDescriptor)); //$NON-NLS-1$
225 assertEquals(value, getAttributeValue(normalDescriptor));
226
227 }
228
229 @Test
230 public void testSyntheticMetadata() throws Exception {
231 MRI descriptor = getSyntheticAttributeDescriptor();
232 MBeanInfo info = getMetadata(descriptor);
233 assertEquals(2, info.getAttributes().length);
234 MBeanAttributeInfo attributeInfo = findCorresponding(info.getAttributes(), descriptor);
235 assertTrue(attributeInfo.isReadable());
236 assertTrue(attributeInfo.isWritable());
237 assertFalse(attributeInfo.isIs());
238 assertEquals("java.lang.String", attributeInfo.getType()); //$NON-NLS-1$
239 }
240
241 @Test
242 public void testExtendedMetadata() throws Exception {
243 MRI descriptor = getExtendedSyntheticAttributeDescriptor();
244 MBeanInfo info = getMetadata(descriptor);
245 assertTrue(info.getDescription().contains("Extended")); //$NON-NLS-1$
246
247 MBeanAttributeInfo extendedInfo = null;
248 for (MBeanAttributeInfo attr : info.getAttributes()) {
249 if (attr.getName().equals(descriptor.getDataPath())) {
250 extendedInfo = attr;
251 }
252 }
253 assertNotNull(extendedInfo);
254 MBeanAttributeInfo attributeInfo = findCorresponding(info.getAttributes(), descriptor);
255 assertTrue(attributeInfo.isReadable());
256 assertTrue(attributeInfo.isWritable());
257 assertFalse(attributeInfo.isIs());
258 assertEquals("java.lang.String", attributeInfo.getType()); //$NON-NLS-1$
259 }
260
261 private MBeanAttributeInfo findCorresponding(MBeanAttributeInfo[] attributes, MRI descriptor) {
262 for (MBeanAttributeInfo info : attributes) {
263 if (descriptor.getDataPath().equals(info.getName())) {
264 return info;
265 }
266 }
267 return null;
268 }
269
270 private MRI getPropertiesSyntheticAttributeDescriptor() {
271 return MRI.createFromQualifiedName("attribute://org.openjdk.jmc.test:type=Test/Properties"); //$NON-NLS-1$
272 }
273
274 private boolean containsMBean(ObjectName mbean) throws Exception {
275 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
276 @SuppressWarnings("rawtypes")
277 Set mbeans = connection.queryMBeans(mbean, null);
278 return mbeans.size() > 0;
279 }
280
281 private MRI getSyntheticAttributeDescriptor() {
282 return MRI.createFromQualifiedName("attribute://org.openjdk.jmc.test:type=Test/Test"); //$NON-NLS-1$
283 }
284
285 private MRI getExtendedSyntheticAttributeDescriptor() {
286 return MRI.createFromQualifiedName("attribute://java.lang:type=ClassLoading/Test"); //$NON-NLS-1$
287 }
288
289 private MRI getNonSyntheticDescriptor() {
290 return MRI.createFromQualifiedName("attribute://java.lang:type=ClassLoading/Verbose"); //$NON-NLS-1$
291 }
292
293 private MRI getCompositeAttributeDescriptor() {
294 return MRI.createFromQualifiedName("attribute://java.lang:type=OperatingSystem/TotalPhysicalMemorySize"); //$NON-NLS-1$
295 }
296
297 private MRI[] getCombinedAttributeDescriptors() {
298 return new MRI[] {MRI.createFromQualifiedName("attribute://java.lang:type=Memory/HeapMemoryUsage/used"), //$NON-NLS-1$
299 MRI.createFromQualifiedName("attribute://java.lang:type=Memory/NonHeapMemoryUsage/max"), //$NON-NLS-1$
300 MRI.createFromQualifiedName("attribute://java.lang:type=Memory/Verbose")}; //$NON-NLS-1$
301 }
302
303 private boolean containsDomain(String checkDomain) throws Exception {
304 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
305 boolean containsDomain = false;
306 for (String domain : connection.getDomains()) {
307 if (checkDomain.equals(domain)) {
308 containsDomain = true;
309 break;
310 }
311 }
312 return containsDomain;
313 }
314
315 private Object getAttributeValue(MRI descriptor) throws Exception {
316 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
317 Object value = connection.getAttribute(descriptor.getObjectName(), descriptor.getDataPath());
318 return value;
319 }
320
343 }
344
345 private void setAttributeValue(MRI descriptor, Object newValue) throws Exception {
346 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
347 connection.setAttribute(descriptor.getObjectName(), new Attribute(descriptor.getDataPath(), newValue));
348 }
349
350 private void setAttributeValues(MRI[] descriptors, Object[] values) throws Exception {
351 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
352 AttributeList list = new AttributeList();
353 for (int i = 0; i < descriptors.length; i += 1) {
354 list.add(new Attribute(descriptors[i].getDataPath(), values[i]));
355 }
356 connection.setAttributes(descriptors[0].getObjectName(), list);
357 }
358
359 @Override
360 @Before
361 public void setUp() throws Exception {
362 super.setUp();
363 localConnection = getDefaultServer().connect("Test"); //$NON-NLS-1$
364 }
365
366 @Override
367 public void tearDown() throws Exception {
368 localConnection.close();
369 super.tearDown();
370 }
371
372 protected IConnectionHandle getLocalConnection() {
373 return localConnection;
374 }
375 }
|
44 import java.util.Map;
45 import java.util.Set;
46
47 import javax.management.Attribute;
48 import javax.management.AttributeList;
49 import javax.management.MBeanAttributeInfo;
50 import javax.management.MBeanInfo;
51 import javax.management.MBeanServerConnection;
52 import javax.management.ObjectName;
53
54 import org.junit.Before;
55 import org.junit.Test;
56
57 import org.openjdk.jmc.rjmx.IConnectionHandle;
58 import org.openjdk.jmc.rjmx.subscription.MRI;
59 import org.openjdk.jmc.rjmx.subscription.MRI.Type;
60 import org.openjdk.jmc.rjmx.subscription.internal.AttributeValueToolkit;
61 import org.openjdk.jmc.rjmx.test.ServerHandleTestCase;
62
63 public class SyntheticAttributesTest extends ServerHandleTestCase {
64 private final static String NEW_VALUE = "new value";
65 protected IConnectionHandle localConnection;
66
67 @Test
68 public void testLookupDomain() throws Exception {
69 assertTrue("Could not find the test domain!", containsDomain("org.openjdk.jmc.test"));
70 }
71
72 @Test
73 public void testFindNonSyntheticDomain() throws Exception {
74 assertTrue("Could not find the java.lang domain!", containsDomain("java.lang"));
75 }
76
77 @Test
78 public void testFindMBean() throws Exception {
79 ObjectName mbean = getSyntheticAttributeDescriptor().getObjectName();
80 assertTrue("Could not find the test mbean!", containsMBean(mbean));
81 }
82
83 @Test
84 public void testFindNonSyntheticMBean() throws Exception {
85 ObjectName mbean = new ObjectName("java.lang:type=Runtime");
86 assertTrue("Could not find the Runtime mbean!", containsMBean(mbean));
87 }
88
89 @Test
90 public void testGetAttribute() throws Exception {
91 MRI descriptor = getSyntheticAttributeDescriptor();
92 Object value = getAttributeValue(descriptor);
93 assertNotNull("Could not retrieve the attribute value", value);
94 }
95
96 @Test
97 public void testGetCompositeAttribute() throws Exception {
98 MRI descriptor = getCompositeAttributeDescriptor();
99 Object value = getAttributeValue(descriptor);
100 assertNotNull("Could not retrieve the attribute value", value);
101 }
102
103 @Test
104 public void testGetAttributes() throws Exception {
105 MRI descriptor = getSyntheticAttributeDescriptor();
106 AttributeList list = getAttributeValues(descriptor.getObjectName(), new String[] {descriptor.getDataPath()});
107 Object value = list.get(0);
108 assertNotNull("Could not retrieve the attribute value", value);
109 }
110
111 @Test
112 public void testGetCompositeAttributes() throws Exception {
113 MRI[] descriptors = getCombinedAttributeDescriptors();
114 ObjectName objectName = descriptors[0].getObjectName();
115 List<String> names = new ArrayList<>();
116 for (MRI ad : descriptors) {
117 names.add(ad.getDataPath());
118 }
119 AttributeList list = getAttributeValues(objectName, names.toArray(new String[descriptors.length]));
120 assertEquals("Could not retrieve all values", descriptors.length, list.size());
121 }
122
123 @Test
124 public void testGetCompositeAttributesThroughMBeanHelperService() throws Exception {
125 MRI[] descriptors = getCombinedAttributeDescriptors();
126 ObjectName objectName = descriptors[0].getObjectName();
127 List<String> names = new ArrayList<>();
128 for (MRI ad : descriptors) {
129 names.add(ad.getDataPath());
130 }
131 Collection<Object> list = getAttributeValuesThroughMBeanHelperService(objectName, names);
132 assertEquals("Could not retrieve all values", descriptors.length, list.size());
133 }
134
135 @Test
136 public void testGetMultipleAttributes() throws Exception {
137 MRI syntheticDescriptor = getExtendedSyntheticAttributeDescriptor();
138 MRI nonsyntheticDescriptor = getNonSyntheticDescriptor();
139 ObjectName mbean = syntheticDescriptor.getObjectName();
140 assertTrue("Not same MBean", mbean.equals(nonsyntheticDescriptor.getObjectName()));
141 String[] attributes = new String[] {nonsyntheticDescriptor.getDataPath(), syntheticDescriptor.getDataPath()};
142 AttributeList values = getAttributeValues(mbean, attributes);
143 assertTrue("Not two values", values.size() == 2);
144 for (Object attribute : values) {
145 assertNotNull(((Attribute) attribute).getValue());
146 }
147 }
148
149 @Test
150 public void testGetExtendedAttribute() throws Exception {
151 MRI descriptor = getExtendedSyntheticAttributeDescriptor();
152 Object value = getAttributeValue(descriptor);
153 assertNotNull("Could not retrieve the extended attribute value", value);
154 }
155
156 @Test
157 public void testSetExtendedAttribute() throws Exception {
158 MRI descriptor = getExtendedSyntheticAttributeDescriptor();
159 String newValue = NEW_VALUE;
160 String oldValue = (String) getAttributeValue(descriptor);
161 setAttributeValue(descriptor, newValue);
162 assertEquals("Could not set the attribute value!", NEW_VALUE, getAttributeValue(descriptor));
163 setAttributeValue(descriptor, oldValue);
164 assertEquals("Could not restore old attribute value!", oldValue, getAttributeValue(descriptor));
165 }
166
167 @Test
168 public void testSetAttribute() throws Exception {
169 MRI descriptor = getSyntheticAttributeDescriptor();
170 String newValue = NEW_VALUE;
171 String oldValue = (String) getAttributeValue(descriptor);
172 setAttributeValue(descriptor, newValue);
173 assertEquals("Could not set the attribute value!", NEW_VALUE, getAttributeValue(descriptor));
174 setAttributeValue(descriptor, oldValue);
175 assertEquals("Could not restore old attribute value!", oldValue, getAttributeValue(descriptor));
176 }
177
178 @Test
179 public void testGetProperties() throws Exception {
180 @SuppressWarnings("unchecked")
181 Map<String, Object> values = (Map<String, Object>) getAttributeValue(
182 getPropertiesSyntheticAttributeDescriptor());
183 assertEquals("Gegga", values.get("denominator"));
184 assertEquals("Moja", values.get("numerator"));
185 assertEquals(100, values.get("someinteger"));
186 assertEquals(0.01f, values.get("factor"));
187 assertEquals(true, values.get("someboolean"));
188 }
189
190 @Test
191 public void testGetNonSyntheticAttribute() throws Exception {
192 MRI descriptor = getNonSyntheticDescriptor();
193 Boolean value = (Boolean) getAttributeValue(descriptor);
194 assertNotNull(value);
195 }
196
197 @Test
198 public void testSetNonSyntheticAttribute() throws Exception {
199 MRI descriptor = getNonSyntheticDescriptor();
200 Boolean value = (Boolean) getAttributeValue(descriptor);
201 assertNotNull(value);
202 setAttributeValue(descriptor, Boolean.valueOf(!value.booleanValue()));
203 assertNotSame(value, getAttributeValue(descriptor));
204 setAttributeValue(descriptor, value);
205 assertEquals(value, getAttributeValue(descriptor));
206 }
207
208 @Test
209 public void testSetAttributes() throws Exception {
210 MRI synthethicDescriptor = getExtendedSyntheticAttributeDescriptor();
211 String newValue = NEW_VALUE;
212 String oldValue = (String) getAttributeValue(synthethicDescriptor);
213
214 MRI normalDescriptor = getNonSyntheticDescriptor();
215 Boolean value = (Boolean) getAttributeValue(normalDescriptor);
216 assertNotNull(value);
217
218 setAttributeValues(new MRI[] {synthethicDescriptor, normalDescriptor},
219 new Object[] {newValue, Boolean.valueOf(!value.booleanValue())});
220 assertEquals("Could not set the attribute value!", NEW_VALUE, getAttributeValue(synthethicDescriptor));
221 assertNotSame(value, getAttributeValue(normalDescriptor));
222
223 setAttributeValues(new MRI[] {synthethicDescriptor, normalDescriptor}, new Object[] {oldValue, value});
224 assertEquals("Could not restore old attribute value!", oldValue, getAttributeValue(synthethicDescriptor));
225 assertEquals(value, getAttributeValue(normalDescriptor));
226
227 }
228
229 @Test
230 public void testSyntheticMetadata() throws Exception {
231 MRI descriptor = getSyntheticAttributeDescriptor();
232 MBeanInfo info = getMetadata(descriptor);
233 assertEquals(2, info.getAttributes().length);
234 MBeanAttributeInfo attributeInfo = findCorresponding(info.getAttributes(), descriptor);
235 assertTrue(attributeInfo.isReadable());
236 assertTrue(attributeInfo.isWritable());
237 assertFalse(attributeInfo.isIs());
238 assertEquals("java.lang.String", attributeInfo.getType());
239 }
240
241 @Test
242 public void testExtendedMetadata() throws Exception {
243 MRI descriptor = getExtendedSyntheticAttributeDescriptor();
244 MBeanInfo info = getMetadata(descriptor);
245 assertTrue(info.getDescription().contains("Extended"));
246
247 MBeanAttributeInfo extendedInfo = null;
248 for (MBeanAttributeInfo attr : info.getAttributes()) {
249 if (attr.getName().equals(descriptor.getDataPath())) {
250 extendedInfo = attr;
251 }
252 }
253 assertNotNull(extendedInfo);
254 MBeanAttributeInfo attributeInfo = findCorresponding(info.getAttributes(), descriptor);
255 assertTrue(attributeInfo.isReadable());
256 assertTrue(attributeInfo.isWritable());
257 assertFalse(attributeInfo.isIs());
258 assertEquals("java.lang.String", attributeInfo.getType());
259 }
260
261 private MBeanAttributeInfo findCorresponding(MBeanAttributeInfo[] attributes, MRI descriptor) {
262 for (MBeanAttributeInfo info : attributes) {
263 if (descriptor.getDataPath().equals(info.getName())) {
264 return info;
265 }
266 }
267 return null;
268 }
269
270 private MRI getPropertiesSyntheticAttributeDescriptor() {
271 return MRI.createFromQualifiedName("attribute://org.openjdk.jmc.test:type=Test/Properties");
272 }
273
274 private boolean containsMBean(ObjectName mbean) throws Exception {
275 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
276 @SuppressWarnings("rawtypes")
277 Set mbeans = connection.queryMBeans(mbean, null);
278 return mbeans.size() > 0;
279 }
280
281 private MRI getSyntheticAttributeDescriptor() {
282 return MRI.createFromQualifiedName("attribute://org.openjdk.jmc.test:type=Test/Test");
283 }
284
285 private MRI getExtendedSyntheticAttributeDescriptor() {
286 return MRI.createFromQualifiedName("attribute://java.lang:type=ClassLoading/Test");
287 }
288
289 private MRI getNonSyntheticDescriptor() {
290 return MRI.createFromQualifiedName("attribute://java.lang:type=ClassLoading/Verbose");
291 }
292
293 private MRI getCompositeAttributeDescriptor() {
294 return MRI.createFromQualifiedName("attribute://java.lang:type=OperatingSystem/TotalPhysicalMemorySize");
295 }
296
297 private MRI[] getCombinedAttributeDescriptors() {
298 return new MRI[] {MRI.createFromQualifiedName("attribute://java.lang:type=Memory/HeapMemoryUsage/used"),
299 MRI.createFromQualifiedName("attribute://java.lang:type=Memory/NonHeapMemoryUsage/max"),
300 MRI.createFromQualifiedName("attribute://java.lang:type=Memory/Verbose")};
301 }
302
303 private boolean containsDomain(String checkDomain) throws Exception {
304 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
305 boolean containsDomain = false;
306 for (String domain : connection.getDomains()) {
307 if (checkDomain.equals(domain)) {
308 containsDomain = true;
309 break;
310 }
311 }
312 return containsDomain;
313 }
314
315 private Object getAttributeValue(MRI descriptor) throws Exception {
316 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
317 Object value = connection.getAttribute(descriptor.getObjectName(), descriptor.getDataPath());
318 return value;
319 }
320
343 }
344
345 private void setAttributeValue(MRI descriptor, Object newValue) throws Exception {
346 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
347 connection.setAttribute(descriptor.getObjectName(), new Attribute(descriptor.getDataPath(), newValue));
348 }
349
350 private void setAttributeValues(MRI[] descriptors, Object[] values) throws Exception {
351 MBeanServerConnection connection = getLocalConnection().getServiceOrThrow(MBeanServerConnection.class);
352 AttributeList list = new AttributeList();
353 for (int i = 0; i < descriptors.length; i += 1) {
354 list.add(new Attribute(descriptors[i].getDataPath(), values[i]));
355 }
356 connection.setAttributes(descriptors[0].getObjectName(), list);
357 }
358
359 @Override
360 @Before
361 public void setUp() throws Exception {
362 super.setUp();
363 localConnection = getDefaultServer().connect("Test");
364 }
365
366 @Override
367 public void tearDown() throws Exception {
368 localConnection.close();
369 super.tearDown();
370 }
371
372 protected IConnectionHandle getLocalConnection() {
373 return localConnection;
374 }
375 }
|