Source Code Cross Referenced for ArrayResultSetFactory.java in  » Testing » mockrunner-0.4 » com » mockrunner » jdbc » 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 » mockrunner 0.4 » com.mockrunner.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.mockrunner.jdbc;
002:
003:        import com.mockrunner.mock.jdbc.MockResultSet;
004:
005:        /**
006:         * A <code>ResultSetFactory</code> implementation which will produce 
007:         * <code>MockResultSet</code> instances based on information given as 
008:         * <code>String</code> arrays.
009:         * 
010:         * <p>
011:         * <code>StringValuesTable</code> and <code>ArrayResultSetFactory</code> can 
012:         * provide easy set up of unit test fixtures and assertion of outcomes with the
013:         * same data structures, without any need for external sources of test data:
014:         * </p>
015:         * 
016:         * <p>
017:         * <pre>
018:         *  private static final String _SQL_SELECT_ALL_EMPLOYEES = 
019:         *      "SELECT * FROM employee";
020:         *  private StringValuesTable <b>_employeeQueryResults</b>;
021:         *  ArrayResultSetFactory <b>_arrayResultSetFactory</b>;
022:         *  private Employee[] _employees;
023:         *  
024:         *  protected void setUp() throws Exception {
025:         *    super.setUp();
026:         *    <b>_employeeQueryResults</b> = new StringValuesTable(
027:         *        "employeeQueryResults", 
028:         *        new String[] {
029:         *            "id", "lastname", "firstname",
030:         *        }, 
031:         *        new String[][] {
032:         *            new String[] {"1", "gibbons", "peter"},
033:         *            new String[] {"2", "lumbergh", "bill"},
034:         *            new String[] {"3", "waddams", "milton"},
035:         *        }
036:         *    );
037:         *    _employees = new Employee[3] {
038:         *        new Employee(
039:         *            <b>_employeeQueryResults.getItem(1, "id")</b>,
040:         *            <b>_employeeQueryResults.getItem(1, "lastname")</b>,
041:         *            <b>_employeeQueryResults.getItem(1, "firstname")</b>,
042:         *        ),
043:         *        ...
044:         *    };
045:         *    ...
046:         *  }
047:         *    
048:         *  public void testGetEmployees() throws Exception {
049:         *    PreparedStatementResultSetHandler preparedStatementResultSetHandler = 
050:         *        getPreparedStatementResultSetHandler();
051:         *    <b>_arrayResultSetFactory</b> = 
052:         *        new ArrayResultSetFactory(<b>_employeeQueryResults</b>);
053:         *    MockResultSet resultSet = 
054:         *        preparedStatementResultSetHandler.createResultSet(
055:         *            <b>_employeeQueryResults.getName()</b>, 
056:         *            <b>arrayResultSetFactory</b>);
057:         *    preparedStatementResultSetHandler.prepareResultSet(
058:         *        _SQL_SELECT_ALL_EMPLOYEES, resultSet);
059:         *        
060:         *    // execute query, perhaps calling method on an EmployeeDAO...
061:         *    
062:         *    assertEquals(
063:         *        <b>_employeeQueryResults.getNumberOfRows()</b>, 
064:         *        resultsList.size());
065:         *    for (int i = 0; i < _employees.length; i++) {
066:         *       assertTrue(resultsList.contains(_employees[i]));
067:         *    }
068:         *    MockResultSet mockResultSet = 
069:         *        preparedStatementResultSetHandler.getResultSet(
070:         *            SQL_SELECT_ALL_EMPLOYEES);
071:         *    int rows = mockResultSet.getRowCount();
072:         *    for (int row = 1; row <= rows; row++) {
073:         *      verifyResultSetRow(
074:         *          <b>_employeeQueryResults.getName()</b>, 
075:         *          row, <b>_employeeQueryResults.getRow(row)</b>);
076:         *    }
077:         *    verifySQLStatementExecuted(_SQL_SELECT_ALL_EMPLOYEES);
078:         *    verifyAllResultSetsClosed();
079:         *    verifyAllStatementsClosed();
080:         *    verifyConnectionClosed();     
081:         *  }
082:         * </pre>
083:         * </p>
084:         * 
085:         * @author Erick G. Reid
086:         */
087:        public class ArrayResultSetFactory implements  ResultSetFactory {
088:            private String[] columnNames = new String[0];
089:            private String[][] stringMatrix = new String[0][0];
090:
091:            /**
092:             * Creates a new <code>ArrayResultSetFactory</code> that will produce
093:             * result sets based on information in the given
094:             * <code>StringValuesTable</code>.
095:             * 
096:             * @param stringValuesTable the <code>StringValuesTable</code> to use. This argument
097:             *                          cannot be <code>null</code>.
098:             */
099:            public ArrayResultSetFactory(StringValuesTable stringValuesTable) {
100:                if (stringValuesTable != null) {
101:                    this .stringMatrix = stringValuesTable.getStringMatrix();
102:                    this .columnNames = stringValuesTable.getColumnNames();
103:                    return;
104:                }
105:                throw new IllegalArgumentException(
106:                        "the string table cannot be null");
107:            }
108:
109:            /**
110:             * Creates a new <code>ArrayResultSetFactory</code> with the given matrix
111:             * for data representation.
112:             * 
113:             * @param stringMatrix the data representation for the result sets this factory will
114:             *                     produce. This argument cannot be <code>null</code>, must
115:             *                     not contain any null values, and each array in the matrix must
116:             *                     contain the same number of elements as the first (<code>stringMatrix[0].length == stringMatrix[n].length</code>
117:             *                     for any given valid row number, <code>n</code>). Further,
118:             *                     this matrix must, at a minimum represent <code>1</code> row
119:             *                     and <code>1</code> column of items (<code>stringMatrix.length >= 1</code>,
120:             *                     and <code>stringMatrix[0].length >= 1</code>).
121:             */
122:            public ArrayResultSetFactory(String[][] stringMatrix) {
123:                this .stringMatrix = StringValuesTable
124:                        .verifyStringMatrix(stringMatrix);
125:            }
126:
127:            /**
128:             * Creates a new <code>ArrayResultSetFactory</code> with the given set of
129:             * column names and the given matrix for data representation.
130:             * 
131:             * @param columnNames the column names for the result sets this factory will
132:             *                    produce. This argument may be <code>null</code> if no column
133:             *                    names are desired, but if a non-<code>null</code> array
134:             *                    reference is given, the array cannot contain any
135:             *                    <code>null</code> nor duplicate elements, and must have the
136:             *                    same number of elements as there are columns in the given
137:             *                    string matrix (<code>stringMatrix[n]</code> for any given
138:             *                    valid row number, <code>n</code>).
139:             * @param stringMatrix the data representation for the result sets this factory will
140:             *                     produce. This argument cannot be <code>null</code>, must
141:             *                     not contain any null values, and each array in the matrix must
142:             *                     contain the same number of elements as the first (<code>stringMatrix[0].length == stringMatrix[n].length</code>
143:             *                     for any given valid row number, <code>n</code>). Further,
144:             *                     this matrix must, at a minimum represent <code>1</code> row
145:             *                     and <code>1</code> column of items (<code>stringMatrix.length >= 1</code>,
146:             *                     and <code>stringMatrix[0].length >= 1</code>).
147:             */
148:            public ArrayResultSetFactory(String[] columnNames,
149:                    String[][] stringMatrix) {
150:                this .stringMatrix = StringValuesTable
151:                        .verifyStringMatrix(stringMatrix);
152:                if (columnNames != null) {
153:                    this .columnNames = StringValuesTable.verifyColumnNames(
154:                            columnNames, stringMatrix);
155:                }
156:            }
157:
158:            /**
159:             * Returns a <code>MockResultSet</code> with the given ID, containing
160:             * values based on the elements given at construction.
161:             * 
162:             * @param id the ID for the result set. This argument cannot be
163:             *           <code>null</code>.
164:             */
165:            public MockResultSet create(String id) {
166:                if (id != null) {
167:                    MockResultSet resultSet = new MockResultSet(id);
168:                    if (columnNames != null) {
169:                        for (int ii = 0; ii < columnNames.length; ii++) {
170:                            resultSet.addColumn(columnNames[ii]);
171:                        }
172:                    }
173:                    for (int jj = 0; jj < stringMatrix.length; jj++) {
174:                        resultSet.addRow(stringMatrix[jj]);
175:                    }
176:                    return resultSet;
177:                }
178:                throw new IllegalArgumentException(
179:                        "the result set ID cannot be null");
180:            }
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.