01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.db;
07:
08: import java.sql.Connection;
09: import java.sql.ResultSet;
10: import java.sql.Statement;
11:
12: import org.h2.test.TestBase;
13:
14: /**
15: * Tests the sequence feature of this database.
16: */
17: public class TestSequence extends TestBase {
18:
19: public void test() throws Exception {
20: testCache();
21: testTwo();
22: }
23:
24: private void testCache() throws Exception {
25: if (config.memory) {
26: return;
27: }
28: deleteDb("sequence");
29: Connection conn = getConnection("sequence");
30: Statement stat = conn.createStatement();
31: stat.execute("create sequence test_Sequence");
32: stat.execute("create sequence test_Sequence3 cache 3");
33: conn.close();
34: conn = getConnection("sequence");
35: stat = conn.createStatement();
36: stat.execute("call next value for test_Sequence");
37: stat.execute("call next value for test_Sequence3");
38: ResultSet rs = stat
39: .executeQuery("select * from information_schema.sequences order by sequence_name");
40: rs.next();
41: check(rs.getString("SEQUENCE_NAME"), "TEST_SEQUENCE");
42: check(rs.getString("CACHE"), "32");
43: rs.next();
44: check(rs.getString("SEQUENCE_NAME"), "TEST_SEQUENCE3");
45: check(rs.getString("CACHE"), "3");
46: checkFalse(rs.next());
47: conn.close();
48: }
49:
50: private void testTwo() throws Exception {
51: deleteDb("sequence");
52: Connection conn = getConnection("sequence");
53: Statement stat = conn.createStatement();
54: stat.execute("create sequence testSequence");
55: conn.setAutoCommit(false);
56:
57: Connection conn2 = getConnection("sequence");
58: Statement stat2 = conn2.createStatement();
59: conn2.setAutoCommit(false);
60:
61: long last = 0;
62: for (int i = 0; i < 100; i++) {
63: long v1 = getNext(stat);
64: check(v1 > last);
65: last = v1;
66: for (int j = 0; j < 100; j++) {
67: long v2 = getNext(stat2);
68: check(v2 > last);
69: last = v2;
70: }
71: }
72:
73: conn2.close();
74: conn.close();
75: }
76:
77: private long getNext(Statement stat) throws Exception {
78: ResultSet rs = stat
79: .executeQuery("call next value for testSequence");
80: rs.next();
81: long value = rs.getLong(1);
82: return value;
83: }
84: }
|