KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal JavaDoc;
8 import java.sql.Connection JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.util.Random JavaDoc;
13
14 public class TestMultiOrder extends TestMultiThread {
15     
16     Connection JavaDoc conn;
17     PreparedStatement JavaDoc insertLine;
18     private static final String JavaDoc[] ITEMS = new String JavaDoc[]{"Apples", "Oranges", "Bananas", "Coffee"};
19
20     static int customerCount;
21     static int orderCount;
22     static int orderLineCount;
23     
24     TestMultiOrder(TestMulti base) throws SQLException JavaDoc {
25         super(base);
26         conn = base.getConnection();
27     }
28     
29     void begin() throws SQLException JavaDoc {
30         insertLine = conn.prepareStatement("insert into orderline(orderid, lineid, text, amount) values(?, ?, ?, ?)");
31         insertCustomer();
32     }
33     
34     void end() throws SQLException JavaDoc {
35         conn.close();
36     }
37
38     void operation() throws SQLException JavaDoc {
39         if(random.nextInt(10)==0) {
40             insertCustomer();
41         } else {
42             insertOrder();
43         }
44     }
45
46     private void insertOrder() throws SQLException JavaDoc {
47         PreparedStatement JavaDoc prep = conn.prepareStatement("insert into orders(customer_id , total) values(?, ?)");
48         prep.setInt(1, random.nextInt(getCustomerCount()));
49         BigDecimal JavaDoc total = new BigDecimal JavaDoc("0");
50         prep.setBigDecimal(2, total);
51         prep.executeUpdate();
52         ResultSet JavaDoc rs = prep.getGeneratedKeys();
53         rs.next();
54         int orderId = rs.getInt(1);
55         int lines = random.nextInt(20);
56         for(int i=0; i<lines; i++) {
57             insertLine.setInt(1, orderId);
58             insertLine.setInt(2, i);
59             insertLine.setString(3, ITEMS[random.nextInt(ITEMS.length)]);
60             BigDecimal JavaDoc amount = new BigDecimal JavaDoc(random.nextInt(100) + "." + random.nextInt(10));
61             insertLine.setBigDecimal(4, amount);
62             total = total.add(amount);
63             insertLine.addBatch();
64         }
65         insertLine.executeBatch();
66         increaseOrderLines(lines);
67         prep = conn.prepareStatement("update orders set total = ? where id = ?");
68         prep.setBigDecimal(1, total);
69         prep.setInt(2, orderId);
70         increaseOrders();
71         prep.execute();
72     }
73
74     private void insertCustomer() throws SQLException JavaDoc {
75         PreparedStatement JavaDoc prep = conn.prepareStatement("insert into customer(id, name) values(?, ?)");
76         int customerId = getNextCustomerId();
77         prep.setInt(1, customerId);
78         prep.setString(2, getString(customerId));
79         prep.execute();
80     }
81
82     private String JavaDoc getString(int id) {
83         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
84         Random JavaDoc rnd = new Random JavaDoc(id);
85         int len = rnd.nextInt(40);
86         for(int i=0; i<len; i++) {
87             String JavaDoc s = "bcdfghklmnprstwz";
88             char c = s.charAt(rnd.nextInt(s.length()));
89             buff.append(i == 0 ? Character.toUpperCase(c) : c);
90             s = "aeiou ";
91
92             buff.append(s.charAt(rnd.nextInt(s.length())));
93         }
94         return buff.toString();
95     }
96     
97     synchronized int getNextCustomerId() {
98         return customerCount++;
99     }
100
101     synchronized int increaseOrders() {
102         return orderCount++;
103     }
104
105     synchronized int increaseOrderLines(int count) {
106         return orderLineCount+=count;
107     }
108
109     public int getCustomerCount() {
110         return customerCount;
111     }
112
113     void first() throws SQLException JavaDoc {
114         Connection JavaDoc conn = base.getConnection();
115         conn.createStatement().execute("drop table customer if exists");
116         conn.createStatement().execute("drop table orders if exists");
117         conn.createStatement().execute("drop table orderline if exists");
118         conn.createStatement().execute("create table customer(id int primary key, name varchar, account decimal)");
119         conn.createStatement().execute("create table orders(id int identity primary key, customer_id int, total decimal)");
120         conn.createStatement().execute("create table orderline(orderid int, lineid int, text varchar, amount decimal, primary key(orderid, lineid))");
121         conn.close();
122     }
123
124     void finalTest() throws Exception JavaDoc {
125         conn = base.getConnection();
126         ResultSet JavaDoc rs = conn.createStatement().executeQuery("select count(*) from customer");
127         rs.next();
128         base.check(rs.getInt(1), customerCount);
129         // System.out.println("customers: " + rs.getInt(1));
130

131         rs = conn.createStatement().executeQuery("select count(*) from orders");
132         rs.next();
133         base.check(rs.getInt(1), orderCount);
134         // System.out.println("orders: " + rs.getInt(1));
135

136         rs = conn.createStatement().executeQuery("select count(*) from orderline");
137         rs.next();
138         base.check(rs.getInt(1), orderLineCount);
139         // System.out.println("orderlines: " + rs.getInt(1));
140

141         conn.close();
142     }
143 }
144
Popular Tags