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   
18  package org.apache.commons.betwixt.digester;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.betwixt.ElementDescriptor;
23  import org.apache.commons.digester.Digester;
24  import org.apache.commons.digester.Rule;
25  import org.xml.sax.helpers.AttributesImpl;
26  
27  
28  
29  /**
30   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
31   * @version $Revision: 561314 $
32   */
33  public class TestOptionDigestion extends TestCase {
34  
35      private Digester digester;
36      private OptionRule optionRule;
37      private Rule nameRule;
38      private Rule valueRule;
39      private ElementDescriptor elementDescriptor;
40  
41      protected void setUp() throws Exception {
42          super.setUp();
43          elementDescriptor = new ElementDescriptor();
44          digester = new Digester();
45          digester.push(elementDescriptor);
46          optionRule = new OptionRule();
47          optionRule.setDigester(digester);
48          nameRule = optionRule.getNameRule();
49          valueRule = optionRule.getValueRule();
50      }
51  
52      public void testGoodDigestion() throws Exception {
53  
54           optionRule.begin("","option", new AttributesImpl());
55           nameRule.begin("","name", new AttributesImpl());
56           nameRule.body("", "name", "one");
57           nameRule.end("","name");
58           valueRule.begin("","value", new AttributesImpl());
59           valueRule.body("", "value", "ONE");
60           valueRule.end("","value");
61           optionRule.end("","option");
62           
63           assertEquals("Option set", "ONE", elementDescriptor.getOptions().getValue("one"));
64      }
65  
66  
67      public void testTwoDigestions() throws Exception {
68  
69           optionRule.begin("","option", new AttributesImpl());
70           nameRule.begin("","name", new AttributesImpl());
71           nameRule.body("", "name", "one");
72           nameRule.end("","name");
73           valueRule.begin("","value", new AttributesImpl());
74           valueRule.body("", "value", "ONE");
75           valueRule.end("","value");
76           optionRule.end("","option");
77           optionRule.begin("","option", new AttributesImpl());
78           valueRule.begin("","value", new AttributesImpl());
79           valueRule.body("", "value", "TWO");
80           valueRule.end("","value");
81           nameRule.begin("","name", new AttributesImpl());
82           nameRule.body("", "name", "two");
83           nameRule.end("","name");
84           optionRule.end("","option");
85           
86           assertEquals("Option set", "ONE", elementDescriptor.getOptions().getValue("one"));
87           assertEquals("Option set", "TWO", elementDescriptor.getOptions().getValue("two"));
88           
89      }
90      
91  
92      public void testGracefulBadMapping() throws Exception {
93  
94           optionRule.begin("","option", new AttributesImpl());
95           nameRule.begin("","name", new AttributesImpl());
96           nameRule.body("", "name", "one");
97           nameRule.end("","name");
98           optionRule.end("","option");
99           optionRule.begin("","option", new AttributesImpl());
100          valueRule.begin("","value", new AttributesImpl());
101          valueRule.body("", "value", "ONE");
102          valueRule.end("","value");
103          optionRule.end("","option");
104          optionRule.begin("","option", new AttributesImpl());
105          nameRule.begin("","name", new AttributesImpl());
106          nameRule.body("", "name", "two");
107          nameRule.end("","name");
108          valueRule.begin("","value", new AttributesImpl());
109          valueRule.body("", "value", "TWO");
110          valueRule.end("","value");
111          optionRule.end("","option");
112          
113          assertEquals("Option set", null, elementDescriptor.getOptions().getValue("one"));
114          assertEquals("Option set", "TWO", elementDescriptor.getOptions().getValue("two"));
115          
116     } 
117 }