mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-23 07:11:55 +00:00
#1402 - Refactor SQL Logging internals (io.ebean.SQL) such that it is easier to capture for testing/asserts
This commit is contained in:
commit
3f8d705439
22 files changed
+305
-131
No files matched your search
@@ -77,6 +77,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiLogManager log() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdownManaged() {
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package io.ebeantest;
|
||||
|
||||
import io.ebeaninternal.api.SpiLogger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Capture the log messages (executed SQL) for testing.
|
||||
*/
|
||||
class CaptureLogger implements SpiLogger {
|
||||
|
||||
private final SpiLogger wrapped;
|
||||
|
||||
private List<String> messages = new ArrayList<>();
|
||||
|
||||
private boolean active;
|
||||
|
||||
CaptureLogger(SpiLogger wrapped) {
|
||||
this.wrapped = wrapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebug() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrace() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String msg) {
|
||||
if (active) {
|
||||
messages.add(msg);
|
||||
}
|
||||
wrapped.debug(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(String msg) {
|
||||
if (active) {
|
||||
messages.add(msg);
|
||||
}
|
||||
wrapped.trace(msg);
|
||||
}
|
||||
|
||||
List<String> start() {
|
||||
this.active = true;
|
||||
return collect();
|
||||
}
|
||||
|
||||
List<String> stop() {
|
||||
this.active = false;
|
||||
return collect();
|
||||
}
|
||||
|
||||
List<String> collect() {
|
||||
List<String> response = messages;
|
||||
messages = new ArrayList<>();
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.ebeantest;
|
||||
|
||||
import io.ebeaninternal.api.SpiLogger;
|
||||
import io.ebeaninternal.api.SpiLoggerFactory;
|
||||
import io.ebeaninternal.server.log.DSpiLogger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Create a logger that captures the SQL and register it for later access in tests.
|
||||
*/
|
||||
public class CapturingLoggerFactory implements SpiLoggerFactory {
|
||||
|
||||
@Override
|
||||
public SpiLogger create(String name) {
|
||||
|
||||
DSpiLogger logger = new DSpiLogger(LoggerFactory.getLogger(name));
|
||||
if (name.equals("io.ebean.SQL")) {
|
||||
return LoggedSql.register(logger);
|
||||
}
|
||||
return logger;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.ebeantest;
|
||||
|
||||
import io.ebeaninternal.api.SpiLogger;
|
||||
import io.ebeaninternal.server.log.DSpiLogger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LoggedSql {
|
||||
|
||||
private static CaptureLogger sqlLogger;
|
||||
|
||||
static SpiLogger register(DSpiLogger logger) {
|
||||
if (sqlLogger == null) {
|
||||
sqlLogger = new CaptureLogger(logger);
|
||||
}
|
||||
return sqlLogger;
|
||||
}
|
||||
|
||||
public static List<String> start() {
|
||||
return sqlLogger.start();
|
||||
}
|
||||
|
||||
public static List<String> stop() {
|
||||
return sqlLogger.stop();
|
||||
}
|
||||
|
||||
public static List<String> collect() {
|
||||
return sqlLogger.collect();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,7 @@
|
||||
package org.ebeantest;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.UnsynchronizedAppenderBase;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import io.ebeantest.LoggedSql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -20,28 +14,11 @@ import java.util.List;
|
||||
*/
|
||||
public class LoggedSqlCollector {
|
||||
|
||||
static Logger logger = (Logger) LoggerFactory.getLogger("io.ebean.SQL");
|
||||
|
||||
private static BasicAppender basicAppender = new BasicAppender();
|
||||
|
||||
static {
|
||||
|
||||
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
basicAppender.setContext(lc);
|
||||
Level level = logger.getEffectiveLevel();
|
||||
if (level.isGreaterOrEqual(Level.INFO)) {
|
||||
logger.setAdditive(false);
|
||||
}
|
||||
logger.addAppender(basicAppender);
|
||||
logger.setLevel(Level.TRACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start collection of the logged SQL statements.
|
||||
*/
|
||||
public static List<String> start() {
|
||||
return basicAppender.collectStart();
|
||||
return LoggedSql.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,54 +26,11 @@ public class LoggedSqlCollector {
|
||||
* the SQL.
|
||||
*/
|
||||
public static List<String> stop() {
|
||||
return basicAppender.collectEnd();
|
||||
return LoggedSql.stop();
|
||||
}
|
||||
|
||||
public static List<String> current() {
|
||||
return basicAppender.collectContinue();
|
||||
return LoggedSql.collect();
|
||||
}
|
||||
|
||||
private static class BasicAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
||||
|
||||
List<String> messages = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
if (started) {
|
||||
messages.add(eventObject.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start collection.
|
||||
*/
|
||||
List<String> collectStart() {
|
||||
List<String> tempMessages = messages;
|
||||
messages = new ArrayList<>();
|
||||
// set started flag
|
||||
start();
|
||||
return tempMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* End collection.
|
||||
*/
|
||||
List<String> collectEnd() {
|
||||
// set stopped state
|
||||
stop();
|
||||
List<String> tempMessages = messages;
|
||||
messages = new ArrayList<>();
|
||||
return tempMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the collected SQL and continue.
|
||||
*/
|
||||
List<String> collectContinue() {
|
||||
List<String> tempMessages = messages;
|
||||
messages = new ArrayList<>();
|
||||
return tempMessages;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
io.ebeantest.CapturingLoggerFactory
|
||||
Reference in new issue
Block a user