1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46 @Test
47 public void testGetChildCollectionThrowsNestedNullException() throws Exception {
48
49
50 new PojoPlugin().init(null, new ModuleConfigImpl());
51
52 TestCollectionHolderHolder tchh = new TestCollectionHolderHolder();
53 tchh.setTch(new TestCollectionHolder());
54
55
56
57
58 Object result = ObjectUtils.getPropertyValue(tchh, "tch.collection");
59
60
61 assertFalse("".equals(result));
62
63
64 assertTrue(null == result);
65 }
66
67
68
69
70
71
72
73 @Test
74 public void testNestedNullIsWriteable() throws Exception {
75
76
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
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
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
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
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
157
158
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
174
175
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 }