001package io.ebean.test; 002 003import io.ebeaninternal.api.SpiLogger; 004 005import java.util.List; 006 007/** 008 * Provides access to the messages logged to <code>io.ebean.SQL</code>. 009 * <p> 010 * This is here to allow easy access to the SQL that was executed during testing and 011 * if desired we can use that to perform asserts in tests. 012 * </p> 013 * <pre>{@code 014 * 015 * // start capturing SQL log messages 016 * LoggedSql.start(); 017 * 018 * List<Customer> customers = 019 * Customer.find.where() 020 * .name.ilike("rob%") 021 * .findList(); 022 * 023 * assertNotNull(customers); 024 * 025 * // perform an insert 026 * new Product("ad", "asd").save() 027 * 028 * 029 * // return the captured SQL log messages 030 * // since LoggedSql.start() 031 * List<String> sql = LoggedSql.stop(); 032 * 033 * assertThat(sql).hasSize(2); 034 * assertThat(sql.get(0)).contains("from customer"); 035 * assertThat(sql.get(1)).contains("into product"); 036 * 037 * }</pre> 038 */ 039public class LoggedSql { 040 041 private static CaptureLogger sqlLogger; 042 043 /** 044 * Internal use - register the logger for <code>io.ebean.SQL</code>. 045 */ 046 static SpiLogger register(SpiLogger logger) { 047 if (sqlLogger == null) { 048 sqlLogger = new CaptureLogger(logger); 049 } 050 return sqlLogger; 051 } 052 053 /** 054 * Start the capture of the <code>io.ebean.SQL</code> messages. 055 */ 056 public static List<String> start() { 057 return sqlLogger.start(); 058 } 059 060 /** 061 * Stop the capture of the <code>io.ebean.SQL</code> messages and return the messages/sql 062 * that was captured since the call to start(). 063 */ 064 public static List<String> stop() { 065 return sqlLogger.stop(); 066 } 067 068 /** 069 * Collect and return the messages/sql that was captured since the call to start() or collect(). 070 * <p> 071 * Unlike stop() collection of messages will continue. 072 * </p> 073 */ 074 public static List<String> collect() { 075 return sqlLogger.collect(); 076 } 077 078}