01: package liquibase.change;
02:
03: import static org.junit.Assert.*;
04: import org.junit.Test;
05: import org.w3c.dom.Element;
06:
07: import javax.xml.parsers.DocumentBuilderFactory;
08:
09: /**
10: * Tests for {@link ColumnConfig}
11: */
12: public class ColumnConfigTest {
13:
14: @Test
15: public void setValue() throws Exception {
16: ColumnConfig column = new ColumnConfig();
17:
18: column.setValue(null);
19: assertNull(column.getValue());
20:
21: column.setValue("abc");
22: assertEquals("abc", column.getValue());
23:
24: column.setValue("");
25: assertEquals("passed empty strings don't override the value",
26: "abc", column.getValue());
27:
28: column.setValue(null);
29: assertEquals("passed null doesn't override the value", "abc",
30: column.getValue());
31:
32: }
33:
34: @Test
35: public void createNode() throws Exception {
36: ColumnConfig column = new ColumnConfig();
37: column.setName("id");
38: column.setType("varchar(255)");
39: column.setDefaultValue("test Value");
40: column.setValue("some value here");
41:
42: ConstraintsConfig constraints = new ConstraintsConfig();
43: constraints.setDeferrable(Boolean.TRUE);
44: constraints.setDeleteCascade(true);
45: constraints.setForeignKeyName("FK_NAME");
46: constraints.setInitiallyDeferred(true);
47: constraints.setNullable(false);
48: constraints.setPrimaryKey(true);
49: constraints.setReferences("state(id)");
50: constraints.setUnique(true);
51: column.setConstraints(constraints);
52:
53: Element element = column.createNode(DocumentBuilderFactory
54: .newInstance().newDocumentBuilder().newDocument());
55: assertEquals("column", element.getTagName());
56: assertEquals("id", element.getAttribute("name"));
57: assertEquals("varchar(255)", element.getAttribute("type"));
58: assertEquals("test Value", element.getAttribute("defaultValue"));
59: assertEquals("some value here", element.getAttribute("value"));
60:
61: Element constraintsElement = (Element) element.getChildNodes()
62: .item(0);
63: assertEquals(8, constraintsElement.getAttributes().getLength());
64: assertEquals("true", constraintsElement
65: .getAttribute("deferrable"));
66: assertEquals("true", constraintsElement
67: .getAttribute("deleteCascade"));
68: assertEquals("FK_NAME", constraintsElement
69: .getAttribute("foreignKeyName"));
70: assertEquals("true", constraintsElement
71: .getAttribute("initiallyDeferred"));
72: assertEquals("false", constraintsElement
73: .getAttribute("nullable"));
74: assertEquals("true", constraintsElement
75: .getAttribute("primaryKey"));
76: assertEquals("state(id)", constraintsElement
77: .getAttribute("references"));
78: assertEquals("true", constraintsElement.getAttribute("unique"));
79:
80: }
81: }
|