01: package liquibase.change;
02:
03: import liquibase.database.Database;
04: import liquibase.database.sql.AddUniqueConstraintStatement;
05: import liquibase.database.sql.SqlStatement;
06: import liquibase.test.DatabaseTest;
07: import liquibase.test.DatabaseTestTemplate;
08: import static org.junit.Assert.*;
09: import org.junit.Test;
10: import org.w3c.dom.Element;
11:
12: import javax.xml.parsers.DocumentBuilderFactory;
13:
14: public class AddUniqueConstraintChangeTest extends AbstractChangeTest {
15:
16: @Test
17: public void getRefactoringName() throws Exception {
18: assertEquals("Add Unique Constraint",
19: new AddUniqueConstraintChange().getChangeName());
20: }
21:
22: @Test
23: public void generateStatement() throws Exception {
24:
25: new DatabaseTestTemplate()
26: .testOnAllDatabases(new DatabaseTest() {
27: public void performTest(Database database)
28: throws Exception {
29: AddUniqueConstraintChange change = new AddUniqueConstraintChange();
30: change.setSchemaName("SCHEMA_NAME");
31: change.setTableName("TABLE_NAME");
32: change.setColumnNames("COL_HERE");
33: change.setConstraintName("PK_NAME");
34: change.setTablespace("TABLESPACE_NAME");
35:
36: SqlStatement[] sqlStatements = change
37: .generateStatements(database);
38: assertEquals(1, sqlStatements.length);
39: assertTrue(sqlStatements[0] instanceof AddUniqueConstraintStatement);
40:
41: assertEquals(
42: "SCHEMA_NAME",
43: ((AddUniqueConstraintStatement) sqlStatements[0])
44: .getSchemaName());
45: assertEquals(
46: "TABLE_NAME",
47: ((AddUniqueConstraintStatement) sqlStatements[0])
48: .getTableName());
49: assertEquals(
50: "COL_HERE",
51: ((AddUniqueConstraintStatement) sqlStatements[0])
52: .getColumnNames());
53: assertEquals(
54: "PK_NAME",
55: ((AddUniqueConstraintStatement) sqlStatements[0])
56: .getConstraintName());
57: assertEquals(
58: "TABLESPACE_NAME",
59: ((AddUniqueConstraintStatement) sqlStatements[0])
60: .getTablespace());
61:
62: }
63: });
64: }
65:
66: @Test
67: public void getConfirmationMessage() throws Exception {
68: AddUniqueConstraintChange change = new AddUniqueConstraintChange();
69: change.setTableName("TABLE_NAME");
70: change.setColumnNames("COL_HERE");
71:
72: assertEquals("Unique constraint added to TABLE_NAME(COL_HERE)",
73: change.getConfirmationMessage());
74: }
75:
76: @Test
77: public void createNode() throws Exception {
78: AddUniqueConstraintChange change = new AddUniqueConstraintChange();
79: change.setSchemaName("SCHEMA_NAME");
80: change.setTableName("TABLE_NAME");
81: change.setColumnNames("COL_HERE");
82: change.setConstraintName("PK_NAME");
83: change.setTablespace("TABLESPACE_NAME");
84:
85: Element node = change.createNode(DocumentBuilderFactory
86: .newInstance().newDocumentBuilder().newDocument());
87: assertEquals("addUniqueConstraint", node.getTagName());
88: assertEquals("SCHEMA_NAME", node.getAttribute("schemaName"));
89: assertEquals("TABLE_NAME", node.getAttribute("tableName"));
90: assertEquals("COL_HERE", node.getAttribute("columnNames"));
91: assertEquals("PK_NAME", node.getAttribute("constraintName"));
92: assertEquals("TABLESPACE_NAME", node.getAttribute("tablespace"));
93: }
94: }
|