View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test accessing ResultSets via DynaBeans.
38   */
39  public class DynaResultSetTest {
40  
41      /**
42       * The mock result set DynaClass to be tested.
43       */
44      protected ResultSetDynaClass dynaClass;
45  
46      /**
47       * Names of the columns for this test. Must match the order they are defined in {@link TestResultSetMetaData}, and must be all lower case.
48       */
49      protected String[] columns = { "bigdecimalproperty", "booleanproperty", "byteproperty", "dateproperty", "doubleproperty", "floatproperty", "intproperty",
50              "longproperty", "nullproperty", "shortproperty", "stringproperty", "timeproperty", "timestampproperty" };
51  
52      /**
53       * Sets up instance variables required by this test case.
54       */
55      @BeforeEach
56      public void setUp() throws Exception {
57          dynaClass = new ResultSetDynaClass(TestResultSet.createProxy());
58      }
59  
60      /**
61       * Tear down instance variables required by this test case.
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          // Invalid argument test
83          assertThrows(NullPointerException.class, () -> dynaClass.getDynaProperty(null));
84          // Negative test
85          DynaProperty dynaProp = dynaClass.getDynaProperty("unknownProperty");
86          assertNull(dynaProp, "unknown property returns null");
87          // Positive test
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         // Grab the third row
118         final Iterator<DynaBean> rows = dynaClass.iterator();
119         rows.next();
120         rows.next();
121         final DynaBean row = rows.next();
122 
123         // Invalid argument test
124         assertThrows(IllegalArgumentException.class, () -> row.get("unknownProperty"));
125 
126         // Verify property values
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      * Test normal case column names (i.e. not converted to lower case)
150      */
151     @Test
152     public void testIteratorResultsNormalCase() throws Exception {
153         final ResultSetDynaClass dynaClass = new ResultSetDynaClass(TestResultSet.createProxy(), false);
154 
155         // Grab the third row
156         final Iterator<DynaBean> rows = dynaClass.iterator();
157         rows.next();
158         rows.next();
159         final DynaBean row = rows.next();
160 
161         // Invalid argument test
162         assertThrows(IllegalArgumentException.class, () -> row.get("unknownProperty"));
163 
164         // Verify property values
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 }