1 16 17 package org.springframework.dao; 18 19 import java.util.Collection ; 20 import java.util.Date ; 21 import java.util.HashSet ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.dao.support.DataAccessUtils; 26 27 31 public class DataAccessUtilsTests extends TestCase { 32 33 public void testWithEmptyCollection() { 34 Collection col = new HashSet (); 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 assertEquals(1, ex.getExpectedSize()); 45 assertEquals(0, ex.getActualSize()); 46 } 47 48 try { 49 DataAccessUtils.objectResult(col, String .class); 50 fail("Should have thrown IncorrectResultSizeDataAccessException"); 51 } 52 catch (IncorrectResultSizeDataAccessException ex) { 53 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 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 assertEquals(1, ex.getExpectedSize()); 75 assertEquals(0, ex.getActualSize()); 76 } 77 } 78 79 public void testWithTooLargeCollection() { 80 Collection col = new HashSet (); 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 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 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 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 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 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 } 175 176 try { 177 DataAccessUtils.longResult(col); 178 fail("Should have thrown TypeMismatchDataAccessException"); 179 } 180 catch (TypeMismatchDataAccessException ex) { 181 } 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)); 193 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 } 202 203 try { 204 DataAccessUtils.longResult(col); 205 fail("Should have thrown TypeMismatchDataAccessException"); 206 } 207 catch (TypeMismatchDataAccessException ex) { 208 } 210 } 211 212 } 213 | Popular Tags |