1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2.sql;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import java.math.BigDecimal;
28 import java.util.Iterator;
29
30 import org.apache.commons.beanutils2.DynaBean;
31 import org.apache.commons.beanutils2.DynaProperty;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 public class DynaResultSetTest {
40
41
42
43
44 protected ResultSetDynaClass dynaClass;
45
46
47
48
49 protected String[] columns = { "bigdecimalproperty", "booleanproperty", "byteproperty", "dateproperty", "doubleproperty", "floatproperty", "intproperty",
50 "longproperty", "nullproperty", "shortproperty", "stringproperty", "timeproperty", "timestampproperty" };
51
52
53
54
55 @BeforeEach
56 public void setUp() throws Exception {
57 dynaClass = new ResultSetDynaClass(TestResultSet.createProxy());
58 }
59
60
61
62
63 @AfterEach
64 public void tearDown() {
65 dynaClass = null;
66 }
67
68 @Test
69 public void testGetDynaProperties() {
70
71 final DynaProperty[] dynaProps = dynaClass.getDynaProperties();
72 assertNotNull(dynaProps, "dynaProps exists");
73 assertEquals(columns.length, dynaProps.length, "dynaProps length");
74 for (int i = 0; i < columns.length; i++) {
75 assertEquals(columns[i], dynaProps[i].getName(), "Property " + columns[i]);
76 }
77
78 }
79
80 @Test
81 public void testGetDynaProperty() {
82
83 assertThrows(NullPointerException.class, () -> dynaClass.getDynaProperty(null));
84
85 DynaProperty dynaProp = dynaClass.getDynaProperty("unknownProperty");
86 assertNull(dynaProp, "unknown property returns null");
87
88 dynaProp = dynaClass.getDynaProperty("stringproperty");
89 assertNotNull(dynaProp, "string property exists");
90 assertEquals("stringproperty", dynaProp.getName(), "string property name");
91 assertEquals(String.class, dynaProp.getType(), "string property class");
92 }
93
94 @Test
95 public void testGetName() {
96 assertEquals("org.apache.commons.beanutils2.sql.ResultSetDynaClass", dynaClass.getName(), "DynaClass name");
97 }
98
99 @Test
100 public void testIteratorCount() {
101
102 final Iterator<?> rows = dynaClass.iterator();
103 assertNotNull(rows, "iterator exists");
104 int n = 0;
105 while (rows.hasNext()) {
106 rows.next();
107 n++;
108 assertFalse(n > 10);
109 }
110 assertEquals(5, n, "iterator rows");
111
112 }
113
114 @Test
115 public void testIteratorResults() {
116
117
118 final Iterator<DynaBean> rows = dynaClass.iterator();
119 rows.next();
120 rows.next();
121 final DynaBean row = rows.next();
122
123
124 assertThrows(IllegalArgumentException.class, () -> row.get("unknownProperty"));
125
126
127
128 final Object bigDecimalProperty = row.get("bigdecimalproperty");
129 assertNotNull(bigDecimalProperty, "bigDecimalProperty exists");
130 assertInstanceOf(BigDecimal.class, bigDecimalProperty, "bigDecimalProperty type");
131 assertEquals(123.45, ((BigDecimal) bigDecimalProperty).doubleValue(), 0.005, "bigDecimalProperty value");
132
133 final Object intProperty = row.get("intproperty");
134 assertNotNull(intProperty, "intProperty exists");
135 assertInstanceOf(Integer.class, intProperty, "intProperty type");
136 assertEquals(103, ((Integer) intProperty).intValue(), "intProperty value");
137
138 final Object nullProperty = row.get("nullproperty");
139 assertNull(nullProperty, "nullProperty null");
140
141 final Object stringProperty = row.get("stringproperty");
142 assertNotNull(stringProperty, "stringProperty exists");
143 assertInstanceOf(String.class, stringProperty, "stringProperty type");
144 assertEquals("This is a string", (String) stringProperty, "stringProperty value");
145
146 }
147
148
149
150
151 @Test
152 public void testIteratorResultsNormalCase() throws Exception {
153 final ResultSetDynaClass dynaClass = new ResultSetDynaClass(TestResultSet.createProxy(), false);
154
155
156 final Iterator<DynaBean> rows = dynaClass.iterator();
157 rows.next();
158 rows.next();
159 final DynaBean row = rows.next();
160
161
162 assertThrows(IllegalArgumentException.class, () -> row.get("unknownProperty"));
163
164
165
166 final Object bigDecimalProperty = row.get("bigDecimalProperty");
167 assertNotNull(bigDecimalProperty, "bigDecimalProperty exists");
168 assertInstanceOf(BigDecimal.class, bigDecimalProperty, "bigDecimalProperty type");
169 assertEquals(123.45, ((BigDecimal) bigDecimalProperty).doubleValue(), 0.005, "bigDecimalProperty value");
170
171 final Object intProperty = row.get("intProperty");
172 assertNotNull(intProperty, "intProperty exists");
173 assertInstanceOf(Integer.class, intProperty, "intProperty type");
174 assertEquals(103, ((Integer) intProperty).intValue(), "intProperty value");
175
176 final Object nullProperty = row.get("nullProperty");
177 assertNull(nullProperty, "nullProperty null");
178
179 final Object stringProperty = row.get("stringProperty");
180 assertNotNull(stringProperty, "stringProperty exists");
181 assertInstanceOf(String.class, stringProperty, "stringProperty type");
182 assertEquals("This is a string", (String) stringProperty, "stringProperty value");
183
184 }
185
186 @Test
187 public void testNewInstance() {
188 assertThrows(UnsupportedOperationException.class, () -> dynaClass.newInstance());
189 }
190
191 }