Added
Link Here
|
1 |
/* |
2 |
* IntEditorTest.java |
3 |
* JUnit based test |
4 |
* |
5 |
* Created on March 10, 2003, 1:10 PM |
6 |
*/ |
7 |
|
8 |
package org.netbeans.beaninfo.editors; |
9 |
|
10 |
import java.beans.*; |
11 |
import junit.framework.*; |
12 |
import org.openide.explorer.propertysheet.*; |
13 |
import org.openide.nodes.*; |
14 |
import org.openide.util.NbBundle; |
15 |
import java.util.Arrays; |
16 |
|
17 |
/** Unit test for priimitive int property editor |
18 |
* |
19 |
* @author Tim Boudreau |
20 |
*/ |
21 |
public class IntEditorTest extends TestCase { |
22 |
String[] goodKeys = new String[] {"zero","one","two","three"}; |
23 |
int[] goodValues = new int[] {0,1,2,3}; |
24 |
String [] goodCode = new String[] {"0", "0+1", "1+1", "1+2"}; |
25 |
|
26 |
IntProp goodProp = new IntProp (0, goodKeys, goodValues, goodCode); |
27 |
|
28 |
String[] badKeys = new String[] {"blah"}; |
29 |
int[] badValues = new int[] {1,15}; |
30 |
|
31 |
String[] emptyKeys = new String[0]; |
32 |
|
33 |
IntProp badMismatchProp = new IntProp (1, emptyKeys, goodValues, null); |
34 |
IntProp badMismatchProp2 = new IntProp (1, goodKeys, goodValues, emptyKeys); |
35 |
|
36 |
public IntEditorTest(java.lang.String testName) { |
37 |
super(testName); |
38 |
|
39 |
} |
40 |
|
41 |
public static Test suite() { |
42 |
TestSuite suite = new TestSuite(IntEditorTest.class); |
43 |
return suite; |
44 |
} |
45 |
|
46 |
/** Test of attachEnv method, of class org.netbeans.beaninfo.editors.IntEditor. */ |
47 |
public void testAttachEnv() { |
48 |
System.out.println("testAttachEnv"); |
49 |
PropertyEditor ped = goodProp.getPropertyEditor(); |
50 |
if (!(ped instanceof ExPropertyEditor)) { |
51 |
fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName()); |
52 |
} else { |
53 |
ExPropertyEditor ex = (ExPropertyEditor) ped; |
54 |
prepareEnv (goodProp, ex, false); |
55 |
} |
56 |
|
57 |
ped = badMismatchProp.getPropertyEditor(); |
58 |
if (!(ped instanceof ExPropertyEditor)) { |
59 |
fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName()); |
60 |
} else { |
61 |
ExPropertyEditor ex = (ExPropertyEditor) ped; |
62 |
prepareEnv (badMismatchProp, ex, true); |
63 |
} |
64 |
|
65 |
ped = badMismatchProp2.getPropertyEditor(); |
66 |
if (!(ped instanceof ExPropertyEditor)) { |
67 |
fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName()); |
68 |
} else { |
69 |
ExPropertyEditor ex = (ExPropertyEditor) ped; |
70 |
prepareEnv (badMismatchProp2, ex, true); |
71 |
} |
72 |
} |
73 |
|
74 |
/** Test of getAsText method, of class org.netbeans.beaninfo.editors.IntEditor. */ |
75 |
public void testGetAsText() { |
76 |
System.out.println("testGetAsText"); |
77 |
|
78 |
// Add your test code below by replacing the default call to fail. |
79 |
ExPropertyEditor ped = getConfiguredEditor (goodProp); |
80 |
ped.setValue (new Integer(0)); |
81 |
String s = ped.getAsText(); |
82 |
if ("zero".equals(s)) return; |
83 |
} |
84 |
|
85 |
/** Test of setAsText method, of class org.netbeans.beaninfo.editors.IntEditor. */ |
86 |
public void testSetAsText() { |
87 |
System.out.println("testSetAsText"); |
88 |
ExPropertyEditor ped = getConfiguredEditor (goodProp); |
89 |
ped.setAsText ("two"); |
90 |
int i = ((Integer) ped.getValue()).intValue(); |
91 |
if (i != 2) { |
92 |
fail ("Set text value of propertyeditor to \"two\" but getValue() returns " + i); |
93 |
} |
94 |
} |
95 |
|
96 |
/** Test of getTags method, of class org.netbeans.beaninfo.editors.IntEditor. */ |
97 |
public void testGetTags() { |
98 |
System.out.println("testGetTags"); |
99 |
ExPropertyEditor ped = getConfiguredEditor (goodProp); |
100 |
String[] s = ped.getTags(); |
101 |
if (s.length != 4) fail ("Keys length should be 4 but is " + s.length); |
102 |
if ("zero".equals(s[0])) return; |
103 |
fail ("First key should be \"zero\" but is " + s[0]); |
104 |
} |
105 |
|
106 |
/** Test of getJavaInitializationString method, of class org.netbeans.beaninfo.editors.IntEditor. */ |
107 |
public void testGetJavaInitializationString() { |
108 |
System.out.println("testGetJavaInitializationString"); |
109 |
ExPropertyEditor ped = getConfiguredEditor (goodProp); |
110 |
ped.setValue (new Integer(3)); |
111 |
String s = ped.getJavaInitializationString(); |
112 |
if (!("1+2".equals (s))) { |
113 |
fail ("GetJavaInitializationString returns " + s + " but should return \"1+1\""); |
114 |
} |
115 |
} |
116 |
|
117 |
private ExPropertyEditor getConfiguredEditor (IntProp p) { |
118 |
ExPropertyEditor result = (ExPropertyEditor) p.getPropertyEditor(); |
119 |
try { |
120 |
result.setValue ((Integer) p.getValue()); |
121 |
prepareEnv (p, result, false); |
122 |
} catch (Exception e) { |
123 |
//Other tests will fail if an exception is thrown, ignore |
124 |
} |
125 |
return result; |
126 |
} |
127 |
|
128 |
private void prepareEnv (IntProp prop, ExPropertyEditor pe, boolean shouldFail) { |
129 |
PropertyEnv env = org.openide.explorer.propertysheet.EnvProvider.getEnv(prop); |
130 |
org.netbeans.beaninfo.editors.IntEditor.EnvException e=null; |
131 |
try { |
132 |
pe.attachEnv (env); |
133 |
|
134 |
} catch (org.netbeans.beaninfo.editors.IntEditor.EnvException ie) { |
135 |
e = ie; |
136 |
} finally { |
137 |
if (shouldFail && e == null) { |
138 |
fail ("EnvException should have been thrown but wasn't."); |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
private class IntProp extends Node.Property { |
144 |
String[] k; |
145 |
int[] v; |
146 |
String[] c; |
147 |
int val; |
148 |
public IntProp (int val, String[] keys, int[] vals, String[] codeVals) { |
149 |
super (Integer.TYPE); |
150 |
this.val = val; |
151 |
v = vals; |
152 |
k = keys; |
153 |
c = codeVals; |
154 |
} |
155 |
|
156 |
public Object getValue (String key) { |
157 |
if ("stringKeys".equals(key)) return k; |
158 |
if ("intValues".equals(key)) return v; |
159 |
if ("codeValues".equals(key)) return c; |
160 |
Object result = super.getValue(key); |
161 |
return result; |
162 |
} |
163 |
|
164 |
public boolean canRead() { |
165 |
return true; |
166 |
} |
167 |
|
168 |
public boolean canWrite() { |
169 |
return true; |
170 |
} |
171 |
|
172 |
public Object getValue() throws IllegalAccessException, java.lang.reflect.InvocationTargetException { |
173 |
return new Integer(val); |
174 |
} |
175 |
|
176 |
public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException, java.lang.reflect.InvocationTargetException { |
177 |
this.val = ((Integer) val).intValue(); |
178 |
} |
179 |
|
180 |
public PropertyEditor getPropertyEditor () { |
181 |
return new org.netbeans.beaninfo.editors.IntEditor (); |
182 |
} |
183 |
|
184 |
} |
185 |
|
186 |
} |