private void init() { if (fileName == null) { throw new IllegalStateException("setLogfile() must be called before init()"); } try { printStream = new PrintStream(new FileOutputStream(fileName, P6SpyOptions.getActiveInstance().getAppend())); } catch (IOException e) { e.printStackTrace(System.err); } }
@Override public void optionChanged(String key, Object oldValue, Object newValue) { if (key.equals(P6SpyOptions.RELOADPROPERTIES)) { reschedule(Boolean.valueOf(newValue.toString()), P6SpyOptions.getActiveInstance().getReloadPropertiesInterval()); } else if (key.equals(P6SpyOptions.RELOADPROPERTIESINTERVAL)) { reschedule(P6SpyOptions.getActiveInstance().getReloadProperties(), (Long) newValue); } }
private static void cleanUp() throws MBeanRegistrationException, InstanceNotFoundException, MalformedObjectNameException { if (instance == null) { return; } for (P6OptionsSource optionsSource : instance.optionsSources) { optionsSource.preDestroy(instance); } if (P6SpyOptions.getActiveInstance().getJmx() // // unregister mbeans (to prevent naming conflicts) && instance.mBeansRegistry != null) { instance.mBeansRegistry.unregisterAllMBeans(P6SpyOptions.getActiveInstance().getJmxPrefix()); } // clean table plz (we need to make sure that all the configured factories will be re-loaded) new DefaultJdbcEventListenerFactory().clearCache(); }
@Before public void setUp() { // make sure to have no modules explicitly loaded by default { System.clearProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST); P6SpyOptions.getActiveInstance().reload(); } }
@Test public void testSetPropertyDiscartedOnAutoReload() throws Exception { // precondition assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); // value modification P6SpyOptions.getActiveInstance().setStackTrace(true); assertTrue(P6SpyOptions.getActiveInstance().getStackTrace()); // no explicit props reload, just modify timestamp and wait till autoreload happens FileUtils.touch(new File(System.getProperty(SpyDotProperties.OPTIONS_FILE_PROPERTY))); Thread.sleep(2000); // jmx value modification discarted assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); }
@After public void after() { // reset formatting setting P6SpyOptions.getActiveInstance().setCustomLogMessageFormat(null); }
@Test public void testImplicitlyDisabledLogCategories() { // let's explicitly remove P6LogFactory (by not including it to list) { System.setProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST, ""); P6SpyOptions.getActiveInstance().reload(); } try { assertDefaultDisabledLogCategories(); } catch(IOException e) { e.printStackTrace(); Assert.fail(); } // cleanup - make sure go back to default modules { System.getProperties().remove(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST); P6SpyOptions.getActiveInstance().reload(); } }
/** * Please note, when modifying this one to check * {@link P6TestOptionsReload#testJmxSetPropertyDiscartedOnExplicitJmxReload()} as well. * * @throws Exception */ @Test public void testSetPropertyDiscartedOnExplicitReload() throws Exception { // precondition assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); // value modification P6SpyOptions.getActiveInstance().setStackTrace(true); assertTrue(P6SpyOptions.getActiveInstance().getStackTrace()); // props reload P6SpyOptions.getActiveInstance().reload(); // value modification discarted assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); }
@Test public void testAutoReloadLifecycle() throws Exception { // precondition assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); // value modification P6SpyOptions.getActiveInstance().setStackTrace(true); assertTrue(P6SpyOptions.getActiveInstance().getStackTrace()); // disable auto reload P6SpyOptions.getActiveInstance().setReloadProperties(false); FileUtils.touch(new File(System.getProperty(SpyDotProperties.OPTIONS_FILE_PROPERTY))); Thread.sleep(2000); // reload didn't happen assertTrue(P6SpyOptions.getActiveInstance().getStackTrace()); // enable auto reload P6SpyOptions.getActiveInstance().setReloadProperties(true); FileUtils.touch(new File(System.getProperty(SpyDotProperties.OPTIONS_FILE_PROPERTY))); Thread.sleep(2000); // reload did happen assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); }
assertEquals(P6TestLogger.class.getName(), P6SpyOptions.getActiveInstance().getAppender()); super.clearLogEntries(); statement.executeQuery(sql); oldAppenderRef = (P6TestLogger) P6SpyOptions.getActiveInstance().getAppenderInstance(); assertNotNull(oldAppenderRef.getLastEntry()); assertTrue(oldAppenderRef.getLastEntry().contains(sql)); P6SpyOptions.getActiveInstance().setAppender(StdoutLogger.class.getName()); assertEquals(StdoutLogger.class.getName(), P6SpyOptions.getActiveInstance().getAppender()); statement.executeQuery(sql); P6SpyOptions.getActiveInstance().setAppender(P6TestLogger.class.getName());
@Test public void formatPreparedStatementWithDollarSign() { String customLogMessageFormat = "%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)\n%(effectiveSqlSingleLine)\n%(sqlSingleLine);\n"; P6SpyOptions.getActiveInstance().setCustomLogMessageFormat(customLogMessageFormat); String logMsg = new CustomLineFormat().formatMessage(0, "1", 1L, "statement", "select value from V$parameter where lower(name)=?", "select value from V$parameter where lower(name)='compatible'", "jdbc:h2:mem:p6spyDSTest"); Assert.assertTrue(logMsg.contains( "select value from V$parameter where lower(name)=?\nselect value from V$parameter where lower(name)='compatible';\n")); }
@Test public void testP6OutageOptionDefaults() { // make sure to have relevant module loaded { System.setProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST, P6OutageFactory.class.getName()); P6SpyOptions.getActiveInstance().reload(); } final P6OutageLoadableOptions opts = P6OutageOptions.getActiveInstance(); Assert.assertNotNull(opts); Assert.assertFalse(opts.getOutageDetection()); Assert.assertEquals(30L, opts.getOutageDetectionInterval()); Assert.assertEquals(30000L, opts.getOutageDetectionIntervalMS()); // cleanup - make sure go back to default modules { System.getProperties().remove(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST); P6SpyOptions.getActiveInstance().reload(); } }
@Test public void testStacktrace() throws SQLException { P6SpyOptions.getActiveInstance().setStackTrace("true"); // perform a query & make sure we get the stack trace String query = "select 'y' from customers"; statement.executeQuery(query); assertTrue(super.getLastLogEntry().contains(query)); assertTrue(super.getLastLogStackTrace().contains("Stack")); // filter on stack trace that will not match super.clearLastLogStackTrace(); P6SpyOptions.getActiveInstance().setStackTraceClass("com.dont.match"); query = "select 'a' from customers"; statement.executeQuery(query); // this will actually match - just the stack trace wont fire assertTrue(super.getLastLogEntry().contains(query)); assertNull(super.getLastLogStackTrace()); super.clearLastLogStackTrace(); P6SpyOptions.getActiveInstance().setStackTraceClass("com.p6spy"); query = "select 'b' from customers"; statement.executeQuery(query); assertTrue(super.getLastLogEntry().contains(query)); assertTrue(super.getLastLogStackTrace().contains("Stack")); }
@Before public void setUpFramework() throws Exception { // clean table plz (we need to make sure that all the configured factories will be re-loaded) new DefaultJdbcEventListenerFactory().clearCache(); Collection<String> driverNames = P6SpyOptions.getActiveInstance().getDriverNames(); String user = P6TestOptions.getActiveInstance().getUser(); String password = P6TestOptions.getActiveInstance().getPassword(); String url = P6TestOptions.getActiveInstance().getUrl(); if (driverNames != null && !driverNames.isEmpty()) { for (String driverName : driverNames) { P6Util.forName(driverName); } } Driver driver = DriverManager.getDriver(url); if (log.isDebugEnabled()) { log.debug("FRAMEWORK USING DRIVER == " + driver.getClass().getName() + " FOR URL " + url); } connection = DriverManager.getConnection(url, user, password).unwrap(ConnectionWrapper.class); P6TestUtil.printAllDrivers(); P6TestUtil.setupTestData(url, user, password); }
@Test public void testIncludeOtherTableShouldLogResultSet() throws SQLException { final String query = "select 'x' from customers"; super.clearLogEntries(); P6SpyOptions.getActiveInstance().setStackTrace("true"); // other table is included => resultset is still logged P6LogOptions.getActiveInstance().setInclude("non_existing_table1"); P6LogOptions.getActiveInstance().setExcludecategories("info,debug,batch"); ResultSet rs = statement.executeQuery(query); while (rs.next()) { @SuppressWarnings("unused") String value = rs.getString(1); } P6LogOptions.getActiveInstance().setInclude(""); assertEquals(0, super.getLogEntriesCount()); }
@Test public void testDisableLogModule() throws SQLException { P6SpyLoadableOptions o = P6SpyOptions.getActiveInstance(); assertNotNull(P6LogOptions.getActiveInstance());
@Test public void testDefaultOptions() { assertP6FactoryClassesEqual(DEFAULT_FACTORIES, P6ModuleManager.getInstance().getFactories()); Assert.assertNotNull(P6SpyOptions.getActiveInstance()); Assert.assertNotNull(P6LogOptions.getActiveInstance()); Assert.assertNull(P6OutageOptions.getActiveInstance()); }
@Test public void testPropertyExposalViaJMX() throws Exception { { final Set<ObjectName> beanNames = jmxClient.getBeanNames(); assertNotNull(beanNames); } { final Boolean filterJmxApi = (Boolean) jmxClient.getAttribute(P6MBeansRegistry.PACKAGE_NAME, P6LogOptions.class.getName(), "Filter"); final Boolean filterApi = P6LogOptions.getActiveInstance().getFilter(); assertNotNull(filterJmxApi); assertEquals(filterApi, filterJmxApi); } { final String moduleListJmxApi = (String) jmxClient.getAttribute(P6MBeansRegistry.PACKAGE_NAME, P6SpyOptions.class.getName(), "Modulelist"); final String moduleListApi = P6SpyOptions.getActiveInstance().getModulelist(); assertNotNull(moduleListJmxApi); assertEquals(moduleListApi, moduleListJmxApi); } { final Boolean outageDetectionJmxApi = (Boolean) jmxClient.getAttribute(P6MBeansRegistry.PACKAGE_NAME, P6OutageOptions.class.getName(), "OutageDetection"); final Boolean outageDetectionApi = P6OutageOptions.getActiveInstance().getOutageDetection(); assertNotNull(outageDetectionJmxApi); assertEquals(outageDetectionApi, outageDetectionJmxApi); } }
@Test public void testP6LogOptionDefaults() { // make sure to have relevant module loaded { System.setProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST, P6LogFactory.class.getName()); P6SpyOptions.getActiveInstance().reload(); } final P6LogLoadableOptions opts = P6LogOptions.getActiveInstance(); Assert.assertNotNull(opts); Assert.assertNull(opts.getSQLExpression()); Assert.assertEquals(0L, opts.getExecutionThreshold()); Assert.assertEquals("info,debug,result,resultset,batch", opts.getExcludecategories()); Assert.assertFalse(opts.getExcludebinary()); Assert.assertTrue(opts.getExcludeCategoriesSet().containsAll( Arrays.asList(DEFAULT_CATEGORIES))); Assert.assertFalse(opts.getFilter()); Assert.assertNull(opts.getIncludeList()); Assert.assertNull(opts.getExcludeList()); Assert.assertNull(opts.getIncludeExcludePattern()); Assert.assertEquals("", opts.getInclude()); Assert.assertEquals("", opts.getExclude()); Assert.assertNull(opts.getSQLExpressionPattern()); }
@Test public void testUnSetSpyAPI() { final P6SpyLoadableOptions opts = P6SpyOptions.getActiveInstance();