1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  /** Test harness for the Descriptors (ElementDescriptor and so on).
25    *
26    * @author Robert Burrell Donkin
27    * @version $Revision: 438373 $
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          // check for NPEs
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          // add an attribute and make sure everything works
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          // add an element and make sure everything works
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          // start again and test in reverse order
64          descriptor = new ElementDescriptor();
65          
66          // add an element and make sure everything works
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          // add an attribute and make sure everything works
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          // try adding content
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          // add an element and make sure everything works
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          // add an attribute and make sure everything works
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