001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.test;
020:
021: import java.util.List;
022: import java.util.ArrayList;
023: import java.util.Map;
024:
025: import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
026: import org.apache.openjpa.lib.jdbc.JDBCEvent;
027: import org.apache.openjpa.lib.jdbc.JDBCListener;
028:
029: /**
030: * Base class for tests that need to check generated SQL.
031: *
032: * @author Patrick Linskey
033: */
034: public abstract class SQLListenerTestCase extends SingleEMFTestCase {
035:
036: protected List<String> sql = new ArrayList<String>();
037: protected int sqlCount;
038:
039: @Override
040: public void setUp(Object... props) {
041: Object[] copy = new Object[props.length + 2];
042: System.arraycopy(props, 0, copy, 0, props.length);
043: copy[copy.length - 2] = "openjpa.jdbc.JDBCListeners";
044: copy[copy.length - 1] = new JDBCListener[] { new Listener() };
045: super .setUp(copy);
046: }
047:
048: /**
049: * Confirm that the specified SQL has been executed.
050: *
051: * @param sqlExp the SQL expression. E.g., "SELECT FOO .*"
052: */
053: public void assertSQL(String sqlExp) {
054: for (String statement : sql) {
055: if (statement.matches(sqlExp))
056: return;
057: }
058:
059: fail("Expected regular expression <" + sqlExp + "> to have"
060: + " existed in SQL statements: " + sql);
061: }
062:
063: /**
064: * Confirm that the specified SQL has not been executed.
065: *
066: * @param sqlExp the SQL expression. E.g., "SELECT BADCOLUMN .*"
067: */
068: public void assertNotSQL(String sqlExp) {
069: boolean failed = false;
070:
071: for (String statement : sql) {
072: if (statement.matches(sqlExp))
073: failed = true;
074: }
075:
076: if (failed)
077: fail("Regular expression <"
078: + sqlExp
079: + ">"
080: + " should not have been executed in SQL statements: "
081: + sql);
082: }
083:
084: /**
085: * Confirm that the executed SQL String contains the specified sqlExp.
086: *
087: * @param sqlExp the SQL expression. E.g., "SELECT BADCOLUMN .*"
088: */
089: public void assertContainsSQL(String sqlExp) {
090: for (String statement : sql) {
091: if (statement.contains(sqlExp))
092: return;
093: }
094:
095: fail("Expected regular expression <" + sqlExp + "> to be"
096: + " contained in SQL statements: " + sql);
097: }
098:
099: /**
100: * Gets the number of SQL issued since last reset.
101: */
102: public int getSQLCount() {
103: return sqlCount;
104: }
105:
106: /**
107: * Resets SQL count.
108: * @return number of SQL counted since last reset.
109: */
110: public int resetSQLCount() {
111: int tmp = sqlCount;
112: sqlCount = 0;
113: return tmp;
114: }
115:
116: public class Listener extends AbstractJDBCListener {
117:
118: @Override
119: public void beforeExecuteStatement(JDBCEvent event) {
120: if (event.getSQL() != null && sql != null) {
121: sql.add(event.getSQL());
122: sqlCount++;
123: }
124: }
125: }
126: }
|