KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > simpl > PropertySettingJobFactoryTest


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.simpl;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.quartz.JobDataMap;
22 import org.quartz.SchedulerException;
23
24 import junit.framework.TestCase;
25
26 /**
27  * Unit test for PropertySettingJobFactory.
28  */

29 public class PropertySettingJobFactoryTest extends TestCase {
30     
31     private PropertySettingJobFactory factory;
32     
33     protected void setUp() throws Exception JavaDoc {
34         factory = new PropertySettingJobFactory();
35         factory.setThrowIfPropertyNotFound(true);
36     }
37     
38     public void testSetBeanPropsPrimatives() throws SchedulerException {
39         JobDataMap jobDataMap = new JobDataMap();
40         jobDataMap.put("intValue", new Integer JavaDoc(1));
41         jobDataMap.put("longValue", new Long JavaDoc(2l));
42         jobDataMap.put("floatValue", new Float JavaDoc(3.0f));
43         jobDataMap.put("doubleValue", new Double JavaDoc(4.0));
44         jobDataMap.put("booleanValue", Boolean.TRUE);
45         jobDataMap.put("shortValue", new Short JavaDoc(((short)5)));
46         jobDataMap.put("charValue", 'a');
47         jobDataMap.put("byteValue", new Byte JavaDoc((byte)6));
48         jobDataMap.put("stringValue", "S1");
49         jobDataMap.put("mapValue", Collections.singletonMap("A", "B"));
50         
51         TestBean myBean = new TestBean();
52         factory.setBeanProps(myBean, jobDataMap);
53         
54         assertEquals(1, myBean.getIntValue());
55         assertEquals(2l, myBean.getLongValue());
56         assertEquals(3.0f, myBean.getFloatValue(), 0.0001);
57         assertEquals(4.0, myBean.getDoubleValue(), 0.0001);
58         assertTrue(myBean.getBooleanValue());
59         assertEquals(5, myBean.getShortValue());
60         assertEquals('a', myBean.getCharValue());
61         assertEquals((byte)6, myBean.getByteValue());
62         assertEquals("S1", myBean.getStringValue());
63         assertTrue(myBean.getMapValue().containsKey("A"));
64     }
65     
66     public void testSetBeanPropsUnknownProperty() {
67         JobDataMap jobDataMap = new JobDataMap();
68         jobDataMap.put("bogusValue", new Integer JavaDoc(1));
69         try {
70             factory.setBeanProps(new TestBean(), jobDataMap);
71             fail();
72         } catch (SchedulerException ignore) {}
73     }
74     
75     public void testSetBeanPropsNullPrimative() {
76         JobDataMap jobDataMap = new JobDataMap();
77         jobDataMap.put("intValue", null);
78         try {
79             factory.setBeanProps(new TestBean(), jobDataMap);
80             fail();
81         } catch (SchedulerException ignore) {}
82     }
83     
84     public void testSetBeanPropsNullNonPrimative() throws SchedulerException {
85         JobDataMap jobDataMap = new JobDataMap();
86         jobDataMap.put("mapValue", null);
87         TestBean testBean = new TestBean();
88         testBean.setMapValue(Collections.singletonMap("A", "B"));
89         factory.setBeanProps(testBean, jobDataMap);
90         assertNull(testBean.getMapValue());
91     }
92     
93     public void testSetBeanPropsWrongPrimativeType() {
94         JobDataMap jobDataMap = new JobDataMap();
95         jobDataMap.put("intValue", new Float JavaDoc(7));
96         try {
97             factory.setBeanProps(new TestBean(), jobDataMap);
98             fail();
99         } catch (SchedulerException ignore) {}
100     }
101
102     public void testSetBeanPropsWrongNonPrimativeType() {
103         JobDataMap jobDataMap = new JobDataMap();
104         jobDataMap.put("mapValue", new Float JavaDoc(7));
105         try {
106             factory.setBeanProps(new TestBean(), jobDataMap);
107             fail();
108         } catch (SchedulerException ignore) {}
109     }
110
111     public void testSetBeanPropsCharStringTooShort() {
112         JobDataMap jobDataMap = new JobDataMap();
113         jobDataMap.put("charValue", "");
114         try {
115             factory.setBeanProps(new TestBean(), jobDataMap);
116             fail();
117         } catch (SchedulerException ignore) {}
118     }
119
120     public void testSetBeanPropsCharStringTooLong() {
121         JobDataMap jobDataMap = new JobDataMap();
122         jobDataMap.put("charValue", "abba");
123         try {
124             factory.setBeanProps(new TestBean(), jobDataMap);
125             fail();
126         } catch (SchedulerException ignore) {}
127     }
128
129     public void testSetBeanPropsFromStrings() throws SchedulerException {
130         JobDataMap jobDataMap = new JobDataMap();
131         jobDataMap.put("intValue", "1");
132         jobDataMap.put("longValue", "2");
133         jobDataMap.put("floatValue", "3.0");
134         jobDataMap.put("doubleValue", "4.0");
135         jobDataMap.put("booleanValue", "true");
136         jobDataMap.put("shortValue", "5");
137         jobDataMap.put("charValue", "a");
138         jobDataMap.put("byteValue", "6");
139         
140         TestBean myBean = new TestBean();
141         factory.setBeanProps(myBean, jobDataMap);
142         
143         assertEquals(1, myBean.getIntValue());
144         assertEquals(2l, myBean.getLongValue());
145         assertEquals(3.0f, myBean.getFloatValue(), 0.0001);
146         assertEquals(4.0, myBean.getDoubleValue(), 0.0001);
147         assertEquals(true, myBean.getBooleanValue());
148         assertEquals(5, myBean.getShortValue());
149         assertEquals('a', myBean.getCharValue());
150         assertEquals((byte)6, myBean.getByteValue());
151     }
152
153     private static final class TestBean {
154         private int intValue;
155         private long longValue;
156         private float floatValue;
157         private double doubleValue;
158         private boolean booleanValue;
159         private byte byteValue;
160         private short shortValue;
161         private char charValue;
162         private String JavaDoc stringValue;
163         private Map JavaDoc mapValue;
164     
165         public boolean getBooleanValue() {
166             return booleanValue;
167         }
168     
169         public void setBooleanValue(boolean booleanValue) {
170             this.booleanValue = booleanValue;
171         }
172     
173         public double getDoubleValue() {
174             return doubleValue;
175         }
176     
177         public void setDoubleValue(double doubleValue) {
178             this.doubleValue = doubleValue;
179         }
180     
181         public float getFloatValue() {
182             return floatValue;
183         }
184     
185         public void setFloatValue(float floatValue) {
186             this.floatValue = floatValue;
187         }
188    
189         public int getIntValue() {
190             return intValue;
191         }
192     
193         public void setIntValue(int intValue) {
194             this.intValue = intValue;
195         }
196     
197         public long getLongValue() {
198             return longValue;
199         }
200     
201         public void setLongValue(long longValue) {
202             this.longValue = longValue;
203         }
204
205         public Map JavaDoc getMapValue() {
206             return mapValue;
207         }
208     
209         public void setMapValue(Map JavaDoc mapValue) {
210             this.mapValue = mapValue;
211         }
212     
213         public String JavaDoc getStringValue() {
214             return stringValue;
215         }
216     
217         public void setStringValue(String JavaDoc stringValue) {
218             this.stringValue = stringValue;
219         }
220
221         public byte getByteValue() {
222             return byteValue;
223         }
224
225         public void setByteValue(byte byteValue) {
226             this.byteValue = byteValue;
227         }
228
229         public char getCharValue() {
230             return charValue;
231         }
232
233         public void setCharValue(char charValue) {
234             this.charValue = charValue;
235         }
236
237         public short getShortValue() {
238             return shortValue;
239         }
240
241         public void setShortValue(short shortValue) {
242             this.shortValue = shortValue;
243         }
244     }
245 }
246
Popular Tags