KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > object > RdbmsOperationTests


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.object;
18
19 import java.sql.Types JavaDoc;
20
21 import javax.sql.DataSource JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.dao.InvalidDataAccessApiUsageException;
26 import org.springframework.jdbc.core.JdbcTemplate;
27 import org.springframework.jdbc.core.SqlParameter;
28 import org.springframework.jdbc.datasource.DriverManagerDataSource;
29
30 /**
31  * @author Trevor Cook
32  */

33 public class RdbmsOperationTests extends TestCase {
34
35     public void testEmptySql() {
36         TestRdbmsOperation operation = new TestRdbmsOperation();
37         try {
38             operation.compile();
39             fail("Shouldn't allow compiling without sql statement");
40         }
41         catch (InvalidDataAccessApiUsageException idaauex) {
42             // OK
43
}
44     }
45
46     public void testSetTypeAfterCompile() {
47         TestRdbmsOperation operation = new TestRdbmsOperation();
48         operation.setDataSource(new DriverManagerDataSource());
49         operation.setSql("select * from mytable");
50         operation.compile();
51         try {
52             operation.setTypes(new int[] {Types.INTEGER });
53             fail("Shouldn't allow setting parameters after compile");
54         }
55         catch (InvalidDataAccessApiUsageException idaauex) {
56             // OK
57
}
58     }
59
60     public void testDeclareParameterAfterCompile() {
61         TestRdbmsOperation operation = new TestRdbmsOperation();
62         operation.setDataSource(new DriverManagerDataSource());
63         operation.setSql("select * from mytable");
64         operation.compile();
65         try {
66             operation.declareParameter(new SqlParameter(Types.INTEGER));
67             fail("Shouldn't allow setting parameters after compile");
68         }
69         catch (InvalidDataAccessApiUsageException idaauex) {
70             // OK
71
}
72     }
73
74     public void testTooFewParameters() {
75         TestRdbmsOperation operation = new TestRdbmsOperation();
76         operation.setSql("select * from mytable");
77         operation.setTypes(new int[] { Types.INTEGER });
78         try {
79             operation.validateParameters(null);
80             fail("Shouldn't validate without enough parameters");
81         }
82         catch (InvalidDataAccessApiUsageException idaauex) {
83             // OK
84
}
85     }
86
87     public void testOperationConfiguredViaJdbcTemplateMustGetDataSource() throws Exception JavaDoc {
88         try {
89             TestRdbmsOperation operation = new TestRdbmsOperation();
90             operation.setSql("foo");
91             operation.compile();
92             fail("Can't compile without providing a DataSource for the JdbcTemplate");
93         }
94         catch (InvalidDataAccessApiUsageException ex) {
95             // Check for helpful error message. Omit leading character
96
// so as not to be fussy about case
97
assertTrue(ex.getMessage().indexOf("ataSource") != -1);
98         }
99     }
100
101     public void testTooManyParameters() {
102         TestRdbmsOperation operation = new TestRdbmsOperation();
103         operation.setSql("select * from mytable");
104         try {
105             operation.validateParameters(new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2) });
106             fail("Shouldn't validate with too many parameters");
107         }
108         catch (InvalidDataAccessApiUsageException idaauex) {
109             // OK
110
}
111     }
112
113     public void testCompileTwice() {
114         TestRdbmsOperation operation = new TestRdbmsOperation();
115         operation.setDataSource(new DriverManagerDataSource());
116         operation.setSql("select * from mytable");
117         operation.setTypes(null);
118         operation.compile();
119         operation.compile();
120     }
121
122     public void testEmptyDataSource() {
123         SqlOperation operation = new SqlOperation() {
124         };
125         operation.setSql("select * from mytable");
126         try {
127         operation.compile();
128         fail("Shouldn't allow compiling without data source");
129         } catch (InvalidDataAccessApiUsageException idaauex) {
130             // OK
131
}
132     }
133
134     public void testParameterPropagation() {
135         SqlOperation operation = new SqlOperation() {
136         };
137         DataSource JavaDoc ds = new DriverManagerDataSource();
138         operation.setDataSource(ds);
139         operation.setFetchSize(10);
140         operation.setMaxRows(20);
141         JdbcTemplate jt = operation.getJdbcTemplate();
142         assertEquals(ds, jt.getDataSource());
143         assertEquals(10, jt.getFetchSize());
144         assertEquals(20, jt.getMaxRows());
145     }
146
147
148     private static class TestRdbmsOperation extends RdbmsOperation {
149
150         protected void compileInternal() {
151         }
152     }
153
154 }
155
Popular Tags