001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit;
023:
024: import org.dbunit.database.IDatabaseConnection;
025: import org.dbunit.dataset.IDataSet;
026: import org.dbunit.operation.DatabaseOperation;
027:
028: import org.slf4j.Logger;
029: import org.slf4j.LoggerFactory;
030:
031: /**
032: * Basic implementation of IDatabaseTester.<br>
033: * Implementations of IDatabaseTester may use this class as a starting point.
034: *
035: * @author Andres Almiray <aalmiray@users.sourceforge.net>
036: */
037: public abstract class AbstractDatabaseTester implements IDatabaseTester {
038:
039: /**
040: * Logger for this class
041: */
042: private static final Logger logger = LoggerFactory
043: .getLogger(AbstractDatabaseTester.class);
044:
045: private IDataSet dataSet;
046: private String schema;
047: private DatabaseOperation setUpOperation = DatabaseOperation.CLEAN_INSERT;
048: private DatabaseOperation tearDownOperation = DatabaseOperation.NONE;
049:
050: public AbstractDatabaseTester() {
051: super ();
052: }
053:
054: public void closeConnection(IDatabaseConnection connection)
055: throws Exception {
056: logger.debug("closeConnection(connection={}) - start",
057: connection);
058:
059: connection.close();
060: }
061:
062: public IDataSet getDataSet() {
063: logger.debug("getDataSet() - start");
064:
065: return dataSet;
066: }
067:
068: public void onSetup() throws Exception {
069: logger.debug("onSetup() - start");
070:
071: executeOperation(getSetUpOperation());
072: }
073:
074: public void onTearDown() throws Exception {
075: logger.debug("onTearDown() - start");
076: executeOperation(getTearDownOperation());
077: }
078:
079: public void setDataSet(IDataSet dataSet) {
080: logger.debug("setDataSet(dataSet={}) - start", dataSet);
081:
082: this .dataSet = dataSet;
083: }
084:
085: public void setSchema(String schema) {
086: logger.debug("setSchema(schema={}) - start", schema);
087:
088: this .schema = schema;
089: }
090:
091: public void setSetUpOperation(DatabaseOperation setUpOperation) {
092: logger.debug("setSetUpOperation(setUpOperation={}) - start",
093: setUpOperation);
094:
095: this .setUpOperation = setUpOperation;
096: }
097:
098: public void setTearDownOperation(DatabaseOperation tearDownOperation) {
099: logger.debug(
100: "setTearDownOperation(tearDownOperation={}) - start",
101: tearDownOperation);
102:
103: this .tearDownOperation = tearDownOperation;
104: }
105:
106: /**
107: * Asserts that propertyName is not a null String and has a length greater
108: * than zero.
109: */
110: protected void assertNotNullNorEmpty(String propertyName,
111: String property) {
112: logger
113: .debug(
114: "assertNotNullNorEmpty(propertyName={}, property={}) - start",
115: propertyName, property);
116:
117: assertTrue(propertyName + " is null", property != null);
118: assertTrue("Invalid " + propertyName,
119: property.trim().length() > 0);
120: }
121:
122: /**
123: * Method used to avoid JUnit dependency
124: * @param message message displayed if assertion is false
125: * @param condition condition to be tested
126: */
127: protected void assertTrue(String message, boolean condition) {
128: if (!condition) {
129: throw new AssertionFailedError(message);
130: }
131:
132: }
133:
134: /**
135: * Returs the schema value.
136: */
137: protected String getSchema() {
138: logger.trace("getSchema() - start");
139:
140: return schema;
141: }
142:
143: /**
144: * Returns the DatabaseOperation to call when starting the test.
145: */
146: protected DatabaseOperation getSetUpOperation() {
147: logger.trace("getSetUpOperation() - start");
148:
149: return setUpOperation;
150: }
151:
152: /**
153: * Returns the DatabaseOperation to call when ending the test.
154: */
155: protected DatabaseOperation getTearDownOperation() {
156: logger.trace("getTearDownOperation() - start");
157:
158: return tearDownOperation;
159: }
160:
161: /**
162: * Executes a DatabaseOperation with a IDatabaseConnection supplied by
163: * {@link getConnection()} and the test dataset.
164: */
165: private void executeOperation(DatabaseOperation operation)
166: throws Exception {
167: logger.debug("executeOperation(operation={}) - start",
168: operation);
169:
170: if (operation != DatabaseOperation.NONE) {
171: IDatabaseConnection connection = getConnection();
172: try {
173: operation.execute(connection, getDataSet());
174: } finally {
175: closeConnection(connection);
176: }
177: }
178: }
179:
180: /**
181: * Exception used to avoid JUnit dependency.
182: * @author Felipe Leme
183: *
184: */
185: public static class AssertionFailedError extends Error {
186:
187: private static final long serialVersionUID = 1L;
188:
189: public AssertionFailedError() {
190: }
191:
192: public AssertionFailedError(String message) {
193: super(message);
194: }
195: }
196:
197: }
|