001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.structure.DatabaseSnapshot;
005: import liquibase.test.DatabaseTestTemplate;
006: import liquibase.test.SqlStatementDatabaseTest;
007: import liquibase.test.TestContext;
008: import static org.junit.Assert.*;
009: import org.junit.Test;
010:
011: public class CreateIndexStatementTest extends AbstractSqlStatementTest {
012: private static final String TABLE_NAME = "CreateIndexTest";
013: private static final String INDEX_NAME = "IDX_CreateIndexTest";
014: private static final String COLUMN_NAME = "testCol";
015: private static final String COLUMN_NAME2 = "testCol2";
016:
017: protected void setupDatabase(Database database) throws Exception {
018: dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME)
019: .addPrimaryKeyColumn("id", "int").addColumn(
020: COLUMN_NAME, "varchar(50)").addColumn(
021: COLUMN_NAME2, "varchar(50)"), database);
022: dropAndCreateTable(new CreateTableStatement(
023: TestContext.ALT_SCHEMA, TABLE_NAME)
024: .addPrimaryKeyColumn("id", "int").addColumn(
025: COLUMN_NAME, "varchar(50)").addColumn(
026: COLUMN_NAME2, "varchar(50)"), database);
027: }
028:
029: protected CreateIndexStatement generateTestStatement() {
030: return new CreateIndexStatement(null, null, null);
031: }
032:
033: @Test
034: public void execute_singleColumnDefaultSchema() throws Exception {
035: new DatabaseTestTemplate()
036: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
037: null, new CreateIndexStatement(INDEX_NAME,
038: null, TABLE_NAME, COLUMN_NAME)) {
039:
040: protected void preExecuteAssert(
041: DatabaseSnapshot snapshot) {
042: assertNull(snapshot.getIndex(INDEX_NAME));
043: }
044:
045: protected void postExecuteAssert(
046: DatabaseSnapshot snapshot) {
047: assertNotNull(snapshot.getIndex(INDEX_NAME));
048: assertEquals(COLUMN_NAME.toUpperCase(),
049: snapshot.getIndex(INDEX_NAME)
050: .getColumnNames().toUpperCase());
051: }
052: });
053: }
054:
055: @Test
056: public void execute_alternateSchema() throws Exception {
057: new DatabaseTestTemplate()
058: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
059: TestContext.ALT_SCHEMA,
060: new CreateIndexStatement(INDEX_NAME,
061: TestContext.ALT_SCHEMA, TABLE_NAME,
062: COLUMN_NAME)) {
063:
064: protected void preExecuteAssert(
065: DatabaseSnapshot snapshot) {
066: assertNull(snapshot.getIndex(INDEX_NAME));
067: }
068:
069: protected void postExecuteAssert(
070: DatabaseSnapshot snapshot) {
071: //todo: assert that index was created in the correct location. What schema does it go in?
072: // snapshot = new DatabaseSnapshot(database, TestContext.ALT_SCHEMA);
073: // assertNotNull(snapshot.getIndex(INDEX_NAME));
074: // assertEquals(COLUMN_NAME.toUpperCase(), snapshot.getIndex(INDEX_NAME).getColumnNames().toUpperCase());
075: }
076: });
077: }
078:
079: @Test
080: public void execute_alternateTablespace() throws Exception {
081: new DatabaseTestTemplate()
082: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
083: null,
084: new CreateIndexStatement(INDEX_NAME, null,
085: TABLE_NAME, COLUMN_NAME)
086: .setTablespace(TestContext.ALT_TABLESPACE)) {
087: protected void preExecuteAssert(
088: DatabaseSnapshot snapshot) {
089: assertNull(snapshot.getIndex(INDEX_NAME));
090: }
091:
092: protected void postExecuteAssert(
093: DatabaseSnapshot snapshot) {
094: assertNotNull(snapshot.getIndex(INDEX_NAME));
095: assertEquals(COLUMN_NAME.toUpperCase(),
096: snapshot.getIndex(INDEX_NAME)
097: .getColumnNames().toUpperCase());
098: //todo: assert tablespace location
099: }
100: });
101: }
102:
103: @Test
104: public void execute_multiColumnDefaultSchema() throws Exception {
105: new DatabaseTestTemplate()
106: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
107: null, new CreateIndexStatement(INDEX_NAME,
108: null, TABLE_NAME, COLUMN_NAME,
109: COLUMN_NAME2)) {
110:
111: protected void preExecuteAssert(
112: DatabaseSnapshot snapshot) {
113: assertNull(snapshot.getIndex(INDEX_NAME));
114: }
115:
116: protected void postExecuteAssert(
117: DatabaseSnapshot snapshot) {
118: assertNotNull(snapshot.getIndex(INDEX_NAME));
119: assertEquals(
120: (COLUMN_NAME + ", " + COLUMN_NAME2)
121: .toUpperCase(), snapshot
122: .getIndex(INDEX_NAME)
123: .getColumnNames().toUpperCase());
124: }
125: });
126: }
127: }
|