BoneCP implements Serializable, Closeable { protected void registerUnregisterJMX(boolean doRegister) { if (this.mbs == null ){ this.mbs = ManagementFactory.getPlatformMBeanServer(); } try { String suffix = ""; if (this.config.getPoolName()!=null){ suffix="-"+this.config.getPoolName(); } ObjectName name = new ObjectName(MBEAN_BONECP +suffix); ObjectName configname = new ObjectName(MBEAN_CONFIG + suffix); if (doRegister){ if (!this.mbs.isRegistered(name)){ this.mbs.registerMBean(this.statistics, name); } if (!this.mbs.isRegistered(configname)){ this.mbs.registerMBean(this.config, configname); } } else { if (this.mbs.isRegistered(name)){ this.mbs.unregisterMBean(name); } if (this.mbs.isRegistered(configname)){ this.mbs.unregisterMBean(configname); } } } catch (Exception e) { logger.error("Unable to start/stop JMX", e); } } BoneCP(BoneCPConfig config); synchronized void shutdown(); void close(); Connection getConnection(); ListenableFuture<Connection> getAsyncConnection(); boolean isConnectionHandleAlive(ConnectionHandle connection); int getTotalLeased(); int getTotalFree(); int getTotalCreatedConnections(); BoneCPConfig getConfig(); Statistics getStatistics(); AtomicBoolean getDbIsDown(); static final String MBEAN_CONFIG; static final String MBEAN_BONECP; }
@Test public void testJMXRegister() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InstanceNotFoundException{ MBeanServer mockMbs = EasyMock.createNiceMock(MBeanServer.class); Field field = testClass.getClass().getDeclaredField("mbs"); field.setAccessible(true); field.set(testClass, mockMbs); expect(mockConfig.getPoolName()).andReturn(null).anyTimes(); ObjectInstance mockInstance = EasyMock.createNiceMock(ObjectInstance.class); expect(mockMbs.isRegistered((ObjectName)anyObject())).andReturn(false).anyTimes(); expect(mockMbs.registerMBean(anyObject(), (ObjectName)anyObject())).andReturn(mockInstance).once().andThrow(new InstanceAlreadyExistsException()).once(); replay(mockMbs, mockInstance, mockConfig); testClass.registerUnregisterJMX(true); verify(mockMbs); }
@Test public void testJMXUnregisterNoName() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InstanceNotFoundException{ MBeanServer mockMbs = EasyMock.createNiceMock(MBeanServer.class); Field field = testClass.getClass().getDeclaredField("mbs"); field.setAccessible(true); field.set(testClass, mockMbs); expect(mockConfig.getPoolName()).andReturn(null).anyTimes(); ObjectInstance mockInstance = EasyMock.createNiceMock(ObjectInstance.class); reset(mockMbs, mockInstance, mockConfig); expect(mockMbs.isRegistered((ObjectName)anyObject())).andReturn(true).anyTimes(); mockMbs.unregisterMBean((ObjectName)anyObject()); replay(mockMbs, mockInstance, mockConfig); testClass.registerUnregisterJMX(false); verify(mockMbs); }
@Test public void testJMXRegisterWithName() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException{ MBeanServer mockMbs = EasyMock.createNiceMock(MBeanServer.class); Field field = testClass.getClass().getDeclaredField("mbs"); field.setAccessible(true); field.set(testClass, mockMbs); expect(mockConfig.getPoolName()).andReturn("poolName").anyTimes(); ObjectInstance mockInstance = EasyMock.createNiceMock(ObjectInstance.class); expect(mockMbs.isRegistered((ObjectName)anyObject())).andReturn(false).anyTimes(); expect(mockMbs.registerMBean(anyObject(), (ObjectName)anyObject())).andReturn(mockInstance).once().andThrow(new InstanceAlreadyExistsException()).once(); replay(mockMbs, mockInstance, mockConfig); testClass.registerUnregisterJMX(true); verify(mockMbs); }
@Test public void testJMXUnRegisterWithName() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InstanceNotFoundException{ MBeanServer mockMbs = EasyMock.createNiceMock(MBeanServer.class); Field field = testClass.getClass().getDeclaredField("mbs"); field.setAccessible(true); field.set(testClass, mockMbs); field = testClass.getClass().getDeclaredField("config"); field.setAccessible(true); field.set(testClass, mockConfig); expect(mockConfig.getPoolName()).andReturn("poolName").anyTimes(); ObjectInstance mockInstance = EasyMock.createNiceMock(ObjectInstance.class); expect(mockMbs.isRegistered((ObjectName)anyObject())).andReturn(true).anyTimes(); mockMbs.unregisterMBean((ObjectName)anyObject()); expectLastCall().times(2).andThrow(new InstanceNotFoundException()).once(); replay(mockMbs, mockInstance, mockConfig); testClass.registerUnregisterJMX(false); testClass.registerUnregisterJMX(false); verify(mockMbs); }