View Javadoc

1   /* $Id: TestRule.java 1103635 2011-05-16 07:37:24Z 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 java.util.List;
22  
23  import org.apache.commons.digester3.Rule;
24  import org.apache.commons.digester3.binder.RuleProvider;
25  import org.xml.sax.Attributes;
26  
27  /**
28   * <p>
29   * This rule implementation is intended to help test digester. The idea is that you can test which rule matches by
30   * looking at the identifier.
31   * </p>
32   * 
33   * @author Robert Burrell Donkin
34   */
35  
36  public class TestRule
37      extends Rule
38  {
39  
40      // ----------------------------------------------------- Instance Variables
41  
42      /** String identifing this particular <code>TestRule</code> */
43      private String identifier;
44  
45      /** Used when testing body text */
46      private String bodyText;
47  
48      /** Used when testing call orders */
49      private List<Rule> order;
50  
51      // ----------------------------------------------------------- Constructors
52  
53      /**
54       * Base constructor.
55       * 
56       * @param identifier Used to tell which TestRule is which
57       */
58      public TestRule( String identifier )
59      {
60  
61          this.identifier = identifier;
62      }
63  
64      /**
65       * Constructor sets namespace URI.
66       * 
67       * @param identifier Used to tell which TestRule is which
68       * @param namespaceURI Set rule namespace
69       */
70      public TestRule( String identifier, String namespaceURI )
71      {
72  
73          this.identifier = identifier;
74          setNamespaceURI( namespaceURI );
75  
76      }
77  
78      // ------------------------------------------------ Rule Implementation
79  
80      /**
81       * 'Begin' call.
82       */
83      @Override
84      public void begin( String namespace, String name, Attributes attributes )
85          throws Exception
86      {
87          appendCall();
88      }
89  
90      /**
91       * 'Body' call.
92       */
93      @Override
94      public void body( String namespace, String name, String text )
95          throws Exception
96      {
97          this.bodyText = text;
98          appendCall();
99      }
100 
101     /**
102      * 'End' call.
103      */
104     @Override
105     public void end( String namespace, String name )
106         throws Exception
107     {
108         appendCall();
109     }
110 
111     // ------------------------------------------------ Methods
112 
113     /**
114      * If a list has been set, append this to the list.
115      */
116     protected void appendCall()
117     {
118         if ( order != null )
119             order.add( this );
120     }
121 
122     /**
123      * Get the body text that was set.
124      */
125     public String getBodyText()
126     {
127         return bodyText;
128     }
129 
130     /**
131      * Get the identifier associated with this test.
132      */
133     public String getIdentifier()
134     {
135         return identifier;
136     }
137 
138     /**
139      * Get call order list.
140      */
141     public List<Rule> getOrder()
142     {
143         return order;
144     }
145 
146     /**
147      * Set call order list
148      */
149     public void setOrder( List<Rule> order )
150     {
151         this.order = order;
152     }
153 
154     /**
155      * Return the identifier.
156      */
157     @Override
158     public String toString()
159     {
160         return identifier;
161     }
162 
163     public static class TestRuleProvider implements RuleProvider<TestRule>
164     {
165 
166         private final String identifier;
167 
168         private final List<Rule> callOrder;
169 
170         public TestRuleProvider( String identifier )
171         {
172             this( identifier, null );
173         }
174 
175         public TestRuleProvider( String identifier, List<Rule> callOrder )
176         {
177             this.identifier = identifier;
178             this.callOrder = callOrder;
179         }
180 
181         public TestRule get()
182         {
183             TestRule testRule = new TestRule( identifier );
184             testRule.setOrder( callOrder );
185             return testRule;
186         }
187 
188     }
189 
190 }