KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > synth > TestMultiNews


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.synth;
6
7 import java.sql.*;
8
9 public class TestMultiNews extends TestMultiThread {
10     
11     private static final String JavaDoc PREFIX_URL = "http://feeds.wizbangblog.com/WizbangFullFeed?m=";
12     
13     int len = 10000;
14     Connection conn;
15
16     TestMultiNews(TestMulti base) throws SQLException {
17         super(base);
18         conn = base.getConnection();
19     }
20
21     void operation() throws SQLException {
22         if(random.nextInt(10)==0) {
23             conn.close();
24             conn = base.getConnection();
25         } else if(random.nextInt(10)==0) {
26             if(random.nextBoolean()) {
27                 conn.commit();
28             } else {
29                 conn.rollback();
30             }
31         } else if(random.nextInt(10)==0) {
32             conn.setAutoCommit(random.nextBoolean());
33         } else {
34             if(random.nextBoolean()) {
35                 PreparedStatement prep;
36                 if(random.nextBoolean()) {
37                     prep = conn.prepareStatement(
38                         "SELECT * FROM NEWS WHERE FLINK = ?");
39                 } else {
40                     prep = conn.prepareStatement(
41                         "SELECT * FROM NEWS WHERE FVALUE = ?");
42                 }
43                 prep.setString(1, PREFIX_URL + random.nextInt(len));
44                 ResultSet rs = prep.executeQuery();
45                 if(!rs.next()) {
46                     throw new SQLException("expected one row, got none");
47                 }
48                 if(rs.next()) {
49                     throw new SQLException("expected one row, got more");
50                 }
51             } else {
52                 PreparedStatement prep = conn.prepareStatement(
53                         "UPDATE NEWS SET FSTATE = ? WHERE FID = ?");
54                 prep.setInt(1, random.nextInt(100));
55                 prep.setInt(2, random.nextInt(len));
56                 int count = prep.executeUpdate();
57                 if(count != 1) {
58                     throw new SQLException("expected one row, got " + count);
59                 }
60             }
61         }
62     }
63
64     void begin() throws SQLException {
65     }
66
67     void end() throws SQLException {
68         conn.close();
69     }
70
71     void finalTest() throws Exception JavaDoc {
72     }
73
74     void first() throws SQLException {
75         Connection conn = base.getConnection();
76         Statement stat = conn.createStatement();
77         stat.execute(
78                 "CREATE TABLE TEST (ID IDENTITY, NAME VARCHAR)");
79         stat.execute(
80                 "CREATE TABLE NEWS (FID NUMERIC(19) PRIMARY KEY, FCOMMENTS LONGVARCHAR, " +
81                 "FLINK VARCHAR(255), FSTATE INTEGER, FVALUE VARCHAR(255))");
82         stat.execute(
83         "CREATE INDEX IF NOT EXISTS NEWS_GUID_VALUE_INDEX ON NEWS(FVALUE)");
84         stat.execute(
85                 "CREATE INDEX IF NOT EXISTS NEWS_LINK_INDEX ON NEWS(FLINK)");
86         stat.execute(
87                 "CREATE INDEX IF NOT EXISTS NEWS_STATE_INDEX ON NEWS(FSTATE)");
88         PreparedStatement prep = conn.prepareStatement(
89                 "INSERT INTO NEWS (FID, FCOMMENTS, FLINK, FSTATE, FVALUE) VALUES " +
90                 "(?, ?, ?, ?, ?) ");
91         PreparedStatement prep2 = conn.prepareStatement(
92                 "INSERT INTO TEST (NAME) VALUES (?)");
93         for(int i=0; i<len; i++) {
94             int x = random.nextInt(10) * 128;
95             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
96             while(buff.length() < x) {
97                 buff.append("Test ");
98                 buff.append(buff.length());
99                 buff.append(' ');
100             }
101             String JavaDoc comment = buff.toString();
102             prep.setInt(1, i); // FID
103
prep.setString(2, comment); // FCOMMENTS
104
prep.setString(3, PREFIX_URL + i); // FLINK
105
prep.setInt(4, 0); // FSTATE
106
prep.setString(5, PREFIX_URL + i); // FVALUE
107
prep.execute();
108             prep2.setString(1, comment);
109             prep2.execute();
110         }
111     }
112
113 }
114
Popular Tags