Source Code Cross Referenced for CachedRowSetDataProviderTest.java in  » IDE-Netbeans » visualweb.api.designer » org » netbeans » modules » visualweb » dataprovider » 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 » IDE Netbeans » visualweb.api.designer » org.netbeans.modules.visualweb.dataprovider 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * To change this template, choose Tools | Templates
003:         * and open the template in the editor.
004:         */
005:
006:        package org.netbeans.modules.visualweb.dataprovider;
007:
008:        import com.sun.data.provider.FieldKey;
009:        import com.sun.data.provider.RowKey;
010:        import com.sun.sql.rowset.CachedRowSetXImpl;
011:        import java.io.File;
012:        import junit.framework.TestCase;
013:        import com.sun.data.provider.impl.CachedRowSetDataProvider;
014:
015:        import java.beans.Beans;
016:        import java.sql.Connection;
017:        import java.sql.DriverManager;
018:        import java.sql.PreparedStatement;
019:        import java.sql.SQLException;
020:        import java.util.logging.Level;
021:        import java.util.logging.Logger;
022:        import junit.framework.TestCase;
023:
024:        /**
025:         * This class makes sure that the rowset behaves correctly around what should
026:         * happen if the rowset is executed, not executed, modified, etc.
027:         * 
028:         * @author David
029:         */
030:        public class CachedRowSetDataProviderTest extends TestCase {
031:
032:            private static final Logger LOGGER = Logger
033:                    .getLogger(CachedRowSetDataProviderTest.class.getName());
034:
035:            private static final String DBURL = "jdbc:derby:mydb;create=true";
036:            private static final String TABLENAME = "mytable";
037:            private static final String IDNAME = "id";
038:            private static final String COL1NAME = "col1";
039:            private static final String COL2NAME = "col2";
040:            private static final String ROWID = "row1";
041:            private static final int NUMROWS = 10;
042:
043:            public CachedRowSetDataProviderTest(String testName) {
044:                super (testName);
045:            }
046:
047:            protected void setUp() throws Exception {
048:                try {
049:                    super .setUp();
050:
051:                    // Comment this out to turn off debugging
052:                    LOGGER.setLevel(Level.FINE);
053:
054:                    initDatabase();
055:                } catch (SQLException sqle) {
056:                    reportSQLException(sqle);
057:                } catch (Throwable t) {
058:                    LOGGER.log(Level.SEVERE, "Failed to set up test", t);
059:                    throw new Exception(t);
060:                }
061:            }
062:
063:            private void reportSQLException(SQLException sqle) {
064:                LOGGER.log(Level.SEVERE, null, sqle);
065:
066:                if (sqle.getNextException() != null) {
067:                    reportSQLException(sqle.getNextException());
068:                }
069:            }
070:
071:            protected void tearDown() throws Exception {
072:                super .tearDown();
073:
074:                // Remove the test database so it's not left lying around
075:                String userdir = System.getProperty("user.dir");
076:                File dbdir = new File(userdir + "/" + "mydb");
077:
078:                LOGGER.log(Level.INFO, "userdir is " + userdir);
079:
080:                deleteRecursively(dbdir);
081:
082:                File logfile = new File(userdir + "/derby.log");
083:                if (logfile.exists()) {
084:                    logfile.delete();
085:                }
086:            }
087:
088:            private void deleteRecursively(File file) throws Exception {
089:                if (!file.exists()) {
090:                    return;
091:                }
092:
093:                if (!file.isDirectory()) {
094:                    file.delete();
095:                    return;
096:                }
097:
098:                File[] children = file.listFiles();
099:
100:                for (int i = 0; i < children.length; i++) {
101:                    deleteRecursively(children[i]);
102:                }
103:
104:                file.delete();
105:            }
106:
107:            private void initDatabase() throws Exception {
108:                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
109:                Connection conn = DriverManager.getConnection(DBURL);
110:
111:                try {
112:                    conn.prepareStatement("DROP TABLE " + TABLENAME).execute();
113:                } catch (SQLException sqle) {
114:                    LOGGER.log(Level.FINE, null, sqle);
115:                }
116:
117:                String create = "CREATE TABLE " + TABLENAME + "(" + IDNAME
118:                        + " int primary key, " + COL1NAME + " varchar(255),  "
119:                        + COL2NAME + " varchar(255))";
120:
121:                conn.prepareStatement(create).execute();
122:
123:                PreparedStatement insert = conn.prepareStatement("INSERT INTO "
124:                        + TABLENAME + " VALUES(?, ?, ?)");
125:
126:                for (int i = 1; i <= NUMROWS; i++) {
127:                    insert.setInt(1, i);
128:                    insert.setString(2, "col1_" + i);
129:                    insert.setString(3, "col2_" + i);
130:
131:                    insert.execute();
132:                }
133:
134:            }
135:
136:            /**
137:             * Make sure the data provider detects a change to the rowset command 
138:             * 
139:             * @throws java.lang.Exception
140:             */
141:            public void testCommandChange() throws Exception {
142:                CachedRowSetXImpl rowset = new CachedRowSetXImpl();
143:
144:                CachedRowSetDataProvider provider = new CachedRowSetDataProvider();
145:                provider.setCachedRowSet(rowset);
146:
147:                rowset.setUrl("jdbc:derby:mydb;create=true");
148:
149:                /** 
150:                 * Select only one row and one extra column
151:                 */
152:                rowset.setCommand("SELECT " + IDNAME + ", " + COL1NAME
153:                        + " FROM " + TABLENAME + " WHERE " + IDNAME + " = 2");
154:
155:                rowset.setTableName(TABLENAME);
156:
157:                checkRows(provider, 1, 2);
158:
159:                rowset.setCommand("SELECT " + IDNAME + " FROM " + TABLENAME);
160:
161:                checkRows(provider, NUMROWS, 1);
162:
163:                provider.close();
164:            }
165:
166:            public void testRowCount() {
167:                int currentRowCount = 0;
168:                CachedRowSetXImpl rowset = new CachedRowSetXImpl();
169:                currentRowCount = getRowCount(rowset);
170:                assert (currentRowCount > 0);
171:                assertEquals(currentRowCount, 1);
172:            }
173:
174:            public void testInsertRow() {
175:                int newRowCount = 0;
176:                int currentRowCount = 0;
177:                try {
178:                    CachedRowSetXImpl rowset = new CachedRowSetXImpl();
179:                    Beans beans = new Beans();
180:                    beans.setDesignTime(true);
181:                    CachedRowSetDataProvider provider = new CachedRowSetDataProvider();
182:                    provider.setCachedRowSet(rowset);
183:                    currentRowCount = getRowCount(rowset);
184:
185:                    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
186:                    Connection conn = DriverManager.getConnection(DBURL);
187:
188:                    PreparedStatement insert = conn
189:                            .prepareStatement("INSERT INTO " + TABLENAME
190:                                    + " VALUES(?, ?, ?)");
191:
192:                    for (int i = 10; i <= NUMROWS; i++) {
193:                        insert.setInt(1, i);
194:                        insert.setString(2, "col1_" + i);
195:                        insert.setString(3, "col2_" + i);
196:
197:                        insert.execute();
198:                    }
199:
200:                    newRowCount = getRowCount(rowset);
201:
202:                } catch (ClassNotFoundException ex) {
203:                    LOGGER.log(Level.FINE, null, ex);
204:                } catch (SQLException sqle) {
205:                    LOGGER.log(Level.FINE, null, sqle);
206:                }
207:
208:                assert (newRowCount > 0);
209:                assertEquals(newRowCount, currentRowCount + 1);
210:                assertEquals(currentRowCount, 3);
211:
212:            }
213:
214:            public void testRevertChanges() {
215:                int newRowCount = 0;
216:                int currentRowCount = 0;
217:
218:                try {
219:                    CachedRowSetXImpl rowset = new CachedRowSetXImpl();
220:                    Beans beans = new Beans();
221:                    beans.setDesignTime(true);
222:                    CachedRowSetDataProvider provider = new CachedRowSetDataProvider();
223:                    provider.setCachedRowSet(rowset);
224:
225:                    /* Select only one row and one extra column
226:                     */
227:                    rowset.setCommand("SELECT " + IDNAME + ", " + COL1NAME
228:                            + " FROM " + TABLENAME + " WHERE " + IDNAME
229:                            + " = 1");
230:
231:                    rowset.setTableName(TABLENAME);
232:
233:                    //commit changes
234:                    rowset.execute();
235:                    provider.commitChanges();
236:                    currentRowCount = getRowCount(rowset);
237:
238:                    // add a new row
239:                    FieldKey fk = new FieldKey(IDNAME);
240:                    RowKey rk = new RowKey(ROWID);
241:                    provider.setValue(fk, rk, beans);
242:                    provider.appendRow();
243:                    newRowCount = getRowCount(rowset);
244:                    // test revert changes
245:                    provider.revertChanges();
246:                    assertEquals(newRowCount, currentRowCount);
247:
248:                } catch (SQLException sqle) {
249:                    LOGGER.log(Level.FINE, null, sqle);
250:                }
251:            }
252:
253:            private void checkRows(CachedRowSetDataProvider provider,
254:                    int expectedRows, int expectedFields) {
255:                int numrows = provider.getRowCount();
256:                assert (numrows == expectedRows);
257:
258:                FieldKey[] keys = provider.getFieldKeys();
259:                int numkeys = keys.length;
260:
261:                assert (numkeys == expectedFields);
262:
263:                provider.cursorFirst();
264:
265:                for (int i = 0; i < numrows; i++) {
266:                    for (int j = 0; j < numkeys; j++) {
267:                        Object value = provider.getValue(keys[j]);
268:                        assert (value != null);
269:                    }
270:                }
271:            }
272:
273:            private int getRowCount(CachedRowSetXImpl rowset) {
274:
275:                CachedRowSetDataProvider provider = new CachedRowSetDataProvider();
276:                provider.setCachedRowSet(rowset);
277:
278:                // Select only one row and one extra column
279:                try {
280:                    rowset.setUrl("jdbc:derby:mydb;create=true");
281:                    rowset.setCommand("SELECT " + IDNAME + ", " + COL1NAME
282:                            + " FROM " + TABLENAME + " WHERE " + IDNAME
283:                            + " = 2");
284:                    rowset.setTableName(TABLENAME);
285:
286:                    //commit changes
287:                    rowset.execute();
288:
289:                } catch (SQLException sqle) {
290:                    LOGGER.log(Level.FINE, null, sqle);
291:                }
292:
293:                return provider.getRowCount();
294:            }
295:
296:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.