001: package liquibase.database.sql;
002:
003: import liquibase.database.*;
004: import liquibase.database.structure.DatabaseSnapshot;
005: import liquibase.exception.JDBCException;
006: import liquibase.test.DatabaseTest;
007: import liquibase.test.DatabaseTestTemplate;
008: import liquibase.test.SqlStatementDatabaseTest;
009: import liquibase.test.TestContext;
010: import static org.junit.Assert.*;
011: import org.junit.Test;
012:
013: public class CreateSequenceStatementTest extends
014: AbstractSqlStatementTest {
015:
016: private static final String SEQ_NAME = "createtest_seq"
017: .toUpperCase();
018:
019: protected void setupDatabase(Database database) throws Exception {
020: dropSequenceIfExists(null, SEQ_NAME, database);
021:
022: dropSequenceIfExists(TestContext.ALT_SCHEMA, SEQ_NAME, database);
023: }
024:
025: protected SqlStatement generateTestStatement() {
026: return new CreateSequenceStatement(null, null);
027: }
028:
029: @Test
030: public void supportsDatabase() throws Exception {
031: new DatabaseTestTemplate()
032: .testOnAvailableDatabases(new DatabaseTest() {
033: public void performTest(Database database)
034: throws Exception {
035: if (database.supportsSequences()) {
036: assertTrue(generateTestStatement()
037: .supportsDatabase(database));
038: } else {
039: assertFalse(generateTestStatement()
040: .supportsDatabase(database));
041: }
042: }
043: });
044: }
045:
046: @Test
047: public void execute() throws Exception {
048: new DatabaseTestTemplate()
049: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
050: null, new CreateSequenceStatement(null,
051: SEQ_NAME)) {
052: protected void preExecuteAssert(
053: DatabaseSnapshot snapshot) {
054: assertNull(snapshot.getSequence(SEQ_NAME));
055: }
056:
057: protected void postExecuteAssert(
058: DatabaseSnapshot snapshot) {
059: assertNotNull(snapshot.getSequence(SEQ_NAME));
060: }
061: });
062: }
063:
064: @Test
065: public void execute_withSchema() throws Exception {
066: new DatabaseTestTemplate()
067: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
068: TestContext.ALT_SCHEMA,
069: new CreateSequenceStatement(
070: TestContext.ALT_SCHEMA, SEQ_NAME)) {
071: protected void preExecuteAssert(
072: DatabaseSnapshot snapshot) {
073: assertNull(snapshot.getSequence(SEQ_NAME));
074: }
075:
076: protected void postExecuteAssert(
077: DatabaseSnapshot snapshot) {
078: assertNotNull(snapshot.getSequence(SEQ_NAME));
079: }
080: });
081: }
082:
083: @Test
084: public void execute_startValue() throws Exception {
085: new DatabaseTestTemplate()
086: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
087: null, new CreateSequenceStatement(null,
088: SEQ_NAME).setStartValue(1000)) {
089:
090: protected boolean expectedException(
091: Database database, JDBCException exception) {
092: return database instanceof FirebirdDatabase;
093: }
094:
095: protected void preExecuteAssert(
096: DatabaseSnapshot snapshot) {
097: assertNull(snapshot.getSequence(SEQ_NAME));
098: }
099:
100: protected void postExecuteAssert(
101: DatabaseSnapshot snapshot) {
102: assertNotNull(snapshot.getSequence(SEQ_NAME));
103: //todo: assert start value
104: }
105: });
106: }
107:
108: @Test
109: public void execute_incrementBy() throws Exception {
110: new DatabaseTestTemplate()
111: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
112: null, new CreateSequenceStatement(null,
113: SEQ_NAME).setIncrementBy(5)) {
114:
115: protected boolean expectedException(
116: Database database, JDBCException exception) {
117: return database instanceof FirebirdDatabase;
118: }
119:
120: protected void preExecuteAssert(
121: DatabaseSnapshot snapshot) {
122: assertNull(snapshot.getSequence(SEQ_NAME));
123: }
124:
125: protected void postExecuteAssert(
126: DatabaseSnapshot snapshot) {
127: assertNotNull(snapshot.getSequence(SEQ_NAME));
128: //todo: assert increment by value
129: }
130:
131: });
132: }
133:
134: @Test
135: public void execute_minValue() throws Exception {
136: new DatabaseTestTemplate()
137: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
138: null, new CreateSequenceStatement(null,
139: SEQ_NAME).setMinValue(15)) {
140:
141: protected boolean expectedException(
142: Database database, JDBCException exception) {
143: return database instanceof FirebirdDatabase
144: || database instanceof HsqlDatabase;
145: }
146:
147: protected void preExecuteAssert(
148: DatabaseSnapshot snapshot) {
149: assertNull(snapshot.getSequence(SEQ_NAME));
150: }
151:
152: protected void postExecuteAssert(
153: DatabaseSnapshot snapshot) {
154: assertNotNull(snapshot.getSequence(SEQ_NAME));
155: //todo: assert min valuevalue
156: }
157: });
158: }
159:
160: @Test
161: public void execute_maxValue() throws Exception {
162: new DatabaseTestTemplate()
163: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
164: null, new CreateSequenceStatement(null,
165: SEQ_NAME).setMaxValue(50)) {
166:
167: protected boolean expectedException(
168: Database database, JDBCException exception) {
169: return database instanceof FirebirdDatabase
170: || database instanceof HsqlDatabase;
171: }
172:
173: protected void preExecuteAssert(
174: DatabaseSnapshot snapshot) {
175: assertNull(snapshot.getSequence(SEQ_NAME));
176: }
177:
178: protected void postExecuteAssert(
179: DatabaseSnapshot snapshot) {
180: assertNotNull(snapshot.getSequence(SEQ_NAME));
181: //todo: assert max value
182: }
183: });
184: }
185:
186: @Test
187: public void execute_order() throws Exception {
188: new DatabaseTestTemplate()
189: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
190: null, new CreateSequenceStatement(null,
191: SEQ_NAME).setOrdered(true)) {
192: protected boolean expectedException(
193: Database database, JDBCException exception) {
194: return !(database instanceof OracleDatabase || database instanceof DB2Database);
195: }
196:
197: protected void preExecuteAssert(
198: DatabaseSnapshot snapshot) {
199: assertNull(snapshot.getSequence(SEQ_NAME));
200: }
201:
202: protected void postExecuteAssert(
203: DatabaseSnapshot snapshot) {
204: assertNotNull(snapshot.getSequence(SEQ_NAME));
205: //todo: assert max value
206: }
207: });
208: }
209: }
|