View Javadoc

1   /* $Id: ObjectParamRuleTestCase.java 1125720 2011-05-21 14:51:26Z simonetripodi $
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.digester3;
20  
21  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.IOException;
27  import java.io.StringReader;
28  import java.util.ArrayList;
29  
30  import org.apache.commons.digester3.binder.AbstractRulesModule;
31  import org.junit.Test;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * <p>
36   * Tests for the <code>ObjectParamRuleTestCase</code>
37   * 
38   * @author Mark Huisman
39   */
40  public class ObjectParamRuleTestCase
41  {
42  
43      // ------------------------------------------------ Individual Test Methods
44  
45      private StringBuilder sb =
46          new StringBuilder().append( "<arraylist><A/><B/><C/><D desc=\"the fourth\"/><E/></arraylist>" );
47  
48      /**
49       * Test method calls with the ObjectParamRule rule. It should be possible to pass any subclass of Object as a
50       * parameter, provided that either the element or the element + attribute has been matched.
51       */
52      @Test
53      public void testBasic()
54          throws SAXException, IOException
55      {
56          Digester digester = newLoader( new AbstractRulesModule()
57          {
58  
59              @Override
60              protected void configure()
61              {
62                  forPattern( "arraylist" ).createObject().ofType( ArrayList.class );
63                  forPattern( "arraylist/A" ).callMethod( "add" ).withParamCount( 1 )
64                      .then()
65                      .objectParam( new Integer( -9 ) );
66                  forPattern( "arraylist/B" ).callMethod( "add" ).withParamCount( 1 )
67                      .then()
68                      .objectParam( new Float( 3.14159 ) );
69                  forPattern( "arraylist/C" ).callMethod( "add" ).withParamCount( 1 )
70                      .then()
71                      .objectParam( new Long( 999999999 ) );
72                  forPattern( "arraylist/D" ).callMethod( "add" ).withParamCount( 1 )
73                      .then()
74                      .objectParam( new String( "foobarbazbing" ) ).matchingAttribute( "desc" );
75                  forPattern( "arraylist/E" ).callMethod( "add" ).withParamCount( 1 )
76                      .then()
77                      .objectParam( new String( "ignore" ) ).matchingAttribute( "nonexistentattribute" );
78              }
79  
80          }).newDigester();
81  
82          // Parse it and obtain the ArrayList
83          ArrayList<?> al = digester.parse( new StringReader( sb.toString() ) );
84          assertNotNull( al );
85          assertEquals( al.size(), 4 );
86          assertTrue( al.contains( new Integer( -9 ) ) );
87          assertTrue( al.contains( new Float( 3.14159 ) ) );
88          assertTrue( al.contains( new Long( 999999999 ) ) );
89          assertTrue( al.contains( new String( "foobarbazbing" ) ) );
90          assertTrue( !( al.contains( new String( "ignore" ) ) ) );
91      }
92  
93  }