001/* $Id: XIncludeTestCase.java 1125771 2011-05-21 19:11:05Z simonetripodi $ 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.commons.digester3; 020 021import static org.apache.commons.digester3.binder.DigesterLoader.newLoader; 022import static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertNotNull; 024 025import java.io.IOException; 026import java.io.InputStream; 027 028import org.apache.commons.digester3.binder.AbstractRulesModule; 029import org.junit.Test; 030import org.xml.sax.SAXException; 031 032/** 033 * <p> 034 * Tests for XInclude aware parsing. 035 * </p> 036 */ 037public class XIncludeTestCase 038{ 039 040 // ------------------------------------------------ Individual Test Methods 041 042 /** 043 * Test XInclude. 044 */ 045 @Test 046 public void testXInclude() 047 throws SAXException, IOException 048 { 049 Digester digester = newLoader( new AbstractRulesModule() 050 { 051 052 @Override 053 protected void configure() 054 { 055 forPattern( "employee" ).createObject().ofType( Employee.class ); 056 forPattern( "employee/firstName" ).callMethod( "setFirstName" ).usingElementBodyAsArgument(); 057 forPattern( "employee/lastName" ).callMethod( "setLastName" ).usingElementBodyAsArgument(); 058 059 forPattern( "employee/address" ).createObject().ofType( Address.class ) 060 .then() 061 .setNext( "addAddress" ); 062 forPattern( "employee/address/type" ).callMethod( "setType" ).usingElementBodyAsArgument(); 063 forPattern( "employee/address/city" ).callMethod( "setCity" ).usingElementBodyAsArgument(); 064 forPattern( "employee/address/state" ).callMethod( "setState" ).usingElementBodyAsArgument(); 065 } 066 067 }) 068 .setNamespaceAware( true ) 069 .setXIncludeAware( true ) 070 .newDigester(); 071 072 // Parse our test input 073 Employee employee = digester.parse( getInputStream( "Test12.xml" ) ); 074 assertNotNull( "failed to parsed an employee", employee ); 075 076 // Test basics 077 assertEquals( "First Name", employee.getFirstName() ); 078 assertEquals( "Last Name", employee.getLastName() ); 079 080 // Test includes have been processed 081 Address ha = employee.getAddress( "home" ); 082 assertNotNull( ha ); 083 assertEquals( "Home City", ha.getCity() ); 084 assertEquals( "HS", ha.getState() ); 085 Address oa = employee.getAddress( "office" ); 086 assertNotNull( oa ); 087 assertEquals( "Office City", oa.getCity() ); 088 assertEquals( "OS", oa.getState() ); 089 090 } 091 092 // ------------------------------------------------ Utility Support Methods 093 094 /** 095 * Return an appropriate InputStream for the specified test file (which must be inside our current package. 096 * 097 * @param name Name of the test file we want 098 * @exception IOException if an input/output error occurs 099 */ 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}