View Javadoc

1   /* $Id: SetPropertiesRuleTestCase.java 1197969 2011-11-05 15:15:10Z 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  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.fail;
26  
27  import java.io.Reader;
28  import java.io.StringReader;
29  
30  import org.apache.commons.digester3.binder.AbstractRulesModule;
31  import org.junit.Test;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * <p>
36   * Test case for <code>SetPropertiesRule</code>.
37   * </p>
38   */
39  public class SetPropertiesRuleTestCase
40  {
41  
42      // ----------------------------------------------------- Instance Variables
43  
44      /**
45       * Simple test xml document used in the tests.
46       */
47      protected final static String TEST_XML_1 =
48          "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
49  
50      /**
51       * Simple test xml document used in the tests.
52       */
53      protected final static String TEST_XML_2 =
54          "<?xml version='1.0'?><root alpa='ALPA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
55  
56      /**
57       * Simple test xml document used in the tests.
58       */
59      protected final static String TEST_XML_3 =
60          "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE' ignore='ignore value'/>";
61  
62      // ------------------------------------------------ Individual Test Methods
63  
64      /**
65       * Positive test for SetPropertiesRule.
66       */
67      @Test
68      public void testPositive()
69          throws Exception
70      {
71          Digester digester = newLoader( new AbstractRulesModule()
72          {
73  
74              @Override
75              protected void configure()
76              {
77                  forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
78                      .then()
79                      .setProperties();
80              }
81  
82          }).newDigester();
83  
84          // Parse the input
85          SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_1 ) );
86  
87          // Check that the properties were set correctly
88          assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
89          assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
90          assertNull( "gamma property not set", bean.getGamma() );
91          assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
92  
93      }
94  
95      /**
96       * Positive test for SetPropertyRule ignoring missing properties.
97       */
98      @Test
99      public void testIgnoreMissing()
100         throws Exception
101     {
102         Digester digester = newLoader( new AbstractRulesModule()
103         {
104 
105             @Override
106             protected void configure()
107             {
108                 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
109                     .then()
110                     .setProperties();
111             }
112 
113         }).newDigester();
114 
115         // Parse the input
116         SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
117 
118         // Check that the properties were set correctly
119         assertNull( "alpha property not set", bean.getAlpha() );
120         assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
121         assertNull( "gamma property not set", bean.getGamma() );
122         assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
123 
124     }
125 
126     /**
127      * Negative test for SetPropertyRule ignoring missing properties.
128      */
129     @Test
130     public void testNegativeNotIgnoreMissing()
131         throws Exception
132     {
133         Digester digester = newLoader( new AbstractRulesModule()
134         {
135 
136             @Override
137             protected void configure()
138             {
139                 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
140                     .then()
141                     .setProperties().ignoreMissingProperty( false );
142             }
143 
144         }).newDigester();
145 
146         try
147         {
148             // Parse the input
149             SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
150             fail( "Should have thrown NoSuchMethodException" );
151             assertNotNull( bean ); // just to prevent compiler warning on unused var
152         }
153         catch ( Exception e )
154         {
155             if ( e instanceof NoSuchMethodException )
156             {
157                 // Expected;
158             }
159             else if ( e instanceof SAXException )
160             {
161                 Exception ee = ( (SAXException) e ).getException();
162                 if ( ee != null )
163                 {
164                     if ( ee instanceof NoSuchMethodException )
165                     {
166                         // Expected result
167                     }
168                     else
169                     {
170                         fail( "Should have thrown SE->NoSuchMethodException, threw " + ee );
171                     }
172                 }
173                 else
174                 {
175                     fail( "Should have thrown NoSuchMethodException, threw " + e.getClass().getName() );
176                 }
177             }
178             else
179             {
180                 fail( "Should have thrown NoSuchMethodException, threw " + e );
181             }
182         }
183     }
184 
185     /**
186      * Negative test for SetPropertyRule ignoring missing properties.
187      */
188     @Test
189     public void testPositiveNotIgnoreMissingWithIgnoreAttributes()
190         throws Exception
191     {
192         Digester digester = newLoader( new AbstractRulesModule()
193         {
194 
195             @Override
196             protected void configure()
197             {
198                 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
199                     .then()
200                     .setProperties()
201                         .addAlias( "ignore" ).forProperty( null )
202                         .ignoreMissingProperty( false );
203             }
204 
205         }).newDigester();
206 
207         // Parse the input
208         SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_3 ) );
209 
210         // Check that the properties were set correctly
211         assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
212         assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
213         assertNull( "gamma property not set", bean.getGamma() );
214         assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
215     }
216 
217     /**
218      * Get input stream from specified String containing XML data.
219      */
220     private Reader xmlTestReader( String xml )
221     {
222         return new StringReader( xml );
223     }
224 
225 }