KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > FieldRetrievingFactoryBeanTests


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.beans.factory.config;
18
19 import java.sql.Connection JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * @author Juergen Hoeller
25  * @since 31.07.2004
26  */

27 public class FieldRetrievingFactoryBeanTests extends TestCase {
28
29     public void testStaticField() throws Exception JavaDoc {
30         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
31         fr.setStaticField("java.sql.Connection.TRANSACTION_SERIALIZABLE");
32         fr.afterPropertiesSet();
33         assertEquals(new Integer JavaDoc(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
34     }
35
36     public void testStaticFieldWithWhitespace() throws Exception JavaDoc {
37         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
38         fr.setStaticField(" java.sql.Connection.TRANSACTION_SERIALIZABLE ");
39         fr.afterPropertiesSet();
40         assertEquals(new Integer JavaDoc(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
41     }
42
43     public void testStaticFieldViaClassAndFieldName() throws Exception JavaDoc {
44         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
45         fr.setTargetClass(Connection JavaDoc.class);
46         fr.setTargetField("TRANSACTION_SERIALIZABLE");
47         fr.afterPropertiesSet();
48         assertEquals(new Integer JavaDoc(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
49     }
50
51     public void testNonStaticField() throws Exception JavaDoc {
52         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
53         PublicFieldHolder target = new PublicFieldHolder();
54         fr.setTargetObject(target);
55         fr.setTargetField("publicField");
56         fr.afterPropertiesSet();
57         assertEquals(target.publicField, fr.getObject());
58     }
59
60     public void testNothingButBeanName() throws Exception JavaDoc {
61         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
62         fr.setBeanName("java.sql.Connection.TRANSACTION_SERIALIZABLE");
63         fr.afterPropertiesSet();
64         assertEquals(new Integer JavaDoc(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
65     }
66
67     public void testJustTargetField() throws Exception JavaDoc {
68         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
69         fr.setTargetField("TRANSACTION_SERIALIZABLE");
70         try {
71             fr.afterPropertiesSet();
72         }
73         catch (IllegalArgumentException JavaDoc ex) {
74             // expected
75
}
76     }
77
78     public void testJustTargetClass() throws Exception JavaDoc {
79         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
80         fr.setTargetClass(Connection JavaDoc.class);
81         try {
82             fr.afterPropertiesSet();
83         }
84         catch (IllegalArgumentException JavaDoc ex) {
85             // expected
86
}
87     }
88
89     public void testJustTargetObject() throws Exception JavaDoc {
90         FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
91         fr.setTargetObject(new PublicFieldHolder());
92         try {
93             fr.afterPropertiesSet();
94         }
95         catch (IllegalArgumentException JavaDoc ex) {
96             // expected
97
}
98     }
99
100
101     private static class PublicFieldHolder {
102
103         public String JavaDoc publicField = "test";
104     }
105
106 }
107
Popular Tags