001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.digester;
019:
020: import junit.framework.TestCase;
021:
022: import org.apache.commons.betwixt.ElementDescriptor;
023: import org.apache.commons.digester.Digester;
024: import org.apache.commons.digester.Rule;
025: import org.xml.sax.helpers.AttributesImpl;
026:
027: /**
028: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
029: * @version $Revision: 438373 $
030: */
031: public class TestOptionDigestion extends TestCase {
032:
033: private Digester digester;
034: private OptionRule optionRule;
035: private Rule nameRule;
036: private Rule valueRule;
037: private ElementDescriptor elementDescriptor;
038:
039: protected void setUp() throws Exception {
040: super .setUp();
041: elementDescriptor = new ElementDescriptor();
042: digester = new Digester();
043: digester.push(elementDescriptor);
044: optionRule = new OptionRule();
045: optionRule.setDigester(digester);
046: nameRule = optionRule.getNameRule();
047: valueRule = optionRule.getValueRule();
048: }
049:
050: public void testGoodDigestion() throws Exception {
051:
052: optionRule.begin("", "option", new AttributesImpl());
053: nameRule.begin("", "name", new AttributesImpl());
054: nameRule.body("", "name", "one");
055: nameRule.end("", "name");
056: valueRule.begin("", "value", new AttributesImpl());
057: valueRule.body("", "value", "ONE");
058: valueRule.end("", "value");
059: optionRule.end("", "option");
060:
061: assertEquals("Option set", "ONE", elementDescriptor
062: .getOptions().getValue("one"));
063: }
064:
065: public void testTwoDigestions() throws Exception {
066:
067: optionRule.begin("", "option", new AttributesImpl());
068: nameRule.begin("", "name", new AttributesImpl());
069: nameRule.body("", "name", "one");
070: nameRule.end("", "name");
071: valueRule.begin("", "value", new AttributesImpl());
072: valueRule.body("", "value", "ONE");
073: valueRule.end("", "value");
074: optionRule.end("", "option");
075: optionRule.begin("", "option", new AttributesImpl());
076: valueRule.begin("", "value", new AttributesImpl());
077: valueRule.body("", "value", "TWO");
078: valueRule.end("", "value");
079: nameRule.begin("", "name", new AttributesImpl());
080: nameRule.body("", "name", "two");
081: nameRule.end("", "name");
082: optionRule.end("", "option");
083:
084: assertEquals("Option set", "ONE", elementDescriptor
085: .getOptions().getValue("one"));
086: assertEquals("Option set", "TWO", elementDescriptor
087: .getOptions().getValue("two"));
088:
089: }
090:
091: public void testGracefulBadMapping() throws Exception {
092:
093: optionRule.begin("", "option", new AttributesImpl());
094: nameRule.begin("", "name", new AttributesImpl());
095: nameRule.body("", "name", "one");
096: nameRule.end("", "name");
097: optionRule.end("", "option");
098: optionRule.begin("", "option", new AttributesImpl());
099: 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()
113: .getValue("one"));
114: assertEquals("Option set", "TWO", elementDescriptor
115: .getOptions().getValue("two"));
116:
117: }
118: }
|