1 16 17 package org.springframework.jdbc.support; 18 19 import java.util.HashMap ; 20 import java.util.LinkedList ; 21 import java.util.Map ; 22 23 import org.springframework.dao.DataRetrievalFailureException; 24 import org.springframework.dao.InvalidDataAccessApiUsageException; 25 26 import junit.framework.TestCase; 27 28 35 public class KeyHolderTests extends TestCase { 36 private KeyHolder kh; 37 38 public void setUp() { 39 kh = new GeneratedKeyHolder(); 40 } 41 42 public void testSingleKey(){ 43 LinkedList l = new LinkedList (); 44 HashMap m = new HashMap (1); 45 m.put("key", new Integer (1)); 46 l.add(m); 47 kh.getKeyList().addAll(l); 48 assertEquals("single key should be returned", 1, kh.getKey().intValue()); 49 } 50 51 public void testSingleKeyNonNumeric(){ 52 LinkedList l = new LinkedList (); 53 HashMap m = new HashMap (1); 54 m.put("key", new String ("1")); 55 l.add(m); 56 kh.getKeyList().addAll(l); 57 try { 58 kh.getKey().intValue(); 59 } 60 catch (DataRetrievalFailureException e) { 61 assertTrue(e.getMessage().startsWith("The generated key is not of a supported numeric type.")); 62 } 63 } 64 65 public void testNoKeyReturnedInMap(){ 66 LinkedList l = new LinkedList (); 67 HashMap m = new HashMap (); 68 l.add(m); 69 kh.getKeyList().addAll(l); 70 try { 71 kh.getKey(); 72 } 73 catch (DataRetrievalFailureException e) { 74 assertTrue(e.getMessage().startsWith("Unable to retrieve the generated key.")); 75 } 76 } 77 78 public void testMultipleKeys(){ 79 LinkedList l = new LinkedList (); 80 HashMap m = new HashMap (1); 81 m.put("key", new Integer (1)); 82 m.put("seq", new Integer (2)); 83 l.add(m); 84 kh.getKeyList().addAll(l); 85 Map keyMap = kh.getKeys(); 86 assertEquals("two keys should be in the map", 2, keyMap.size()); 87 try { 88 kh.getKey(); 89 } 90 catch (InvalidDataAccessApiUsageException e) { 91 assertTrue(e.getMessage().startsWith("The getKey method should only be used when a single key is returned.")); 92 } 93 } 94 95 public void testMultipleKeyRows(){ 96 LinkedList l = new LinkedList (); 97 HashMap m = new HashMap (1); 98 m.put("key", new Integer (1)); 99 m.put("seq", new Integer (2)); 100 l.add(m); 101 l.add(m); 102 kh.getKeyList().addAll(l); 103 104 assertEquals("two rows should be in the list", 2, kh.getKeyList().size()); 105 try { 106 kh.getKeys(); 107 } 108 catch (InvalidDataAccessApiUsageException e) { 109 assertTrue(e.getMessage().startsWith("The getKeys method should only be used when keys for a single row are returned.")); 110 } 111 } 112 } 113 | Popular Tags |