1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23
24
25
26
27
28
29 public class TestDescriptors extends AbstractTestCase {
30
31 public static void main( String[] args ) {
32 TestRunner.run( suite() );
33 }
34
35 public static Test suite() {
36 return new TestSuite(TestDescriptors.class);
37 }
38
39 public TestDescriptors(String testName) {
40 super(testName);
41 }
42
43 public void testElementDescriptorLazyInit() {
44 ElementDescriptor descriptor = new ElementDescriptor();
45
46
47 assertTrue("Empty descriptor has no children", !descriptor.hasChildren());
48 assertTrue("Empty descriptor has no content", !descriptor.hasContent());
49 assertTrue("Empty descriptor has no attributes", !descriptor.hasAttributes());
50
51
52 descriptor.addAttributeDescriptor(new AttributeDescriptor("test:one"));
53 assertTrue("Empty descriptor has no children", !descriptor.hasChildren());
54 assertTrue("Empty descriptor has no content", !descriptor.hasContent());
55 assertTrue("Descriptor has attributes (1)", descriptor.hasAttributes());
56
57
58 descriptor.addElementDescriptor(new ElementDescriptor("test:two"));
59 assertTrue("Descriptor has children (1)", descriptor.hasChildren());
60 assertTrue("Descriptor has content (1)", descriptor.hasContent());
61 assertTrue("Descriptor has attributes (2)", descriptor.hasAttributes());
62
63
64 descriptor = new ElementDescriptor();
65
66
67 descriptor.addElementDescriptor(new ElementDescriptor("test:one"));
68 assertTrue("Descriptor has children (2)", descriptor.hasChildren());
69 assertTrue("Descriptor has content (2)", descriptor.hasContent());
70 assertTrue("Descriptor has no attributes (1)", !descriptor.hasAttributes());
71
72
73 descriptor.addAttributeDescriptor(new AttributeDescriptor("test:two"));
74 assertTrue("Descriptor has children (3)", descriptor.hasChildren());
75 assertTrue("Descriptor has content (3)", descriptor.hasContent());
76 assertTrue("Descriptor has attributes (2)", descriptor.hasAttributes());
77
78
79 descriptor = new ElementDescriptor();
80 descriptor.addContentDescriptor(new AttributeDescriptor("test:one"));
81 assertTrue("Descriptor has no children (1)", !descriptor.hasChildren());
82 assertTrue("Descriptor has content (3)", descriptor.hasContent());
83 assertTrue("Descriptor has no attributes (2)", !descriptor.hasAttributes());
84
85
86 descriptor.addElementDescriptor(new ElementDescriptor("test:two"));
87 assertTrue("Descriptor has children (4)", descriptor.hasChildren());
88 assertTrue("Descriptor has content (4)", descriptor.hasContent());
89 assertTrue("Descriptor has no attributes (3)", !descriptor.hasAttributes());
90
91
92 descriptor.addAttributeDescriptor(new AttributeDescriptor("test:three"));
93 assertTrue("Descriptor has children (5)", descriptor.hasChildren());
94 assertTrue("Descriptor has content (5)", descriptor.hasContent());
95 assertTrue("Descriptor has attributes (3)", descriptor.hasAttributes());
96 }
97
98 public void testGetElementDescriptorByName()
99 {
100 ElementDescriptor descriptor = new ElementDescriptor("Flintstones");
101 descriptor.addElementDescriptor(new ElementDescriptor("Freddy"));
102 descriptor.addElementDescriptor(new ElementDescriptor("Wilma"));
103 descriptor.addElementDescriptor(new ElementDescriptor("Pebbles"));
104
105 ElementDescriptor returned = descriptor.getElementDescriptor("Freddy");
106 assertTrue("Freddy is a Flintstone", returned != null);
107 assertEquals("Freddy is the right flintstone", "Freddy", returned.getLocalName());
108
109 returned = descriptor.getElementDescriptor("Wilma");
110 assertTrue("Wilma is a Flintstone", returned != null);
111 assertEquals("Wilma is the right flintstone", "Wilma", returned.getLocalName());
112
113 returned = descriptor.getElementDescriptor("Barney");
114 assertTrue("Barney is not a Flintstone", returned == null);
115 }
116
117 public void testGetElementDescriptorByNameNullMatch()
118 {
119 ElementDescriptor descriptor = new ElementDescriptor("Flintstones");
120 descriptor.addElementDescriptor(new ElementDescriptor("Freddy"));
121 descriptor.addElementDescriptor(new ElementDescriptor("Wilma"));
122 descriptor.addElementDescriptor(new ElementDescriptor("Pebbles"));
123 descriptor.addElementDescriptor(new ElementDescriptor());
124
125 ElementDescriptor returned = descriptor.getElementDescriptor("NotFreddy");
126 assertTrue("NotFreddy matched", returned != null);
127 assertEquals("NotFreddy match by null descriptor", null, returned.getLocalName());
128 }
129 }
130