001: package com.jamonapi.proxy;
002:
003: /** Class that tests the various JAMon proxy classes via the main method */
004: import java.sql.Connection;
005: import java.sql.DriverManager;
006: import java.sql.PreparedStatement;
007: import java.sql.ResultSet;
008: import java.sql.Statement;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import com.jamonapi.FactoryEnabled;
013: import com.jamonapi.Monitor;
014: import com.jamonapi.MonitorFactory;
015:
016: public class MonProxyTester {
017:
018: private static void printDebug(String title, Object[][] data) {
019: System.out.println("\n***" + title + "***");
020: if (data != null) {
021: for (int i = 0; i < data.length; i++) {
022: System.out.print("row=" + i + " - ");
023: for (int j = 0; j < data[0].length; j++)
024: System.out.print(data[i][j] + ", ");
025:
026: System.out.println();
027: }
028: }
029:
030: }
031:
032: private static ResultSet testStatement(Connection conn)
033: throws Exception {
034:
035: Statement st = conn.createStatement();
036: ResultSet rs = st
037: .executeQuery("select * from SYSTEM_TYPEINFO where LOCAL_TYPE_NAME='INTEGER'");
038: return rs;
039: }
040:
041: private static ResultSet testPreparedStatement(Connection conn)
042: throws Exception {
043: PreparedStatement ps = conn
044: .prepareStatement("select * from SYSTEM_TYPEINFO where LOCAL_TYPE_NAME=?");
045: ps.setString(1, "INTEGER");
046:
047: ResultSet rs = ps.executeQuery();
048: return rs;
049: }
050:
051: private static void throwException(Connection conn) {
052:
053: try {
054: Statement st = conn.createStatement();
055: st
056: .executeQuery("select * from I_DO_NOT_EXIST where LOCAL_TYPE_NAME='INTEGER'");
057: } catch (Exception e) {
058: //typical of code people do in hiding exceptions
059: }
060:
061: }
062:
063: // use extra column here to have different stats for prepared statement reuse and not reuse.
064: private static PreparedStatement getPreparedStatement(
065: Connection conn) throws Exception {
066: PreparedStatement ps = conn
067: .prepareStatement("select *,'PreparedStatement Reuse Query' from SYSTEM_TYPEINFO where LOCAL_TYPE_NAME=?");
068: ps.setString(1, "INTEGER");
069: return ps;
070:
071: }
072:
073: private static void testLoopResultSet(ResultSet rs)
074: throws Exception {
075: while (rs.next()) {
076: rs.getObject(1); // Is SQL the first column is indexed
077: }
078: }
079:
080: private static void testDisplayResultSet(ResultSet rs)
081: throws Exception {
082: while (rs.next()) {
083: System.out.println(rs.getObject(1) + " ");
084: }
085: }
086:
087: /** Test classes for interface heirarchy checking */
088:
089: private interface Tag0 {
090:
091: }
092:
093: private interface Tag1 {
094:
095: }
096:
097: private interface Base0 extends Tag0 {
098:
099: }
100:
101: private interface Base1 extends Base0 {
102:
103: }
104:
105: private interface Base2 extends Base1, Tag0, Base0 /* note Base0 twice in heirarchy */{
106:
107: }
108:
109: private static class MyClass0 implements Base2 {
110:
111: }
112:
113: private static class MyClass1 extends MyClass0 implements Tag1 {
114:
115: }
116:
117: private static void testInterfaces(Class cls) {
118:
119: System.out.println("\nInterfaceHeirarchy for " + cls);
120: Class[] ifaces = MonProxyFactory.getInterfaces(cls);
121:
122: int len = (ifaces == null) ? 0 : ifaces.length;
123: for (int i = 0; i < len; i++)
124: System.out.println(i + ") " + ifaces[i]);
125:
126: }
127:
128: private static void mainTestMethod(String name, Connection conn,
129: int times, Params params, FactoryEnabled mf)
130: throws Exception {
131: testCounter++;
132:
133: System.out.println("\n\n\n" + testCounter + ") " + name + "** "
134: + params);
135: Monitor mon = mf.start(testCounter + ") " + name + " ** "
136: + params.toString());
137: Monitor monTotal = mf.start("totalTime");
138: MonProxyFactory.enableResultSet(params.isResultSetEnabled);
139: MonProxyFactory.enableInterface(params.isInterfaceEnabled);
140: MonProxyFactory.enableSQLDetail(params.isSQLDetailEnabled);
141: MonProxyFactory.enableSQLSummary(params.isSQLSummaryEnabled);
142: MonProxyFactory
143: .enableExceptionSummary(params.isExceptionSummaryEnabled);
144: MonProxyFactory
145: .enableExceptionDetail(params.isExceptionDetailEnabled);
146: MonProxyFactory.enable(params.isEnabled);
147:
148: PreparedStatement ps = getPreparedStatement(conn);
149: for (int i = 0; i < times; i++) {
150: ps.executeQuery();
151: testLoopResultSet(testStatement(conn));
152: testLoopResultSet(testPreparedStatement(conn));
153: }
154:
155: throwException(conn);
156:
157: monTotal.stop();
158: String message = name + " execution time: "
159: + mon.stop().getLastValue();
160: System.out.println(message);
161:
162: printDebug(testCounter + ".1) " + "sqlBuffer", MonProxyFactory
163: .getSQLDetail());
164: printDebug(testCounter + ".2) " + "exceptionsBuffer",
165: MonProxyFactory.getExceptionDetail());
166: printDebug(testCounter + ".3) " + "jamon data - " + message,
167: MonitorFactory.getRootMonitor().getBasicData());
168:
169: MonProxyFactory.resetSQLDetail();
170: MonProxyFactory.resetExceptionDetail();
171: MonitorFactory.reset();
172:
173: }
174:
175: private static int testCounter = 0;
176:
177: private static void testEquals() {
178:
179: Tag0 noProxyObj = new MyClass0();
180: Tag0 proxyObj = (Tag0) MonProxyFactory.monitor(noProxyObj);
181: Tag0 proxyObj2 = (Tag0) MonProxyFactory.monitor(noProxyObj);
182: Tag0 proxyObjTwice = (Tag0) MonProxyFactory.monitor(proxyObj);
183:
184: System.out.println("\nAll of the following should equal true");
185: System.out.println("proxy.equals(noproxy)="
186: + proxyObj.equals(noProxyObj));
187: System.out.println("proxy.equals(proxy)="
188: + proxyObj.equals(proxyObj));
189: System.out.println("proxy1.equals(proxy2)="
190: + proxyObj.equals(proxyObj2));
191: System.out.println("proxy2.equals(proxy1)="
192: + proxyObj2.equals(proxyObj));
193: System.out.println("proxyObjTwice.equals(proxy1)="
194: + proxyObj.equals(proxyObjTwice));
195: System.out.println("noproxy.equals(noproxy)="
196: + noProxyObj.equals(noProxyObj));
197:
198: System.out.println("\nAll of the following will equal false");
199: System.out
200: .println("noproxy.equals(proxy)="
201: + noProxyObj.equals(proxyObj)
202: + " - will be false as the nonproxied class will not no that a proxied class is the same.");
203: System.out.println("proxyObjTwice.equals(null)="
204: + proxyObj.equals(null));
205:
206: }
207:
208: public static void main(String[] args) throws Exception {
209:
210: int times = 2000;
211: Params params = new Params();
212: FactoryEnabled mf = new FactoryEnabled();
213:
214: Class.forName("org.hsqldb.jdbcDriver");
215: Connection conn = DriverManager.getConnection("jdbc:hsqldb:.",
216: "sa", "");
217:
218: mainTestMethod("Non monitored connection first time", conn,
219: times, params, mf);
220: mainTestMethod("Non monitored connection second time", conn,
221: times, params, mf);
222:
223: MonProxyFactory.enableAll(false);
224: // returns regular connection
225: conn = MonProxyFactory.monitor(conn);
226: mainTestMethod(
227: "MonProxyFactory disabled at creation of Connection (should be fast as regular connection)",
228: conn, times, params, mf);
229:
230: // returns monitored connection
231: MonProxyFactory.enableAll(true);
232: conn = MonProxyFactory.monitor(conn);
233:
234: mainTestMethod("MonProxyFactory defaults first time", conn,
235: times, params, mf);
236: mainTestMethod("MonProxyFactory defaults second time", conn,
237: times, params, mf);
238:
239: params = new Params();
240: params.isResultSetEnabled = true;
241: mainTestMethod("MonProxyFactory all enabled", conn, times,
242: params, mf);
243:
244: params = new Params();
245: params.isEnabled = false;
246: mainTestMethod(
247: "MonProxyFactory disabled (uses monitored connection but disabled)",
248: conn, times, params, mf);
249:
250: params = new Params();
251: params.isInterfaceEnabled = false;
252: mainTestMethod("interface disabled", conn, times, params, mf);
253:
254: params = new Params();
255: params.isInterfaceEnabled = false;
256: params.isSQLDetailEnabled = false;
257: mainTestMethod("sql detail and interface disabled", conn,
258: times, params, mf);
259:
260: params = new Params();
261: List list = new ArrayList();
262: list.add("SYSTEM_TYPEINFO");// could be tables or other keywords
263: list.add("LOCAL_TYPE_NAME");
264: MonProxyFactory.setMatchStrings(list);
265: mainTestMethod("MonProxy defaults with keyword match", conn,
266: times, params, mf);
267: MonProxyFactory.setMatchStrings(null);
268:
269: System.out.println("\n\n**** ResultSets *****");
270: testDisplayResultSet(testStatement(conn));
271: testDisplayResultSet(testPreparedStatement(conn));
272:
273: conn.close();
274:
275: printDebug("totals", mf.getRootMonitor().getBasicData());
276:
277: // Check to ensure all classes return all interfaces they implement
278: testInterfaces(new MyClass0().getClass());
279: testInterfaces(new MyClass1().getClass());
280: testInterfaces(null);
281: testInterfaces(String[].class);
282: Object obj = new MyClass1();
283: testInterfaces(obj.getClass());
284:
285: testEquals();
286:
287: }
288:
289: }
|