View Javadoc
1   /**
2    * Copyright 2005-2015 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.apache.struts.config.impl.ModuleConfigImpl;
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.kuali.rice.krad.bo.DocumentAttachment;
24  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
25  import org.kuali.rice.krad.service.PersistenceStructureService;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  import java.lang.reflect.InvocationHandler;
29  import java.lang.reflect.InvocationTargetException;
30  import java.lang.reflect.Method;
31  import java.lang.reflect.Proxy;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.HashMap;
35  
36  import static org.junit.Assert.*;
37  
38  public class PojoPluginTest {
39  
40      /**
41       * <p>Testing scenario that was not working in the linked issue off of
42       * KULRICE-6877: KualiMaintainbleImpl#performCollectionForceUpperCase blowing up</p>
43       * 
44       * @throws Exception
45       */
46      @Test
47      public void testGetChildCollectionThrowsNestedNullException() throws Exception {
48  
49          // We need to initialize PropertyUtils to use our plugins
50          new PojoPlugin().init(null, new ModuleConfigImpl());
51  
52          TestCollectionHolderHolder tchh = new TestCollectionHolderHolder();
53          tchh.setTch(new TestCollectionHolder());
54          
55          // this simulates a situation in which the property (tch) is a proxied object 
56          // that can't be fetched, so getting it works (returns the proxy) but trying 
57          // to access the collection underneath it throws a NestedNullException
58          Object result = ObjectUtils.getPropertyValue(tchh, "tch.collection");
59  
60          // before, the empty string was being returned, which doesn't make sense for a collection
61          assertFalse("".equals(result));
62  
63          // now we return null
64          assertTrue(null == result);
65      }
66  
67      /**
68       * <p>Testing scenario that isWriteable blows up with NestedNullException when property value is null
69       * KULRICE-6877: KualiMaintainbleImpl#performCollectionForceUpperCase blowing up</p>
70       *
71       * @throws Exception
72       */
73      @Test
74      public void testNestedNullIsWriteable() throws Exception {
75  
76          // We need to initialize PropertyUtils to use our plugins
77          new PojoPlugin().init(null, new ModuleConfigImpl());
78  
79          TestCollectionHolderHolder tchh = new TestCollectionHolderHolder();
80          assertTrue(PropertyUtils.isWriteable(tchh, "tch2.collection"));
81  
82  
83      }
84  
85      /**
86       * Tests that original IndexOutOfBoundsException is thrown when the bean is not a PersistableBusinessObject
87       */
88      @Test
89      public void testGenerateIndexedPropertyNonPBO() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {
90          IndexOutOfBoundsException ioobe = new IndexOutOfBoundsException("test exception");
91          try {
92              new PojoPropertyUtilsBean().generateIndexedProperty(new TestCollectionHolder(), "collection", 0, ioobe);
93              Assert.fail("Expected to throw IndexOutOfBoundsException");
94          } catch (IndexOutOfBoundsException e) {
95              assertEquals(ioobe, e);
96          }
97      }
98  
99      /**
100      * Tests that original IndexOutOfBoundsException is thrown when the property is not a List
101      */
102     @Test
103     public void testGenerateIndexedPropertyNonList() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {
104         IndexOutOfBoundsException ioobe = new IndexOutOfBoundsException("test exception");
105         try {
106             new PojoPropertyUtilsBean().generateIndexedProperty(new DocumentAttachment(), "attachmentContent", 0, ioobe);
107             Assert.fail("Expected to throw IndexOutOfBoundsException");
108         } catch (IndexOutOfBoundsException e) {
109             assertEquals(ioobe, e);
110         }
111     }
112 
113     @Test
114     public void testUndefinedOJBClass() {
115         final Object notAnOjbObject = new HashMap();
116         // stub out the persistence service
117         PojoPropertyUtilsBean.PersistenceStructureServiceProvider.persistenceStructureService =
118                 (PersistenceStructureService) Proxy.newProxyInstance(this.getClass().getClassLoader(),
119                         new Class[] { PersistenceStructureService.class },
120                         new InvocationHandler() {
121                             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
122                                 if ("listCollectionObjectTypes".equals(method.getName())) {
123                                     return new HashMap();
124                                 }
125                                 return null;
126                             }
127                         });
128         assertNull(new PojoPropertyUtilsBean.PersistenceStructureServiceProvider().getCollectionItemClass(notAnOjbObject, "abcd"));
129     }
130 
131     /**
132      * Ugly name, but it holds a TestCollectionHolder
133      */
134     public static class TestCollectionHolderHolder extends PersistableBusinessObjectBase {
135         private TestCollectionHolder tch = null;
136         private TestCollectionHolder2 tch2;
137 
138         public TestCollectionHolder getTch() {
139             return tch;
140         }
141 
142         public void setTch(TestCollectionHolder tch) {
143             this.tch = tch;
144         }
145 
146         public TestCollectionHolder2 getTch2() {
147             return tch2;
148         }
149 
150         public void setTch2(TestCollectionHolder2 tch2) {
151             this.tch2 = tch2;
152         }
153     }
154 
155     /**
156      * Test class that holds a collection, but trying to get it results in a
157      * NestedNullException.
158      * @throws NestedNullException
159      */
160     public static class TestCollectionHolder extends PersistableBusinessObjectBase {
161         private Collection collection = Collections.emptyList();
162 
163         public Collection getCollection() {
164             throw new NestedNullException();
165         }
166 
167         public void setCollection(Collection collection) {
168             this.collection = collection;
169         }
170     }
171 
172     /**
173      * Test class that holds a collection, but trying to get it results in a
174      * NestedNullException.
175      * @throws NestedNullException
176      */
177     public static class TestCollectionHolder2 extends PersistableBusinessObjectBase {
178         private Collection collection = Collections.emptyList();
179 
180         public Collection getCollection() {
181             return collection;
182         }
183 
184         public void setCollection(Collection collection) {
185             this.collection = collection;
186         }
187     }
188 
189 
190 }