01: package liquibase.change;
02:
03: import liquibase.database.MockDatabase;
04: import liquibase.database.sql.InsertStatement;
05: import liquibase.database.sql.SqlStatement;
06: import static org.junit.Assert.*;
07: import org.junit.Before;
08: import org.junit.Test;
09: import org.w3c.dom.Document;
10: import org.w3c.dom.Element;
11: import org.w3c.dom.NodeList;
12:
13: import javax.xml.parsers.DocumentBuilderFactory;
14:
15: /**
16: * Tests for {@link InsertDataChange}
17: */
18: public class InsertDataChangeTest extends AbstractChangeTest {
19:
20: InsertDataChange refactoring;
21:
22: @Before
23: public void setUp() throws Exception {
24: refactoring = new InsertDataChange();
25: refactoring.setTableName("TABLE_NAME");
26:
27: ColumnConfig col1 = new ColumnConfig();
28: col1.setName("id");
29: col1.setValueNumeric("123");
30:
31: ColumnConfig col2 = new ColumnConfig();
32: col2.setName("name");
33: col2.setValue("Andrew");
34:
35: ColumnConfig col3 = new ColumnConfig();
36: col3.setName("age");
37: col3.setValueNumeric("21");
38:
39: refactoring.addColumn(col1);
40: refactoring.addColumn(col2);
41: refactoring.addColumn(col3);
42: }
43:
44: @Test
45: public void getRefactoringName() throws Exception {
46: assertEquals("Insert Row", refactoring.getChangeName());
47: }
48:
49: @Test
50: public void generateStatement() throws Exception {
51: SqlStatement[] sqlStatements = refactoring
52: .generateStatements(new MockDatabase());
53: assertEquals(1, sqlStatements.length);
54: assertTrue(sqlStatements[0] instanceof InsertStatement);
55: assertEquals("123", ((InsertStatement) sqlStatements[0])
56: .getColumnValue("id").toString());
57: assertEquals("Andrew", ((InsertStatement) sqlStatements[0])
58: .getColumnValue("name").toString());
59: assertEquals("21", ((InsertStatement) sqlStatements[0])
60: .getColumnValue("age").toString());
61: }
62:
63: @Test
64: public void getConfirmationMessage() throws Exception {
65: assertEquals("New row inserted into TABLE_NAME", refactoring
66: .getConfirmationMessage());
67: }
68:
69: @Test
70: public void createNode() throws Exception {
71: Document document = DocumentBuilderFactory.newInstance()
72: .newDocumentBuilder().newDocument();
73: Element node = refactoring.createNode(document);
74:
75: assertEquals("insert", node.getTagName());
76: assertEquals("TABLE_NAME", node.getAttribute("tableName"));
77:
78: NodeList columns = node.getChildNodes();
79: assertEquals(3, columns.getLength());
80:
81: assertEquals("column", ((Element) columns.item(0)).getTagName());
82: assertEquals("id", ((Element) columns.item(0))
83: .getAttribute("name"));
84: assertEquals("123", ((Element) columns.item(0))
85: .getAttribute("valueNumeric"));
86:
87: assertEquals("column", ((Element) columns.item(1)).getTagName());
88: assertEquals("name", ((Element) columns.item(1))
89: .getAttribute("name"));
90: assertEquals("Andrew", ((Element) columns.item(1))
91: .getAttribute("value"));
92:
93: assertEquals("column", ((Element) columns.item(2)).getTagName());
94: assertEquals("age", ((Element) columns.item(2))
95: .getAttribute("name"));
96: assertEquals("21", ((Element) columns.item(2))
97: .getAttribute("valueNumeric"));
98: }
99: }
|