@After public void tearDown() throws Exception { LOGGER.debug("Doing tearDown"); // Stops the threaded executor JmxCollector is using to poll MBean attribute if (jmxCollector != null) { jmxCollector.destroy(); } if (rrdDb == null) return; if (!rrdDb.isClosed()) { rrdDb.close(); } String path = rrdDb.getPath(); File rrdFile = new File(path); if (rrdFile.exists()) { boolean status = rrdFile.delete(); if (status) { LOGGER.debug("Successfully deleted rrdFile " + path); } else { LOGGER.debug("Unable to delete rrdFile " + path); } } else { LOGGER.debug("rrdFile " + path + " does not exist - cannot delete"); } }
@Test @Ignore public void testRrdFileCreationWhenRrdFileAlreadyExists() throws Exception { // Set sample rate to 1 sec (default is 60 seconds) so that unit test runs quickly int sampleRate = 1; createJmxCollector("Uptime", JmxCollector.COUNTER_DATA_SOURCE_TYPE, sampleRate); String rrdFilename1 = jmxCollector.getRrdPath(); assertThat(rrdFilename1, is(TEST_DIR + rrdPath)); rrdDb = new RrdDb(rrdFilename1); assertThat(rrdDb, not(nullValue())); assertThat(rrdDb.isClosed(), is(false)); // Attempt to create again LOGGER.debug("Creating JmxCollector again ..."); dataSourceName = "uptime"; rrdPath = dataSourceName + ".rrd"; JmxCollector jmxCollector2 = new JmxCollector(); jmxCollector2.setMbeanName("java.lang:type=Runtime"); jmxCollector2.setMbeanAttributeName("Uptime"); jmxCollector2.setRrdPath(rrdPath); jmxCollector2.setRrdDataSourceName(dataSourceName); jmxCollector2.setRrdDataSourceType(JmxCollector.COUNTER_DATA_SOURCE_TYPE); jmxCollector2.setSampleRate(sampleRate); jmxCollector2.setMetricsDir(TEST_DIR); // Simulates what Spring beans container would do jmxCollector2.configureCollector(); // Verify the 2 JMX Collectors are using the same RRD file String rrdFilename2 = jmxCollector2.getRrdPath(); assertThat(rrdFilename2, is(TEST_DIR + rrdPath)); assertThat(rrdFilename1, equalTo(rrdFilename2)); jmxCollector2.destroy(); }