KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > KeyHolderTests


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.jdbc.support;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.springframework.dao.DataRetrievalFailureException;
24 import org.springframework.dao.InvalidDataAccessApiUsageException;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Tests for the KeyHolder and GeneratedKeyHolder
30  * and it appears that JdbcUtils doesn't work exactly as documented.
31  *
32  * @author trisberg
33  * @since Jul 18, 2004
34  */

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 JavaDoc l = new LinkedList JavaDoc();
44         HashMap JavaDoc m = new HashMap JavaDoc(1);
45         m.put("key", new Integer JavaDoc(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 JavaDoc l = new LinkedList JavaDoc();
53         HashMap JavaDoc m = new HashMap JavaDoc(1);
54         m.put("key", new String JavaDoc("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 JavaDoc l = new LinkedList JavaDoc();
67         HashMap JavaDoc m = new HashMap JavaDoc();
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 JavaDoc l = new LinkedList JavaDoc();
80         HashMap JavaDoc m = new HashMap JavaDoc(1);
81         m.put("key", new Integer JavaDoc(1));
82         m.put("seq", new Integer JavaDoc(2));
83         l.add(m);
84         kh.getKeyList().addAll(l);
85         Map JavaDoc 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 JavaDoc l = new LinkedList JavaDoc();
97         HashMap JavaDoc m = new HashMap JavaDoc(1);
98         m.put("key", new Integer JavaDoc(1));
99         m.put("seq", new Integer JavaDoc(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