MBeanHolder { void register(final MBeanServer server, String[] warnExceptions) { ObjectInstance instance; try { instance = server.registerMBean(mbean, requestedObjectName); registrations.put(server, instance.getObjectName()); } catch (Exception e) { String exClass = e.getClass().getName(); if (warnExceptions == null) warnExceptions = new String[] {}; for (String exCls : warnExceptions) { if (exClass.equals(exCls)) { log.warn("register: problem registering MBean " + mbean, e); return; } } if (e instanceof InstanceAlreadyExistsException || e instanceof MBeanRegistrationException || e instanceof NotCompliantMBeanException) { log.error("register: Failure registering MBean " + mbean, e); } else if (e instanceof RuntimeException) { throw ((RuntimeException) e); } } } MBeanHolder(final Object mbean, final ObjectName requestedObjectName);   }
@Test public void testPlainRegistration() throws Exception { MBeanHolder mbh = new MBeanHolder(new Object(), new ObjectName("foo:bar=123")); Map<?,?> registrations = (Map<?,?>) getPrivateField(mbh, "registrations"); Logger ml = Mockito.mock(Logger.class); setPrivateField(mbh, "log", ml); MBeanServer ms = mockMBeanServer(); assertEquals(0, registrations.size()); mbh.register(ms, new String [] {}); assertEquals(1, registrations.size()); assertEquals(new ObjectName("foo:bar=123"), registrations.get(ms)); Mockito.verify(ml, Mockito.never()).error(Mockito.anyString(), Mockito.isA(Throwable.class)); }
@Test public void testRegistrationException() throws Exception { MBeanHolder mbh = new MBeanHolder(new Object(), new ObjectName("foo:bar=123")); Map<?,?> registrations = (Map<?,?>) getPrivateField(mbh, "registrations"); Logger ml = Mockito.mock(Logger.class); setPrivateField(mbh, "log", ml); MBeanServer ms = Mockito.mock(MBeanServer.class); Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("foo:bar=123")))). thenThrow(new InstanceAlreadyExistsException()); assertEquals(0, registrations.size()); mbh.register(ms, new String [] {}); assertEquals(0, registrations.size()); Mockito.verify(ml, Mockito.times(1)).error(Mockito.anyString(), Mockito.isA(InstanceAlreadyExistsException.class)); }
@Test public void testExceptionsAsWarnings() throws Exception { MBeanHolder mbh = new MBeanHolder(new Object(), new ObjectName("foo:bar=123")); Map<?,?> registrations = (Map<?,?>) getPrivateField(mbh, "registrations"); Logger ml = Mockito.mock(Logger.class); setPrivateField(mbh, "log", ml); MBeanServer ms = Mockito.mock(MBeanServer.class); Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("foo:bar=123")))). thenThrow(new InstanceAlreadyExistsException()); assertEquals(0, registrations.size()); mbh.register(ms, new String [] {InstanceAlreadyExistsException.class.getName()}); assertEquals(0, registrations.size()); Mockito.verify(ml, Mockito.times(1)).warn(Mockito.anyString(), Mockito.isA(InstanceAlreadyExistsException.class)); }
@Test public void testExceptionsAsWarnings2() throws Exception { MBeanHolder mbh = new MBeanHolder(new Object(), new ObjectName("foo:bar=123")); Map<?,?> registrations = (Map<?,?>) getPrivateField(mbh, "registrations"); Logger ml = Mockito.mock(Logger.class); setPrivateField(mbh, "log", ml); MBeanServer ms = Mockito.mock(MBeanServer.class); Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("foo:bar=123")))). thenThrow(new NullPointerException()); assertEquals(0, registrations.size()); mbh.register(ms, new String [] {InstanceAlreadyExistsException.class.getName(), NullPointerException.class.getName()}); assertEquals(0, registrations.size()); Mockito.verify(ml, Mockito.times(1)).warn(Mockito.anyString(), Mockito.isA(NullPointerException.class)); }
@Test public void testThrowRuntimeException() throws Exception { MBeanHolder mbh = new MBeanHolder(new Object(), new ObjectName("foo:bar=123")); Map<?,?> registrations = (Map<?,?>) getPrivateField(mbh, "registrations"); Logger ml = Mockito.mock(Logger.class); setPrivateField(mbh, "log", ml); MBeanServer ms = Mockito.mock(MBeanServer.class); Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("foo:bar=123")))). thenThrow(new NullPointerException()); assertEquals(0, registrations.size()); try { mbh.register(ms, new String [] {}); fail("Should have thrown a NullPointerException"); } catch (NullPointerException npe) { } }