KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > sqlcount > AbstractCountTest


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 JavaDoc;
15
16 /**
17  * provides methods to count the number statements.
18  *
19  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
20  * @version $Id: AbstractCountTest.java,v 1.3 2003/11/10 12:55:40 oliverm Exp $
21  */

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 JavaDoc SPY_PROPS_FILE = new File JavaDoc("testsuite-spy.properties");
27
28   /**
29    * sets the spy.properties file name.
30    */

31   protected void setUp() throws Exception JavaDoc
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   /**
40    * start count SQL statements
41    */

42   protected final void resetStmtCount()
43   {
44     stmtCount = CountLogger.getSQLStatementCount();
45   }
46
47   /**
48    * assert that the number of statements issued since the last call of {@link #resetStmtCount()}.
49    * is between two specified numbers.
50    *
51    * @param msg short description of the actions since the last call of {@link #resetStmtCount()}.
52    */

53   protected final void assertStmtCount(String JavaDoc 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   /**
70    * assert that the number of statements issued since the last call of {@link #resetStmtCount()}.
71    * is equal to a specified number.
72    *
73    * @param msg short description of the actions since the last call of {@link #resetStmtCount()}.
74    */

75   protected final void assertStmtCount(String JavaDoc msg, int expected)
76   {
77     assertStmtCount(msg, expected, expected);
78   }
79
80   private void logStmtCount(String JavaDoc msg, int num)
81   {
82     logger.info(msg + ": " + num);
83   }
84
85   protected final void logStmtCount(String JavaDoc msg)
86   {
87     logStmtCount(msg, CountLogger.getSQLStatementCount() - stmtCount);
88   }
89
90   /**
91    * fail ifF the specified PersistenceBroker does not use P6Spy.
92    */

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