1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt;
17  
18  import junit.framework.Test;
19  import junit.framework.TestSuite;
20  import junit.textui.TestRunner;
21  
22  /*** Test harness for the Descriptors (ElementDescriptor and so on).
23    *
24    * @author Robert Burrell Donkin
25    * @version $Revision: 1.5 $
26    */
27  public class TestDescriptors extends AbstractTestCase {
28      
29      public static void main( String[] args ) {
30          TestRunner.run( suite() );
31      }
32      
33      public static Test suite() {
34          return new TestSuite(TestDescriptors.class);
35      }
36      
37      public TestDescriptors(String testName) {
38          super(testName);
39      }
40      
41      public void testElementDescriptorLazyInit() {
42          ElementDescriptor descriptor = new ElementDescriptor();
43          
44          // check for NPEs
45          assertTrue("Empty descriptor has no children", !descriptor.hasChildren());
46          assertTrue("Empty descriptor has no content", !descriptor.hasContent());
47          assertTrue("Empty descriptor has no attributes", !descriptor.hasAttributes());
48          
49          // add an attribute and make sure everything works
50          descriptor.addAttributeDescriptor(new AttributeDescriptor("test:one"));
51          assertTrue("Empty descriptor has no children", !descriptor.hasChildren());
52          assertTrue("Empty descriptor has no content", !descriptor.hasContent());
53          assertTrue("Descriptor has attributes (1)", descriptor.hasAttributes());        
54                  
55          // add an element and make sure everything works
56          descriptor.addElementDescriptor(new ElementDescriptor("test:two"));
57          assertTrue("Descriptor has children (1)", descriptor.hasChildren());
58          assertTrue("Descriptor has content (1)", descriptor.hasContent());
59          assertTrue("Descriptor has attributes (2)", descriptor.hasAttributes());        
60          
61          // start again and test in reverse order
62          descriptor = new ElementDescriptor();
63          
64          // add an element and make sure everything works
65          descriptor.addElementDescriptor(new ElementDescriptor("test:one"));
66          assertTrue("Descriptor has children (2)", descriptor.hasChildren());
67          assertTrue("Descriptor has content (2)", descriptor.hasContent());
68          assertTrue("Descriptor has no attributes (1)", !descriptor.hasAttributes());      
69          
70          // add an attribute and make sure everything works
71          descriptor.addAttributeDescriptor(new AttributeDescriptor("test:two"));
72          assertTrue("Descriptor has children (3)", descriptor.hasChildren());
73          assertTrue("Descriptor has content (3)", descriptor.hasContent());
74          assertTrue("Descriptor has attributes (2)", descriptor.hasAttributes());        
75          
76          // try adding content
77          descriptor = new ElementDescriptor();
78          descriptor.addContentDescriptor(new AttributeDescriptor("test:one"));
79          assertTrue("Descriptor has no children (1)", !descriptor.hasChildren());
80          assertTrue("Descriptor has content (3)", descriptor.hasContent());
81          assertTrue("Descriptor has no attributes (2)", !descriptor.hasAttributes());        
82          
83          // add an element and make sure everything works
84          descriptor.addElementDescriptor(new ElementDescriptor("test:two"));
85          assertTrue("Descriptor has children (4)", descriptor.hasChildren());
86          assertTrue("Descriptor has content (4)", descriptor.hasContent());
87          assertTrue("Descriptor has no attributes (3)", !descriptor.hasAttributes());      
88          
89          // add an attribute and make sure everything works
90          descriptor.addAttributeDescriptor(new AttributeDescriptor("test:three"));
91          assertTrue("Descriptor has children (5)", descriptor.hasChildren());
92          assertTrue("Descriptor has content (5)", descriptor.hasContent());
93          assertTrue("Descriptor has attributes (3)", descriptor.hasAttributes());       
94      }
95  }
96