0001: /*
0002: * $Id: SQLUnitMockDatabase.java,v 1.21 2006/01/07 02:27:23 spal Exp $
0003: * $Source: /cvsroot/sqlunit/sqlunit/test/java/mock/SQLUnitMockDatabase.java,v $
0004: * SQLUnit - a test harness for unit testing database stored procedures.
0005: * Copyright (C) 2003 The SQLUnit Team
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License
0009: * as published by the Free Software Foundation; either version 2
0010: * of the License, or (at your option) any later version.
0011: *
0012: * This program is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015: * GNU General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * along with this program; if not, write to the Free Software
0019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0020: */
0021: package net.sourceforge.sqlunit.test.mock;
0022:
0023: import net.sourceforge.sqlunit.test.Dictionary;
0024:
0025: import com.mockrunner.mock.jdbc.MockBlob;
0026: import com.mockrunner.mock.jdbc.MockClob;
0027: import com.mockrunner.mock.jdbc.MockResultSet;
0028:
0029: import org.apache.log4j.Logger;
0030:
0031: import java.io.BufferedReader;
0032: import java.io.ByteArrayOutputStream;
0033: import java.io.FileInputStream;
0034: import java.io.InputStream;
0035: import java.io.InputStreamReader;
0036: import java.math.BigDecimal;
0037: import java.sql.Statement;
0038: import java.sql.Types;
0039:
0040: /**
0041: * Mock database to supply results.
0042: * @author Sujit Pal (spal@users.sourceforge.net)
0043: * @version $Revision: 1.21 $
0044: */
0045: public class SQLUnitMockDatabase extends AbstractMockDatabase {
0046:
0047: private static final Logger LOG = Logger
0048: .getLogger(SQLUnitMockDatabase.class);
0049:
0050: // constants
0051: private static final int BYTES_IN_KB = 1024;
0052: private static final int FOUR_RESULTSETS = 4;
0053: private static final int TOPIC_TYPE_ID = 3;
0054: private static final int TOPIC_ID1 = 1;
0055: private static final int TOPIC_ID2 = 1000;
0056: private static final int LOCALE_ID = 1;
0057: private static final int ERROR_CODE = 1234;
0058:
0059: private static final int CASE_ZERO = 0;
0060: private static final int CASE_ONE = 1;
0061: private static final int CASE_TWO = 2;
0062: private static final int CASE_THREE = 3;
0063: private static final int CASE_FOUR = 4;
0064:
0065: // misc ids
0066: private static final int BOND_ID = 7;
0067: private static final int SUPERMAN_ID = 2;
0068: private static final int SPIDERMAN_ID = 1;
0069: private static final int BATMAN_ID = 3;
0070: private static final float NUMERIC_VALUE = 300.12f;
0071: private static final float DECIMAL_VALUE = 301.13f;
0072: private static final String DATABASE_NAME = "mockdatabase";
0073:
0074: // used with the foreach tag testing methods.
0075: private static int foreachUpdateCount = 0;
0076: private static boolean inInsert1Row = false;
0077: private static boolean inInsert2Row = false;
0078:
0079: /**
0080: * A simple method returning a single resultset with a single int column.
0081: * @param index the result set id.
0082: * @return a single MockResult with a single row and single column.
0083: */
0084: public final MockResultSet aSimpleResult(final Integer index) {
0085: if (index == null) {
0086: return null;
0087: }
0088: int rsid = index.intValue();
0089: switch (rsid) {
0090: case 0:
0091: return MockResultSetUtils.buildZerothResultSet(1);
0092: case 1:
0093: MockResultSet mrs = new MockResultSet("simpleResult:1");
0094: mrs
0095: .setResultSetMetaData(MockResultSetUtils
0096: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0097: "col1", Types.INTEGER) }));
0098: mrs.addRow(new Object[] { new Integer(1) });
0099: LOG.debug("simpleResult:1:" + mrs.toString());
0100: return mrs;
0101: default:
0102: return null;
0103: }
0104: }
0105:
0106: /**
0107: * A simple method returning a single resultset with a single int column.
0108: * @param index the result set id.
0109: * @return a single MockResult with a single row and single column.
0110: */
0111: public final MockResultSet anotherSimpleResult(final Integer index) {
0112: if (index == null) {
0113: return null;
0114: }
0115: int rsid = index.intValue();
0116: switch (rsid) {
0117: case 0:
0118: return MockResultSetUtils.buildZerothResultSet(1);
0119: case 1:
0120: MockResultSet mrs = new MockResultSet(
0121: "anotherSimpleResult:1");
0122: mrs
0123: .setResultSetMetaData(MockResultSetUtils
0124: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0125: "col1", Types.INTEGER) }));
0126: mrs.addRow(new Object[] { new Integer(2) });
0127: LOG.debug("anotherSimpleResult:1:" + mrs.toString());
0128: return mrs;
0129: default:
0130: return null;
0131: }
0132: }
0133:
0134: /**
0135: * A method that returns the string "another" as a single column in
0136: * a single row in a single resultset.
0137: * @param index the result set id.
0138: * @return a single MockResultSet with single row and single column.
0139: */
0140: public final MockResultSet anotherInResultSet(final Integer index) {
0141: if (index == null) {
0142: return null;
0143: }
0144: int rsid = index.intValue();
0145: switch (rsid) {
0146: case 0:
0147: return MockResultSetUtils.buildZerothResultSet(1);
0148: case 1:
0149: MockResultSet mrs = new MockResultSet(
0150: "anotherInResultSet:1");
0151: mrs
0152: .setResultSetMetaData(MockResultSetUtils
0153: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0154: "another", Types.VARCHAR) }));
0155: mrs.addRow(new Object[] { new String("another") });
0156: LOG.debug("anotherInResultSet:1:" + mrs.toString());
0157: return mrs;
0158: default:
0159: return null;
0160: }
0161: }
0162:
0163: /**
0164: * A simple method returning a single resultset with a single int column
0165: * which contains a NULL.
0166: * @param index the result set id.
0167: * @return a single MockResult with a single row and single column.
0168: */
0169: public final MockResultSet aSimpleNullResult(final Integer index) {
0170: if (index == null) {
0171: return null;
0172: }
0173: int rsid = index.intValue();
0174: switch (rsid) {
0175: case 0:
0176: return MockResultSetUtils.buildZerothResultSet(1);
0177: case 1:
0178: MockResultSet mrs = new MockResultSet(
0179: "anotherSimpleResult:1");
0180: mrs
0181: .setResultSetMetaData(MockResultSetUtils
0182: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0183: "col1", Types.INTEGER) }));
0184: mrs.addRow(new Object[] { null });
0185: LOG.debug("anotherSimpleResult:1:" + mrs.toString());
0186: return mrs;
0187: default:
0188: return null;
0189: }
0190: }
0191:
0192: /**
0193: * Returns a MockResult with a single row and a single column named 'foo'
0194: * containing the String value 'foo'.
0195: * @param index the result set id.
0196: * @return a single MockResult with a single row and single column.
0197: */
0198: public final MockResultSet aSimpleStringResult(final Integer index) {
0199: if (index == null) {
0200: return null;
0201: }
0202: int rsid = index.intValue();
0203: switch (rsid) {
0204: case 0:
0205: return MockResultSetUtils.buildZerothResultSet(1);
0206: case 1:
0207: MockResultSet mrs = new MockResultSet("foo:1");
0208: mrs
0209: .setResultSetMetaData(MockResultSetUtils
0210: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0211: "foo", Types.VARCHAR) }));
0212: mrs.addRow(new Object[] { "foo" });
0213: LOG.debug("foo:1:" + mrs.toString());
0214: return mrs;
0215: default:
0216: return null;
0217: }
0218: }
0219:
0220: /**
0221: * Returns a single resultset with multiple columns in each row. This
0222: * is used to test the functionality of the assert-resultset-equals
0223: * assertion.
0224: * @param index the result set id.
0225: * @return a single MockResult with two rows and three columns.
0226: */
0227: public final MockResultSet aSimpleMultiColResult(final Integer index) {
0228: if (index == null) {
0229: return null;
0230: }
0231: int rsid = index.intValue();
0232: switch (rsid) {
0233: case 0:
0234: return MockResultSetUtils.buildZerothResultSet(1);
0235: case 1:
0236: MockResultSet mrs = new MockResultSet(
0237: "aSimpleMultiColResult:1");
0238: mrs
0239: .setResultSetMetaData(MockResultSetUtils
0240: .buildMetaData(new ColumnMetaData[] {
0241: new ColumnMetaData("col1",
0242: Types.INTEGER),
0243: new ColumnMetaData("col2",
0244: Types.VARCHAR),
0245: new ColumnMetaData("col3",
0246: Types.INTEGER) }));
0247: mrs.addRow(new Object[] { new Integer(1),
0248: new String("Larry"), new Integer(31) });
0249: mrs.addRow(new Object[] { new Integer(2),
0250: new String("Curly"), new Integer(32) });
0251: LOG.debug("aSimpleMultiColResult:1:" + mrs.toString());
0252: return mrs;
0253: default:
0254: return null;
0255: }
0256: }
0257:
0258: /**
0259: * A method returning multiple result sets. Each call returns a single
0260: * result set, but different values of index will return different
0261: * result sets. Mimics a stored procedure call with multiple result
0262: * sets returned, such as found in Sybase T-SQL.
0263: * @param index the result set id.
0264: * @return a single MockResultSet per index call.
0265: */
0266: public final MockResultSet multipleResults(final Integer index) {
0267: if (index == null) {
0268: return null;
0269: }
0270: int rsid = index.intValue();
0271: switch (rsid) {
0272: case CASE_ZERO:
0273: return MockResultSetUtils
0274: .buildZerothResultSet(FOUR_RESULTSETS);
0275: case CASE_ONE:
0276: MockResultSet mrs1 = new MockResultSet("multipleResults:1");
0277: mrs1
0278: .setResultSetMetaData(MockResultSetUtils
0279: .buildMetaData(new ColumnMetaData[] {
0280: new ColumnMetaData("topicId",
0281: Types.INTEGER),
0282: new ColumnMetaData("topic",
0283: Types.VARCHAR),
0284: new ColumnMetaData("topicTypeId",
0285: Types.INTEGER) }));
0286: mrs1.addRow(new Object[] { new Integer(1),
0287: new String("Junk Topics"),
0288: new Integer(TOPIC_TYPE_ID) });
0289: LOG.debug("multipleResults:1:" + mrs1.toString());
0290: return mrs1;
0291: case CASE_TWO:
0292: return new MockResultSet("multipleResults:2");
0293: case CASE_THREE:
0294: return new MockResultSet("multipleResults:3");
0295: case CASE_FOUR:
0296: MockResultSet mrs4 = new MockResultSet("multipleResults:4");
0297: mrs4
0298: .setResultSetMetaData(MockResultSetUtils
0299: .buildMetaData(new ColumnMetaData[] {
0300: new ColumnMetaData("topicId",
0301: Types.INTEGER),
0302: new ColumnMetaData(
0303: "ancestorTopicId",
0304: Types.INTEGER),
0305: new ColumnMetaData("topic",
0306: Types.VARCHAR),
0307: new ColumnMetaData("localeId",
0308: Types.INTEGER),
0309: new ColumnMetaData("description",
0310: Types.VARCHAR) }));
0311: mrs4.addRow(new Object[] { new Integer(TOPIC_ID1),
0312: new Integer(TOPIC_ID1),
0313: new String("Unused Topics"),
0314: new Integer(LOCALE_ID), new String("Unused") });
0315: mrs4.addRow(new Object[] { new Integer(TOPIC_ID1),
0316: new Integer(TOPIC_ID2),
0317: new String("Deprecated Topics"),
0318: new Integer(LOCALE_ID), new String("Deprecated") });
0319: LOG.debug("multipleResults:4:" + mrs4.toString());
0320: return mrs4;
0321: default:
0322: return null;
0323: }
0324: }
0325:
0326: /**
0327: * Another method returning multiple result sets. This is similar to
0328: * the multipleResults() call, but returns different datatypes
0329: * than that one.
0330: * @param index the result set id.
0331: * @return a single MockResultSet per index call.
0332: */
0333: public final MockResultSet anotherMultipleResultsDifferentTypes(
0334: final Integer index) {
0335: if (index == null) {
0336: return null;
0337: }
0338: int rsid = index.intValue();
0339: switch (rsid) {
0340: case CASE_ZERO:
0341: return MockResultSetUtils
0342: .buildZerothResultSet(FOUR_RESULTSETS);
0343: case CASE_ONE:
0344: MockResultSet mrs1 = new MockResultSet(
0345: "anotherMultipleResultsDifferentTypes:1");
0346: mrs1.setResultSetMetaData(MockResultSetUtils
0347: .buildMetaData(new ColumnMetaData[] {
0348: new ColumnMetaData("topicId",
0349: Types.SMALLINT),
0350: new ColumnMetaData("topic", Types.CHAR),
0351: new ColumnMetaData("topicTypeId",
0352: Types.SMALLINT) }));
0353: mrs1
0354: .addRow(new Object[] {
0355: new Short((new Integer(1)).shortValue()),
0356: new String("Junk Topics"),
0357: new Short((new Integer(TOPIC_TYPE_ID))
0358: .shortValue()) });
0359: LOG.debug("anotherMultipleResultsDifferentTypes:1:"
0360: + mrs1.toString());
0361: return mrs1;
0362: case CASE_TWO:
0363: return new MockResultSet(
0364: "anotherMultipleResultsDifferentTypes:2");
0365: case CASE_THREE:
0366: return new MockResultSet(
0367: "anotherMultipleResultsDifferentTypes:3");
0368: case CASE_FOUR:
0369: MockResultSet mrs4 = new MockResultSet(
0370: "anotherMultipleResultsDifferentTypes:4");
0371: mrs4.setResultSetMetaData(MockResultSetUtils
0372: .buildMetaData(new ColumnMetaData[] {
0373: new ColumnMetaData("topicId",
0374: Types.SMALLINT),
0375: new ColumnMetaData("ancestorTopicId",
0376: Types.SMALLINT),
0377: new ColumnMetaData("topic", Types.CHAR),
0378: new ColumnMetaData("localeId",
0379: Types.SMALLINT),
0380: new ColumnMetaData("description",
0381: Types.CHAR) }));
0382: mrs4.addRow(new Object[] {
0383: new Short((new Integer(TOPIC_ID1)).shortValue()),
0384: new Short((new Integer(TOPIC_ID1)).shortValue()),
0385: new String("Unused Topics"),
0386: new Short((new Integer(LOCALE_ID)).shortValue()),
0387: new String("Unused") });
0388: mrs4.addRow(new Object[] {
0389: new Short((new Integer(TOPIC_ID1)).shortValue()),
0390: new Short((new Integer(TOPIC_ID2)).shortValue()),
0391: new String("Deprecated Topics"),
0392: new Short((new Integer(LOCALE_ID)).shortValue()),
0393: new String("Deprecated") });
0394: LOG.debug("anotherMultipleResultsDifferentTypes:4:"
0395: + mrs4.toString());
0396: return mrs4;
0397: default:
0398: return null;
0399: }
0400: }
0401:
0402: /**
0403: * A method returning a return code only.
0404: * @param index the result set id.
0405: * @return a MockResult.
0406: */
0407: public final MockResultSet simpleReturnCode(final Integer index) {
0408: if (index == null) {
0409: return null;
0410: }
0411: int rsid = index.intValue();
0412: switch (rsid) {
0413: case -1:
0414: return MockResultSetUtils.buildScalarOutParam("100");
0415: case 0:
0416: return MockResultSetUtils.buildZerothResultSet(0);
0417: default:
0418: return null;
0419: }
0420: }
0421:
0422: /**
0423: * A method that is designed to throw an SQLException. Since the framework
0424: * does not allow us to throw an SQLException directly, we will wrap this
0425: * in a ResultSet with type = Integer.MAX_INT.
0426: * @param index the result set id.
0427: * @return a MockResultSet at index 1 containing an embedded SQLException.
0428: */
0429: public final MockResultSet exceptionResult(final Integer index) {
0430: if (index == null) {
0431: return null;
0432: }
0433: int rsid = index.intValue();
0434: switch (rsid) {
0435: case 0:
0436: return MockResultSetUtils.buildZerothResultSet(1);
0437: case 1:
0438: return MockResultSetUtils.buildSQLExceptionResultSet(
0439: ERROR_CODE, "Test Exception");
0440: default:
0441: return null;
0442: }
0443: }
0444:
0445: /**
0446: * Returns an Oracle style REF CURSOR in the first outparam element.
0447: * @param index the result set id.
0448: * @return a MockResultSet at the specified index.
0449: */
0450: public final MockResultSet refCursorReturn(final Integer index) {
0451: if (index == null) {
0452: return null;
0453: }
0454: int rsid = index.intValue();
0455: switch (rsid) {
0456: case -1:
0457: MockResultSet mrs = new MockResultSet("refCursorReturn:-1");
0458: mrs
0459: .setResultSetMetaData(MockResultSetUtils
0460: .buildMetaData(new ColumnMetaData[] {
0461: new ColumnMetaData("agentId",
0462: Types.INTEGER),
0463: new ColumnMetaData("name",
0464: Types.VARCHAR),
0465: new ColumnMetaData("drink",
0466: Types.VARCHAR) }));
0467: mrs.addRow(new Object[] { new Integer(BOND_ID),
0468: new String("James Bond"), new String("Martini") });
0469: return mrs;
0470: case 0:
0471: return MockResultSetUtils.buildZerothResultSet(0);
0472: default:
0473: return null;
0474: }
0475: }
0476:
0477: /**
0478: * Returns a result code and a single resultset.
0479: * @param index the result set id.
0480: * @return a MockResultSet at the specified index.
0481: */
0482: public final MockResultSet resultAndOneResultSet(final Integer index) {
0483: if (index == null) {
0484: return null;
0485: }
0486: int rsid = index.intValue();
0487: switch (rsid) {
0488: case -1:
0489: return MockResultSetUtils.buildScalarOutParam("143");
0490: case 0:
0491: return MockResultSetUtils.buildZerothResultSet(1);
0492: case 1:
0493: MockResultSet mrs = new MockResultSet("refCursorReturn:-1");
0494: mrs
0495: .setResultSetMetaData(MockResultSetUtils
0496: .buildMetaData(new ColumnMetaData[] {
0497: new ColumnMetaData("agentId",
0498: Types.INTEGER),
0499: new ColumnMetaData("name",
0500: Types.VARCHAR),
0501: new ColumnMetaData("drink",
0502: Types.VARCHAR) }));
0503: mrs.addRow(new Object[] { new Integer(BOND_ID),
0504: new String("James Bond"), new String("Martini") });
0505: return mrs;
0506: default:
0507: return null;
0508: }
0509: }
0510:
0511: /**
0512: * Returns no return code and no resultset.
0513: * @param index the result set id.
0514: * @return a MockResultSet at the specified index.
0515: */
0516: public final MockResultSet voidReturnAndNoResultSet(
0517: final Integer index) {
0518: if (index == null) {
0519: return null;
0520: }
0521: int rsid = index.intValue();
0522: switch (rsid) {
0523: case 0:
0524: return MockResultSetUtils.buildZerothResultSet(0);
0525: default:
0526: return null;
0527: }
0528: }
0529:
0530: /**
0531: * Returns the result code and the exception. This models the case in
0532: * Sybase ASA where T-SQL allows you to continue and throw a return code
0533: * even after an exception is raised.
0534: * @param index the result set id.
0535: * @return a MockResultSet at the specified index.
0536: */
0537: public final MockResultSet resultAndException(final Integer index) {
0538: if (index == null) {
0539: return null;
0540: }
0541: int rsid = index.intValue();
0542: switch (rsid) {
0543: case -1:
0544: return MockResultSetUtils.buildScalarOutParam("-2001");
0545: case 0:
0546: return MockResultSetUtils.buildZerothResultSet(1);
0547: case 1:
0548: return MockResultSetUtils.buildSQLExceptionResultSet(
0549: ERROR_CODE, "Test Exception");
0550: default:
0551: return null;
0552: }
0553: }
0554:
0555: /**
0556: * Returns an error code and a multi-line exception message. This is
0557: * used to test cases such as Sybase ASA which may return a trailing
0558: * newline.
0559: * @param index the result set id.
0560: * @return a MockResultSet at the specified index.
0561: */
0562: public final MockResultSet resultAndMultilineException(
0563: final Integer index) {
0564: if (index == null) {
0565: return null;
0566: }
0567: int rsid = index.intValue();
0568: switch (rsid) {
0569: case -1:
0570: return MockResultSetUtils.buildScalarOutParam("-2001");
0571: case 0:
0572: return MockResultSetUtils.buildZerothResultSet(1);
0573: case 1:
0574: return MockResultSetUtils
0575: .buildSQLExceptionResultSet(ERROR_CODE,
0576: "Test Exception\nMultiline Exception\n");
0577: default:
0578: return null;
0579: }
0580: }
0581:
0582: /**
0583: * Returns a update count of 1.
0584: * @param index the result set id.
0585: * @return a MockResultSet at the specified index.
0586: */
0587: public final MockResultSet updateCount1(final Integer index) {
0588: if (index == null) {
0589: return null;
0590: }
0591: int rsid = index.intValue();
0592: switch (rsid) {
0593: case 0:
0594: return MockResultSetUtils
0595: .buildZerothResultSetWithUpdateCount(1);
0596: default:
0597: return null;
0598: }
0599: }
0600:
0601: /**
0602: * Returns a update count of 2.
0603: * @param index the result set id.
0604: * @return a MockResultSet at the specified index.
0605: */
0606: public final MockResultSet updateCount2(final Integer index) {
0607: if (index == null) {
0608: return null;
0609: }
0610: int rsid = index.intValue();
0611: switch (rsid) {
0612: case 0:
0613: return MockResultSetUtils
0614: .buildZerothResultSetWithUpdateCount(2);
0615: default:
0616: return null;
0617: }
0618: }
0619:
0620: /**
0621: * Returns an update count of Statement.EXECUTE_FAILED.
0622: * @param index the result set id.
0623: * @return a MockResultSet with an embedded updatecount.
0624: */
0625: public final MockResultSet updateCountExecuteFailed(
0626: final Integer index) {
0627: if (index == null) {
0628: return null;
0629: }
0630: int rsid = index.intValue();
0631: switch (rsid) {
0632: case 0:
0633: return MockResultSetUtils
0634: .buildZerothResultSetWithUpdateCount(Statement.EXECUTE_FAILED);
0635: default:
0636: return null;
0637: }
0638: }
0639:
0640: /**
0641: * Returns a Java object in a single result set.
0642: * @param index the result set id.
0643: * @return a MockResultSet with an embedded Java object.
0644: */
0645: public final MockResultSet bytesInResultSet(final Integer index) {
0646: if (index == null) {
0647: return null;
0648: }
0649: int rsid = index.intValue();
0650: switch (rsid) {
0651: case 0:
0652: return MockResultSetUtils.buildZerothResultSet(1);
0653: case 1:
0654: MockResultSet mrs = new MockResultSet("bytesResultSet:1");
0655: mrs
0656: .setResultSetMetaData(MockResultSetUtils
0657: .buildMetaData(new ColumnMetaData[] {
0658: new ColumnMetaData("id",
0659: Types.INTEGER),
0660: new ColumnMetaData("lobcol",
0661: Types.BINARY) }));
0662: Dictionary stooges = new Dictionary();
0663: stooges.setEntry("1", "Larry");
0664: stooges.setEntry("2", "Curly");
0665: stooges.setEntry("3", "Moe");
0666: stooges.writeTo("/tmp/Dictionary.ser");
0667: mrs.addRow(new Object[] { new Integer(1), stooges });
0668: LOG.debug("bytesResultSet:1:" + mrs.toString());
0669: return mrs;
0670: default:
0671: return null;
0672: }
0673: }
0674:
0675: /**
0676: * Returns an inputstream in a single result set.
0677: * @param index the result set id.
0678: * @return a MockResultSet with an embedded InputStream.
0679: */
0680: public final MockResultSet lobInResultSet(final Integer index) {
0681: if (index == null) {
0682: return null;
0683: }
0684: int rsid = index.intValue();
0685: switch (rsid) {
0686: case 0:
0687: return MockResultSetUtils.buildZerothResultSet(1);
0688: case 1:
0689: MockResultSet mrs = new MockResultSet("lobResultSet:1");
0690: mrs.setResultSetMetaData(MockResultSetUtils
0691: .buildMetaData(new ColumnMetaData[] {
0692: new ColumnMetaData("id", Types.INTEGER),
0693: new ColumnMetaData("lobcol",
0694: Types.LONGVARBINARY) }));
0695: Dictionary stooges = new Dictionary();
0696: stooges.setEntry("1", "Larry");
0697: stooges.setEntry("2", "Curly");
0698: stooges.setEntry("3", "Moe");
0699: stooges.writeTo("/tmp/Dictionary.ser");
0700: try {
0701: mrs.addRow(new Object[] { new Integer(1),
0702: new FileInputStream("/tmp/Dictionary.ser") });
0703: } catch (Exception e) {
0704: // :NOTE: will never happen
0705: }
0706: LOG.debug("lobResultSet:1:" + mrs.toString());
0707: return mrs;
0708: default:
0709: return null;
0710: }
0711: }
0712:
0713: /**
0714: * Returns a canned XML string from an included file. The test for this
0715: * is to compare the XML returned from the method with that in the
0716: * included file.
0717: * @param index the result set id.
0718: * @return a MockResultSet object.
0719: */
0720: public final MockResultSet xmlResultSet(final Integer index) {
0721: if (index == null) {
0722: return null;
0723: }
0724: int rsid = index.intValue();
0725: switch (rsid) {
0726: case 0:
0727: return MockResultSetUtils.buildZerothResultSet(1);
0728: case 1:
0729: StringBuffer buf = new StringBuffer();
0730: try {
0731: BufferedReader reader = new BufferedReader(
0732: new InputStreamReader(new FileInputStream(
0733: "test/mock/xmltest.out")));
0734: String line;
0735: while ((line = reader.readLine()) != null) {
0736: buf.append(line).append("\n");
0737: }
0738: reader.close();
0739: } catch (Exception e) {
0740: // :NOTE: will never happen
0741: }
0742: MockResultSet mrs = new MockResultSet("xmlResult:1");
0743: mrs
0744: .setResultSetMetaData(MockResultSetUtils
0745: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0746: "xmlclob", Types.LONGVARCHAR) }));
0747: mrs.addRow(new Object[] { buf.toString() });
0748: LOG.debug("xmlResult:1:" + mrs.toString());
0749: return mrs;
0750: default:
0751: return null;
0752: }
0753: }
0754:
0755: /**
0756: * Returns a CLOB datatype from the same canned XML file as the method
0757: * above, but specifies this as a CLOB data type.
0758: * @param index the result set id.
0759: * @return a MockResultSet object.
0760: */
0761: public final MockResultSet clobResult(final Integer index) {
0762: if (index == null) {
0763: return null;
0764: }
0765: int rsid = index.intValue();
0766: switch (rsid) {
0767: case 0:
0768: return MockResultSetUtils.buildZerothResultSet(1);
0769: case 1:
0770: StringBuffer buf = new StringBuffer();
0771: try {
0772: BufferedReader reader = new BufferedReader(
0773: new InputStreamReader(new FileInputStream(
0774: "test/mock/xmltest.out")));
0775: String line;
0776: while ((line = reader.readLine()) != null) {
0777: buf.append(line).append("\n");
0778: }
0779: reader.close();
0780: } catch (Exception e) {
0781: // :NOTE: will never happen
0782: }
0783: MockResultSet mrs = new MockResultSet("clobResult:1");
0784: mrs
0785: .setResultSetMetaData(MockResultSetUtils
0786: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0787: "xmlclob", Types.CLOB) }));
0788: MockClob clob = new MockClob(buf.toString());
0789: mrs.addRow(new Object[] { clob });
0790: LOG.debug("clobResult:1:" + mrs.toString());
0791: return mrs;
0792: default:
0793: return null;
0794: }
0795: }
0796:
0797: /**
0798: * Returns a BLOB datatype which is the contents of SQLUnit.class.
0799: * @param index the resultset id.
0800: * @return a MockResultSet.
0801: */
0802: public final MockResultSet blobResult(final Integer index) {
0803: if (index == null) {
0804: return null;
0805: }
0806: int rsid = index.intValue();
0807: switch (rsid) {
0808: case 0:
0809: return MockResultSetUtils.buildZerothResultSet(1);
0810: case 1:
0811: // build a Dictionary.ser file in /tmp
0812: Dictionary stooges = new Dictionary();
0813: stooges.setEntry("1", "Larry");
0814: stooges.setEntry("2", "Curly");
0815: stooges.setEntry("3", "Moe");
0816: stooges.writeTo("/tmp/Dictionary.ser");
0817: ByteArrayOutputStream bos = new ByteArrayOutputStream();
0818: try {
0819: InputStream istream = new FileInputStream(
0820: "/tmp/Dictionary.ser");
0821: byte[] buf = new byte[BYTES_IN_KB];
0822: int len = 0;
0823: while ((len = istream.read(buf, 0, BYTES_IN_KB)) != -1) {
0824: bos.write(buf, 0, len);
0825: }
0826: istream.close();
0827: } catch (Exception e) {
0828: // :NOTE: will never happen
0829: }
0830: MockResultSet mrs = new MockResultSet("blobResult:1");
0831: mrs
0832: .setResultSetMetaData(MockResultSetUtils
0833: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0834: "bytecode", Types.BLOB) }));
0835: MockBlob blob = new MockBlob(bos.toByteArray());
0836: mrs.addRow(new Object[] { blob });
0837: LOG.debug("blobResult:1:" + mrs.toString());
0838: return mrs;
0839: default:
0840: return null;
0841: }
0842: }
0843:
0844: /**
0845: * Returns a JAVA_OBJECT which is the same as that serialized in Dictionary.
0846: * @param index the result set id.
0847: * @return a ResultSet.
0848: */
0849: public final MockResultSet javaObjectResult(final Integer index) {
0850: if (index == null) {
0851: return null;
0852: }
0853: int rsid = index.intValue();
0854: switch (rsid) {
0855: case 0:
0856: return MockResultSetUtils.buildZerothResultSet(1);
0857: case 1:
0858: // build a Dictionary.ser file in /tmp
0859: Dictionary stooges = new Dictionary();
0860: stooges.setEntry("1", "Larry");
0861: stooges.setEntry("2", "Curly");
0862: stooges.setEntry("3", "Moe");
0863: stooges.writeTo("/tmp/Dictionary.ser");
0864: ByteArrayOutputStream bos = new ByteArrayOutputStream();
0865: try {
0866: InputStream istream = new FileInputStream(
0867: "/tmp/Dictionary.ser");
0868: byte[] buf = new byte[BYTES_IN_KB];
0869: int len = 0;
0870: while ((len = istream.read(buf, 0, BYTES_IN_KB)) != -1) {
0871: bos.write(buf, 0, len);
0872: }
0873: istream.close();
0874: } catch (Exception e) {
0875: // :NOTE: will never happen
0876: }
0877: MockResultSet mrs = new MockResultSet("blobResult:1");
0878: mrs
0879: .setResultSetMetaData(MockResultSetUtils
0880: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0881: "myobj", Types.JAVA_OBJECT) }));
0882: mrs.addRow(new Object[] { stooges });
0883: LOG.debug("javaObjectResult:1:" + mrs.toString());
0884: return mrs;
0885: default:
0886: return null;
0887: }
0888: }
0889:
0890: /**
0891: * Method to test the foreach tag. This method is meant to be used with
0892: * the insert[12]Row() and returnCountOfRows() methods. Side effect of
0893: * this method is to zero out the static variable foreachUpdateCount.
0894: * @param index the result set id.
0895: * @return an update count.
0896: */
0897: public final MockResultSet deleteRows(final Integer index) {
0898: if (index == null) {
0899: return null;
0900: }
0901: int rsid = index.intValue();
0902: switch (rsid) {
0903: case 0:
0904: foreachUpdateCount = 0;
0905: inInsert1Row = false;
0906: inInsert2Row = false;
0907: return MockResultSetUtils
0908: .buildZerothResultSetWithUpdateCount(0);
0909: default:
0910: return null;
0911: }
0912: }
0913:
0914: /**
0915: * Method to test the foreach tag. This method is meant to be used
0916: * with the deleteRows(), insert2Row() and returnCountOfRowsInserted()
0917: * methods.
0918: * @param index the result set id.
0919: * @return an updatecount.
0920: */
0921: public final MockResultSet insert1Row(final Integer index) {
0922: if (index == null) {
0923: return null;
0924: }
0925: int rsid = index.intValue();
0926: switch (rsid) {
0927: case 0:
0928: if (!inInsert1Row) {
0929: foreachUpdateCount = foreachUpdateCount + 1;
0930: inInsert1Row = true;
0931: }
0932: return MockResultSetUtils
0933: .buildZerothResultSetWithUpdateCount(1);
0934: default:
0935: return null;
0936: }
0937: }
0938:
0939: /**
0940: * Method to test the foreach tag. This method is meant to be used
0941: * with the deleteRows(), insert1Row() and returnCountOfRowsInserted()
0942: * methods.
0943: * @param index the result set id.
0944: * @return an updatecount.
0945: */
0946: public final MockResultSet insert2Row(final Integer index) {
0947: if (index == null) {
0948: return null;
0949: }
0950: int rsid = index.intValue();
0951: switch (rsid) {
0952: case 0:
0953: if (!inInsert2Row) {
0954: foreachUpdateCount = foreachUpdateCount + 2;
0955: inInsert2Row = true;
0956: }
0957: return MockResultSetUtils
0958: .buildZerothResultSetWithUpdateCount(2);
0959: default:
0960: return null;
0961: }
0962: }
0963:
0964: /**
0965: * Method to test the foreach tag. This method is meant to be used
0966: * with the deleteRows(), insert1Row() and insert2Row() methods.
0967: * @param index the result set id.
0968: * @return a count of the number of rows inserted.
0969: */
0970: public final MockResultSet returnCountOfRowsInserted(
0971: final Integer index) {
0972: if (index == null) {
0973: return null;
0974: }
0975: int rsid = index.intValue();
0976: switch (rsid) {
0977: case 0:
0978: return MockResultSetUtils.buildZerothResultSet(1);
0979: case 1:
0980: MockResultSet mrs = new MockResultSet("returnCount:1");
0981: mrs
0982: .setResultSetMetaData(MockResultSetUtils
0983: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
0984: "col1", Types.INTEGER) }));
0985: // it comes into case 0 twice for the update, first to
0986: // calculate the number of rows, and second to calculate
0987: // the updatecount, so we need to halve it here.
0988: mrs
0989: .addRow(new Object[] { new Integer(
0990: foreachUpdateCount) });
0991: foreachUpdateCount = 0;
0992: inInsert1Row = false;
0993: inInsert2Row = false;
0994: LOG.debug("returnCount:1:" + mrs.toString());
0995: return mrs;
0996: default:
0997: return null;
0998: }
0999: }
1000:
1001: /**
1002: * Returns a resultset which is sorted "naturally", ie by ascending
1003: * order of columns as they appear in the resultset.
1004: * @param index the result set id.
1005: * @return a resultset.
1006: */
1007: public final MockResultSet super HeroesSortedNaturally(
1008: final Integer index) {
1009: if (index == null) {
1010: return null;
1011: }
1012: int rsid = index.intValue();
1013: switch (rsid) {
1014: case 0:
1015: return MockResultSetUtils.buildZerothResultSet(1);
1016: case 1:
1017: MockResultSet mrs = new MockResultSet("superHeroes:natural");
1018: mrs
1019: .setResultSetMetaData(MockResultSetUtils
1020: .buildMetaData(new ColumnMetaData[] {
1021: new ColumnMetaData("id",
1022: Types.INTEGER),
1023: new ColumnMetaData("lname",
1024: Types.VARCHAR),
1025: new ColumnMetaData("fname",
1026: Types.VARCHAR),
1027: new ColumnMetaData("alias",
1028: Types.VARCHAR) }));
1029: mrs.addRow(new Object[] { new Integer(SPIDERMAN_ID),
1030: "Parker", "Peter", "Spiderman" });
1031: mrs.addRow(new Object[] { new Integer(SUPERMAN_ID), "Kent",
1032: "Clark", "Superman" });
1033: mrs.addRow(new Object[] { new Integer(BATMAN_ID), "Wayne",
1034: "Bruce", "Batman" });
1035: LOG.debug("superHeroes:natural:" + mrs.toString());
1036: return mrs;
1037: default:
1038: return null;
1039: }
1040: }
1041:
1042: /**
1043: * Same as superHeroesSortedNaturally() but rows are returned sorted
1044: * by last name (lname).
1045: * @param index the result set id.
1046: * @return a resultset.
1047: */
1048: public final MockResultSet super HeroesSortedByAliasAndName(
1049: final Integer index) {
1050: if (index == null) {
1051: return null;
1052: }
1053: int rsid = index.intValue();
1054: switch (rsid) {
1055: case 0:
1056: return MockResultSetUtils.buildZerothResultSet(1);
1057: case 1:
1058: MockResultSet mrs = new MockResultSet(
1059: "superHeroes:byAliasNme");
1060: mrs
1061: .setResultSetMetaData(MockResultSetUtils
1062: .buildMetaData(new ColumnMetaData[] {
1063: new ColumnMetaData("id",
1064: Types.INTEGER),
1065: new ColumnMetaData("lname",
1066: Types.VARCHAR),
1067: new ColumnMetaData("fname",
1068: Types.VARCHAR),
1069: new ColumnMetaData("alias",
1070: Types.VARCHAR) }));
1071: mrs.addRow(new Object[] { new Integer(BATMAN_ID), "Wayne",
1072: "Bruce", "Batman" });
1073: mrs.addRow(new Object[] { new Integer(SPIDERMAN_ID),
1074: "Parker", "Peter", "Spiderman" });
1075: mrs.addRow(new Object[] { new Integer(SUPERMAN_ID), "Kent",
1076: "Clark", "Superman" });
1077: LOG.debug("superHeroes:byAliasAndName:" + mrs.toString());
1078: return mrs;
1079: default:
1080: return null;
1081: }
1082: }
1083:
1084: /**
1085: * Same as superHeroesSortedNaturally() but rows are returned sorted
1086: * by alias.
1087: * @param index the result set id.
1088: * @return a result set.
1089: */
1090: public final MockResultSet super HeroesSortedByAlias(
1091: final Integer index) {
1092: if (index == null) {
1093: return null;
1094: }
1095: int rsid = index.intValue();
1096: switch (rsid) {
1097: case 0:
1098: return MockResultSetUtils.buildZerothResultSet(1);
1099: case 1:
1100: MockResultSet mrs = new MockResultSet("superHeroes:byAlias");
1101: mrs
1102: .setResultSetMetaData(MockResultSetUtils
1103: .buildMetaData(new ColumnMetaData[] {
1104: new ColumnMetaData("id",
1105: Types.INTEGER),
1106: new ColumnMetaData("lname",
1107: Types.VARCHAR),
1108: new ColumnMetaData("fname",
1109: Types.VARCHAR),
1110: new ColumnMetaData("alias",
1111: Types.VARCHAR) }));
1112: mrs.addRow(new Object[] { new Integer(BATMAN_ID), "Wayne",
1113: "Bruce", "Batman" });
1114: mrs.addRow(new Object[] { new Integer(SPIDERMAN_ID),
1115: "Parker", "Peter", "Spiderman" });
1116: mrs.addRow(new Object[] { new Integer(SUPERMAN_ID), "Kent",
1117: "Clark", "Superman" });
1118: LOG.debug("superHeroes:byAlias:" + mrs.toString());
1119: return mrs;
1120: default:
1121: return null;
1122: }
1123: }
1124:
1125: /**
1126: * Same as superHeroesSortedNaturally() but rows are returned sorted
1127: * descending by alias.
1128: * @param index the result set id.
1129: * @return a result set.
1130: */
1131: public final MockResultSet super HeroesSortedByAliasDesc(
1132: final Integer index) {
1133: if (index == null) {
1134: return null;
1135: }
1136: int rsid = index.intValue();
1137: switch (rsid) {
1138: case 0:
1139: return MockResultSetUtils.buildZerothResultSet(1);
1140: case 1:
1141: MockResultSet mrs = new MockResultSet(
1142: "superHeroes:aliasDesc");
1143: mrs
1144: .setResultSetMetaData(MockResultSetUtils
1145: .buildMetaData(new ColumnMetaData[] {
1146: new ColumnMetaData("id",
1147: Types.INTEGER),
1148: new ColumnMetaData("lname",
1149: Types.VARCHAR),
1150: new ColumnMetaData("fname",
1151: Types.VARCHAR),
1152: new ColumnMetaData("alias",
1153: Types.VARCHAR) }));
1154: mrs.addRow(new Object[] { new Integer(SUPERMAN_ID), "Kent",
1155: "Clark", "Superman" });
1156: mrs.addRow(new Object[] { new Integer(SPIDERMAN_ID),
1157: "Parker", "Peter", "Spiderman" });
1158: mrs.addRow(new Object[] { new Integer(BATMAN_ID), "Wayne",
1159: "Bruce", "Batman" });
1160: LOG.debug("superHeroes:byAliasDesc:" + mrs.toString());
1161: return mrs;
1162: default:
1163: return null;
1164: }
1165: }
1166:
1167: /**
1168: * Returns a single row with 2 columns, one NUMERIC and one DECIMAL.
1169: * @param index the result set id.
1170: * @return a result set.
1171: */
1172: public final MockResultSet returnNumericAndDecimalInResultSet(
1173: final Integer index) {
1174: if (index == null) {
1175: return null;
1176: }
1177: int rsid = index.intValue();
1178: switch (rsid) {
1179: case 0:
1180: return MockResultSetUtils.buildZerothResultSet(1);
1181: case 1:
1182: MockResultSet mrs = new MockResultSet("numericAndDecimal:1");
1183: mrs.setResultSetMetaData(MockResultSetUtils
1184: .buildMetaData(new ColumnMetaData[] {
1185: new ColumnMetaData("numeric_f",
1186: Types.NUMERIC),
1187: new ColumnMetaData("decimal_f",
1188: Types.DECIMAL) }));
1189: mrs.addRow(new Object[] { new BigDecimal(NUMERIC_VALUE),
1190: new BigDecimal(DECIMAL_VALUE) });
1191: LOG.debug(mrs.toString());
1192: return mrs;
1193: default:
1194: return null;
1195: }
1196: }
1197:
1198: /**
1199: * Returns a NUMERIC in an OUT Param.
1200: * @param index the result set id.
1201: * @return the result set.
1202: */
1203: public final MockResultSet returnNumericOutParam(final Integer index) {
1204: if (index == null) {
1205: return null;
1206: }
1207: int rsid = index.intValue();
1208: switch (rsid) {
1209: case -1:
1210: return MockResultSetUtils.buildScalarOutParam("5.26");
1211: case 0:
1212: return MockResultSetUtils.buildZerothResultSet(0);
1213: default:
1214: return null;
1215: }
1216: }
1217:
1218: /**
1219: * Returns a canned NUMERIC value in an INOUT Param. The INOUT param
1220: * will always contain 300.01 when it is returned from this method.
1221: * @param index the result set id.
1222: * @return the result set.
1223: */
1224: public final MockResultSet incrementNumericAndReturn(
1225: final Integer index) {
1226: if (index == null) {
1227: return null;
1228: }
1229: int rsid = index.intValue();
1230: switch (rsid) {
1231: case -1:
1232: return MockResultSetUtils.buildScalarOutParam("300.01");
1233: case 0:
1234: return MockResultSetUtils.buildZerothResultSet(0);
1235: default:
1236: return null;
1237: }
1238: }
1239:
1240: /**
1241: * Same as returnNumericOutParam since underlying datatype is BigDecimal
1242: * in both cases, and the scalar is built based on actual datatype.
1243: * @param index the result set id.
1244: * @return the result set.
1245: */
1246: public final MockResultSet returnDecimalOutParam(final Integer index) {
1247: return returnNumericOutParam(index);
1248: }
1249:
1250: /**
1251: * Same as incrementNumericAndReturn1 since the underlying datatype is
1252: * BigDecimal in both cases, and the scalar is built based on actual
1253: * datatype.
1254: * @param index the result set id.
1255: * @return the result set.
1256: */
1257: public final MockResultSet incrementDecimalAndReturn(
1258: final Integer index) {
1259: return incrementNumericAndReturn(index);
1260: }
1261:
1262: /**
1263: * Takes no parameters and returns the name of the database. Used in
1264: * testing the functionality of the func tag.
1265: * @param index the result set id.
1266: * @return the result set.
1267: */
1268: public final MockResultSet selectDatabase(final Integer index) {
1269: if (index == null) {
1270: return null;
1271: }
1272: int rsid = index.intValue();
1273: switch (rsid) {
1274: case 0:
1275: return MockResultSetUtils.buildZerothResultSet(1);
1276: case 1:
1277: MockResultSet mrs = new MockResultSet("selectDatabase");
1278: mrs
1279: .setResultSetMetaData(MockResultSetUtils
1280: .buildMetaData(new ColumnMetaData[] { new ColumnMetaData(
1281: "database", Types.VARCHAR) }));
1282: mrs.addRow(new Object[] { new String(DATABASE_NAME) });
1283: LOG.debug(mrs.toString());
1284: return mrs;
1285: default:
1286: return null;
1287: }
1288: }
1289: }
|