001: package liquibase.change;
002:
003: import liquibase.database.MSSQLDatabase;
004: import liquibase.database.OracleDatabase;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.exception.StatementNotSupportedOnDatabaseException;
007: import liquibase.exception.UnsupportedChangeException;
008: import liquibase.util.XMLUtil;
009: import static org.junit.Assert.*;
010: import org.junit.Before;
011: import org.junit.Test;
012: import org.w3c.dom.Element;
013:
014: import javax.xml.parsers.DocumentBuilderFactory;
015:
016: public class AbstractSQLChangeTest extends AbstractChangeTest {
017:
018: private RawSQLChange refactoring;
019:
020: @Before
021: public void setUp() throws Exception {
022: refactoring = new RawSQLChange();
023: }
024:
025: @Test
026: public void getRefactoringName() throws Exception {
027: assertEquals("Custom SQL", new RawSQLChange().getChangeName());
028: }
029:
030: @Test
031: public void generateStatement() throws Exception {
032: refactoring.setSql("SQL STATEMENT HERE");
033: OracleDatabase database = new OracleDatabase();
034: assertEquals("SQL STATEMENT HERE", refactoring
035: .generateStatements(database)[0]
036: .getSqlStatement(database));
037: }
038:
039: @Test
040: public void getConfirmationMessage() throws Exception {
041: assertEquals("Custom SQL executed", refactoring
042: .getConfirmationMessage());
043: }
044:
045: @Test
046: public void createNode() throws Exception {
047: refactoring.setSql("SOME SQL HERE");
048:
049: Element element = refactoring.createNode(DocumentBuilderFactory
050: .newInstance().newDocumentBuilder().newDocument());
051: assertEquals("sql", element.getTagName());
052:
053: assertEquals("SOME SQL HERE", XMLUtil.getTextContent(element));
054: }
055:
056: @Test
057: public void multiLineSQLFileSemiColon() throws Exception {
058: SQLFileChange change2 = new SQLFileChange();
059: change2.setSql("SELECT * FROM customer;\n"
060: + "SELECT * from table;");
061: OracleDatabase database = new OracleDatabase();
062: SqlStatement[] statements = change2
063: .generateStatements(database);
064:
065: assertEquals(2, statements.length);
066: assertEquals("SELECT * FROM customer", statements[0]
067: .getSqlStatement(database));
068: assertEquals("SELECT * from table", statements[1]
069: .getSqlStatement(database));
070: }
071:
072: @Test
073: public void singleLineEndInSemiColon() throws Exception {
074: SQLFileChange change2 = new SQLFileChange();
075: change2.setSql("SELECT * FROM customer;");
076: OracleDatabase database = new OracleDatabase();
077: SqlStatement[] statements = change2
078: .generateStatements(database);
079: assertEquals(1, statements.length);
080: assertEquals("SELECT * FROM customer", statements[0]
081: .getSqlStatement(database));
082: }
083:
084: @Test
085: public void singleLineEndGo() throws Exception {
086: SQLFileChange change2 = new SQLFileChange();
087: change2.setSql("SELECT * FROM customer\ngo");
088: MSSQLDatabase database = new MSSQLDatabase();
089: SqlStatement[] statements = change2
090: .generateStatements(database);
091: assertEquals(1, statements.length);
092: assertEquals("SELECT * FROM customer", statements[0]
093: .getSqlStatement(database));
094: }
095:
096: @Test
097: public void singleLineBeginGo() throws Exception {
098: SQLFileChange change2 = new SQLFileChange();
099: change2.setSql("goSELECT * FROM customer\ngo");
100: MSSQLDatabase database = new MSSQLDatabase();
101: SqlStatement[] statements = change2
102: .generateStatements(database);
103: assertEquals(1, statements.length);
104: assertEquals("goSELECT * FROM customer", statements[0]
105: .getSqlStatement(database));
106: }
107:
108: @Test
109: public void multiLineSQLFileGoShouldFind() throws Exception {
110: SQLFileChange change2 = new SQLFileChange();
111: change2.setSql("SELECT * FROM customer\ngo\n"
112: + "SELECT * from table\ngo");
113: MSSQLDatabase database = new MSSQLDatabase();
114: SqlStatement[] statements = change2
115: .generateStatements(database);
116: assertEquals(2, statements.length);
117: assertEquals("SELECT * FROM customer", statements[0]
118: .getSqlStatement(database));
119: assertEquals("SELECT * from table", statements[1]
120: .getSqlStatement(database));
121: }
122:
123: @Test
124: public void multiLineSQLFileGoShouldNotFind() throws Exception {
125: SQLFileChange change2 = new SQLFileChange();
126: change2.setSql("SELECT * FROM go\ngo\n"
127: + "SELECT * from gogo\ngo\n");
128: MSSQLDatabase database = new MSSQLDatabase();
129: SqlStatement[] statements = change2
130: .generateStatements(database);
131:
132: assertEquals(2, statements.length);
133: assertEquals("SELECT * FROM go", statements[0]
134: .getSqlStatement(database));
135: assertEquals("SELECT * from gogo", statements[1]
136: .getSqlStatement(database));
137: }
138:
139: @Test
140: public void stripComments() throws UnsupportedChangeException,
141: StatementNotSupportedOnDatabaseException {
142: SQLFileChange change2 = new SQLFileChange();
143: change2.setSql("SELECT * FROM x\n -- A comment");
144: change2.setStripComments(true);
145: MSSQLDatabase database = new MSSQLDatabase();
146: SqlStatement[] statements = change2
147: .generateStatements(database);
148:
149: assertEquals(1, statements.length);
150: assertEquals("SELECT * FROM x\n", statements[0]
151: .getSqlStatement(database));
152: }
153:
154: @Test
155: public void turnOffSplitting() throws UnsupportedChangeException,
156: StatementNotSupportedOnDatabaseException {
157: SQLFileChange change2 = new SQLFileChange();
158: change2.setSql("SELECT * FROM x;SELECT * FROM x;");
159: change2.setSplitStatements(false);
160: MSSQLDatabase database = new MSSQLDatabase();
161: SqlStatement[] statements = change2
162: .generateStatements(database);
163:
164: assertEquals(1, statements.length);
165: assertEquals("SELECT * FROM x;SELECT * FROM x;", statements[0]
166: .getSqlStatement(database));
167: }
168:
169: @Test
170: public void defaultSplittingAndNoStripping() throws Exception {
171: SQLFileChange change2 = new SQLFileChange();
172: change2
173: .setSql("SELECT * FROM x;\n/*A Comment*/SELECT * FROM x;");
174:
175: MSSQLDatabase database = new MSSQLDatabase();
176: SqlStatement[] statements = change2
177: .generateStatements(database);
178:
179: assertEquals(2, statements.length);
180: assertEquals("SELECT * FROM x", statements[0]
181: .getSqlStatement(database));
182: assertEquals("/*A Comment*/SELECT * FROM x", statements[1]
183: .getSqlStatement(database));
184: }
185: }
|