e.g. Calendar Search Help
You must enter a value before pressing Search
spring

Class: org.springframework.dao.DataAccessUtilsTests   ©

 OK to copy?
001 /*
002  * Copyright 2002-2004 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 
017 package org.springframework.dao;
018 
019 import java.util.Collection;
020 import java.util.Date;
021 import java.util.HashSet;
022 
023 import junit.framework.TestCase;
024 
025 import org.springframework.dao.support.DataAccessUtils;
026 
027 /**
028  * @author Juergen Hoeller
029  * @since 20.10.2004
030  */
031 public class DataAccessUtilsTests extends TestCase {
032 
033     public void testWithEmptyCollection() {
034         Collection col = new HashSet();
035 
036         assertNull(DataAccessUtils.uniqueResult(col));
037 
038         try {
039             DataAccessUtils.requiredUniqueResult(col);
040             fail("Should have thrown IncorrectResultSizeDataAccessException");
041         }
042         catch (IncorrectResultSizeDataAccessException ex) {
043             // expected
044             assertEquals(1, ex.getExpectedSize());
045             assertEquals(0, ex.getActualSize());
046         }
047 
048         try {
049             DataAccessUtils.objectResult(col, String.class);
050             fail("Should have thrown IncorrectResultSizeDataAccessException");
051         }
052         catch (IncorrectResultSizeDataAccessException ex) {
053             // expected
054             assertEquals(1, ex.getExpectedSize());
055             assertEquals(0, ex.getActualSize());
056         }
057 
058         try {
059             DataAccessUtils.intResult(col);
060             fail("Should have thrown IncorrectResultSizeDataAccessException");
061         }
062         catch (IncorrectResultSizeDataAccessException ex) {
063             // expected
064             assertEquals(1, ex.getExpectedSize());
065             assertEquals(0, ex.getActualSize());
066         }
067 
068         try {
069             DataAccessUtils.longResult(col);
070             fail("Should have thrown IncorrectResultSizeDataAccessException");
071         }
072         catch (IncorrectResultSizeDataAccessException ex) {
073             // expected
074             assertEquals(1, ex.getExpectedSize());
075             assertEquals(0, ex.getActualSize());
076         }
077     }
078 
079     public void testWithTooLargeCollection() {
080         Collection col = new HashSet();
081         col.add("test1");
082         col.add("test2");
083 
084         try {
085             DataAccessUtils.uniqueResult(col);
086             fail("Should have thrown IncorrectResultSizeDataAccessException");
087         }
088         catch (IncorrectResultSizeDataAccessException ex) {
089             // expected
090             assertEquals(1, ex.getExpectedSize());
091             assertEquals(2, ex.getActualSize());
092         }
093 
094 
095         try {
096             DataAccessUtils.requiredUniqueResult(col);
097             fail("Should have thrown IncorrectResultSizeDataAccessException");
098         }
099         catch (IncorrectResultSizeDataAccessException ex) {
100             // expected
101             assertEquals(1, ex.getExpectedSize());
102             assertEquals(2, ex.getActualSize());
103         }
104 
105         try {
106             DataAccessUtils.objectResult(col, String.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 col = new HashSet();
138         col.add(new Integer(5));
139 
140         assertEquals(new Integer(5), DataAccessUtils.uniqueResult(col));
141         assertEquals(new Integer(5), DataAccessUtils.requiredUniqueResult(col));
142         assertEquals(new Integer(5), DataAccessUtils.objectResult(col, Integer.class));
143         assertEquals("5", DataAccessUtils.objectResult(col, String.class));
144         assertEquals(5, DataAccessUtils.intResult(col));
145         assertEquals(5, DataAccessUtils.longResult(col));
146     }
147 
148     public void testWithLong() {
149         Collection col = new HashSet();
150         col.add(new Long(5));
151 
152         assertEquals(new Long(5), DataAccessUtils.uniqueResult(col));
153         assertEquals(new Long(5), DataAccessUtils.requiredUniqueResult(col));
154         assertEquals(new Long(5), DataAccessUtils.objectResult(col, Long.class));
155         assertEquals("5", DataAccessUtils.objectResult(col, String.class));
156         assertEquals(5, DataAccessUtils.intResult(col));
157         assertEquals(5, DataAccessUtils.longResult(col));
158     }
159 
160     public void testWithString() {
161         Collection col = new HashSet();
162         col.add("test1");
163 
164         assertEquals("test1", DataAccessUtils.uniqueResult(col));
165         assertEquals("test1", DataAccessUtils.requiredUniqueResult(col));
166         assertEquals("test1", DataAccessUtils.objectResult(col, String.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 date = new Date();
187         Collection col = new HashSet();
188         col.add(date);
189 
190         assertEquals(date, DataAccessUtils.uniqueResult(col));
191         assertEquals(date, DataAccessUtils.requiredUniqueResult(col));
192         assertEquals(date, DataAccessUtils.objectResult(col, Date.class));
Rate193         assertEquals(date.toString(), DataAccessUtils.objectResult(col, String.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 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
193 0% of 0