KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > DefaultPropertyModelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer.propertysheet;
21
22 import java.awt.IllegalComponentStateException JavaDoc;
23 import java.beans.PropertyDescriptor JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27
28 /** A test of a property model.
29  */

30 public class DefaultPropertyModelTest extends NbTestCase {
31
32     public DefaultPropertyModelTest(String JavaDoc name) {
33         super(name);
34     }
35
36     public void testLookupOfAPropertyReadOnlyProperty() throws Exception JavaDoc {
37         Object JavaDoc obj = new Object JavaDoc();
38         DefaultPropertyModel model = new DefaultPropertyModel(obj, "class");
39         
40         
41         assertEquals("Calls the get method", model.getValue(), obj.getClass());
42     }
43     
44     public void testLookupOfAPropertyReadWriteProperty() throws Exception JavaDoc {
45         ServerSocket JavaDoc obj = new ServerSocket JavaDoc(0);
46         
47         DefaultPropertyModel model = new DefaultPropertyModel(obj, "soTimeout");
48         
49         
50         assertEquals("Calls the get method", model.getValue(), new Integer JavaDoc(obj.getSoTimeout()));
51         
52         model.setValue(new Integer JavaDoc(100));
53         
54         assertEquals("Value change", 100, obj.getSoTimeout());
55         assertEquals("Model updated", model.getValue(), new Integer JavaDoc(obj.getSoTimeout()));
56     }
57     
58     //
59
// Test of explicit beaninfo
60
//
61

62     public void testUsageOfExplicitPropertyDescriptor() throws Exception JavaDoc {
63         PropertyDescriptor JavaDoc pd = new PropertyDescriptor JavaDoc(
64                 "myProp", this.getClass(),
65                 "getterUsageOfExplicitPropertyDescriptor",
66                 "setterUsageOfExplicitPropertyDescriptor"
67                 );
68         
69         DefaultPropertyModel model = new DefaultPropertyModel(this, pd);
70         
71         assertEquals("Getter returns this", model.getValue(), this);
72         
73         String JavaDoc msgToThrow = "msgToThrow";
74         try {
75             model.setValue(msgToThrow);
76             fail("Setter should throw an exception");
77         } catch (InvocationTargetException JavaDoc ex) {
78             // when an exception occurs it should throw InvocationTargetException
79
assertEquals("The right message", msgToThrow, ex.getTargetException().getMessage());
80         }
81     }
82     
83     public Object JavaDoc getterUsageOfExplicitPropertyDescriptor() {
84         return this;
85     }
86     
87     public void setterUsageOfExplicitPropertyDescriptor(Object JavaDoc any) {
88         throw new IllegalComponentStateException JavaDoc(any.toString());
89     }
90     
91     //
92
// End of explicit beaninfo
93
//
94
}
95
96
Popular Tags