001: package liquibase.change;
002:
003: import liquibase.ClassLoaderFileOpener;
004: import liquibase.database.MSSQLDatabase;
005: import liquibase.database.OracleDatabase;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.exception.SetupException;
008: import liquibase.util.MD5Util;
009: import liquibase.util.StreamUtil;
010: import static org.junit.Assert.*;
011: import org.junit.Before;
012: import org.junit.Test;
013: import org.w3c.dom.Element;
014:
015: import javax.xml.parsers.DocumentBuilderFactory;
016:
017: /**
018: * Tests the SQL File with a simple text file. No real SQL is used with the
019: * tests.
020: *
021: * @author <a href="mailto:csuml@yahoo.co.uk">Paul Keeble</a>
022: *
023: */
024: public class SQLFileChangeTest extends AbstractChangeTest {
025:
026: SQLFileChange change;
027: String fileName;
028:
029: @Before
030: public void setUp() throws Exception {
031: //file contains contents "TESTDATA"
032: fileName = "liquibase/change/SQLFileTestData.sql";
033: change = new SQLFileChange();
034: ClassLoaderFileOpener opener = new ClassLoaderFileOpener();
035: change.setFileOpener(opener);
036: change.setPath(fileName);
037: change.setUp();
038: }
039:
040: @Test
041: public void setFileOpener() {
042: assertNotNull(change.getFileOpener());
043: }
044:
045: @Test
046: public void createNode() throws Exception {
047:
048: Element element = change.createNode(DocumentBuilderFactory
049: .newInstance().newDocumentBuilder().newDocument());
050: assertEquals("sqlFile", element.getTagName());
051:
052: assertEquals(fileName, element.getAttribute("path"));
053: }
054:
055: @Test
056: public void generateStatement() throws Exception {
057: assertEquals(fileName, change.getPath());
058:
059: OracleDatabase database = new OracleDatabase();
060: assertEquals("TESTDATA", change.generateStatements(database)[0]
061: .getSqlStatement(database));
062:
063: assertEquals(MD5Util.computeMD5(change.getSql()), change
064: .getMD5Sum());
065: }
066:
067: @Test
068: public void generateStatementFileNotFound() throws Exception {
069: try {
070: change.setPath("doesnotexist.sql");
071: change.setUp();
072: change.generateStatements(new OracleDatabase());
073:
074: fail("The file does not exist so should not be found");
075: } catch (SetupException fnfe) {
076: //expected
077: }
078: }
079:
080: @Test
081: public void multiLineSQLFileSemiColon() throws Exception {
082: SQLFileChange change2 = new SQLFileChange();
083: change2.setSql("SELECT * FROM customer;"
084: + StreamUtil.getLineSeparator()
085: + "SELECT * from table;"
086: + StreamUtil.getLineSeparator()
087: + "SELECT * from table2;"
088: + StreamUtil.getLineSeparator());
089: OracleDatabase database = new OracleDatabase();
090: SqlStatement[] statements = change2
091: .generateStatements(database);
092:
093: assertEquals(3, statements.length);
094: assertEquals("SELECT * FROM customer", statements[0]
095: .getSqlStatement(database));
096: assertEquals("SELECT * from table", statements[1]
097: .getSqlStatement(database));
098: assertEquals("SELECT * from table2", statements[2]
099: .getSqlStatement(database));
100: }
101:
102: @Test
103: public void singleLineEndInSemiColon() throws Exception {
104: SQLFileChange change2 = new SQLFileChange();
105: change2.setSql("SELECT * FROM customer;");
106: OracleDatabase database = new OracleDatabase();
107: SqlStatement[] statements = change2
108: .generateStatements(database);
109: assertEquals(1, statements.length);
110: assertEquals("SELECT * FROM customer", statements[0]
111: .getSqlStatement(database));
112: }
113:
114: @Test
115: public void singleLineEndGo() throws Exception {
116: SQLFileChange change2 = new SQLFileChange();
117: change2.setSql("SELECT * FROM customer\ngo");
118: MSSQLDatabase database = new MSSQLDatabase();
119: SqlStatement[] statements = change2
120: .generateStatements(database);
121: assertEquals(1, statements.length);
122: assertEquals("SELECT * FROM customer", statements[0]
123: .getSqlStatement(database));
124: }
125:
126: @Test
127: public void singleLineBeginGo() throws Exception {
128: SQLFileChange change2 = new SQLFileChange();
129: change2.setSql("goSELECT * FROM customer\ngo");
130: MSSQLDatabase database = new MSSQLDatabase();
131: SqlStatement[] statements = change2
132: .generateStatements(database);
133: assertEquals(1, statements.length);
134: assertEquals("goSELECT * FROM customer", statements[0]
135: .getSqlStatement(database));
136: }
137:
138: @Test
139: public void multiLineSQLFileGoShouldFind() throws Exception {
140: SQLFileChange change2 = new SQLFileChange();
141: change2.setSql("SELECT * FROM customer\ngo\n"
142: + "SELECT * from table\ngo");
143: MSSQLDatabase database = new MSSQLDatabase();
144: SqlStatement[] statements = change2
145: .generateStatements(database);
146: assertEquals(2, statements.length);
147: assertEquals("SELECT * FROM customer", statements[0]
148: .getSqlStatement(database));
149: assertEquals("SELECT * from table", statements[1]
150: .getSqlStatement(database));
151: }
152:
153: @Test
154: public void multiLineSQLFileGoShouldNotFind() throws Exception {
155: SQLFileChange change2 = new SQLFileChange();
156: change2.setSql("SELECT * FROM go\ngo\n"
157: + "SELECT * from gogo\ngo\n");
158: MSSQLDatabase database = new MSSQLDatabase();
159: SqlStatement[] statements = change2
160: .generateStatements(database);
161:
162: assertEquals(2, statements.length);
163: assertEquals("SELECT * FROM go", statements[0]
164: .getSqlStatement(database));
165: assertEquals("SELECT * from gogo", statements[1]
166: .getSqlStatement(database));
167: }
168:
169: @Test
170: public void getConfirmationMessage() throws Exception {
171: change.setPath(fileName);
172: assertEquals("SQL in file " + fileName + " executed", change
173: .getConfirmationMessage());
174: }
175:
176: @Test
177: public void getRefactoringName() throws Exception {
178: assertEquals("SQL From File", change.getChangeName());
179:
180: }
181:
182: }
|