KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > ObjectParamRuleTestCase


1 /* $Id: ObjectParamRuleTestCase.java 155412 2005-02-26 12:58:36Z dirkv $
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
19 package org.apache.commons.digester;
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32
33 /**
34  * <p>Tests for the <code>ObjectParamRuleTestCase</code>
35  *
36  * @author Mark Huisman
37  */

38 public class ObjectParamRuleTestCase extends TestCase {
39
40
41     // ----------------------------------------------------- Instance Variables
42

43
44     /**
45      * The digester instance we will be processing.
46      */

47     protected Digester digester = null;
48
49
50     // ----------------------------------------------------------- Constructors
51

52
53     /**
54      * Construct a new instance of this test case.
55      *
56      * @param name Name of the test case
57      */

58     public ObjectParamRuleTestCase(String JavaDoc name) {
59
60         super(name);
61
62     }
63
64
65     public static void main(String JavaDoc[] args){
66
67         // so we can run standalone
68
junit.textui.TestRunner.run(suite());
69
70     }
71
72
73     // --------------------------------------------------- Overall Test Methods
74

75
76     /**
77      * Set up instance variables required by this test case.
78      */

79     public void setUp() {
80
81         digester = new Digester();
82
83     }
84
85
86     /**
87      * Return the tests included in this test suite.
88      */

89     public static Test suite() {
90
91         return (new TestSuite(ObjectParamRuleTestCase.class));
92
93     }
94
95
96     /**
97      * Tear down instance variables required by this test case.
98      */

99     public void tearDown() {
100
101         digester = null;
102
103     }
104
105
106
107     // ------------------------------------------------ Individual Test Methods
108

109     private StringBuffer JavaDoc sb = new StringBuffer JavaDoc().
110         append("<arraylist><A/><B/><C/><D desc=\"the fourth\"/><E/></arraylist>");
111
112
113     /**
114      * Test method calls with the ObjectParamRule rule. It should be possible to
115      * pass any subclass of Object as a parameter, provided that either the element
116      * or the element + attribute has been matched.
117      */

118     public void testBasic() throws SAXException JavaDoc, IOException JavaDoc {
119
120         // Configure the digester as required
121
digester.addObjectCreate("arraylist", ArrayList JavaDoc.class);
122
123         // Test adding a variety of objects
124
digester.addCallMethod("arraylist/A", "add", 1);
125         ObjectParamRule opr = new ObjectParamRule(0, new Integer JavaDoc(-9));
126         digester.addRule("arraylist/A", opr);
127         digester.addCallMethod("arraylist/B", "add", 1);
128         opr = new ObjectParamRule(0, new Float JavaDoc(3.14159));
129         digester.addRule("arraylist/B", opr);
130         digester.addCallMethod("arraylist/C", "add", 1);
131         opr = new ObjectParamRule(0, new Long JavaDoc(999999999));
132         digester.addRule("arraylist/C", opr);
133         digester.addCallMethod("arraylist/D", "add", 1);
134         opr = new ObjectParamRule(0, "desc", new String JavaDoc("foobarbazbing"));
135         digester.addRule("arraylist/D", opr);
136         // note that this will add a null parameter to the method call and will
137
// not be added to the arraylist.
138
digester.addCallMethod("arraylist/E", "add", 1);
139         opr = new ObjectParamRule(0, "nonexistentattribute", new String JavaDoc("ignore"));
140         digester.addRule("arraylist/E", opr);
141
142         //Parse it and obtain the ArrayList
143
ArrayList JavaDoc al = (ArrayList JavaDoc)digester.parse(new StringReader JavaDoc(sb.toString()));
144         this.assertNotNull(al);
145         this.assertEquals(al.size(), 4);
146         this.assertTrue(al.contains(new Integer JavaDoc(-9)));
147         this.assertTrue(al.contains(new Float JavaDoc(3.14159)));
148         this.assertTrue(al.contains(new Long JavaDoc(999999999)));
149         this.assertTrue(al.contains(new String JavaDoc("foobarbazbing")));
150         this.assertTrue(!(al.contains(new String JavaDoc("ignore"))));
151     }
152
153 }
154
155
Popular Tags