View Javadoc

1   /* $Id: AbstractAnnotatedPojoTestCase.java 1127412 2011-05-25 07:29:59Z 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  package org.apache.commons.digester3.annotations;
19  
20  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.InputStream;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  
27  import org.apache.commons.digester3.Digester;
28  import org.apache.commons.digester3.binder.RulesModule;
29  
30  /**
31   * Abstract implementation of Class->Digester Rules->parse & confronting.
32   * 
33   * @since 2.1
34   */
35  public abstract class AbstractAnnotatedPojoTestCase
36  {
37  
38      /**
39       * Loads the digester rules parsing the expected object class, parses the
40       * XML and verify the digester produces the same result.
41       *
42       * @param expected the expected object
43       * @throws Exception if any error occurs
44       */
45      public final void verifyExpectedEqualsToParsed(Object expected) throws Exception {
46          final Class<?> clazz = expected.getClass();
47  
48          String resource = clazz.getSimpleName() + ".xml";
49          InputStream input = clazz.getResourceAsStream(resource);
50  
51          Collection<RulesModule> modules = this.getAuxModules();
52          modules.add(new FromAnnotationsRuleModule()
53          {
54  
55              @Override
56              protected void configureRules()
57              {
58                  bindRulesFrom( clazz );
59              }
60  
61          });
62  
63          Digester digester = newLoader(modules).newDigester();
64          Object actual = digester.parse(input);
65  
66          if (input != null) {
67              input.close();
68          }
69  
70          assertEquals(expected, actual);
71      }
72  
73      protected Collection<RulesModule> getAuxModules() {
74          return new ArrayList<RulesModule>();
75      }
76  
77  }