KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > xmlrules > IncludeTest


1 /* $Id: IncludeTest.java 180258 2005-06-06 08:25:55Z skitching $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.digester.xmlrules;
19
20 import java.util.ArrayList JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import org.xml.sax.InputSource JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.digester.Digester;
27 import org.apache.commons.digester.Rule;
28
29 /**
30  * Test for the include class functionality
31  */

32 public class IncludeTest extends TestCase {
33
34     public static class TestDigesterRuleSource implements DigesterRulesSource {
35         public void getRules(Digester digester) {
36             digester.addRule("bar",
37                 new Rule() {
38                     public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text) {
39                         ((ArrayList JavaDoc) this.digester.peek()).add(text);
40                     }
41                 });
42         }
43     }
44
45     public IncludeTest(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public void testBasicInclude() throws Exception JavaDoc {
50         String JavaDoc rulesXml = "<?xml version='1.0'?>"
51                 + "<digester-rules>"
52                 + " <pattern value='root/foo'>"
53                 + " <include class='org.apache.commons.digester.xmlrules.IncludeTest$TestDigesterRuleSource'/>"
54                 + " </pattern>"
55                 + "</digester-rules>";
56                 
57         String JavaDoc xml = "<?xml version='1.0' ?><root><foo><bar>short</bar></foo></root>";
58         
59         ArrayList JavaDoc list = new ArrayList JavaDoc();
60         Digester digester = DigesterLoader.createDigester(new InputSource JavaDoc(new StringReader JavaDoc(rulesXml)));
61         digester.push(list);
62         digester.parse(new StringReader JavaDoc(xml));
63                                                                         
64         assertEquals("Number of entries", 1, list.size());
65         assertEquals("Entry value", "short", list.get(0));
66     }
67 }
68
Popular Tags