KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > dao > DataAccessUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17 package org.springframework.dao;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.HashSet JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.dao.support.DataAccessUtils;
26
27 /**
28  * @author Juergen Hoeller
29  * @since 20.10.2004
30  */

31 public class DataAccessUtilsTests extends TestCase {
32
33     public void testWithEmptyCollection() {
34         Collection JavaDoc col = new HashSet JavaDoc();
35
36         assertNull(DataAccessUtils.uniqueResult(col));
37
38         try {
39             DataAccessUtils.requiredUniqueResult(col);
40             fail("Should have thrown IncorrectResultSizeDataAccessException");
41         }
42         catch (IncorrectResultSizeDataAccessException ex) {
43             // expected
44
assertEquals(1, ex.getExpectedSize());
45             assertEquals(0, ex.getActualSize());
46         }
47
48         try {
49             DataAccessUtils.objectResult(col, String JavaDoc.class);
50             fail("Should have thrown IncorrectResultSizeDataAccessException");
51         }
52         catch (IncorrectResultSizeDataAccessException ex) {
53             // expected
54
assertEquals(1, ex.getExpectedSize());
55             assertEquals(0, ex.getActualSize());
56         }
57
58         try {
59             DataAccessUtils.intResult(col);
60             fail("Should have thrown IncorrectResultSizeDataAccessException");
61         }
62         catch (IncorrectResultSizeDataAccessException ex) {
63             // expected
64
assertEquals(1, ex.getExpectedSize());
65             assertEquals(0, ex.getActualSize());
66         }
67
68         try {
69             DataAccessUtils.longResult(col);
70             fail("Should have thrown IncorrectResultSizeDataAccessException");
71         }
72         catch (IncorrectResultSizeDataAccessException ex) {
73             // expected
74
assertEquals(1, ex.getExpectedSize());
75             assertEquals(0, ex.getActualSize());
76         }
77     }
78
79     public void testWithTooLargeCollection() {
80         Collection JavaDoc col = new HashSet JavaDoc();
81         col.add("test1");
82         col.add("test2");
83
84         try {
85             DataAccessUtils.uniqueResult(col);
86             fail("Should have thrown IncorrectResultSizeDataAccessException");
87         }
88         catch (IncorrectResultSizeDataAccessException ex) {
89             // expected
90
assertEquals(1, ex.getExpectedSize());
91             assertEquals(2, ex.getActualSize());
92         }
93
94
95         try {
96             DataAccessUtils.requiredUniqueResult(col);
97             fail("Should have thrown IncorrectResultSizeDataAccessException");
98         }
99         catch (IncorrectResultSizeDataAccessException ex) {
100             // expected
101
assertEquals(1, ex.getExpectedSize());
102             assertEquals(2, ex.getActualSize());
103         }
104
105         try {
106             DataAccessUtils.objectResult(col, String JavaDoc.class);
107             fail("Should have thrown IncorrectResultSizeDataAccessException");
108         }
109         catch (IncorrectResultSizeDataAccessException ex) {
110             // expected
111
assertEquals(1, ex.getExpectedSize());
112             assertEquals(2, ex.getActualSize());
113         }
114
115         try {
116             DataAccessUtils.intResult(col);
117             fail("Should have thrown IncorrectResultSizeDataAccessException");
118         }
119         catch (IncorrectResultSizeDataAccessException ex) {
120             // expected
121
assertEquals(1, ex.getExpectedSize());
122             assertEquals(2, ex.getActualSize());
123         }
124
125         try {
126             DataAccessUtils.longResult(col);
127             fail("Should have thrown IncorrectResultSizeDataAccessException");
128         }
129         catch (IncorrectResultSizeDataAccessException ex) {
130             // expected
131
assertEquals(1, ex.getExpectedSize());
132             assertEquals(2, ex.getActualSize());
133         }
134     }
135
136     public void testWithInteger() {
137         Collection JavaDoc col = new HashSet JavaDoc();
138         col.add(new Integer JavaDoc(5));
139
140         assertEquals(new Integer JavaDoc(5), DataAccessUtils.uniqueResult(col));
141         assertEquals(new Integer JavaDoc(5), DataAccessUtils.requiredUniqueResult(col));
142         assertEquals(new Integer JavaDoc(5), DataAccessUtils.objectResult(col, Integer JavaDoc.class));
143         assertEquals("5", DataAccessUtils.objectResult(col, String JavaDoc.class));
144         assertEquals(5, DataAccessUtils.intResult(col));
145         assertEquals(5, DataAccessUtils.longResult(col));
146     }
147
148     public void testWithLong() {
149         Collection JavaDoc col = new HashSet JavaDoc();
150         col.add(new Long JavaDoc(5));
151
152         assertEquals(new Long JavaDoc(5), DataAccessUtils.uniqueResult(col));
153         assertEquals(new Long JavaDoc(5), DataAccessUtils.requiredUniqueResult(col));
154         assertEquals(new Long JavaDoc(5), DataAccessUtils.objectResult(col, Long JavaDoc.class));
155         assertEquals("5", DataAccessUtils.objectResult(col, String JavaDoc.class));
156         assertEquals(5, DataAccessUtils.intResult(col));
157         assertEquals(5, DataAccessUtils.longResult(col));
158     }
159
160     public void testWithString() {
161         Collection JavaDoc col = new HashSet JavaDoc();
162         col.add("test1");
163
164         assertEquals("test1", DataAccessUtils.uniqueResult(col));
165         assertEquals("test1", DataAccessUtils.requiredUniqueResult(col));
166         assertEquals("test1", DataAccessUtils.objectResult(col, String JavaDoc.class));
167
168         try {
169             DataAccessUtils.intResult(col);
170             fail("Should have thrown TypeMismatchDataAccessException");
171         }
172         catch (TypeMismatchDataAccessException ex) {
173             // expected
174
}
175
176         try {
177             DataAccessUtils.longResult(col);
178             fail("Should have thrown TypeMismatchDataAccessException");
179         }
180         catch (TypeMismatchDataAccessException ex) {
181             // expected
182
}
183     }
184
185     public void testWithDate() {
186         Date JavaDoc date = new Date JavaDoc();
187         Collection JavaDoc col = new HashSet JavaDoc();
188         col.add(date);
189
190         assertEquals(date, DataAccessUtils.uniqueResult(col));
191         assertEquals(date, DataAccessUtils.requiredUniqueResult(col));
192         assertEquals(date, DataAccessUtils.objectResult(col, Date JavaDoc.class));
193         assertEquals(date.toString(), DataAccessUtils.objectResult(col, String JavaDoc.class));
194
195         try {
196             DataAccessUtils.intResult(col);
197             fail("Should have thrown TypeMismatchDataAccessException");
198         }
199         catch (TypeMismatchDataAccessException ex) {
200             // expected
201
}
202
203         try {
204             DataAccessUtils.longResult(col);
205             fail("Should have thrown TypeMismatchDataAccessException");
206         }
207         catch (TypeMismatchDataAccessException ex) {
208             // expected
209
}
210     }
211
212 }
213
Popular Tags