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  
23  /*** Test harness for the Descriptors (ElementDescriptor and so on).
24    *
25    * @author Robert Burrell Donkin
26    * @version $Revision: 1.6 $
27    */
28  public class TestDescriptors extends AbstractTestCase {
29      
30      public static void main( String[] args ) {
31          TestRunner.run( suite() );
32      }
33      
34      public static Test suite() {
35          return new TestSuite(TestDescriptors.class);
36      }
37      
38      public TestDescriptors(String testName) {
39          super(testName);
40      }
41      
42      public void testElementDescriptorLazyInit() {
43          ElementDescriptor descriptor = new ElementDescriptor();
44          
45          // check for NPEs
46          assertTrue("Empty descriptor has no children", !descriptor.hasChildren());
47          assertTrue("Empty descriptor has no content", !descriptor.hasContent());
48          assertTrue("Empty descriptor has no attributes", !descriptor.hasAttributes());
49          
50          // add an attribute and make sure everything works
51          descriptor.addAttributeDescriptor(new AttributeDescriptor("test:one"));
52          assertTrue("Empty descriptor has no children", !descriptor.hasChildren());
53          assertTrue("Empty descriptor has no content", !descriptor.hasContent());
54          assertTrue("Descriptor has attributes (1)", descriptor.hasAttributes());        
55                  
56          // add an element and make sure everything works
57          descriptor.addElementDescriptor(new ElementDescriptor("test:two"));
58          assertTrue("Descriptor has children (1)", descriptor.hasChildren());
59          assertTrue("Descriptor has content (1)", descriptor.hasContent());
60          assertTrue("Descriptor has attributes (2)", descriptor.hasAttributes());        
61          
62          // start again and test in reverse order
63          descriptor = new ElementDescriptor();
64          
65          // add an element and make sure everything works
66          descriptor.addElementDescriptor(new ElementDescriptor("test:one"));
67          assertTrue("Descriptor has children (2)", descriptor.hasChildren());
68          assertTrue("Descriptor has content (2)", descriptor.hasContent());
69          assertTrue("Descriptor has no attributes (1)", !descriptor.hasAttributes());      
70          
71          // add an attribute and make sure everything works
72          descriptor.addAttributeDescriptor(new AttributeDescriptor("test:two"));
73          assertTrue("Descriptor has children (3)", descriptor.hasChildren());
74          assertTrue("Descriptor has content (3)", descriptor.hasContent());
75          assertTrue("Descriptor has attributes (2)", descriptor.hasAttributes());        
76          
77          // try adding content
78          descriptor = new ElementDescriptor();
79          descriptor.addContentDescriptor(new AttributeDescriptor("test:one"));
80          assertTrue("Descriptor has no children (1)", !descriptor.hasChildren());
81          assertTrue("Descriptor has content (3)", descriptor.hasContent());
82          assertTrue("Descriptor has no attributes (2)", !descriptor.hasAttributes());        
83          
84          // add an element and make sure everything works
85          descriptor.addElementDescriptor(new ElementDescriptor("test:two"));
86          assertTrue("Descriptor has children (4)", descriptor.hasChildren());
87          assertTrue("Descriptor has content (4)", descriptor.hasContent());
88          assertTrue("Descriptor has no attributes (3)", !descriptor.hasAttributes());      
89          
90          // add an attribute and make sure everything works
91          descriptor.addAttributeDescriptor(new AttributeDescriptor("test:three"));
92          assertTrue("Descriptor has children (5)", descriptor.hasChildren());
93          assertTrue("Descriptor has content (5)", descriptor.hasContent());
94          assertTrue("Descriptor has attributes (3)", descriptor.hasAttributes());       
95      }
96      
97      public void testGetElementDescriptorByName() 
98      {
99          ElementDescriptor descriptor = new ElementDescriptor("Flintstones");
100         descriptor.addElementDescriptor(new ElementDescriptor("Freddy"));
101         descriptor.addElementDescriptor(new ElementDescriptor("Wilma"));
102         descriptor.addElementDescriptor(new ElementDescriptor("Pebbles"));
103         
104         ElementDescriptor returned = descriptor.getElementDescriptor("Freddy");
105         assertTrue("Freddy is a Flintstone", returned != null);
106         assertEquals("Freddy is the right flintstone", "Freddy", returned.getLocalName());
107         
108         returned = descriptor.getElementDescriptor("Wilma");
109         assertTrue("Wilma is a Flintstone", returned != null);
110         assertEquals("Wilma is the right flintstone", "Wilma", returned.getLocalName());
111         
112         returned = descriptor.getElementDescriptor("Barney");
113         assertTrue("Barney is not a Flintstone", returned == null);
114     }
115     
116     public void testGetElementDescriptorByNameNullMatch() 
117     {
118         ElementDescriptor descriptor = new ElementDescriptor("Flintstones");
119         descriptor.addElementDescriptor(new ElementDescriptor("Freddy"));
120         descriptor.addElementDescriptor(new ElementDescriptor("Wilma"));
121         descriptor.addElementDescriptor(new ElementDescriptor("Pebbles"));
122         descriptor.addElementDescriptor(new ElementDescriptor());
123         
124         ElementDescriptor returned = descriptor.getElementDescriptor("NotFreddy");
125         assertTrue("NotFreddy matched", returned != null);
126         assertEquals("NotFreddy match by null descriptor", null, returned.getLocalName());
127     }
128 }
129