001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt;
018:
019: import junit.framework.Test;
020: import junit.framework.TestSuite;
021: import junit.textui.TestRunner;
022:
023: /** Test harness for the Descriptors (ElementDescriptor and so on).
024: *
025: * @author Robert Burrell Donkin
026: * @version $Revision: 438373 $
027: */
028: public class TestDescriptors extends AbstractTestCase {
029:
030: public static void main(String[] args) {
031: TestRunner.run(suite());
032: }
033:
034: public static Test suite() {
035: return new TestSuite(TestDescriptors.class);
036: }
037:
038: public TestDescriptors(String testName) {
039: super (testName);
040: }
041:
042: public void testElementDescriptorLazyInit() {
043: ElementDescriptor descriptor = new ElementDescriptor();
044:
045: // check for NPEs
046: assertTrue("Empty descriptor has no children", !descriptor
047: .hasChildren());
048: assertTrue("Empty descriptor has no content", !descriptor
049: .hasContent());
050: assertTrue("Empty descriptor has no attributes", !descriptor
051: .hasAttributes());
052:
053: // add an attribute and make sure everything works
054: descriptor.addAttributeDescriptor(new AttributeDescriptor(
055: "test:one"));
056: assertTrue("Empty descriptor has no children", !descriptor
057: .hasChildren());
058: assertTrue("Empty descriptor has no content", !descriptor
059: .hasContent());
060: assertTrue("Descriptor has attributes (1)", descriptor
061: .hasAttributes());
062:
063: // add an element and make sure everything works
064: descriptor.addElementDescriptor(new ElementDescriptor(
065: "test:two"));
066: assertTrue("Descriptor has children (1)", descriptor
067: .hasChildren());
068: assertTrue("Descriptor has content (1)", descriptor
069: .hasContent());
070: assertTrue("Descriptor has attributes (2)", descriptor
071: .hasAttributes());
072:
073: // start again and test in reverse order
074: descriptor = new ElementDescriptor();
075:
076: // add an element and make sure everything works
077: descriptor.addElementDescriptor(new ElementDescriptor(
078: "test:one"));
079: assertTrue("Descriptor has children (2)", descriptor
080: .hasChildren());
081: assertTrue("Descriptor has content (2)", descriptor
082: .hasContent());
083: assertTrue("Descriptor has no attributes (1)", !descriptor
084: .hasAttributes());
085:
086: // add an attribute and make sure everything works
087: descriptor.addAttributeDescriptor(new AttributeDescriptor(
088: "test:two"));
089: assertTrue("Descriptor has children (3)", descriptor
090: .hasChildren());
091: assertTrue("Descriptor has content (3)", descriptor
092: .hasContent());
093: assertTrue("Descriptor has attributes (2)", descriptor
094: .hasAttributes());
095:
096: // try adding content
097: descriptor = new ElementDescriptor();
098: descriptor.addContentDescriptor(new AttributeDescriptor(
099: "test:one"));
100: assertTrue("Descriptor has no children (1)", !descriptor
101: .hasChildren());
102: assertTrue("Descriptor has content (3)", descriptor
103: .hasContent());
104: assertTrue("Descriptor has no attributes (2)", !descriptor
105: .hasAttributes());
106:
107: // add an element and make sure everything works
108: descriptor.addElementDescriptor(new ElementDescriptor(
109: "test:two"));
110: assertTrue("Descriptor has children (4)", descriptor
111: .hasChildren());
112: assertTrue("Descriptor has content (4)", descriptor
113: .hasContent());
114: assertTrue("Descriptor has no attributes (3)", !descriptor
115: .hasAttributes());
116:
117: // add an attribute and make sure everything works
118: descriptor.addAttributeDescriptor(new AttributeDescriptor(
119: "test:three"));
120: assertTrue("Descriptor has children (5)", descriptor
121: .hasChildren());
122: assertTrue("Descriptor has content (5)", descriptor
123: .hasContent());
124: assertTrue("Descriptor has attributes (3)", descriptor
125: .hasAttributes());
126: }
127:
128: public void testGetElementDescriptorByName() {
129: ElementDescriptor descriptor = new ElementDescriptor(
130: "Flintstones");
131: descriptor
132: .addElementDescriptor(new ElementDescriptor("Freddy"));
133: descriptor.addElementDescriptor(new ElementDescriptor("Wilma"));
134: descriptor
135: .addElementDescriptor(new ElementDescriptor("Pebbles"));
136:
137: ElementDescriptor returned = descriptor
138: .getElementDescriptor("Freddy");
139: assertTrue("Freddy is a Flintstone", returned != null);
140: assertEquals("Freddy is the right flintstone", "Freddy",
141: returned.getLocalName());
142:
143: returned = descriptor.getElementDescriptor("Wilma");
144: assertTrue("Wilma is a Flintstone", returned != null);
145: assertEquals("Wilma is the right flintstone", "Wilma", returned
146: .getLocalName());
147:
148: returned = descriptor.getElementDescriptor("Barney");
149: assertTrue("Barney is not a Flintstone", returned == null);
150: }
151:
152: public void testGetElementDescriptorByNameNullMatch() {
153: ElementDescriptor descriptor = new ElementDescriptor(
154: "Flintstones");
155: descriptor
156: .addElementDescriptor(new ElementDescriptor("Freddy"));
157: descriptor.addElementDescriptor(new ElementDescriptor("Wilma"));
158: descriptor
159: .addElementDescriptor(new ElementDescriptor("Pebbles"));
160: descriptor.addElementDescriptor(new ElementDescriptor());
161:
162: ElementDescriptor returned = descriptor
163: .getElementDescriptor("NotFreddy");
164: assertTrue("NotFreddy matched", returned != null);
165: assertEquals("NotFreddy match by null descriptor", null,
166: returned.getLocalName());
167: }
168: }
|