01: package info.jtrac.domain;
02:
03: import java.util.Collections;
04: import java.util.HashSet;
05: import java.util.List;
06: import java.util.Map;
07:
08: import java.util.Set;
09: import junit.framework.TestCase;
10:
11: public class MetadataTest extends TestCase {
12:
13: private Metadata getMetadata() {
14: Metadata metadata = new Metadata();
15: String xmlString = "<metadata><fields>"
16: + "<field name='cusInt01' label='Test Label'/>"
17: + "<field name='cusInt02' label='Test Label 2'/>"
18: + "</fields></metadata>";
19: metadata.setXmlString(xmlString);
20: return metadata;
21: }
22:
23: public void testGetFieldByName() {
24: Metadata m = getMetadata();
25: Field f = m.getField("cusInt01");
26: assertEquals("Test Label", f.getLabel());
27: }
28:
29: public void testGetFieldsFromXml() {
30: Metadata m = getMetadata();
31: Map<Field.Name, Field> fields = m.getFields();
32: assertTrue(fields.size() == 2);
33: Field[] fa = fields.values().toArray(new Field[0]);
34: assertEquals("cusInt01", fa[0].getName() + "");
35: assertEquals("Test Label", fa[0].getLabel());
36: assertEquals("cusInt02", fa[1].getName() + "");
37: assertEquals("Test Label 2", fa[1].getLabel());
38: }
39:
40: public void testMetadataInheritance() {
41: Metadata m1 = getMetadata();
42: Metadata m2 = new Metadata();
43: String xmlString = "<metadata><fields>"
44: + "<field name='cusInt03' label='Test Label 3'/>"
45: + "<field name='cusInt04' label='Test Label 4'/>"
46: + "</fields></metadata>";
47: m2.setXmlString(xmlString);
48: m2.setParent(m1);
49: Map<Field.Name, Field> fields = m2.getFields();
50: assertEquals(fields.size(), 4);
51: Set<Field.Name> names = m2.getUnusedFieldNames();
52: assertEquals(names.contains(Field.Name.CUS_INT_01), false);
53: assertEquals(names.contains(Field.Name.CUS_INT_04), false);
54: assertEquals(names.size(), Field.Name.values().length - 4);
55: }
56:
57: public void testInitRolesThenAddRolesAndStates() {
58: Metadata m = new Metadata();
59: m.initRoles();
60: assertEquals("New, Open and Closed available by default", 3, m
61: .getStateCount());
62: assertEquals("DEFAULT available by default", 1, m
63: .getRoleCount());
64: Field f = new Field(Field.Name.CUS_INT_01);
65: m.add(f);
66: assertEquals(1, m.getFieldCount());
67: assertEquals("New", m.getStatusValue(0));
68: assertEquals("Open", m.getStatusValue(1));
69: assertEquals("Closed", m.getStatusValue(99));
70: assertEquals("", m.getStatusValue(50));
71: }
72:
73: public void testGetEditableFields() {
74: Metadata m = new Metadata();
75: m.initRoles();
76: Field f = new Field(Field.Name.CUS_STR_01);
77: m.add(f);
78: // query for editable fields across all roles
79: List<Field> fields = m.getEditableFields();
80: assertEquals(0, fields.size());
81: // query for editable fields for DEFAULT role and when status is OPEN
82: fields = m.getEditableFields("DEFAULT", State.OPEN);
83: assertEquals(0, fields.size());
84: // now make the field editable for given state and role
85: m.switchMask(State.OPEN, "DEFAULT", "cusStr01"); // should now be editable when status is open
86: fields = m.getEditableFields();
87: assertEquals(1, fields.size());
88: fields = m.getEditableFields("DEFAULT", State.OPEN);
89: assertEquals(1, fields.size());
90:
91: }
92:
93: }
|