Source Code Cross Referenced for TestSimpleIndex.java in  » Database-DBMS » h2database » org » h2 » test » synth » 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 » Database DBMS » h2database » org.h2.test.synth 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003:         * (license2)
004:         * Initial Developer: H2 Group
005:         */
006:        package org.h2.test.synth;
007:
008:        import java.sql.Connection;
009:        import java.sql.ResultSet;
010:        import java.sql.SQLException;
011:        import java.sql.Statement;
012:
013:        import org.h2.test.TestBase;
014:        import org.h2.test.synth.sql.RandomGen;
015:
016:        /**
017:         * A test that runs random operations against a table to test the various index
018:         * implementations.
019:         */
020:        public class TestSimpleIndex extends TestBase {
021:
022:            Connection conn;
023:            Statement stat;
024:            RandomGen random;
025:
026:            public void test() throws Exception {
027:                deleteDb("simpleIndex");
028:                conn = getConnection("simpleIndex");
029:                random = new RandomGen(null);
030:                stat = conn.createStatement();
031:                for (int i = 0; i < 10000; i++) {
032:                    testIndex(i);
033:                }
034:            }
035:
036:            private void testIndex(int seed) throws Exception {
037:                random.setSeed(seed);
038:                String unique = random.nextBoolean() ? "UNIQUE " : "";
039:                int len = random.getInt(2) + 1;
040:                StringBuffer buff = new StringBuffer();
041:                for (int i = 0; i < len; i++) {
042:                    if (i > 0) {
043:                        buff.append(", ");
044:                    }
045:                    buff.append((char) ('A' + random.getInt(3)));
046:                }
047:                String cols = buff.toString();
048:                execute("CREATE MEMORY TABLE TEST_M(A INT, B INT, C INT, DATA VARCHAR(255))");
049:                execute("CREATE CACHED TABLE TEST_D(A INT, B INT, C INT, DATA VARCHAR(255))");
050:                execute("CREATE MEMORY TABLE TEST_MI(A INT, B INT, C INT, DATA VARCHAR(255))");
051:                execute("CREATE CACHED TABLE TEST_DI(A INT, B INT, C INT, DATA VARCHAR(255))");
052:                execute("CREATE " + unique + "INDEX M ON TEST_MI(" + cols + ")");
053:                execute("CREATE " + unique + "INDEX D ON TEST_DI(" + cols + ")");
054:                for (int i = 0; i < 100; i++) {
055:                    println("i=" + i);
056:                    testRows(i);
057:                }
058:                execute("DROP INDEX M");
059:                execute("DROP INDEX D");
060:                execute("DROP TABLE TEST_M");
061:                execute("DROP TABLE TEST_D");
062:                execute("DROP TABLE TEST_MI");
063:                execute("DROP TABLE TEST_DI");
064:            }
065:
066:            private void testRows(int id) throws Exception {
067:                String a = randomValue(), b = randomValue(), c = randomValue();
068:                String data = a + "/" + b + "/" + c;
069:                String sql = "VALUES(" + a + ", " + b + ", " + c + ", '" + data
070:                        + "')";
071:                boolean em, ed;
072:                // if(id==73) {
073:                // print("halt");
074:                // }
075:                try {
076:                    execute("INSERT INTO TEST_MI " + sql);
077:                    em = false;
078:                } catch (SQLException e) {
079:                    em = true;
080:                }
081:                try {
082:                    execute("INSERT INTO TEST_DI " + sql);
083:                    ed = false;
084:                } catch (SQLException e) {
085:                    ed = true;
086:                }
087:                if (em != ed) {
088:                    error("different result: ");
089:                }
090:                if (!em) {
091:                    execute("INSERT INTO TEST_M " + sql);
092:                    execute("INSERT INTO TEST_D " + sql);
093:                }
094:                StringBuffer buff = new StringBuffer("WHERE 1=1");
095:                int len = random.getLog(10);
096:                for (int i = 0; i < len; i++) {
097:                    buff.append(" AND ");
098:                    buff.append('A' + random.getInt(3));
099:                    switch (random.getInt(10)) {
100:                    case 0:
101:                        buff.append("<");
102:                        buff.append((random.getInt(100) - 50));
103:                        break;
104:                    case 1:
105:                        buff.append("<=");
106:                        buff.append((random.getInt(100) - 50));
107:                        break;
108:                    case 2:
109:                        buff.append(">");
110:                        buff.append((random.getInt(100) - 50));
111:                        break;
112:                    case 3:
113:                        buff.append(">=");
114:                        buff.append((random.getInt(100) - 50));
115:                        break;
116:                    case 4:
117:                        buff.append("<>");
118:                        buff.append((random.getInt(100) - 50));
119:                        break;
120:                    case 5:
121:                        buff.append(" IS NULL");
122:                        break;
123:                    case 6:
124:                        buff.append(" IS NOT NULL");
125:                        break;
126:                    default:
127:                        buff.append("=");
128:                        buff.append((random.getInt(100) - 50));
129:                    }
130:                }
131:                String where = buff.toString();
132:                String r1 = getResult("SELECT DATA FROM TEST_M " + where
133:                        + " ORDER BY DATA");
134:                String r2 = getResult("SELECT DATA FROM TEST_D " + where
135:                        + " ORDER BY DATA");
136:                String r3 = getResult("SELECT DATA FROM TEST_MI " + where
137:                        + " ORDER BY DATA");
138:                String r4 = getResult("SELECT DATA FROM TEST_DI " + where
139:                        + " ORDER BY DATA");
140:                check(r1, r2);
141:                check(r1, r3);
142:                check(r1, r4);
143:            }
144:
145:            private String getResult(String sql) throws Exception {
146:                ResultSet rs = stat.executeQuery(sql);
147:                StringBuffer buff = new StringBuffer();
148:                while (rs.next()) {
149:                    buff.append(rs.getString(1));
150:                    buff.append("; ");
151:                }
152:                rs.close();
153:                return buff.toString();
154:            }
155:
156:            private String randomValue() {
157:                return random.getInt(10) == 0 ? "NULL" : ""
158:                        + (random.getInt(100) - 50);
159:            }
160:
161:            private void execute(String sql) throws Exception {
162:                try {
163:                    println(sql + ";");
164:                    stat.execute(sql);
165:                    println("> update count: 1");
166:                } catch (SQLException e) {
167:                    println("> exception");
168:                    throw e;
169:                }
170:            }
171:
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.