Source Code Cross Referenced for SortedTable.java in  » Testing » DbUnit » org » dbunit » dataset » 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 » DbUnit » org.dbunit.dataset 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *
003:         * The DbUnit Database Testing Framework
004:         * Copyright (C)2002-2004, DbUnit.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or (at your option) any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         */
021:
022:        package org.dbunit.dataset;
023:
024:        import org.slf4j.Logger;
025:        import org.slf4j.LoggerFactory;
026:
027:        import org.dbunit.DatabaseUnitRuntimeException;
028:        import org.dbunit.dataset.datatype.DataType;
029:
030:        import java.util.Arrays;
031:        import java.util.Comparator;
032:
033:        /**
034:         * This is a ITable decorator that provide a sorted view of the decorated table.
035:         * This implementation does not keep a separate copy of the decorated table data.
036:         *
037:         * @author Manuel Laflamme
038:         * @version $Revision: 554 $
039:         * @since Feb 19, 2003
040:         */
041:        public class SortedTable extends AbstractTable {
042:
043:            /**
044:             * Logger for this class
045:             */
046:            private static final Logger logger = LoggerFactory
047:                    .getLogger(SortedTable.class);
048:
049:            private final ITable _table;
050:            private final Column[] _columns;
051:            private Integer[] _indexes;
052:
053:            /**
054:             * Sort the decorated table by specified columns order.
055:             */
056:            public SortedTable(ITable table, Column[] columns) {
057:                _table = table;
058:                _columns = columns;
059:            }
060:
061:            /**
062:             * Sort the decorated table by specified columns order.
063:             */
064:            public SortedTable(ITable table, String[] columnNames)
065:                    throws DataSetException {
066:                _table = table;
067:                _columns = new Column[columnNames.length];
068:
069:                Column[] columns = table.getTableMetaData().getColumns();
070:                for (int i = 0; i < columnNames.length; i++) {
071:                    String columnName = columnNames[i];
072:                    _columns[i] = DataSetUtils.getColumn(columnName, columns);
073:                }
074:            }
075:
076:            /**
077:             * Sort the decorated table by specified metadata columns order. All
078:             * metadata columns will be used.
079:             */
080:            public SortedTable(ITable table, ITableMetaData metaData)
081:                    throws DataSetException {
082:                this (table, metaData.getColumns());
083:            }
084:
085:            /**
086:             * Sort the decorated table by its own columns order. All
087:             * table columns will be used.
088:             */
089:            public SortedTable(ITable table) throws DataSetException {
090:                this (table, table.getTableMetaData());
091:            }
092:
093:            private int getOriginalRowIndex(int row) throws DataSetException {
094:                logger.debug("getOriginalRowIndex(row=" + row + ") - start");
095:
096:                if (_indexes == null) {
097:                    Integer[] indexes = new Integer[getRowCount()];
098:                    for (int i = 0; i < indexes.length; i++) {
099:                        indexes[i] = new Integer(i);
100:                    }
101:
102:                    try {
103:                        Arrays.sort(indexes, new RowComparator());
104:                    } catch (DatabaseUnitRuntimeException e) {
105:                        logger.error("getOriginalRowIndex()", e);
106:
107:                        throw (DataSetException) e.getException();
108:                    }
109:
110:                    _indexes = indexes;
111:                }
112:
113:                return _indexes[row].intValue();
114:            }
115:
116:            ////////////////////////////////////////////////////////////////////////////
117:            // ITable interface
118:
119:            public ITableMetaData getTableMetaData() {
120:                logger.debug("getTableMetaData() - start");
121:
122:                return _table.getTableMetaData();
123:            }
124:
125:            public int getRowCount() {
126:                logger.debug("getRowCount() - start");
127:
128:                return _table.getRowCount();
129:            }
130:
131:            public Object getValue(int row, String column)
132:                    throws DataSetException {
133:                logger.debug("getValue(row=" + row + ", column=" + column
134:                        + ") - start");
135:
136:                assertValidRowIndex(row);
137:
138:                return _table.getValue(getOriginalRowIndex(row), column);
139:            }
140:
141:            ////////////////////////////////////////////////////////////////////////////
142:            // Comparator interface
143:
144:            private class RowComparator implements  Comparator {
145:
146:                /**
147:                 * Logger for this class
148:                 */
149:                private final Logger logger = LoggerFactory
150:                        .getLogger(RowComparator.class);
151:
152:                public int compare(Object o1, Object o2) {
153:                    logger.debug("compare(o1=" + o1 + ", o2=" + o2
154:                            + ") - start");
155:
156:                    Integer i1 = (Integer) o1;
157:                    Integer i2 = (Integer) o2;
158:
159:                    try {
160:                        for (int i = 0; i < _columns.length; i++) {
161:                            String columnName = _columns[i].getColumnName();
162:                            Object value1 = _table.getValue(i1.intValue(),
163:                                    columnName);
164:                            Object value2 = _table.getValue(i2.intValue(),
165:                                    columnName);
166:
167:                            if (value1 == null && value2 == null) {
168:                                continue;
169:                            }
170:
171:                            if (value1 == null && value2 != null) {
172:                                return -1;
173:                            }
174:
175:                            if (value1 != null && value2 == null) {
176:                                return 1;
177:                            }
178:
179:                            String stringValue1 = DataType.asString(value1);
180:                            String stringValue2 = DataType.asString(value2);
181:                            int result = stringValue1.compareTo(stringValue2);
182:                            if (result != 0) {
183:                                return result;
184:                            }
185:                        }
186:                    } catch (DataSetException e) {
187:                        logger.error("compare()", e);
188:
189:                        throw new DatabaseUnitRuntimeException(e);
190:                    }
191:
192:                    return 0;
193:                }
194:            }
195:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.