MBeanRegistry { public void register(final Object o, final String name) { String packageName = o.getClass().getPackage().getName(); ImmutableMap<String, String> attributes = new ImmutableMap.Builder<String, String>() .put("name", name) .put("type", o.getClass().getSimpleName()).build(); try { ObjectName objectName = new ObjectName(packageName, new Hashtable<>(attributes)); server.registerMBean(o, objectName); } catch (MalformedObjectNameException e) { LOGGER.warn(format("invalid object name: %s, cause: %s", name, e.getMessage()), e); } catch (NotCompliantMBeanException e) { LOGGER.warn(format("non-compliant mxbean: %s, cause: %s", name, e.getMessage()), e); } catch (InstanceAlreadyExistsException e) { LOGGER.warn(format("mxbean already exists by name: %s, cause: %s", name, e.getMessage()), e); } catch (MBeanRegistrationException e) { LOGGER.warn(format("unable to register mxbean name: %s, cause: %s", name, e.getMessage()), e); } } @Inject  MBeanRegistry(MBeanServer server); void register(final Object o, final String name);  }
@Test public void register() throws Exception { MBeanRegistry mBeanRegistry = new MBeanRegistry(mbServer); mBeanRegistry.register(object, "name"); }
@Ignore("powermock:Objectname ctor") @Test public void registerMalformedObjectNameException() throws Exception { MalformedObjectNameException mone = mock(MalformedObjectNameException.class); when(mbServer.registerMBean(any(Object.class), any(ObjectName.class))).thenThrow(mone); MBeanRegistry mBeanRegistry = new MBeanRegistry(mbServer); mBeanRegistry.register(object, "name"); verify(mone, times(2)).getMessage(); }
@Test public void registerNotCompliantMBeanException() throws Exception { NotCompliantMBeanException ncmbe = mock(NotCompliantMBeanException.class); when(mbServer.registerMBean(any(Object.class), any(ObjectName.class))).thenThrow(ncmbe); MBeanRegistry mBeanRegistry = new MBeanRegistry(mbServer); mBeanRegistry.register(object, "name"); verify(ncmbe, times(2)).getMessage(); }
@Test public void registerInstanceAlreadyExistsException() throws Exception { InstanceAlreadyExistsException iae = mock(InstanceAlreadyExistsException.class); when(mbServer.registerMBean(any(Object.class), any(ObjectName.class))).thenThrow(iae); MBeanRegistry mBeanRegistry = new MBeanRegistry(mbServer); mBeanRegistry.register(object, "name"); verify(iae, times(2)).getMessage(); }
@Test public void registerMBeanRegistrationExcpetion() throws Exception { MBeanRegistrationException mre = mock(MBeanRegistrationException.class); when(mbServer.registerMBean(any(Object.class), any(ObjectName.class))).thenThrow(mre); MBeanRegistry mBeanRegistry = new MBeanRegistry(mbServer); mBeanRegistry.register(object, "name"); verify(mre, times(2)).getMessage(); }