View Javadoc

1   /* $Id: SetPropertyRuleTestCase.java 1125741 2011-05-21 16:22:25Z 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  import java.lang.reflect.InvocationTargetException;
30  
31  import org.apache.commons.digester3.binder.AbstractRulesModule;
32  import org.apache.commons.digester3.binder.DigesterLoader;
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.xml.sax.SAXException;
37  
38  /**
39   * <p>
40   * Test case for <code>SetPropertyRule</code>.
41   * </p>
42   */
43  public class SetPropertyRuleTestCase
44  {
45  
46      // ----------------------------------------------------- Instance Variables
47  
48      /**
49       * Simple test xml document used in the tests.
50       */
51      protected final static String TEST_XML_1 = "<?xml version='1.0'?><root>"
52          + "<set name='alpha' value='ALPHA VALUE'/>" + "<set name='beta' value='BETA VALUE'/>"
53          + "<set name='delta' value='DELTA VALUE'/>" + "</root>";
54  
55      /**
56       * Simple test xml document used in the tests.
57       */
58      protected final static String TEST_XML_2 = "<?xml version='1.0'?><root>"
59          + "<set name='unknown' value='UNKNOWN VALUE'/>" + "</root>";
60  
61      private final DigesterLoader loader = newLoader( new AbstractRulesModule()
62      {
63  
64          @Override
65          protected void configure()
66          {
67              forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" );
68              forPattern( "root/set" ).setProperty( "name" ).extractingValueFromAttribute( "value" );
69          }
70  
71      });
72  
73      /**
74       * The digester instance we will be processing.
75       */
76      protected Digester digester = null;
77  
78      // --------------------------------------------------- Overall Test Methods
79  
80      /**
81       * Set up instance variables required by this test case.
82       */
83      @Before
84      public void setUp()
85      {
86  
87          digester = loader.newDigester();
88  
89      }
90  
91      /**
92       * Tear down instance variables required by this test case.
93       */
94      @After
95      public void tearDown()
96      {
97  
98          digester = null;
99  
100     }
101 
102     // ------------------------------------------------ Individual Test Methods
103 
104     /**
105      * Positive test for SetPropertyRule.
106      */
107     @Test
108     public void testPositive()
109         throws Exception
110     {
111         // Parse the input
112         SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_1 ) );
113 
114         // Check that the properties were set correctly
115         assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
116         assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
117         assertNull( "gamma property not set", bean.getGamma() );
118         assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
119 
120     }
121 
122     /**
123      * Negative test for SetPropertyRule.
124      */
125     @Test
126     public void testNegative()
127     {
128         // Parse the input (should fail)
129         try
130         {
131             SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
132             fail( "Should have thrown NoSuchMethodException" );
133             assertNotNull( bean ); // just to prevent compiler warning on unused var
134         }
135         catch ( Exception e )
136         {
137             if ( e instanceof NoSuchMethodException )
138             {
139                 // Expected result
140             }
141             else if ( e instanceof InvocationTargetException )
142             {
143                 Throwable t = ( (InvocationTargetException) e ).getTargetException();
144                 if ( t instanceof NoSuchMethodException )
145                 {
146                     // Expected result
147                 }
148                 else
149                 {
150                     fail( "Should have thrown ITE->NoSuchMethodException, threw " + t );
151                 }
152             }
153             else if ( e instanceof SAXException )
154             {
155                 Exception ee = ( (SAXException) e ).getException();
156                 if ( ee != null )
157                 {
158                     if ( ee instanceof NoSuchMethodException )
159                     {
160                         // Expected result
161                     }
162                     else
163                     {
164                         fail( "Should have thrown SE->NoSuchMethodException, threw " + ee );
165                     }
166                 }
167                 else
168                 {
169                     fail( "Should have thrown NoSuchMethodException, threw " + e.getClass().getName() );
170                 }
171             }
172             else
173             {
174                 fail( "Should have thrown NoSuchMethodException, threw " + e );
175             }
176         }
177 
178     }
179 
180     /**
181      * Get input stream from specified String containing XML data.
182      */
183     private Reader xmlTestReader( String xml )
184     {
185         return new StringReader( xml );
186     }
187 
188     /**
189      * Test SetPropertyRule when matched XML element has no attributes. See: DIGESTER-114
190      */
191     @Test
192     public void testElementWithNoAttributes()
193         throws Exception
194     {
195         String TEST_XML_3 = "<?xml version='1.0'?><root><set/></root>";
196 
197         // Parse the input - should not throw an exception
198         @SuppressWarnings( "unused" )
199         SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_3 ) );
200     }
201 
202 }