| 1 5 package org.h2.test.synth; 6 7 import java.math.BigDecimal ; 8 import java.sql.Connection ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.util.Random ; 13 14 public class TestMultiOrder extends TestMultiThread { 15 16 Connection conn; 17 PreparedStatement insertLine; 18 private static final String [] ITEMS = new String []{"Apples", "Oranges", "Bananas", "Coffee"}; 19 20 static int customerCount; 21 static int orderCount; 22 static int orderLineCount; 23 24 TestMultiOrder(TestMulti base) throws SQLException { 25 super(base); 26 conn = base.getConnection(); 27 } 28 29 void begin() throws SQLException { 30 insertLine = conn.prepareStatement("insert into orderline(orderid, lineid, text, amount) values(?, ?, ?, ?)"); 31 insertCustomer(); 32 } 33 34 void end() throws SQLException { 35 conn.close(); 36 } 37 38 void operation() throws SQLException { 39 if(random.nextInt(10)==0) { 40 insertCustomer(); 41 } else { 42 insertOrder(); 43 } 44 } 45 46 private void insertOrder() throws SQLException { 47 PreparedStatement prep = conn.prepareStatement("insert into orders(customer_id , total) values(?, ?)"); 48 prep.setInt(1, random.nextInt(getCustomerCount())); 49 BigDecimal total = new BigDecimal ("0"); 50 prep.setBigDecimal(2, total); 51 prep.executeUpdate(); 52 ResultSet 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 amount = new BigDecimal (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 { 75 PreparedStatement 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 getString(int id) { 83 StringBuffer buff = new StringBuffer (); 84 Random rnd = new Random (id); 85 int len = rnd.nextInt(40); 86 for(int i=0; i<len; i++) { 87 String 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 { 114 Connection 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 { 125 conn = base.getConnection(); 126 ResultSet rs = conn.createStatement().executeQuery("select count(*) from customer"); 127 rs.next(); 128 base.check(rs.getInt(1), customerCount); 129 131 rs = conn.createStatement().executeQuery("select count(*) from orders"); 132 rs.next(); 133 base.check(rs.getInt(1), orderCount); 134 136 rs = conn.createStatement().executeQuery("select count(*) from orderline"); 137 rs.next(); 138 base.check(rs.getInt(1), orderLineCount); 139 141 conn.close(); 142 } 143 } 144 | Popular Tags |