View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web.struts.form.pojo;
17  
18  import org.apache.commons.beanutils.NestedNullException;
19  import org.apache.commons.beanutils.PropertyUtils;
20  import org.junit.Test;
21  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
22  import org.kuali.rice.krad.util.ObjectUtils;
23  
24  import java.util.Collection;
25  import java.util.Collections;
26  
27  import static org.junit.Assert.assertFalse;
28  import static org.junit.Assert.assertTrue;
29  
30  public class PojoPluginTest {
31  
32      /**
33       * <p>Testing scenario that was not working in the linked issue off of
34       * KULRICE-6877: KualiMaintainbleImpl#performCollectionForceUpperCase blowing up</p>
35       * 
36       * @throws Exception
37       */
38      @Test
39      public void testGetChildCollectionThrowsNestedNullException() throws Exception {
40  
41          // We need to initialize PropertyUtils to use our plugins
42          new PojoPlugin().init(null, null);
43  
44          TestCollectionHolderHolder tchh = new TestCollectionHolderHolder();
45          tchh.setTch(new TestCollectionHolder());
46          
47          // this simulates a situation in which the property (tch) is a proxied object 
48          // that can't be fetched, so getting it works (returns the proxy) but trying 
49          // to access the collection underneath it throws a NestedNullException
50          Object result = ObjectUtils.getPropertyValue(tchh, "tch.collection");
51  
52          // before, the empty string was being returned, which doesn't make sense for a collection
53          assertFalse("".equals(result));
54  
55          // now we return null
56          assertTrue(null == result);
57      }
58  
59      /**
60       * <p>Testing scenario that isWriteable blows up with NestedNullException when property value is null
61       * KULRICE-6877: KualiMaintainbleImpl#performCollectionForceUpperCase blowing up</p>
62       *
63       * @throws Exception
64       */
65      @Test
66      public void testNestedNullIsWriteable() throws Exception {
67  
68          // We need to initialize PropertyUtils to use our plugins
69          new PojoPlugin().init(null, null);
70  
71          TestCollectionHolderHolder tchh = new TestCollectionHolderHolder();
72          assertTrue(PropertyUtils.isWriteable(tchh,  "tch2.collection"));
73  
74  
75      }
76  
77      /**
78       * Ugly name, but it holds a TestCollectionHolder
79       */
80      public static class TestCollectionHolderHolder extends PersistableBusinessObjectBase {
81          private TestCollectionHolder tch = null;
82          private TestCollectionHolder2 tch2;
83  
84          public TestCollectionHolder getTch() {
85              return tch;
86          }
87  
88          public void setTch(TestCollectionHolder tch) {
89              this.tch = tch;
90          }
91  
92          public TestCollectionHolder2 getTch2() {
93              return tch2;
94          }
95  
96          public void setTch2(TestCollectionHolder2 tch2) {
97              this.tch2 = tch2;
98          }
99      }
100 
101     /**
102      * Test class that holds a collection, but trying to get it results in a
103      * NestedNullException.
104      * @throws NestedNullException
105      */
106     public static class TestCollectionHolder extends PersistableBusinessObjectBase {
107         private Collection collection = Collections.emptyList();
108 
109         public Collection getCollection() {
110             throw new NestedNullException();
111         }
112 
113         public void setCollection(Collection collection) {
114             this.collection = collection;
115         }
116     }
117 
118     /**
119      * Test class that holds a collection, but trying to get it results in a
120      * NestedNullException.
121      * @throws NestedNullException
122      */
123     public static class TestCollectionHolder2 extends PersistableBusinessObjectBase {
124         private Collection collection = Collections.emptyList();
125 
126         public Collection getCollection() {
127             return collection;
128         }
129 
130         public void setCollection(Collection collection) {
131             this.collection = collection;
132         }
133     }
134 
135 
136 }