001: package org.apache.ojb.broker.sqlcount;
002:
003: import junit.framework.TestCase;
004: import com.p6spy.engine.common.P6SpyProperties;
005: import com.p6spy.engine.spy.P6SpyDriver;
006: import org.apache.ojb.p6spy.CountLogger;
007: import org.apache.ojb.broker.util.logging.Logger;
008: import org.apache.ojb.broker.util.logging.LoggerFactory;
009: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
010: import org.apache.ojb.broker.metadata.MetadataManager;
011: import org.apache.ojb.broker.PBKey;
012: import org.apache.ojb.broker.PersistenceBrokerFactory;
013:
014: import java.io.File;
015:
016: /**
017: * provides methods to count the number statements.
018: *
019: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
020: * @version $Id: AbstractCountTest.java,v 1.3 2003/11/10 12:55:40 oliverm Exp $
021: */
022: public abstract class AbstractCountTest extends TestCase {
023: private int stmtCount;
024: protected final Logger logger = LoggerFactory.getLogger(this
025: .getClass());
026: private static final File SPY_PROPS_FILE = new File(
027: "testsuite-spy.properties");
028:
029: /**
030: * sets the spy.properties file name.
031: */
032: protected void setUp() throws Exception {
033: if (!SPY_PROPS_FILE.exists())
034: fail("Missing file: " + SPY_PROPS_FILE.getAbsolutePath());
035: P6SpyProperties.setSpyProperties(SPY_PROPS_FILE.getName());
036: checkP6spyEnabled(PersistenceBrokerFactory.getDefaultKey());
037: }
038:
039: /**
040: * start count SQL statements
041: */
042: protected final void resetStmtCount() {
043: stmtCount = CountLogger.getSQLStatementCount();
044: }
045:
046: /**
047: * assert that the number of statements issued since the last call of {@link #resetStmtCount()}.
048: * is between two specified numbers.
049: *
050: * @param msg short description of the actions since the last call of {@link #resetStmtCount()}.
051: */
052: protected final void assertStmtCount(String msg, int minExpected,
053: int maxExpected) {
054: int stmtNum = CountLogger.getSQLStatementCount() - stmtCount;
055: if (stmtNum > maxExpected)
056: fail(msg
057: + ": more SQL statements than expected. Expected: "
058: + maxExpected + ", was: " + stmtNum);
059: else if (minExpected > 0 && stmtNum == 0)
060: fail("No SQL statements, maybe CountLogger not enabled?");
061: else if (stmtNum < minExpected)
062: fail(msg
063: + ": less SQL statements than expected (Performance improvement? Please correct test limit)."
064: + " Expected: " + minExpected + ", was: " + stmtNum);
065: else {
066: logStmtCount(msg, stmtNum);
067: }
068: }
069:
070: /**
071: * assert that the number of statements issued since the last call of {@link #resetStmtCount()}.
072: * is equal to a specified number.
073: *
074: * @param msg short description of the actions since the last call of {@link #resetStmtCount()}.
075: */
076: protected final void assertStmtCount(String msg, int expected) {
077: assertStmtCount(msg, expected, expected);
078: }
079:
080: private void logStmtCount(String msg, int num) {
081: logger.info(msg + ": " + num);
082: }
083:
084: protected final void logStmtCount(String msg) {
085: logStmtCount(msg, CountLogger.getSQLStatementCount()
086: - stmtCount);
087: }
088:
089: /**
090: * fail ifF the specified PersistenceBroker does not use P6Spy.
091: */
092: protected final void checkP6spyEnabled(PBKey pbKey) {
093: JdbcConnectionDescriptor conDesc = MetadataManager
094: .getInstance().connectionRepository().getDescriptor(
095: pbKey);
096: if (!P6SpyDriver.class.getName().equals(conDesc.getDriver())) {
097: fail("this test works only with p6spy.\n"
098: + "Please set 'driver="
099: + P6SpyDriver.class.getName()
100: + "' in file repository_database.xml"
101: + " or use ant build property '-DuseP6Spy=true'");
102: }
103: }
104: }
|