View Javadoc

1   /* $Id: XIncludeTestCase.java 1125771 2011-05-21 19:11:05Z 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  
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import org.apache.commons.digester3.binder.AbstractRulesModule;
29  import org.junit.Test;
30  import org.xml.sax.SAXException;
31  
32  /**
33   * <p>
34   * Tests for XInclude aware parsing.
35   * </p>
36   */
37  public class XIncludeTestCase
38  {
39  
40      // ------------------------------------------------ Individual Test Methods
41  
42      /**
43       * Test XInclude.
44       */
45      @Test
46      public void testXInclude()
47          throws SAXException, IOException
48      {
49          Digester digester = newLoader( new AbstractRulesModule()
50          {
51  
52              @Override
53              protected void configure()
54              {
55                  forPattern( "employee" ).createObject().ofType( Employee.class );
56                  forPattern( "employee/firstName" ).callMethod( "setFirstName" ).usingElementBodyAsArgument();
57                  forPattern( "employee/lastName" ).callMethod( "setLastName" ).usingElementBodyAsArgument();
58  
59                  forPattern( "employee/address" ).createObject().ofType( Address.class )
60                      .then()
61                      .setNext( "addAddress" );
62                  forPattern( "employee/address/type" ).callMethod( "setType" ).usingElementBodyAsArgument();
63                  forPattern( "employee/address/city" ).callMethod( "setCity" ).usingElementBodyAsArgument();
64                  forPattern( "employee/address/state" ).callMethod( "setState" ).usingElementBodyAsArgument();
65              }
66  
67          })
68          .setNamespaceAware( true )
69          .setXIncludeAware( true )
70          .newDigester();
71  
72          // Parse our test input
73          Employee employee = digester.parse( getInputStream( "Test12.xml" ) );
74          assertNotNull( "failed to parsed an employee", employee );
75  
76          // Test basics
77          assertEquals( "First Name", employee.getFirstName() );
78          assertEquals( "Last Name", employee.getLastName() );
79  
80          // Test includes have been processed
81          Address ha = employee.getAddress( "home" );
82          assertNotNull( ha );
83          assertEquals( "Home City", ha.getCity() );
84          assertEquals( "HS", ha.getState() );
85          Address oa = employee.getAddress( "office" );
86          assertNotNull( oa );
87          assertEquals( "Office City", oa.getCity() );
88          assertEquals( "OS", oa.getState() );
89  
90      }
91  
92      // ------------------------------------------------ Utility Support Methods
93  
94      /**
95       * Return an appropriate InputStream for the specified test file (which must be inside our current package.
96       * 
97       * @param name Name of the test file we want
98       * @exception IOException if an input/output error occurs
99       */
100     protected InputStream getInputStream( String name )
101         throws IOException
102     {
103 
104         return ( this.getClass().getResourceAsStream( "/org/apache/commons/digester3/" + name ) );
105 
106     }
107 
108 }