001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.OracleDatabase;
005: import liquibase.database.sql.RawSqlStatement;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.database.structure.DatabaseObject;
008: import liquibase.util.StreamUtil;
009: import static org.easymock.EasyMock.*;
010: import static org.easymock.classextension.EasyMock.createMock;
011: import static org.easymock.classextension.EasyMock.replay;
012: import static org.junit.Assert.*;
013: import org.junit.Test;
014: import org.w3c.dom.Document;
015: import org.w3c.dom.Element;
016:
017: import java.io.StringWriter;
018: import java.sql.Connection;
019: import java.sql.Statement;
020: import java.util.Set;
021:
022: /**
023: * Base test class for changes
024: */
025: public abstract class AbstractChangeTest {
026:
027: @Test
028: public abstract void getRefactoringName() throws Exception;
029:
030: @Test
031: public abstract void generateStatement() throws Exception;
032:
033: @Test
034: public abstract void getConfirmationMessage() throws Exception;
035:
036: @Test
037: public abstract void createNode() throws Exception;
038:
039: @Test
040: public void saveStatement() throws Exception {
041: Change change = new AbstractChange("test", "Test Refactoring") {
042: public SqlStatement[] generateStatements(Database database) {
043: return new SqlStatement[] { new RawSqlStatement(
044: "GENERATED STATEMENT") };
045: }
046:
047: public String getConfirmationMessage() {
048: return null;
049: }
050:
051: public Element createNode(Document changeLogFileDOM) {
052: return null;
053: }
054:
055: public Set<DatabaseObject> getAffectedDatabaseObjects() {
056: return null;
057: }
058: };
059:
060: StringWriter stringWriter = new StringWriter();
061:
062: OracleDatabase database = new OracleDatabase();
063: change.saveStatements(database, stringWriter);
064:
065: assertEquals("GENERATED STATEMENT;"
066: + StreamUtil.getLineSeparator()
067: + StreamUtil.getLineSeparator(), stringWriter
068: .getBuffer().toString());
069: }
070:
071: @Test
072: public void executeStatement() throws Exception {
073: Change change = new AbstractChange("test", "Test Refactorign") {
074: public SqlStatement[] generateStatements(Database database) {
075: return new SqlStatement[] { new RawSqlStatement(
076: "GENERATED STATEMENT;") };
077: }
078:
079: public String getConfirmationMessage() {
080: return null;
081: }
082:
083: public Element createNode(Document changeLogFileDOM) {
084: return null;
085: }
086:
087: public Set<DatabaseObject> getAffectedDatabaseObjects() {
088: return null;
089: }
090: };
091:
092: Connection conn = createMock(Connection.class);
093: Statement statement = createMock(Statement.class);
094: conn.setAutoCommit(false);
095: expect(conn.createStatement()).andReturn(statement);
096:
097: expect(statement.execute("GENERATED STATEMENT;"))
098: .andStubReturn(true);
099: statement.close();
100: expectLastCall();
101: replay(conn);
102: replay(statement);
103:
104: OracleDatabase database = new OracleDatabase();
105: database.setConnection(conn);
106:
107: change.executeStatements(database);
108:
109: verify(conn);
110: verify(statement);
111: }
112: }
|