private void failOnNonP6TestLoggerUsage() { if (!(P6LogQuery.getLogger() instanceof P6TestLogger)) { throw new IllegalStateException(); } }
private P6TestLogger getP6TestLogger() { failOnNonP6TestLoggerUsage(); return (P6TestLogger) P6LogQuery.getLogger(); } }
private void validateNotSpyEnabled(DataSource ds) throws SQLException { assertNotNull("JNDI data source not found", ds); // get the connection Connection con = ds.getConnection(); if (con.getMetaData().getDatabaseProductName().contains("HSQL")) { con.createStatement().execute("set database sql syntax ora true"); } con.createStatement().execute("select current_date from dual"); assertNull(((P6TestLogger) P6LogQuery.getLogger()).getLastEntry()); } }
private void validateSpyEnabled(DataSource ds) throws SQLException { assertNotNull("JNDI data source not found", ds); // get the connection Connection con = ds.getConnection(); // verify that the connection class is a proxy assertTrue("Connection is not a proxy", AbstractWrapper.isProxy(con.getClass())); if (con.getMetaData().getDatabaseProductName().contains("HSQL")) { con.createStatement().execute("set database sql syntax ora true"); } con.createStatement().execute("select current_date from dual"); assertTrue(((P6TestLogger) P6LogQuery.getLogger()).getLastEntry().indexOf("select current_date") != -1); }
@Test public void testGenericDataSourceWithDriverManager() throws Exception { // Create and bind the real data source // Note: This will get the driver from the DriverManager realDs = new TestBasicDataSource(); realDs.setDriverClassName(driverClass); realDs.setUrl(url); realDs.setUsername(user); realDs.setPassword(password); realDs.setUseDriverManager(true); realDsResource = new Resource("jdbc/realDs", realDs); P6TestUtil.setupTestData(realDs); // get the data source from JNDI DataSource ds = new JndiDataSourceLookup().getDataSource("jdbc/spyDs"); assertNotNull("JNDI data source not found", ds); // get the connection con = ds.getConnection(); // verify that the connection class is a proxy assertTrue("Connection is not a proxy", AbstractWrapper.isProxy(con)); Statement stmt = con.createStatement(); stmt.execute("select 1 from customers"); stmt.close(); assertTrue(((P6TestLogger) P6LogQuery.getLogger()).getLastEntry().indexOf("select 1") != -1); assertEquals("Incorrect number of spy log messages", 1, ((P6TestLogger) P6LogQuery.getLogger()).getLogs().size()); }
@Before public void setup() throws Exception { // make sure to reinit properly new P6TestFramework("ds") { }; user = "sa"; password ="sa"; // please note non-typical DB name // however it seems that the typical one "jdbc:h2:mem:p6spy" // caused authorization exception, see: https://github.com/p6spy/p6spy/issues/76 // in some test execution sequences (on some setups only) // I suspect non-proper cleanup in some of the previous tests causing this error // url = "jdbc:h2:mem:p6spy"; url = "jdbc:h2:mem:p6spyDSTest"; driverClass = "org.h2.Driver"; P6DataSource spyDs = new P6DataSource(); spyDs.setRealDataSource("jdbc/realDs"); spyDsResource = new Resource("jdbc/spyDs", spyDs); ((P6TestLogger) P6LogQuery.getLogger()).clearLogs(); }
@Before public void setUp() throws Exception { // make sure to reinit properly new P6TestFramework("ds") { }; jndiResources = new ArrayList<Resource>(); // create the real data sources and bind to jndi JdbcDataSource realDs1 = new JdbcDataSource(); realDs1.setUser("sa"); realDs1.setURL("jdbc:h2:mem:multids1"); jndiResources.add(new Resource("jdbc/realDs1", realDs1)); JdbcDataSource realDs2 = new JdbcDataSource(); realDs2.setUser("sa"); realDs2.setURL("jdbc:h2:mem:multids2"); jndiResources.add(new Resource("jdbc/realDs2", realDs2)); JDBCDataSource realDs3 = new JDBCDataSource(); realDs3.setUser("sa"); realDs3.setPassword(""); realDs3.setUrl("jdbc:hsqldb:mem:multids3"); jndiResources.add(new Resource("jdbc/realDs3", realDs3)); // create the spy wrapper data sources and bind to jndi P6DataSource spyDs1 = new P6DataSource(); spyDs1.setRealDataSource("jdbc/realDs1"); jndiResources.add(new Resource("jdbc/spyDs1", spyDs1)); P6DataSource spyDs3 = new P6DataSource(); spyDs3.setRealDataSource("jdbc/realDs3"); jndiResources.add(new Resource("jdbc/spyDs3", spyDs3)); // reset captured log messages ((P6TestLogger) P6LogQuery.getLogger()).clearLogs(); }
@Test public void testCanSetCustomLoggingFormat() { ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.p6spy.log-format:test %{connectionId}"); contextRunner.run(context -> { JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").contains(LoggingEventListener.class); assertThat(P6LogQuery.getLogger()).extracting("strategy").extracting("class").contains(CustomLineFormat.class); }); }