001/* $Id: AbstractAnnotatedPojoTestCase.java 1127412 2011-05-25 07:29:59Z 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 */ 018package org.apache.commons.digester3.annotations; 019 020import static org.apache.commons.digester3.binder.DigesterLoader.newLoader; 021import static org.junit.Assert.assertEquals; 022 023import java.io.InputStream; 024import java.util.ArrayList; 025import java.util.Collection; 026 027import org.apache.commons.digester3.Digester; 028import org.apache.commons.digester3.binder.RulesModule; 029 030/** 031 * Abstract implementation of Class->Digester Rules->parse & confronting. 032 * 033 * @since 2.1 034 */ 035public abstract class AbstractAnnotatedPojoTestCase 036{ 037 038 /** 039 * Loads the digester rules parsing the expected object class, parses the 040 * XML and verify the digester produces the same result. 041 * 042 * @param expected the expected object 043 * @throws Exception if any error occurs 044 */ 045 public final void verifyExpectedEqualsToParsed(Object expected) throws Exception { 046 final Class<?> clazz = expected.getClass(); 047 048 String resource = clazz.getSimpleName() + ".xml"; 049 InputStream input = clazz.getResourceAsStream(resource); 050 051 Collection<RulesModule> modules = this.getAuxModules(); 052 modules.add(new FromAnnotationsRuleModule() 053 { 054 055 @Override 056 protected void configureRules() 057 { 058 bindRulesFrom( clazz ); 059 } 060 061 }); 062 063 Digester digester = newLoader(modules).newDigester(); 064 Object actual = digester.parse(input); 065 066 if (input != null) { 067 input.close(); 068 } 069 070 assertEquals(expected, actual); 071 } 072 073 protected Collection<RulesModule> getAuxModules() { 074 return new ArrayList<RulesModule>(); 075 } 076 077}