/** * If the assignment matches the {@link JmxAttributeDescriptor}. * <p> * By default uses filter provided by {@link #getJmxSensorAssignmentFilter()}. * * @param jmxAttributeDescriptor * Descriptor to check. * @return True if the assignment matches the descriptor. */ protected boolean matches(JmxAttributeDescriptor jmxAttributeDescriptor) { return getJmxSensorAssignmentFilter().matches(jmxSensorAssignment, jmxAttributeDescriptor); }
/** * Tests if the given {@link JmxAttributeDescriptor} matches the jmx bean sensor assignment. * * @param assignment * {@link JmxBeanSensorAssignment} * @param attributeDescriptor * {@link JmxAttributeDescriptor} to check * @return Returns <code>true</code> if the data in the descriptor fits the monitoring * definition in the assignment, <code>false</code> otherwise */ public boolean matches(JmxBeanSensorAssignment assignment, JmxAttributeDescriptor attributeDescriptor) { // not matching if the attribute is not readable if (!attributeDescriptor.ismBeanAttributeIsReadable()) { return false; } if (!matchesObjectName(assignment, attributeDescriptor.getmBeanObjectName())) { return false; } if (!matchesAttribute(assignment, attributeDescriptor.getAttributeName())) { return false; } return true; }
@Test public void filterDoesNotMatch() { when(filter.matches(assignment, descriptor)).thenReturn(false); boolean added = applier.addMonitoringPoint(agentConfig, descriptor); assertThat(added, is(false)); verifyZeroInteractions(registrationService); } }
@Test public void notMatchingNotReadable() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(false); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void notMatchingWrongObjectName() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("something not valid"); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void notMatchingAssignmentObjectNameNull() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(assignment.getObjectName()).thenReturn(null); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void matchingAllAttributes() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(descriptor.getAttributeName()).thenReturn("attribute"); when(assignment.getObjectName()).thenReturn(objectName); when(assignment.getAttributes()).thenReturn(Collections.<String> emptySet()); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(true); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(true)); }
@Test public void notMatchingObjectName() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(assignment.getObjectName()).thenReturn(objectName); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(false); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void matchingSpecificAttribute() { String attributeName = "attribute"; when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(descriptor.getAttributeName()).thenReturn(attributeName); when(assignment.getObjectName()).thenReturn(objectName); when(assignment.getAttributes()).thenReturn(Collections.singleton(attributeName)); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(true); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(true)); }
@Test public void notMatchingAttribute() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(descriptor.getAttributeName()).thenReturn("attribute"); when(assignment.getAttributes()).thenReturn(Collections.singleton("not matching")); when(assignment.getObjectName()).thenReturn(objectName); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(true); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); } }
@Test public void add() { long platformId = 7L; String attributeName = "attributeName"; String objectName = "objectname"; long attributeId = 13L; String attributeType = "type"; String attributeDecs = "desc"; boolean isIs = false; boolean readable = true; boolean writable = false; when(filter.matches(assignment, descriptor)).thenReturn(true); when(agentConfig.getPlatformId()).thenReturn(platformId); when(descriptor.getAttributeName()).thenReturn(attributeName); when(descriptor.getmBeanAttributeDescription()).thenReturn(attributeDecs); when(descriptor.getmBeanObjectName()).thenReturn(objectName); when(descriptor.getmBeanAttributeId()).thenReturn(attributeId); when(descriptor.getmBeanAttributeType()).thenReturn(attributeType); when(descriptor.ismBeanAttributeIsIs()).thenReturn(isIs); when(descriptor.ismBeanAttributeIsReadable()).thenReturn(readable); when(descriptor.ismBeanAttributeIsWritable()).thenReturn(writable); boolean added = applier.addMonitoringPoint(agentConfig, descriptor); assertThat(added, is(true)); verify(registrationService).registerJmxSensorDefinitionDataIdent(platformId, objectName, attributeName, attributeDecs, attributeType, isIs, readable, writable); }