sqlunit

Home
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
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » Testing » sqlunit 
SQLUnit
License:GNU General Public License (GPL)
URL:http://sqlunit.sourceforge.net
Description:SQLUnit is a regression and unit testing harness for testing database stored procedures.
Package NameComment
net.sourceforge.sqlunit Classes for the SQLUnit core package. The SQLUnit package is designed as a set of individual XML handlers for the various elements of the SQLUnit test suite authoring XML. There is a one to one correspondence between the SQLUnit element and the SQLUnit handler class as illustrated in the table below:
XML Element SQLUnit Handler Class
sqlunit
  • connection
  • setup
    • sql
    • include
  • test
    • prepare
    • sql or call
    • result
  • teardown
    • sql
    • include
 
  • net.sourceforge.sqlunit.ConnectionHandler
  • net.sourceforge.sqlunit.SetupHandler
    • net.sourceforge.sqlunit.SqlHandler
    • net.sourceforge.sqlunit.IncludeHandler
  • net.sourceforge.sqlunit.TestHandler
    • net.sourceforge.sqlunit.PrepareHandler
    • net.sourceforge.sqlunit.SqlHandler|CallHandler
    • net.sourceforge.sqlunit.ResultHandler
  • net.sourceforge.sqlunit.TeardownHandler
    • net.sourceforge.sqlunit.SqlHandler
    • net.sourceforge.sqlunit.IncludeHandler

Each handler class reads the JDOM element and extracts the information to pass back to the SQLUnit class. The SQLUnit class is designed as a JUnit test class. The JUnit class contains a single test method called testWrapper() which is responsible for reading the XML file and carrying out the operations specified in the setup element, followed by each test in sequence, followed by the operations in the teardown element. The ConnectionHandler class is called before all the other operations to get a JDBC connection to the database.

All handler classes implement the IHandler interface, which specifies a single process method which takes a JDOM Element org.jdom.Element and a java.sql.Connection object and returns a java.lang.Object. A given handler implementation may not need to return an Object, in which case it must return null. The HandlerFactory uses the handlers.properties Java properties file to instantiate the appropriate instance of a handler given the JDOM Element name.

Apart from the handlers, there are some auxilliary classes that provide utilities to the handlers. The SymbolTable class is a container of variables that are declared within an SQLUnit test script. The DatabaseResult object is a HashMap of fields and values returned from processing a SQL statement or stored procedure, or parsed out of a SQLUnit result element. The key for this HashMap is a DatabaseResultKey which is really a tuple of (resultsetId, rowId, colId) for each column returned by a SQL statement or stored procedure specified by the SQLUnit sql or call elements and parsed out of the SQLUnit result element. The SqlTypeUtils class is responsible for converting between database type to String and back, depending on the method called.

The SQLUnitException class is a common class that is shared by the SQLUnit package. The content of the SQLUnitException error messages is handled by specifying constant Strings in the IErrorCodes interface class.

net.sourceforge.sqlunit.ant The SQLUnit Ant Task.
net.sourceforge.sqlunit.beans Beans for the SQLUnit handlers.
net.sourceforge.sqlunit.handlers Handler implementations for SQLUnit. The SQLUnit package is designed as a set of individual XML handlers for the various elements of the SQLUnit test suite authoring XML. There is a one to one correspondence between the SQLUnit element and the SQLUnit handler class as illustrated in the table below:
XML Element SQLUnit Handler Class
sqlunit
  • connection
  • setup
    • sql
    • include
  • test
    • prepare
    • sql or call
    • result
  • teardown
    • sql
    • include
 
  • net.sourceforge.sqlunit.ConnectionHandler
  • net.sourceforge.sqlunit.SetupHandler
    • net.sourceforge.sqlunit.SqlHandler
    • net.sourceforge.sqlunit.IncludeHandler
  • net.sourceforge.sqlunit.TestHandler
    • net.sourceforge.sqlunit.PrepareHandler
    • net.sourceforge.sqlunit.SqlHandler|CallHandler
    • net.sourceforge.sqlunit.ResultHandler
  • net.sourceforge.sqlunit.TeardownHandler
    • net.sourceforge.sqlunit.SqlHandler
    • net.sourceforge.sqlunit.IncludeHandler

Each handler class reads the JDOM element and extracts the information to pass back to the SQLUnit class. The SQLUnit class is designed as a JUnit test class. The JUnit class contains a single test method called testWrapper() which is responsible for reading the XML file and carrying out the operations specified in the setup element, followed by each test in sequence, followed by the operations in the teardown element. The ConnectionHandler class is called before all the other operations to get a JDBC connection to the database.

All handler classes implement the IHandler interface, which specifies a single process method which takes a JDOM Element org.jdom.Element and a java.sql.Connection object and returns a java.lang.Object. A given handler implementation may not need to return an Object, in which case it must return null. The HandlerFactory uses the handlers.properties Java properties file to instantiate the appropriate instance of a handler given the JDOM Element name.

Apart from the handlers, there are some auxilliary classes that provide utilities to the handlers. The SymbolTable class is a container of variables that are declared within an SQLUnit test script. The DatabaseResult object is a HashMap of fields and values returned from processing a SQL statement or stored procedure, or parsed out of a SQLUnit result element. The key for this HashMap is a DatabaseResultKey which is really a tuple of (resultsetId, rowId, colId) for each column returned by a SQL statement or stored procedure specified by the SQLUnit sql or call elements and parsed out of the SQLUnit result element. The SqlTypeUtils class is responsible for converting between database type to String and back, depending on the method called.

The SQLUnitException class is a common class that is shared by the SQLUnit package. The content of the SQLUnitException error messages is handled by specifying constant Strings in the IErrorCodes interface class.

net.sourceforge.sqlunit.matchers Matcher plug-ins for SQLUnit. Matcher plug-in classes define rules to match between two columns. The default matching is an exact match, but Matchers can be built that match within a percentage or a range. This is not an exhaustive list, more are expected to be added as users contribute them.
net.sourceforge.sqlunit.parsers
net.sourceforge.sqlunit.reporters Reporter plug-ins for SQLUnit. SQLUnit comes configured with the default text reporter, but provides the ability to hook in a reporter of your choice that works with your existing system. All reporter plug-ins should implement the IReporter interface, and needs to provide code that will be executed by SQLUnit for reporting.

Acknowledgement

The plug-in code was contributed largely by Rob Nielsen. He needed to use SQLUnit with Canoo, so he factored out the reporting code from SQLUnit and made it into the TextReporter, and built the CanooWebTestReporter plug-in.
net.sourceforge.sqlunit.test Miscellaneous Testing classes. These are JUnit test classes and some other classes that support the JUnit or the Mock test frameworks.
net.sourceforge.sqlunit.test.mock Mock Testing framework for SQLUnit. Provides a framework to test SQLUnit code without a database. The database independence is achieved by using the MockRunner JDBC framework.

Instead of a real database, the testing framework relies on introspecting a specified class. The class implements the IMockDatabase interface and contains methods with the signature: public MockResultSet methodName(Integer resultSetId); The IMockDatabase interface mandates a getResultSet(String,int) method which returns a MockResultSet. The implementation of this method is provided in the AbstractMockDatabase class. Clients wishing to develop their own mock database classes should extend this class.

Examples of methods that mimic the behavior of stored procedures can be found in the SQLUnitMockDatabase class which extends AbstractMockDatabase. Things to note are:

  1. The count of the number of results (used for the Statement.execute() and CallableStatement.getMoreResults()) is returned in wrapped in result set 0.
  2. Outparam values are generated internally using the negative of the supplied index. Thus, the value of OUTPARAM at position 1 will be wrapped in the resultset that is returned by invoking the appropriate method with -1. Scalar OUTPARAM values are specified as Strings, but are converted to the appropriate Object on retrieval. REF CURSOR OUTPARAM objects are returned as themselves since they are essentially ResultSet objects.
  3. Exceptions are not thrown directly, but they are wrapped in the resultset that is being made to throw the exception.
  4. ResultSets are returned from the method using the supplied index. Thus the first ResultSet will be returned by invoking the method with Integer(1).
The functionality of wrapping and unwrapping scalar and SQLException objects back and forth from MockResultSet objects can be found in the MockResultSetUtils class.

To make this work with the MockRunner framework, the following classes were extended from the MockRunner distribution:

  1. SQLUnitMockConnection extends com.mockrunner.mock.jdbc.MockConnection. The extensions were mainly to call the Introspecting versions of the ResultSetHandler classes instead of MockRunner's ResultSetHandlers.
  2. SQLUnitMockDriver extends com.mockrunner.mock.jdbc.MockDriver. This version provides a URL signature which the mock framework will respond to.
  3. SQLUnitMockStatement extends com.mockrunner.mock.jdbc.MockStatement. Calls the Introspecting version of the ResultSetHandler.
  4. SQLUnitMockPreparedStatement extends com.mockrunner.mock.jdbc.MockPreparedStatement. Calls the Introspecting version of the ResultSetHandler.
  5. SQLUnitMockCallableStatement extends com.mockrunner.mock.jdbc.MockStatement. Calls the Introspecting version of the ResultSetHandler.
  6. IntrospectingStatementResultSetHandler extends com.mockrunner.jdbc.StatementResultSetHandler. Invokes the IntrospectingResultSetFactory to execute Statement objects.
  7. IntrospectingPreparedStatementResultSetHandler extends com.mockrunner.jdbc.PreparedStatementResultSetHandler. Invokes the IntrospectingResultSetFactory to execute PreparedStatement objects.
  8. IntrospectingCallableStatementResultSetHandler extends com.mockrunner.jdbc.CallableStatementResultSetHandler. Invokes the IntrospectingResultSetFactory to execute CallableStatement objects.
  9. IntrospectingResultSetHandler implements the com.mockrunner.jdbc.ResultSetHandler. Implements multi-resultset handling and getting the result sets using Introspection.
A sample test file which uses the SQLUnit mock testing framework can be found at test/mock/test.xml in the distribution.
net.sourceforge.sqlunit.tools Contains tools to build SQLUnit test cases. These tools were moved from their old locations and merged into a single tools package to take advantage of code reuse possibilities.

Backward compatibility information

To those using these tools for the first time, there should be no issues at all. Each tool has an associated configuration file which can be used to predefine information such as JDBC parameters. The only changes to this file are as follows:

guiconfig.properties.sample: The addition of a new indentsize property which allows you to specify how many characters per indent you want. It defaults to 2, so if thats OK, you dont need to change your properties file.

tuirc.properties.sample: The captureFile property has been renamed to capturefile so as to be in line with the similarly named property in the guiconfig.properties.sample file. This was necessary to take advantage of the code sharing mentioned above.

net.sourceforge.sqlunit.types Implementations of various datatypes.
net.sourceforge.sqlunit.utils Utility classes for SQLUnit.
www.___j_a_v__a___2_s.co___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.