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