KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > JdbcBeanDefinitionReaderTests


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.core.support;
18
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.Statement JavaDoc;
21
22 import org.easymock.MockControl;
23
24 import org.springframework.beans.TestBean;
25 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
26 import org.springframework.jdbc.AbstractJdbcTests;
27
28 /**
29  * @author Rod Johnson
30  */

31 public class JdbcBeanDefinitionReaderTests extends AbstractJdbcTests {
32
33     public void testValid() throws Exception JavaDoc {
34         String JavaDoc sql = "SELECT NAME AS NAME, PROPERTY AS PROPERTY, VALUE AS VALUE FROM T";
35
36         MockControl ctrlResultSet = MockControl.createControl(ResultSet JavaDoc.class);
37         ResultSet JavaDoc mockResultSet = (ResultSet JavaDoc) ctrlResultSet.getMock();
38         ctrlResultSet.expectAndReturn(mockResultSet.next(), true, 2);
39         ctrlResultSet.expectAndReturn(mockResultSet.next(), false);
40
41         // first row
42
ctrlResultSet.expectAndReturn(mockResultSet.getString(1), "one");
43         ctrlResultSet.expectAndReturn(mockResultSet.getString(2), "class");
44         ctrlResultSet.expectAndReturn(mockResultSet.getString(3), "org.springframework.beans.TestBean");
45
46         // second row
47
ctrlResultSet.expectAndReturn(mockResultSet.getString(1), "one");
48         ctrlResultSet.expectAndReturn(mockResultSet.getString(2), "age");
49         ctrlResultSet.expectAndReturn(mockResultSet.getString(3), "53");
50
51         mockResultSet.close();
52         ctrlResultSet.setVoidCallable();
53
54         MockControl ctrlStatement = MockControl.createControl(Statement JavaDoc.class);
55         Statement JavaDoc mockStatement = (Statement JavaDoc) ctrlStatement.getMock();
56         ctrlStatement.expectAndReturn(mockStatement.executeQuery(sql), mockResultSet);
57         ctrlStatement.expectAndReturn(mockStatement.getWarnings(), null);
58         mockStatement.close();
59         ctrlStatement.setVoidCallable();
60
61         mockConnection.createStatement();
62         ctrlConnection.setReturnValue(mockStatement);
63
64         ctrlResultSet.replay();
65         ctrlStatement.replay();
66         replay();
67
68         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
69         JdbcBeanDefinitionReader reader = new JdbcBeanDefinitionReader(bf);
70         reader.setDataSource(mockDataSource);
71         reader.loadBeanDefinitions(sql);
72         assertEquals("Incorrect number of bean definitions", 1, bf.getBeanDefinitionCount());
73         TestBean tb = (TestBean) bf.getBean("one");
74         assertEquals("Age in TestBean was wrong.", 53, tb.getAge());
75
76         ctrlResultSet.verify();
77         ctrlStatement.verify();
78     }
79
80 }
81
Popular Tags