1 package org.apache.ojb.broker.sqlcount; 2 3 import junit.framework.TestCase; 4 import com.p6spy.engine.common.P6SpyProperties; 5 import com.p6spy.engine.spy.P6SpyDriver; 6 import org.apache.ojb.p6spy.CountLogger; 7 import org.apache.ojb.broker.util.logging.Logger; 8 import org.apache.ojb.broker.util.logging.LoggerFactory; 9 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 10 import org.apache.ojb.broker.metadata.MetadataManager; 11 import org.apache.ojb.broker.PBKey; 12 import org.apache.ojb.broker.PersistenceBrokerFactory; 13 14 import java.io.File ; 15 16 22 public abstract class AbstractCountTest extends TestCase 23 { 24 private int stmtCount; 25 protected final Logger logger = LoggerFactory.getLogger(this.getClass()); 26 private static final File SPY_PROPS_FILE = new File ("testsuite-spy.properties"); 27 28 31 protected void setUp() throws Exception 32 { 33 if (!SPY_PROPS_FILE.exists()) 34 fail("Missing file: " + SPY_PROPS_FILE.getAbsolutePath()); 35 P6SpyProperties.setSpyProperties(SPY_PROPS_FILE.getName()); 36 checkP6spyEnabled(PersistenceBrokerFactory.getDefaultKey()); 37 } 38 39 42 protected final void resetStmtCount() 43 { 44 stmtCount = CountLogger.getSQLStatementCount(); 45 } 46 47 53 protected final void assertStmtCount(String msg, int minExpected, int maxExpected) 54 { 55 int stmtNum = CountLogger.getSQLStatementCount() - stmtCount; 56 if (stmtNum > maxExpected) 57 fail(msg + ": more SQL statements than expected. Expected: " + maxExpected + ", was: " + stmtNum); 58 else if (minExpected > 0 && stmtNum == 0) 59 fail("No SQL statements, maybe CountLogger not enabled?"); 60 else if (stmtNum < minExpected) 61 fail(msg + ": less SQL statements than expected (Performance improvement? Please correct test limit)." 62 + " Expected: " + minExpected + ", was: " + stmtNum); 63 else 64 { 65 logStmtCount(msg, stmtNum); 66 } 67 } 68 69 75 protected final void assertStmtCount(String msg, int expected) 76 { 77 assertStmtCount(msg, expected, expected); 78 } 79 80 private void logStmtCount(String msg, int num) 81 { 82 logger.info(msg + ": " + num); 83 } 84 85 protected final void logStmtCount(String msg) 86 { 87 logStmtCount(msg, CountLogger.getSQLStatementCount() - stmtCount); 88 } 89 90 93 protected final void checkP6spyEnabled(PBKey pbKey) 94 { 95 JdbcConnectionDescriptor conDesc 96 = MetadataManager.getInstance().connectionRepository().getDescriptor(pbKey); 97 if (!P6SpyDriver.class.getName().equals(conDesc.getDriver())) 98 { 99 fail("this test works only with p6spy.\n" + 100 "Please set 'driver=" + P6SpyDriver.class.getName() + "' in file repository_database.xml" + 101 " or use ant build property '-DuseP6Spy=true'"); 102 } 103 } 104 } 105 | Popular Tags |