KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > beans > test > ThreadSafeTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.beans.test;
8
9
10 import junit.framework.TestCase;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.beans.BeanProperty;
14 import com.inversoft.beans.SynchronizedBeanProperty;
15 import com.inversoft.util.typeconverter.TypeConversionException;
16
17
18 /**
19  * This class contains all the tests for the thread safe property classes
20  * in the JBeans package
21  * @author Brian Pontarelli
22  */

23 public class ThreadSafeTest extends TestCase {
24
25     public ThreadSafeTest(String JavaDoc name) {
26         super(name);
27     }
28
29     /**
30      * This tests the thread safe bean property simple methods
31      */

32     public void testThreadSafeBeanPropertyMethods() {
33
34         // Tests that the read only property returns the correct value
35
try {
36             Bean1 bean1 = new Bean1();
37             BeanProperty prop = new SynchronizedBeanProperty("readOnly", Bean1.class);
38
39             assertTrue("Should return readOnly", prop.getPropertyValue(bean1).equals("readOnly"));
40
41         } catch (BeanException be) {
42             fail(be.toString());
43         }
44
45         // Tests that the read only property fails correctly when being set
46
try {
47             Bean1 bean1 = new Bean1();
48             SynchronizedBeanProperty prop = new SynchronizedBeanProperty("readOnly", Bean1.class);
49
50             // Should fail
51
prop.setPropertyValue(bean1, "foo", true);
52             fail("Should have failed because read-only");
53
54         } catch (BeanException be) {
55             //System.err.println(be.toString());
56
assertTrue("Should have no root cause or target",
57                 be.getCause() == null && be.getTarget() == null);
58         } catch (TypeConversionException tce) {
59             fail(tce.toString());
60         }
61
62         // Tests the bean property gets set correctly and no object mangling happens
63
try {
64             Bean1 bean1 = new Bean1();
65             SynchronizedBeanProperty prop = new SynchronizedBeanProperty("string1", Bean1.class);
66             String JavaDoc value = "foo";
67
68             // Test the simple value setting
69
prop.setPropertyValue(bean1, value);
70
71             // Test that the value getting is the same object reference
72
assertTrue("Should point to same object", prop.getPropertyValue(bean1) == value);
73         } catch (BeanException be) {
74             fail(be.toString());
75         }
76
77         // Test the simple null value setting
78
try {
79             Bean1 bean1 = new Bean1();
80             SynchronizedBeanProperty prop = new SynchronizedBeanProperty("string1", Bean1.class);
81             String JavaDoc [] values = null;
82
83             prop.setPropertyValue(bean1, values, true);
84         } catch (BeanException be) {
85             fail(be.toString());
86         } catch (TypeConversionException tce) {
87             fail(tce.toString());
88         }
89
90         // Test the non-simple null value setting with conversion
91
try {
92             Bean1 bean1 = new Bean1();
93             SynchronizedBeanProperty prop = new SynchronizedBeanProperty("integer1", Bean1.class);
94             String JavaDoc [] values = null;
95
96             prop.setPropertyValue(bean1, values, true);
97         } catch (BeanException be) {
98             fail(be.toString());
99         } catch (TypeConversionException tce) {
100             fail(tce.toString());
101         }
102
103         // Test the super-non-simple null value setting with conversion and an
104
// empty array
105
try {
106             Bean1 bean1 = new Bean1();
107             SynchronizedBeanProperty prop = new SynchronizedBeanProperty("integer1", Bean1.class);
108             String JavaDoc [] values = new String JavaDoc[0];
109
110             prop.setPropertyValue(bean1, values, true);
111         } catch (BeanException be) {
112             fail(be.toString());
113         } catch (TypeConversionException tce) {
114             fail(tce.toString());
115         }
116
117         // Tests a simple automatic conversion from a string to an integer
118
try {
119             Bean1 bean1 = new Bean1();
120             SynchronizedBeanProperty prop = new SynchronizedBeanProperty("integer1", Bean1.class);
121             String JavaDoc value = "1024";
122
123             prop.setPropertyValue(bean1, value, true);
124
125             assertTrue("Should have converted value to new Integer(1024)",
126                        bean1.getInteger1().intValue() == 1024);
127         } catch (BeanException be) {
128             fail(be.toString());
129         } catch (TypeConversionException tce) {
130             fail(tce.toString());
131         }
132     }
133 }
134
135
Popular Tags