Source Code Cross Referenced for Assertions.java in  » Testing » sqlunit » net » sourceforge » sqlunit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » sqlunit » net.sourceforge.sqlunit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * $Id: Assertions.java,v 1.21 2006/06/10 20:29:14 spal Exp $
0003:         * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/Assertions.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;
0022:
0023:        import java.lang.reflect.InvocationTargetException;
0024:        import java.lang.reflect.Method;
0025:        import java.util.HashMap;
0026:        import java.util.Iterator;
0027:        import java.util.List;
0028:        import java.util.Map;
0029:
0030:        import junit.framework.Assert;
0031:        import junit.framework.AssertionFailedError;
0032:        import net.sourceforge.sqlunit.beans.BatchDatabaseResult;
0033:        import net.sourceforge.sqlunit.beans.Col;
0034:        import net.sourceforge.sqlunit.beans.DatabaseResult;
0035:        import net.sourceforge.sqlunit.beans.OutParam;
0036:        import net.sourceforge.sqlunit.beans.ResultSetBean;
0037:        import net.sourceforge.sqlunit.beans.Row;
0038:
0039:        import org.apache.log4j.Logger;
0040:
0041:        /**
0042:         * Provides methods to assert various conditions for doing SQLUnit tests.
0043:         * Inspired by the JUnit Assert class.
0044:         * @author Sujit Pal (spal@users.sourceforge.net)
0045:         * @version $Revision: 1.21 $
0046:         */
0047:        public final class Assertions {
0048:
0049:            private static final Logger LOG = Logger
0050:                    .getLogger(Assertions.class);
0051:
0052:            // DatabaseResult assert methods
0053:            private static final String[][] DATABASE_RESULT_ASSERT_MAPPING = {
0054:                    { "none", "assertNone" },
0055:                    { "equal", "assertEqual" },
0056:                    { "not-equal", "assertNotEqual" },
0057:                    { "exception-matches", "assertExceptionMatches" },
0058:                    { "not-exception", "assertNotException" },
0059:                    { "number-outparams-equal", "assertNumberOfOutParamsEqual" },
0060:                    { "outparams-equal", "assertOutParamsEqual" },
0061:                    { "updatecounts-equal", "assertUpdateCountsEqual" },
0062:                    { "number-resultsets-equal",
0063:                            "assertNumberOfResultSetsEqual" },
0064:                    { "resultsets-equal", "assertResultSetsEqual" },
0065:                    { "resultset-types-equal", "assertResultSetTypesEqual" },
0066:                    { "resultset-values-equal", "assertResultSetValuesEqual" },
0067:                    { "fail-with-failure", "assertFailWithFailure" } };
0068:            // BatchDatabaseResult assert methods
0069:            private static final String[][] BATCH_DATABASE_RESULT_ASSERT_MAPPING = {
0070:                    { "none", "assertNone" }, { "equal", "assertEqual" },
0071:                    { "not-equal", "assertNotEqual" },
0072:                    { "failed-at-equal", "assertFailedAtEqual" },
0073:                    { "expected-count-equal", "assertExpectedCountEqual" },
0074:                    { "update-count-equal", "assertUpdateCountEqual" },
0075:                    { "fail-with-failure", "assertFailWithFailure" } };
0076:            // parameters for the DatabaseResult methods
0077:            private static Class[] databaseResultParamClasses = { String.class,
0078:                    DatabaseResult.class, DatabaseResult.class, List.class };
0079:            // parameters for the BatchDatabaseResult methods.
0080:            private static Class[] batchDatabaseResultParamClasses = {
0081:                    String.class, BatchDatabaseResult.class,
0082:                    BatchDatabaseResult.class };
0083:            // column equality enums
0084:            private static final int COL_TYPES_EQUAL = 1;
0085:            private static final int COL_VALUES_EQUAL = 2;
0086:            private static final int COL_TYPE_VALUE_EQUAL = 3;
0087:
0088:            private static Map databaseResultAssertMap = null;
0089:            private static Map batchDatabaseResultAssertMap = null;
0090:
0091:            /**
0092:             * Private Constructor. Assertions cannot be instantiated.
0093:             */
0094:            private Assertions() {
0095:                // cannot be instantiated
0096:            }
0097:
0098:            // -------- these are the methods that get called --------
0099:
0100:            /**
0101:             * Invokes a named assertion (or a comma-separated list of assertions)
0102:             * and applies it to the supplied DatabaseResult objects.
0103:             * @param failureMessage a user-supplied failure message.
0104:             * @param expR the specified DatabaseResult object.
0105:             * @param gotR the actual DatabaseResult object.
0106:             * @param matchPatterns a List of MatchPattern objects, may be null.
0107:             * @param condition a single assertion or a comma-separated list of
0108:             * assertions.
0109:             * @exception SQLUnitException if the assertion failed.
0110:             */
0111:            public static void assertIsTrue(final String failureMessage,
0112:                    final DatabaseResult expR, final DatabaseResult gotR,
0113:                    final List matchPatterns, final String condition)
0114:                    throws SQLUnitException {
0115:                if (databaseResultAssertMap == null) {
0116:                    databaseResultAssertMap = buildMap(DATABASE_RESULT_ASSERT_MAPPING);
0117:                }
0118:                Assertions.invokeAssertion(condition,
0119:                        databaseResultParamClasses, databaseResultAssertMap,
0120:                        new Object[] {
0121:                                (failureMessage == null ? "" : failureMessage),
0122:                                expR, gotR, matchPatterns });
0123:            }
0124:
0125:            /**
0126:             * Invokes a named assertion (or a comma-separated list of assertions)
0127:             * and applies it to the supplied BatchDatabaseResult objects.
0128:             * @param failureMessage a user-supplied failure message.
0129:             * @param expR the specified BatchDatabaseResult object.
0130:             * @param gotR the actual BatchDatabaseResult object.
0131:             * @param condition a single assertion or a comma-separated list of
0132:             * assertions.
0133:             * @exception SQLUnitException if the assertion failed.
0134:             */
0135:            public static void assertIsTrue(final String failureMessage,
0136:                    final BatchDatabaseResult expR,
0137:                    final BatchDatabaseResult gotR, final String condition)
0138:                    throws SQLUnitException {
0139:                if (batchDatabaseResultAssertMap == null) {
0140:                    batchDatabaseResultAssertMap = buildMap(BATCH_DATABASE_RESULT_ASSERT_MAPPING);
0141:                }
0142:                Assertions.invokeAssertion(condition,
0143:                        batchDatabaseResultParamClasses,
0144:                        batchDatabaseResultAssertMap, new Object[] {
0145:                                (failureMessage == null ? "" : failureMessage),
0146:                                expR, gotR });
0147:            }
0148:
0149:            // ------- DatabaseResult asserts -------
0150:
0151:            /**
0152:             * Asserts nothing. This is used to assert that there are no
0153:             * equality or non-equality relationships between the generated
0154:             * and the expected result.
0155:             * @param failureMessage the user-supplied failure message.
0156:             * @param expR the specified DatabaseResult object.
0157:             * @param gotR the actual DatabaseResult object.
0158:             * @param matchPatterns a List of MatchPattern objects, may be null.
0159:             * @exception SQLUnitException if the assertion failed.
0160:             * @sqlunit.assert name="none"
0161:             *  description="Asserts nothing about the generated and the actual
0162:             *  results. This is typically used to disable assertions for the 
0163:             *  particular test."
0164:             *  usage="diff,test"
0165:             */
0166:            private static void assertNone(final String failureMessage,
0167:                    final DatabaseResult expR, DatabaseResult gotR,
0168:                    final List matchPatterns) throws SQLUnitException {
0169:                return;
0170:            }
0171:
0172:            /**
0173:             * Asserts that two DatabaseResult objects are equal, or if a List of
0174:             * MatchPattern objects are supplied, then they match.
0175:             * @param failureMessage the user-supplied failure message.
0176:             * @param expR the specified DatabaseResult object.
0177:             * @param gotR the actual DatabaseResult object.
0178:             * @param matchPatterns a List of MatchPattern objects, may be null.
0179:             * @exception SQLUnitExeeption if the assertion failed.
0180:             * @sqlunit.assert name="equal"
0181:             *  description="Asserts that the two results are equal. This is actually 
0182:             *  a macro assertion, which resolves to a number of subassertions, which 
0183:             *  are asserted serially. This is the default assert if not specified 
0184:             *  in a diff or test tag."
0185:             *  usage="diff,test"
0186:             */
0187:            private static void assertEqual(final String failureMessage,
0188:                    final DatabaseResult expR, final DatabaseResult gotR,
0189:                    final List matchPatterns) throws SQLUnitException {
0190:                String conditions = "exception-matches,"
0191:                        + "number-outparams-equal," + "outparams-equal,"
0192:                        + "updatecounts-equal," + "number-resultsets-equal,"
0193:                        + "resultsets-equal";
0194:                assertIsTrue(failureMessage, expR, gotR, matchPatterns,
0195:                        conditions);
0196:            }
0197:
0198:            /**
0199:             * Asserts that teh two DatabaseResult objects are not equal.
0200:             * @param failureMessage the user-supplied failure message.
0201:             * @param expR the specified DatabaseResult object.
0202:             * @param gotR the actual DatabaseResult object.
0203:             * @param matchPatterns a List of MatchPattern objects, may be null.
0204:             * @exception SQLUnitExeeption if the assertion failed.
0205:             * @sqlunit.assert name="not-equal"
0206:             *  description="Asserts that the two results are not equal. This is an 
0207:             *  inversion of the equals assertion."
0208:             *  usage="diff,test"
0209:             */
0210:            private static void assertNotEqual(final String failureMessage,
0211:                    final DatabaseResult expR, final DatabaseResult gotR,
0212:                    final List matchPatterns) throws SQLUnitException {
0213:                try {
0214:                    assertEqual(failureMessage, expR, gotR, matchPatterns);
0215:                } catch (SQLUnitException e) {
0216:                    return;
0217:                }
0218:                throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0219:                        new String[] { "not-equal", "", expR.toString(),
0220:                                expR.toString(), failureMessage });
0221:            }
0222:
0223:            /**
0224:             * Asserts that both or neither DatabaseResult objects resulted in an
0225:             * exception. If both resulted in an exception, then the exception
0226:             * code and message are matched based on whether they are supplied
0227:             * in the expected result or not.
0228:             * @param failureMessage the user-supplied failure message.
0229:             * @param expR the specified DatabaseResult object.
0230:             * @param gotR the actual DatabaseResult object.
0231:             * @param matchPattern the List of MatchPattern objects, may be null.
0232:             * @exception SQLUnitException if the assertion failed.
0233:             * @sqlunit.assert name="exception-matches"
0234:             *  description="Asserts that the two results have identical exceptions."
0235:             *  usage="diff,test"
0236:             */
0237:            private static void assertExceptionMatches(
0238:                    final String failureMessage, final DatabaseResult expR,
0239:                    final DatabaseResult gotR, final List matchPatterns)
0240:                    throws SQLUnitException {
0241:                if (expR.isException()) {
0242:                    // then gotR must be an exception
0243:                    try {
0244:                        Assert.assertTrue(failureMessage, gotR.isException());
0245:                    } catch (AssertionFailedError e) {
0246:                        throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0247:                                new String[] { "exception-matches",
0248:                                        "(exception != resultset)",
0249:                                        expR.toString(), gotR.toString(),
0250:                                        failureMessage });
0251:                    }
0252:                    // gotR is an exception, check against code if specified
0253:                    if (expR.getException().getErrorCode() != null) {
0254:                        try {
0255:                            Assert.assertEquals(failureMessage, expR
0256:                                    .getException().getErrorCode(), gotR
0257:                                    .getException().getErrorCode());
0258:                        } catch (AssertionFailedError e) {
0259:                            throw new SQLUnitException(
0260:                                    IErrorCodes.ASSERT_FAILED, new String[] {
0261:                                            "exception-matches",
0262:                                            "("
0263:                                                    + expR.getException()
0264:                                                            .getErrorCode()
0265:                                                    + " != "
0266:                                                    + gotR.getException()
0267:                                                            .getErrorCode()
0268:                                                    + " at "
0269:                                                    + "exception.code)",
0270:                                            expR.toString(), gotR.toString(),
0271:                                            failureMessage });
0272:                        }
0273:                    }
0274:                    // if exception message specified, check against that
0275:                    if (expR.getException().getErrorMessage() != null) {
0276:                        try {
0277:                            Assert.assertEquals(failureMessage, expR
0278:                                    .getException().getErrorMessage(), gotR
0279:                                    .getException().getErrorMessage());
0280:                        } catch (AssertionFailedError e) {
0281:                            throw new SQLUnitException(
0282:                                    IErrorCodes.ASSERT_FAILED, new String[] {
0283:                                            "exception-matches",
0284:                                            "("
0285:                                                    + expR.getException()
0286:                                                            .getErrorMessage()
0287:                                                    + " != "
0288:                                                    + gotR.getException()
0289:                                                            .getErrorMessage()
0290:                                                    + " at "
0291:                                                    + "exception.message)",
0292:                                            expR.toString(), gotR.toString(),
0293:                                            failureMessage });
0294:                        }
0295:                    }
0296:                } else {
0297:                    // check to see if the result is an unexpected exception
0298:                    try {
0299:                        Assert.assertTrue(failureMessage, !gotR.isException());
0300:                    } catch (AssertionFailedError e) {
0301:                        throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0302:                                new String[] { "exception-matches",
0303:                                        "(result != exception)",
0304:                                        expR.toString(), gotR.toString(),
0305:                                        failureMessage });
0306:                    }
0307:                }
0308:            }
0309:
0310:            /**
0311:             * Asserts that the result returned is not an exception. This can be 
0312:             * useful to regression test stored procedures after schema changes to
0313:             * make sure nothing broke as a result of the schema change.
0314:             * @param failureMessage the user-supplied failure message.
0315:             * @param expR the specified DatabaseResult object.
0316:             * @param gotR the actual DatabaseResult object.
0317:             * @param matchPattern the List of MatchPattern objects, may be null.
0318:             * @exception SQLUnitException if the assertion failed.
0319:             * @sqlunit.assert name="not-exception"
0320:             *  description="Asserts that the result returned is NOT an exception"
0321:             *  usage="diff,test"
0322:             */
0323:            private static void assertNotException(final String failureMessage,
0324:                    final DatabaseResult expR, final DatabaseResult gotR,
0325:                    final List matchPatterns) throws SQLUnitException {
0326:                if (gotR.isException()) {
0327:                    throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0328:                            new String[] { "not-exception",
0329:                                    "(result == exception)", expR.toString(),
0330:                                    gotR.toString(), failureMessage });
0331:                }
0332:            }
0333:
0334:            /**
0335:             * Asserts that the number of outparams specified in the DatabaseResult
0336:             * expR is equal to that retrieved in DatabaseResult gotR.
0337:             * @param failureMessage the user-supplied failure message.
0338:             * @param expR the specified DatabaseResult object.
0339:             * @param gotR the actual DatabaseResult object.
0340:             * @param matchPatterns the List of MatchPattern objects.
0341:             * @exception SQLUnitException if the assertion fails.
0342:             * @sqlunit.assert name="number-outparams-equal"
0343:             *  description="Asserts that the two results have the same number of
0344:             *  outparam elements."
0345:             *  usage="diff,test"
0346:             */
0347:            private static void assertNumberOfOutParamsEqual(
0348:                    final String failureMessage, final DatabaseResult expR,
0349:                    final DatabaseResult gotR, final List matchPatterns)
0350:                    throws SQLUnitException {
0351:                try {
0352:                    Assert.assertEquals(failureMessage,
0353:                            expR.getOutParams().length,
0354:                            gotR.getOutParams().length);
0355:                } catch (AssertionFailedError e) {
0356:                    throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0357:                            new String[] {
0358:                                    "number-outparams-equal",
0359:                                    "(" + expR.getOutParams().length + " != "
0360:                                            + gotR.getOutParams().length + ")",
0361:                                    expR.toString(), gotR.toString(),
0362:                                    failureMessage });
0363:                }
0364:            }
0365:
0366:            /**
0367:             * Asserts that the outparams in both the DatabaseResult objects are
0368:             * equal.
0369:             * @param failureMessage the user-supplied failure message.
0370:             * @param expR the specified DatabaseResult object.
0371:             * @param gotR the actual DatabaseResult object.
0372:             * @param matchPatterns the List of MatchPattern objects.
0373:             * @exception SQLUnitException if the assertion failed.
0374:             * @sqlunit.assert name="outparams-equal"
0375:             *  description="Asserts that the outparams in both the results are equal."
0376:             *  usage="diff,test"
0377:             */
0378:            private static void assertOutParamsEqual(
0379:                    final String failureMessage, final DatabaseResult expR,
0380:                    final DatabaseResult gotR, final List matchPatterns)
0381:                    throws SQLUnitException {
0382:                if (expR.getOutParams() != null) {
0383:                    OutParam[] expOutParams = expR.getOutParams();
0384:                    OutParam[] gotOutParams = gotR.getOutParams();
0385:                    // the map holds id to outparam for actuals so we can match by id
0386:                    Map outParamIdMap = new HashMap();
0387:                    for (int i = 0; i < gotOutParams.length; i++) {
0388:                        outParamIdMap.put(gotOutParams[i].getId(),
0389:                                gotOutParams[i]);
0390:                    }
0391:                    for (int i = 0; i < expOutParams.length; i++) {
0392:                        OutParam expOutParam = expOutParams[i];
0393:                        OutParam gotOutParam = (OutParam) outParamIdMap
0394:                                .get(expOutParam.getId());
0395:                        if (gotOutParam == null) {
0396:                            throw new AssertionFailedError("outparam["
0397:                                    + (i + 1) + "].id(" + expOutParam.getId()
0398:                                    + ") out of bounds");
0399:                        }
0400:                        try {
0401:                            Assert.assertEquals(failureMessage, expOutParam,
0402:                                    gotOutParam);
0403:                        } catch (AssertionFailedError e) {
0404:                            throw new SQLUnitException(
0405:                                    IErrorCodes.ASSERT_FAILED, new String[] {
0406:                                            "outparams-equal",
0407:                                            "(" + expOutParam.toString()
0408:                                                    + " != "
0409:                                                    + gotOutParam.toString()
0410:                                                    + " at " + "outparams[" + i
0411:                                                    + "])", expR.toString(),
0412:                                            gotR.toString(), failureMessage });
0413:                        }
0414:                    }
0415:                }
0416:            }
0417:
0418:            /**
0419:             * Asserts that the update counts, if available in the expected 
0420:             * DatabaseResult object, is equal to that found in the actual
0421:             * DatabaseResult object.
0422:             * @param failureMessage the user-supplied failure message.
0423:             * @param expR the specified DatabaseResult object.
0424:             * @param gotR the actual DatabaseResult object.
0425:             * @param matchPatterns the List of MatchPattern objects.
0426:             * @exception SQLUnitException if the assertion failed.
0427:             * @sqlunit.assert name="update-counts-equal"
0428:             *  description="Asserts that the update counts in the two results
0429:             *  are equal."
0430:             *  usage="diff,test"
0431:             */
0432:            private static void assertUpdateCountsEqual(
0433:                    final String failureMessage, final DatabaseResult expR,
0434:                    final DatabaseResult gotR, final List matchPatterns)
0435:                    throws SQLUnitException {
0436:                try {
0437:                    Assert.assertEquals(failureMessage, expR.getUpdateCount(),
0438:                            gotR.getUpdateCount());
0439:                } catch (AssertionFailedError e) {
0440:                    throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0441:                            new String[] {
0442:                                    "update-counts-equal",
0443:                                    "(" + expR.getUpdateCount() + " != "
0444:                                            + gotR.getUpdateCount() + ")",
0445:                                    expR.toString(), gotR.toString(),
0446:                                    failureMessage });
0447:                }
0448:            }
0449:
0450:            /**
0451:             * Asserts that the number of resultsets in both the DatabaseResult
0452:             * objects are equal.
0453:             * @param failureMessage the user-supplied failure message.
0454:             * @param expR the specified DatabaseResult object.
0455:             * @param gotR the actual DatabaseResult object.
0456:             * @param matchPatterns the List of MatchPattern objects.
0457:             * @exception SQLUnitException if the assertion failed.
0458:             * @sqlunit.assert name="number-of-resultsets-equal"
0459:             *  description="Asserts that the number of resultsets in the two
0460:             *  results are equal."
0461:             *  usage="diff,test"
0462:             */
0463:            private static void assertNumberOfResultSetsEqual(
0464:                    final String failureMessage, final DatabaseResult expR,
0465:                    final DatabaseResult gotR, final List matchPatterns)
0466:                    throws SQLUnitException {
0467:                try {
0468:                    Assert.assertEquals(failureMessage,
0469:                            expR.getResultSets().length,
0470:                            gotR.getResultSets().length);
0471:                } catch (AssertionFailedError e) {
0472:                    throw new SQLUnitException(
0473:                            IErrorCodes.ASSERT_FAILED,
0474:                            new String[] {
0475:                                    "number-of-resultsets-equal",
0476:                                    "(" + expR.getResultSets().length + " != "
0477:                                            + gotR.getResultSets().length + ")",
0478:                                    expR.toString(), gotR.toString(),
0479:                                    failureMessage });
0480:                }
0481:            }
0482:
0483:            /**
0484:             * Asserts that the ResultSets in the two DatabaseResult objects are
0485:             * equal. If the matchPatterns is not null, then the two objects are
0486:             * matched against the pattern, rather than checked for exact 
0487:             * equality.
0488:             * @param failureMessage the user-supplied failure message.
0489:             * @param expR the specified DatabaseResult object.
0490:             * @param gotR the actual DatabaseResult object.
0491:             * @param matchPatterns the List of MatchPattern objects.
0492:             * @exception SQLUnitException if the assertion failed.
0493:             * @sqlunit.assert name="resultsets-equal"
0494:             *  description="Asserts that the resultsets are equal, or match if
0495:             *  the test contains embedded match tags."
0496:             *  usage="diff,test"
0497:             */
0498:            private static void assertResultSetsEqual(
0499:                    final String failureMessage, final DatabaseResult expR,
0500:                    final DatabaseResult gotR, final List matchPatterns)
0501:                    throws SQLUnitException {
0502:                subAssertResultSetsEqual(failureMessage, expR, gotR,
0503:                        matchPatterns, COL_TYPE_VALUE_EQUAL);
0504:            }
0505:
0506:            /**
0507:             * Asserts that the columns of the two resultsets are equal in value.
0508:             * All other tests that are done for assertResultSetEquals() will be
0509:             * run, except that the column types will not be considered. The values
0510:             * in the columns of the expected and returned resultsets have to be
0511:             * exactly equal.
0512:             * @param failureMessage the user-supplied failure message.
0513:             * @param expR the specified DatabaseResult object.
0514:             * @param gotR the actual DatabaseResult object.
0515:             * @param matchPatterns a List of MatchPattern objects, this will be
0516:             * overriden and the TypelessMatcher will be passed in instead.
0517:             * @exception SQLUnitException if the assertion fails.
0518:             * @sqlunit.assert name="resultset-values-equal"
0519:             *  description="Asserts that the columns in the specified and actual
0520:             *  resultset have the same values. No assertion is made for types."
0521:             *  usage="diff,test"
0522:             */
0523:            private static void assertResultSetValuesEqual(
0524:                    final String failureMessage, final DatabaseResult expR,
0525:                    final DatabaseResult gotR, final List matchPatterns)
0526:                    throws SQLUnitException {
0527:                subAssertResultSetsEqual(failureMessage, expR, gotR,
0528:                        matchPatterns, COL_VALUES_EQUAL);
0529:            }
0530:
0531:            /**
0532:             * Asserts that the columns of the two resultsets are of the same type.
0533:             * All other tests that are done for assertResultSetEquals() will be
0534:             * run, except that the column values will not be considered.
0535:             * @param failureMessage the user-supplied failure message.
0536:             * @param expR the specified DatabaseResult object.
0537:             * @param gotR the actual DatabaseResult object.
0538:             * @exception SQLUnitException if the assertion fails.
0539:             * @sqlunit.assert name="resultset-types-equal"
0540:             *  description="Asserts that the columns in the specified and actual
0541:             *  resultsets are of the same type."
0542:             *  usage="diff,test"
0543:             */
0544:            private static void assertResultSetTypesEqual(
0545:                    final String failureMessage, final DatabaseResult expR,
0546:                    final DatabaseResult gotR, final List matchPatterns)
0547:                    throws SQLUnitException {
0548:                subAssertResultSetsEqual(failureMessage, expR, gotR,
0549:                        matchPatterns, COL_TYPES_EQUAL);
0550:            }
0551:
0552:            /**
0553:             * Asserts that the test will fail and the failure message supplied will 
0554:             * be the same as the first line of the failure message returned by
0555:             * SQLUnit. This is used for checking the SQLUnit code but may have 
0556:             * some practical applications.
0557:             * @param failureMessage the user-supplied failure message.
0558:             * @param expR the specified DatabaseResult object.
0559:             * @param gotR the actual DatabaseResult object.
0560:             * @param matchPatterns a List of match patterns.
0561:             * @exception SQLUnitExcep3tion if the assertion failed.
0562:             * @sqlunit.assert name="fail-with-failure"
0563:             *  description="Asserts that the test will fail with the specified
0564:             *  error message."
0565:             *  usage="diff,test"
0566:             */
0567:            private static void assertFailWithFailure(
0568:                    final String failureMessage, final DatabaseResult expR,
0569:                    final DatabaseResult gotR, final List matchPatterns)
0570:                    throws SQLUnitException {
0571:                String passedInFailureMessage = failureMessage;
0572:                if (passedInFailureMessage == null) {
0573:                    passedInFailureMessage = "NULL";
0574:                }
0575:                String gotFailureMessage = "NULL";
0576:                try {
0577:                    assertEqual(passedInFailureMessage, expR, gotR,
0578:                            matchPatterns);
0579:                } catch (SQLUnitException e) {
0580:                    gotFailureMessage = getFirstLine(e.getMessage());
0581:                }
0582:                if (!gotFailureMessage.equals("NULL")
0583:                        && !gotFailureMessage.equals(passedInFailureMessage)) {
0584:                    throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0585:                            new String[] {
0586:                                    "fail-with-failure",
0587:                                    "(\"" + passedInFailureMessage + "\" != \""
0588:                                            + gotFailureMessage + "\")",
0589:                                    expR.toString(), expR.toString(),
0590:                                    failureMessage });
0591:                }
0592:            }
0593:
0594:            // --------- BatchDatabaseResult asserts --------
0595:
0596:            /**
0597:             * Asserts nothing. This is used to assert that there are no
0598:             * equality or non-equality relationships between the generated
0599:             * and the expected result.
0600:             * @param failureMessage the user-supplied failure message.
0601:             * @param expR the specified BatchDatabaseResult object.
0602:             * @param gotR the actual BatchDatabaseResult object.
0603:             * @exception SQLUnitException if the assertion failed.
0604:             * @sqlunit.assert name="none"
0605:             *  description="Asserts nothing about the generated and the actual
0606:             *  batch results. This is typically used to disable assertions for the 
0607:             *  particular test."
0608:             *  usage="diff,test"
0609:             */
0610:            private static void assertNone(final String failureMessage,
0611:                    final BatchDatabaseResult expR, BatchDatabaseResult gotR)
0612:                    throws SQLUnitException {
0613:                return;
0614:            }
0615:
0616:            /**
0617:             * Asserts that the two BatchDatabaseResults are equal.
0618:             * @param failureMessage the user-supplied failure message.
0619:             * @param expR the specified BatchDatabaseResult object.
0620:             * @param gotR the actual BatchDatabaseResult object.
0621:             * @exception SQLUnitException if the assertion failed.
0622:             * @sqlunit.assert name="equal"
0623:             *  description="Asserts that the two batchresults are equal. This is
0624:             *  a macro assertion, composed of a sequence of assertions, which are
0625:             *  executed serially. This is the default assertion if no assert 
0626:             *  attribute is supplied for the batchtest tag."
0627:             *  usage="batchtest"
0628:             */
0629:            private static void assertEqual(final String failureMessage,
0630:                    final BatchDatabaseResult expR,
0631:                    final BatchDatabaseResult gotR) throws SQLUnitException {
0632:                String conditions = "failed-at-equal,"
0633:                        + "expected-count-equal," + "update-count-equal";
0634:                assertIsTrue(failureMessage, expR, gotR, conditions);
0635:            }
0636:
0637:            /**
0638:             * Asserts that the two BatchDatabaseResults are not equal.
0639:             * @param failureMessage the user-supplied failure message.
0640:             * @param expR the specified BatchDatabaseResult object.
0641:             * @param gotR the actual BatchDatabaseResult object.
0642:             * @exception SQLUnitException if the assertion failed.
0643:             * @sqlunit.assert name="not-equal"
0644:             *  description="Asserts that the two batchresults are not equal. This
0645:             *  is the inverse of the equal assertion."
0646:             *  usage="batchtest"
0647:             */
0648:            private static void assertNotEqual(final String failureMessage,
0649:                    final BatchDatabaseResult expR,
0650:                    final BatchDatabaseResult gotR) throws SQLUnitException {
0651:                try {
0652:                    assertEqual(failureMessage, expR, gotR);
0653:                } catch (SQLUnitException e) {
0654:                    return;
0655:                }
0656:                throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0657:                        new String[] { "not-equal", "", expR.toString(),
0658:                                gotR.toString(), failureMessage });
0659:            }
0660:
0661:            /**
0662:             * Asserts that the failed-at attribute of the batchresult, if 
0663:             * specified, points to the point of actual failure of the result
0664:             * generated by the batchcall or batchsql.
0665:             * @param failureMessage the user-supplied failure message.
0666:             * @param expR the specified BatchDatabaseResult object.
0667:             * @param gotR the actual BatchDatabaseResult object.
0668:             * @exception SQLUnitException if the assertion failed.
0669:             * @sqlunit.assert name="failed-at-equal"
0670:             *  description="Asserts that the failed-at attribute of the batchresult
0671:             *  tag, if specified, points to the point where the result generated
0672:             *  by the batchcall or batchsql actually failed."
0673:             *  usage="batchtest"
0674:             */
0675:            private static void assertFailedAtEqual(
0676:                    final String failureMessage,
0677:                    final BatchDatabaseResult expR,
0678:                    final BatchDatabaseResult gotR) throws SQLUnitException {
0679:                // execute the assert only if failed-at is specified in expR
0680:                if (expR.getFailedAtIndex() > -1) {
0681:                    if (expR.getActualCount() >= (expR.getFailedAtIndex() + 1)) {
0682:                        // updatecounts are explicitly specified
0683:                        if (gotR.getUpdateCountAt(expR.getFailedAtIndex())
0684:                                .equals(
0685:                                        expR.getUpdateCountAt(expR
0686:                                                .getFailedAtIndex()))) {
0687:                            throw new SQLUnitException(
0688:                                    IErrorCodes.ASSERT_FAILED,
0689:                                    new String[] {
0690:                                            "failed-at-equal",
0691:                                            "("
0692:                                                    + expR
0693:                                                            .getUpdateCountAt(expR
0694:                                                                    .getFailedAtIndex())
0695:                                                    + " == "
0696:                                                    + gotR
0697:                                                            .getUpdateCountAt(expR
0698:                                                                    .getFailedAtIndex())
0699:                                                    + " at updatecount["
0700:                                                    + expR.getFailedAtIndex()
0701:                                                    + "])", expR.toString(),
0702:                                            gotR.toString(), failureMessage });
0703:                        }
0704:                    } else {
0705:                        // updatecounts are specified as expected-count
0706:                        if (!gotR.getUpdateCountAt(expR.getFailedAtIndex())
0707:                                .equals("EXECUTE_FAILED")) {
0708:                            throw new SQLUnitException(
0709:                                    IErrorCodes.ASSERT_FAILED,
0710:                                    new String[] {
0711:                                            "failed-at-equal",
0712:                                            "(EXECUTE_FAILED != "
0713:                                                    + gotR
0714:                                                            .getUpdateCountAt(expR
0715:                                                                    .getFailedAtIndex())
0716:                                                    + " at updatecount["
0717:                                                    + expR.getFailedAtIndex()
0718:                                                    + "])", expR.toString(),
0719:                                            gotR.toString(), failureMessage });
0720:                        }
0721:                    }
0722:                }
0723:            }
0724:
0725:            /**
0726:             * Asserts that the number of updatecount elements in the source and
0727:             * target BatchDatabaseResults are equal.
0728:             * @param failureMessage the user-supplied failure message.
0729:             * @param expR the specified BatchDatabaseResult object.
0730:             * @param gotR the actual BatchDatabaseResult object.
0731:             * @exception SQLUnitException if the assertion failed.
0732:             * @sqlunit.assert name="expected-count-equal"
0733:             *  description="Asserts that the number of update counts specified
0734:             *  in the expected batchresult, either as an expected-count attribute,
0735:             *  or listed explicitly, is the same as the one in the generated 
0736:             *  batchresult."
0737:             *  usage="batchtest"
0738:             */
0739:            private static void assertExpectedCountEqual(
0740:                    final String failureMessage,
0741:                    final BatchDatabaseResult expR,
0742:                    final BatchDatabaseResult gotR) throws SQLUnitException {
0743:                if (expR.getExpectedCount() > -1) {
0744:                    // expected-count supplied in the expR, compare counts
0745:                    if (expR.getExpectedCount() != gotR.getActualCount()) {
0746:                        throw new SQLUnitException(
0747:                                IErrorCodes.ASSERT_FAILED,
0748:                                new String[] {
0749:                                        "expected-count-equal",
0750:                                        "("
0751:                                                + expR.getExpectedCount()
0752:                                                + " != "
0753:                                                + gotR.getActualCount()
0754:                                                + " at batchresult.expected-count)",
0755:                                        expR.toString(), gotR.toString(),
0756:                                        failureMessage });
0757:                    }
0758:                } else {
0759:                    // compare the counts in both results
0760:                    // caller can specify no updatecount elements if this is not
0761:                    // to be checked.
0762:                    if (expR.getActualCount() > 0) {
0763:                        if (expR.getActualCount() != gotR.getActualCount()) {
0764:                            throw new SQLUnitException(
0765:                                    IErrorCodes.ASSERT_FAILED,
0766:                                    new String[] {
0767:                                            "expected-count-equal",
0768:                                            "("
0769:                                                    + expR.getActualCount()
0770:                                                    + " != "
0771:                                                    + gotR.getActualCount()
0772:                                                    + " at batchresult.#updatecount)",
0773:                                            expR.toString(), gotR.toString(),
0774:                                            failureMessage });
0775:                        }
0776:                    }
0777:                }
0778:            }
0779:
0780:            /**
0781:             * Asserts that the update counts is equal in both the expected and
0782:             * actual BatchDatabaseResult objects.
0783:             * @param failureMessage the user-supplied failure message.
0784:             * @param expR the specified BatchDatabaseResult object.
0785:             * @param gotR the actual BatchDatabaseResult object.
0786:             * @exception SQLUnitException if the assertion failed.
0787:             * @sqlunit.assert name="updatecount-equal"
0788:             *  description="Asserts that the updatecount values are the same
0789:             *  for the result specified by batchresult and the result generated
0790:             *  by the call to batchsql or batchcall."
0791:             *  usage="batchtest"
0792:             */
0793:            private static void assertUpdateCountEqual(
0794:                    final String failureMessage,
0795:                    final BatchDatabaseResult expR,
0796:                    final BatchDatabaseResult gotR) throws SQLUnitException {
0797:                // only do the assert if the expected-count is not specified
0798:                if (expR.getExpectedCount() == -1) {
0799:                    int expectedActual = expR.getActualCount();
0800:                    for (int i = 0; i < expectedActual; i++) {
0801:                        // skip a failed updatecount since we know they are not equal
0802:                        if (i == expR.getFailedAtIndex()) {
0803:                            continue;
0804:                        }
0805:                        if (!expR.getUpdateCountAt(i).equals(
0806:                                gotR.getUpdateCountAt(i))) {
0807:                            throw new SQLUnitException(
0808:                                    IErrorCodes.ASSERT_FAILED, new String[] {
0809:                                            "updatecount-equal",
0810:                                            "("
0811:                                                    + expR.getUpdateCountAt(i)
0812:                                                            .toString()
0813:                                                    + " != "
0814:                                                    + gotR.getUpdateCountAt(i)
0815:                                                            .toString(),
0816:                                            "at updatecount[" + i + "])",
0817:                                            expR.toString(), gotR.toString(),
0818:                                            failureMessage });
0819:                        }
0820:                    }
0821:                }
0822:            }
0823:
0824:            /**
0825:             * Asserts that the test will fail and the failure message supplied will
0826:             * be the same as the first line of the failure message returned from the
0827:             * test. This is primarily used for testing SQLUnit code, but may have 
0828:             * some practical uses as well.
0829:             * @param failureMessage the user-supplied failure message.
0830:             * @param expR the specified BatchDatabaseResult object.
0831:             * @param gotR the actual BatchDatabaseResult object.
0832:             * @exception SQLUnitException if the assertion failed.
0833:             * @sqlunit.assert name="fail-with-failure"
0834:             *  description="Asserts that the test will fail with the user-supplied
0835:             *  failure message."
0836:             *  usage="batchtest"
0837:             */
0838:            private static void assertFailWithFailure(
0839:                    final String failureMessage,
0840:                    final BatchDatabaseResult expR,
0841:                    final BatchDatabaseResult gotR) throws SQLUnitException {
0842:                String passedInFailureMessage = failureMessage;
0843:                if (passedInFailureMessage == null) {
0844:                    passedInFailureMessage = "NULL";
0845:                }
0846:                String gotFailureMessage = "NULL";
0847:                try {
0848:                    assertEqual(passedInFailureMessage, expR, gotR);
0849:                } catch (SQLUnitException e) {
0850:                    gotFailureMessage = getFirstLine(e.getMessage());
0851:                }
0852:                if (!gotFailureMessage.equals("NULL")
0853:                        && !gotFailureMessage.equals(passedInFailureMessage)) {
0854:                    throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
0855:                            new String[] {
0856:                                    "not-equal",
0857:                                    "(\"" + passedInFailureMessage + "\" != \""
0858:                                            + gotFailureMessage + "\")",
0859:                                    expR.toString(), gotR.toString(),
0860:                                    failureMessage });
0861:                }
0862:            }
0863:
0864:            /**
0865:             * Builds a Map of assert condition calls to the assert method names
0866:             * from a two dimensional String array.
0867:             * @param data a two dimensional String array.
0868:             * @return a Map of name-value pairs.
0869:             */
0870:            private static Map buildMap(final String[][] data) {
0871:                Map map = new HashMap();
0872:                for (int i = 0; i < data.length; i++) {
0873:                    String key = data[i][0];
0874:                    String value = data[i][1];
0875:                    map.put(key, value);
0876:                }
0877:                return map;
0878:            }
0879:
0880:            /**
0881:             * Returns the first line of a multi-line string.
0882:             * @param multiline a line with one or more embedded newlines.
0883:             * @return the first line.
0884:             */
0885:            private static String getFirstLine(final String multiline) {
0886:                int pos = multiline.indexOf(IErrorCodes.LF);
0887:                if (pos <= 0) {
0888:                    return multiline;
0889:                } else {
0890:                    return multiline.substring(0, pos);
0891:                }
0892:            }
0893:
0894:            /**
0895:             * Invokes the assertion with the method name and an array of Object
0896:             * parameters to the method.
0897:             * @param condition the name of the assertion.
0898:             * @param paramClasses an array of Class objects.
0899:             * @param methodMap a Mapping of conditions to method names.
0900:             * @param params an array of Object parameters to the assertion.
0901:             * @exception SQLUnitException if the assertion failed.
0902:             */
0903:            private static void invokeAssertion(final String condition,
0904:                    final Class[] paramClasses, final Map methodMap,
0905:                    final Object[] params) throws SQLUnitException {
0906:                // assertions can be a comma-separated String, get them
0907:                String[] conditions = condition.split("\\s*,\\s*");
0908:                for (int i = 0; i < conditions.length; i++) {
0909:                    String methodName = (String) methodMap.get(conditions[i]);
0910:                    if (methodName == null) {
0911:                        throw new SQLUnitException(
0912:                                IErrorCodes.NO_ASSERT_METHOD,
0913:                                new String[] { conditions[i] });
0914:                    }
0915:                    try {
0916:                        Method method = Assertions.class.getDeclaredMethod(
0917:                                methodName, paramClasses);
0918:                        method.invoke(null, params);
0919:                    } catch (InvocationTargetException e) {
0920:                        // this will be an SQLUnitException
0921:                        throw new SQLUnitException(e.getCause().getMessage(), e
0922:                                .getCause());
0923:                    } catch (Exception e) {
0924:                        throw new SQLUnitException(
0925:                                IErrorCodes.GENERIC_ERROR,
0926:                                new String[] { "System",
0927:                                        e.getClass().getName(), e.getMessage() },
0928:                                e);
0929:                    }
0930:                }
0931:            }
0932:
0933:            /**
0934:             * Asserts that the two resultsets are equal. The equality of columns
0935:             * is governed by the matchMode argument passed in.
0936:             * @param failureMessage the user-supplied failure message.
0937:             * @param expR the expected DatabaseResult object.
0938:             * @param gotR the supplied DatabaseResult object.
0939:             * @param matchPatterns a List of Matcher objects which will govern
0940:             * the kind of value matching of Column objects.
0941:             * @param matchMode whether the matching should be full, type-based
0942:             * only or value-based only.
0943:             * @exception SQLUnitException if thrown.
0944:             * @exception AssertionFailedError if thrown.
0945:             */
0946:            private static void subAssertResultSetsEqual(
0947:                    final String failureMessage, final DatabaseResult expR,
0948:                    final DatabaseResult gotR, final List matchPatterns,
0949:                    final int matchMode) throws SQLUnitException,
0950:                    AssertionFailedError {
0951:                String matchModeStr = "resultsets-equal";
0952:                try {
0953:                    ResultSetBean[] expResultSets = expR.getResultSets();
0954:                    ResultSetBean[] gotResultSets = gotR.getResultSets();
0955:                    for (int i = 0; i < expResultSets.length; i++) {
0956:                        if (expResultSets[i].isRowCountOverriden()) {
0957:                            // row count override check
0958:                            subAssertRowCountOverrideEqual(expResultSets[i],
0959:                                    gotResultSets[i], expResultSets[i].getId());
0960:                            continue;
0961:                        }
0962:                        if (!expResultSets[i].isPartial()) {
0963:                            // actual row count check
0964:                            subAssertRowCountEqual(expResultSets[i],
0965:                                    gotResultSets[i], expResultSets[i].getId());
0966:                        }
0967:                        Row[] expRows = expResultSets[i].getRows();
0968:                        Row[] gotRows = gotResultSets[i].getRows();
0969:                        // sort both Row[] objects in place
0970:                        if (expResultSets[i].getSortBy() != null) {
0971:                            // special value "none" turns off sorting
0972:                            if (!("NONE").equalsIgnoreCase(expResultSets[i]
0973:                                    .getSortBy()[0])) {
0974:                                // sort only the expResultSet, since the gotResultSet
0975:                                // is pre-sorted by the database
0976:                                expResultSets[i].sort();
0977:                            }
0978:                        } else {
0979:                            // sort both gotR and expR by their natural order
0980:                            expResultSets[i].sort();
0981:                            gotResultSets[i].sort();
0982:                        }
0983:                        for (int j = 0; j < expRows.length; j++) {
0984:                            if (!expRows[j].isPartial()) {
0985:                                // check each row for column count equality
0986:                                subAssertColumnCountEqual(expRows[j],
0987:                                        gotRows[j], expResultSets[i].getId(),
0988:                                        expRows[j].getId());
0989:                            }
0990:                            Row expRow;
0991:                            Row gotRow;
0992:                            if (expResultSets[i].isPartial()) {
0993:                                // Read the (1-based) id from expRow, then pull up
0994:                                // appropriate gotRow by its 0-based position
0995:                                expRow = expRows[j];
0996:                                try {
0997:                                    gotRow = gotRows[Integer.parseInt(expRow
0998:                                            .getId()) - 1];
0999:                                } catch (NumberFormatException e) {
1000:                                    throw new AssertionFailedError("row["
1001:                                            + (i + 1) + "," + (j + 1) + "].id("
1002:                                            + expRow.getId()
1003:                                            + ") must be numeric");
1004:                                } catch (ArrayIndexOutOfBoundsException e) {
1005:                                    throw new AssertionFailedError("row["
1006:                                            + (i + 1) + "," + (j + 1) + "].id("
1007:                                            + expRow.getId()
1008:                                            + ") out of bounds");
1009:                                }
1010:                            } else {
1011:                                expRow = expRows[j];
1012:                                gotRow = gotRows[j];
1013:                            }
1014:                            Col[] expCols = expRow.getCols();
1015:                            Col[] gotCols = gotRow.getCols();
1016:                            for (int k = 0; k < expCols.length; k++) {
1017:                                Col expCol;
1018:                                Col gotCol;
1019:                                if (expRow.isPartial()) {
1020:                                    // Read the (1-based) id from expCol, then pull
1021:                                    // up appropriate gotCol by its 0-based position.
1022:                                    expCol = expCols[k];
1023:                                    try {
1024:                                        gotCol = gotCols[Integer
1025:                                                .parseInt(expCol.getId()) - 1];
1026:                                    } catch (NumberFormatException e) {
1027:                                        throw new AssertionFailedError("col["
1028:                                                + (i + 1) + "," + (j + 1) + ","
1029:                                                + (k + 1) + "].id("
1030:                                                + expCol.getId()
1031:                                                + ") must be numeric");
1032:                                    } catch (ArrayIndexOutOfBoundsException e) {
1033:                                        throw new AssertionFailedError("col["
1034:                                                + (i + 1) + "," + (j + 1) + ","
1035:                                                + (k + 1) + "].id("
1036:                                                + expCol.getId()
1037:                                                + ") out of bounds");
1038:                                    }
1039:                                } else {
1040:                                    expCol = expCols[k];
1041:                                    gotCol = gotCols[k];
1042:                                }
1043:                                boolean matcherFound = false;
1044:                                if (matchPatterns != null) {
1045:                                    // user defined matching
1046:                                    Iterator mpiter = matchPatterns.iterator();
1047:                                    while (mpiter.hasNext()) {
1048:                                        MatchPattern mp = (MatchPattern) mpiter
1049:                                                .next();
1050:                                        if (mp.canApply((i + 1), (j + 1),
1051:                                                (k + 1))) {
1052:                                            matcherFound = true;
1053:                                            subAssertColumnsMatch(expCol,
1054:                                                    gotCol, mp,
1055:                                                    expResultSets[i].getId(),
1056:                                                    expRow.getId(), expCol
1057:                                                            .getId());
1058:                                        }
1059:                                    } // mpiter loop
1060:                                } // matchPatterns != null
1061:                                if (matchPatterns == null || !matcherFound) {
1062:                                    switch (matchMode) {
1063:                                    case Assertions.COL_TYPES_EQUAL:
1064:                                        matchModeStr = "resultset-types-equal";
1065:                                        subAssertColumnTypesEqual(expCol,
1066:                                                gotCol, expResultSets[i]
1067:                                                        .getId(), expRow
1068:                                                        .getId(), expCol
1069:                                                        .getId());
1070:                                        break;
1071:                                    case Assertions.COL_VALUES_EQUAL:
1072:                                        matchModeStr = "resultset-values-equal";
1073:                                        subAssertColumnValuesEqual(expCol,
1074:                                                gotCol, expResultSets[i]
1075:                                                        .getId(), expRow
1076:                                                        .getId(), expCol
1077:                                                        .getId());
1078:                                        break;
1079:                                    case Assertions.COL_TYPE_VALUE_EQUAL:
1080:                                    default:
1081:                                        // default match, exact equality
1082:                                        matchModeStr = "resultsets-equal";
1083:                                        subAssertColumnsEqual(expCol, gotCol,
1084:                                                expResultSets[i].getId(),
1085:                                                expRow.getId(), expCol.getId());
1086:                                        break;
1087:                                    }
1088:                                }
1089:                            } // col loop (k)
1090:                        } // row loop (j)
1091:                    } // resultset loop (i)
1092:                } catch (AssertionFailedError e) {
1093:                    throw new SQLUnitException(IErrorCodes.ASSERT_FAILED,
1094:                            new String[] { matchModeStr,
1095:                                    "(" + e.getMessage() + ")",
1096:                                    expR.toString(), gotR.toString(),
1097:                                    failureMessage }, e);
1098:                }
1099:            }
1100:
1101:            /**
1102:             * Asserts that the rowcount attribute is the same for both the
1103:             * specified and actual ResultSetBean objects.
1104:             * @param expResultSet the bean representing the specified ResultSet.
1105:             * @param gotResultSet the bean representing the actual ResultSet.
1106:             * @param resultSetIndex the index of the resultset within the result.
1107:             * @exception AssertionFailedError if the assertion failed.
1108:             */
1109:            private static void subAssertRowCountOverrideEqual(
1110:                    final ResultSetBean expResultSet,
1111:                    final ResultSetBean gotResultSet,
1112:                    final String resultSetIndex) throws AssertionFailedError {
1113:                try {
1114:                    Assert.assertEquals(expResultSet.getRowCount(),
1115:                            gotResultSet.getRowCount());
1116:                } catch (AssertionFailedError e) {
1117:                    throw new AssertionFailedError(expResultSet.getRowCount()
1118:                            + " != " + gotResultSet.getRowCount()
1119:                            + " at result[" + resultSetIndex + "].rowcount");
1120:                }
1121:            }
1122:
1123:            /**
1124:             * Asserts that the number of rows in the specified Row[] object
1125:             * is the same as the one returned from the SQL or stored procedure
1126:             * call.
1127:             * @param expResultSet the bean representing the specified ResultSet.
1128:             * @param gotResultSet the bean representing the actual ResultSet.
1129:             * @param resultSetIndex the index of the resultset within the result.
1130:             * @exception AssertionFailedError if the assertion failed.
1131:             */
1132:            private static void subAssertRowCountEqual(
1133:                    final ResultSetBean expResultSet,
1134:                    final ResultSetBean gotResultSet,
1135:                    final String resultSetIndex) throws AssertionFailedError {
1136:                try {
1137:                    Assert.assertEquals(expResultSet.getRows().length,
1138:                            gotResultSet.getRows().length);
1139:                } catch (AssertionFailedError e) {
1140:                    throw new AssertionFailedError(
1141:                            expResultSet.getRows().length + " != "
1142:                                    + gotResultSet.getRows().length
1143:                                    + " at result[" + resultSetIndex
1144:                                    + "].#rows");
1145:                }
1146:            }
1147:
1148:            /**
1149:             * Asserts that the number of columns in the two Row objects are equal.
1150:             * @param expRow the bean representing the specified Row.
1151:             * @param gotRow the bean representing the actual Row.
1152:             * @param resultSetIndex the index of the ResultSet within the Result.
1153:             * @param rowIndex the index of the row within the ResultSet.
1154:             * @exception AssertionFailedError if the assertion failed.
1155:             */
1156:            private static void subAssertColumnCountEqual(final Row expRow,
1157:                    final Row gotRow, final String resultSetIndex,
1158:                    final String rowIndex) throws AssertionFailedError {
1159:                try {
1160:                    Assert.assertEquals(expRow.getCols().length, gotRow
1161:                            .getCols().length);
1162:                } catch (AssertionFailedError e) {
1163:                    throw new AssertionFailedError(expRow.getCols().length
1164:                            + " != " + gotRow.getCols().length + " at result["
1165:                            + resultSetIndex + "," + rowIndex + "].#cols");
1166:                }
1167:            }
1168:
1169:            /**
1170:             * Asserts that the two Column objects match per the MatchPattern's
1171:             * rules.
1172:             * @param expCol the bean representing the specified Column.
1173:             * @param gotCol the bean representing the actual Column.
1174:             * @param mp the MatchPattern object.
1175:             * @param resultSetIndex the index of the resultset within the result.
1176:             * @param rowIndex the index of the row within the resultset.
1177:             * @param colIndex the index of the column within the row.
1178:             * @exception SQLUnitException if the Matcher could not be instantiated.
1179:             * @exception AssertionFailedError if the assertion failed.
1180:             */
1181:            private static void subAssertColumnsMatch(final Col expCol,
1182:                    final Col gotCol, final MatchPattern mp,
1183:                    final String resultSetIndex, final String rowIndex,
1184:                    final String colIndex) throws SQLUnitException,
1185:                    AssertionFailedError {
1186:                boolean matched = mp.applyMatcher(expCol.getValue(), gotCol
1187:                        .getValue());
1188:                if (!matched) {
1189:                    throw new AssertionFailedError(expCol.getValue() + " !~ "
1190:                            + gotCol.getValue() + " (" + mp.getMatcherClass()
1191:                            + ") at result[" + resultSetIndex + "," + rowIndex
1192:                            + "," + colIndex + "]");
1193:                }
1194:            }
1195:
1196:            /**
1197:             * Asserts that the two Column objects are same in value and type.
1198:             * @param expCol the bean representing the specified Column.
1199:             * @param gotCol the bean representing the actual Column.
1200:             * @param resultSetIndex the index of the resultset within the result.
1201:             * @param rowIndex the index of the row within the resultset.
1202:             * @param colIndex the index of the column within the row.
1203:             * @exception AssertionFailedError if the assertion failed.
1204:             */
1205:            private static void subAssertColumnsEqual(final Col expCol,
1206:                    final Col gotCol, final String resultSetIndex,
1207:                    final String rowIndex, final String colIndex)
1208:                    throws AssertionFailedError {
1209:                try {
1210:                    Assert.assertEquals(expCol, gotCol);
1211:                } catch (AssertionFailedError e) {
1212:                    throw new AssertionFailedError(expCol.toString() + " != "
1213:                            + gotCol.toString() + " at result["
1214:                            + resultSetIndex + "," + rowIndex + "," + colIndex
1215:                            + "]");
1216:                }
1217:            }
1218:
1219:            /**
1220:             * Asserts that the two Column objects resolve to the same toString()
1221:             * value for their contents.
1222:             * @param expCol the bean representing the specified Column.
1223:             * @param gotCol the bean representing the actual Column.
1224:             * @param resultSetIndex the index of the resultset within the result.
1225:             * @param rowIndex the index of the row within the resultset.
1226:             * @param colIndex the index of the column within the row.
1227:             * @exception AssertionFailedError if the assertion failed.
1228:             */
1229:            private static void subAssertColumnValuesEqual(final Col expCol,
1230:                    final Col gotCol, final String resultSetIndex,
1231:                    final String rowIndex, final String colIndex)
1232:                    throws AssertionFailedError {
1233:                try {
1234:                    Assert.assertEquals(expCol.getValue(), gotCol.getValue());
1235:                } catch (AssertionFailedError e) {
1236:                    throw new AssertionFailedError(expCol.getValue() + " != "
1237:                            + gotCol.getValue() + " at result["
1238:                            + resultSetIndex + "," + rowIndex + "," + colIndex
1239:                            + "]");
1240:                }
1241:            }
1242:
1243:            /**
1244:             * Asserts that the two Column objects are of the same type.
1245:             * @param expCol the bean representing the specified Column.
1246:             * @param gotCol the bean representing the actual Column.
1247:             * @param resultSetIndex the index of the resultset within the result.
1248:             * @param rowIndex the index of the row within the resultset.
1249:             * @param colIndex the index of the column within the row.
1250:             * @exception AssertionFailedError if the assertion failed.
1251:             */
1252:            private static void subAssertColumnTypesEqual(final Col expCol,
1253:                    final Col gotCol, final String resultSetIndex,
1254:                    final String rowIndex, final String colIndex)
1255:                    throws AssertionFailedError {
1256:                try {
1257:                    Assert.assertEquals(expCol.getType(), gotCol.getType());
1258:                } catch (AssertionFailedError e) {
1259:                    throw new AssertionFailedError(expCol.getType() + " != "
1260:                            + gotCol.getType() + " at result[" + resultSetIndex
1261:                            + "," + rowIndex + "," + colIndex + "]");
1262:                }
1263:            }
1264:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.