Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.j2seproject.api; |
43 |
|
44 |
import java.beans.PropertyChangeListener; |
45 |
import java.io.IOException; |
46 |
import java.util.Map; |
47 |
import javax.swing.JPanel; |
48 |
import javax.swing.JTextField; |
49 |
import javax.swing.JToggleButton; |
50 |
import javax.swing.text.Document; |
51 |
import org.junit.BeforeClass; |
52 |
import org.junit.Test; |
53 |
import static org.junit.Assert.*; |
54 |
import org.netbeans.api.project.Project; |
55 |
import org.netbeans.junit.MockServices; |
56 |
import org.netbeans.spi.project.support.ant.EditableProperties; |
57 |
import org.netbeans.spi.project.support.ant.PropertyEvaluator; |
58 |
import org.netbeans.spi.project.support.ant.ui.StoreGroup; |
59 |
import org.openide.util.Exceptions; |
60 |
import org.openide.util.Lookup; |
61 |
|
62 |
/** |
63 |
* Test of SPI class org.netbeans.modules.java.j2seproject.api.J2SECustomPropertyHandler |
64 |
* |
65 |
* @author Petr Somol |
66 |
*/ |
67 |
public class J2SECustomPropertyHandlerTest { |
68 |
|
69 |
public J2SECustomPropertyHandlerTest() { |
70 |
} |
71 |
|
72 |
@BeforeClass |
73 |
public static void setUpClass() throws Exception { |
74 |
MockServices.setServices(MockProjectExtenderPropertiesHandler.class); |
75 |
} |
76 |
|
77 |
private static MockProjectExtenderProperties extProperties = MockProjectExtenderProperties.getInstance(); |
78 |
|
79 |
/** |
80 |
* Simulates property storage. |
81 |
*/ |
82 |
private static EditableProperties storedProperties = new EditableProperties(true); |
83 |
|
84 |
/** |
85 |
* Simulates reaction to clicking OK in Project Properties dialog. |
86 |
* Both standard and extended properties thus need to be saved. |
87 |
*/ |
88 |
public void invokeCentralizedSave() { |
89 |
// first save standard project properties |
90 |
System.out.println("invokeCentralizedSave():"); |
91 |
// then invoke all registered CustomPropertyHandlers to save all extended properties |
92 |
Project thisProjectMockup = null; |
93 |
for (J2SECustomPropertyHandler saver : Lookup.getDefault().lookupAll(J2SECustomPropertyHandler.class)) { |
94 |
saver.save(thisProjectMockup); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* Simulates reaction to closing the Project Properties dialog. |
100 |
*/ |
101 |
public void invokeCentralizedCleanup() { |
102 |
System.out.println("invokeCentralizedCleanup():"); |
103 |
// then invoke all registered CustomPropertyHandlers to clean whatever necessary |
104 |
Project thisProjectMockup = null; |
105 |
for (J2SECustomPropertyHandler cleaner : Lookup.getDefault().lookupAll(J2SECustomPropertyHandler.class)) { |
106 |
cleaner.cleanup(thisProjectMockup); |
107 |
} |
108 |
} |
109 |
|
110 |
/** |
111 |
* Test of save method, of class J2SECustomPropertyHandler. |
112 |
*/ |
113 |
@Test |
114 |
public void testCentralizedPropertiesSave() { |
115 |
MockupCategory1Panel panel1 = new MockupCategory1Panel(); |
116 |
MockupCategory2Panel panel2 = new MockupCategory2Panel(); |
117 |
System.out.println("testCentralizedPropertiesSave():"); |
118 |
assertEquals(storedProperties.size(), 0); |
119 |
invokeCentralizedSave(); |
120 |
assertEquals(storedProperties.size(), 2); // empty document is not saved |
121 |
assertEquals(storedProperties.get("mock.property1"), "false"); |
122 |
assertEquals(storedProperties.get("mock.property2"), "false"); |
123 |
assertNull(storedProperties.get("mock.property3")); |
124 |
System.out.println("OK"); |
125 |
// simulate user invoked property change in first Category panel followed by click on OK button |
126 |
panel1.simulateUserButtonAction(); |
127 |
invokeCentralizedSave(); |
128 |
assertEquals(storedProperties.size(), 2); // empty document is not saved |
129 |
assertEquals(storedProperties.get("mock.property1"), "true"); |
130 |
assertEquals(storedProperties.get("mock.property2"), "false"); |
131 |
assertNull(storedProperties.get("mock.property3")); |
132 |
System.out.println("OK"); |
133 |
// simulate user invoked property change in second Category panel followed by click on OK button |
134 |
panel2.simulateUserButtonAction(); |
135 |
invokeCentralizedSave(); |
136 |
assertEquals(storedProperties.size(), 2); // empty document is not saved |
137 |
assertEquals(storedProperties.get("mock.property1"), "true"); |
138 |
assertEquals(storedProperties.get("mock.property2"), "true"); |
139 |
assertNull(storedProperties.get("mock.property3")); |
140 |
System.out.println("OK"); |
141 |
// simulate user invoked property change in first and second Category panel followed by click on OK button |
142 |
panel1.simulateUserButtonAction(); |
143 |
panel2.simulateUserTextFieldAction("USER WROTE THIS"); |
144 |
invokeCentralizedSave(); |
145 |
assertEquals(storedProperties.size(), 3); |
146 |
assertEquals(storedProperties.get("mock.property1"), "false"); |
147 |
assertEquals(storedProperties.get("mock.property2"), "true"); |
148 |
assertEquals(storedProperties.get("mock.property3"), "USER WROTE THIS"); |
149 |
System.out.println("OK"); |
150 |
// simulate Cancel in properties dialog |
151 |
panel1.simulateUserButtonAction(); |
152 |
panel2.simulateUserButtonAction(); |
153 |
panel2.simulateUserTextFieldAction("USER WROTE SOMETHING ELSE"); |
154 |
invokeCentralizedCleanup(); |
155 |
assertNull(MockProjectExtenderProperties.extProperties); |
156 |
assertEquals(storedProperties.size(), 3); |
157 |
assertEquals(storedProperties.get("mock.property1"), "false"); |
158 |
assertEquals(storedProperties.get("mock.property2"), "true"); |
159 |
assertEquals(storedProperties.get("mock.property3"), "USER WROTE THIS"); |
160 |
System.out.println("OK"); |
161 |
} |
162 |
|
163 |
/** |
164 |
* Extended properties centralized property handler service. |
165 |
*/ |
166 |
public static final class MockProjectExtenderPropertiesHandler implements J2SECustomPropertyHandler { |
167 |
|
168 |
@Override |
169 |
public void save(Project p) { |
170 |
// in real p would be used to lookup the correct ProjectExtenderProperties instance |
171 |
MockProjectExtenderProperties prop = MockProjectExtenderProperties.getInstanceIfExists(); |
172 |
if(prop != null) { |
173 |
try { |
174 |
prop.store(); |
175 |
} catch (IOException ex) { |
176 |
Exceptions.printStackTrace(ex); |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
@Override |
182 |
public void cleanup(Project p) { |
183 |
// in real p would be used to lookup the correct ProjectExtenderProperties instance |
184 |
MockProjectExtenderProperties prop = MockProjectExtenderProperties.getInstanceIfExists(); |
185 |
if(prop != null) { |
186 |
prop.cleanup(); |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
/** |
192 |
* Class managing properties specific to the project extension module. |
193 |
*/ |
194 |
public static final class MockProjectExtenderProperties { |
195 |
|
196 |
private static MockProjectExtenderProperties extProperties = null; |
197 |
|
198 |
public static final String PROPERTY1 = "mock.property1"; |
199 |
public static final String PROPERTY2 = "mock.property2"; |
200 |
public static final String PROPERTY3 = "mock.property3"; |
201 |
private JToggleButton.ToggleButtonModel model1; |
202 |
private JToggleButton.ToggleButtonModel model2; |
203 |
private Document model3; |
204 |
private StoreGroup mockPropGroup = new StoreGroup(); |
205 |
|
206 |
public JToggleButton.ToggleButtonModel getModel1() { |
207 |
return model1; |
208 |
} |
209 |
public JToggleButton.ToggleButtonModel getModel2() { |
210 |
return model2; |
211 |
} |
212 |
public Document getModel3() { |
213 |
return model3; |
214 |
} |
215 |
|
216 |
MockProjectExtenderProperties() { |
217 |
EditableProperties mockCentralProperties = new EditableProperties(true); |
218 |
MockPropertyEvaluator pe = new MockPropertyEvaluator(mockCentralProperties); |
219 |
model1 = mockPropGroup.createToggleButtonModel(pe, PROPERTY1); |
220 |
model2 = mockPropGroup.createToggleButtonModel(pe, PROPERTY2); |
221 |
model3 = mockPropGroup.createStringDocument(pe, PROPERTY3); |
222 |
} |
223 |
|
224 |
public static MockProjectExtenderProperties getInstanceIfExists() { |
225 |
if(extProperties != null) { |
226 |
return extProperties; |
227 |
} |
228 |
return null; |
229 |
} |
230 |
|
231 |
public static MockProjectExtenderProperties getInstance() { |
232 |
if(extProperties == null) { |
233 |
extProperties = new MockProjectExtenderProperties(); |
234 |
} |
235 |
return extProperties; |
236 |
} |
237 |
|
238 |
/** |
239 |
* Method implementing actual saving of extended properties. |
240 |
*/ |
241 |
public void store() throws IOException { |
242 |
try { |
243 |
mockPropGroup.store(storedProperties); |
244 |
// in reality cache or save to *.properties file |
245 |
if(false) { |
246 |
throw new IOException(); |
247 |
} |
248 |
|
249 |
} catch (IOException ioex) {} |
250 |
} |
251 |
|
252 |
/** |
253 |
* Clean up temporary memory structures. |
254 |
*/ |
255 |
public void cleanup() { |
256 |
extProperties = null; |
257 |
} |
258 |
} |
259 |
|
260 |
public static final class MockupCategory1Panel extends JPanel { |
261 |
JToggleButton button = new JToggleButton(); |
262 |
MockupCategory1Panel() { |
263 |
button.setModel(extProperties.getModel1()); |
264 |
} |
265 |
public void simulateUserButtonAction() { |
266 |
button.doClick(); |
267 |
} |
268 |
} |
269 |
|
270 |
public static final class MockupCategory2Panel extends JPanel { |
271 |
JToggleButton button = new JToggleButton(); |
272 |
JTextField textField = new JTextField(); |
273 |
MockupCategory2Panel() { |
274 |
button.setModel(extProperties.getModel2()); |
275 |
textField.setDocument(extProperties.getModel3()); |
276 |
} |
277 |
public void simulateUserButtonAction() { |
278 |
button.doClick(); |
279 |
} |
280 |
public void simulateUserTextFieldAction(String str) { |
281 |
textField.setText(str); |
282 |
} |
283 |
} |
284 |
|
285 |
public static final class MockPropertyEvaluator implements PropertyEvaluator { |
286 |
|
287 |
private final EditableProperties ep; |
288 |
|
289 |
MockPropertyEvaluator(EditableProperties ep) { |
290 |
this.ep = ep; |
291 |
} |
292 |
|
293 |
@Override |
294 |
public String getProperty(String prop) { |
295 |
return ep.getProperty(prop); |
296 |
} |
297 |
|
298 |
@Override |
299 |
public String evaluate(String text) { |
300 |
return null; |
301 |
} |
302 |
|
303 |
@Override |
304 |
public Map<String, String> getProperties() { |
305 |
return null; |
306 |
} |
307 |
|
308 |
@Override |
309 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
310 |
} |
311 |
|
312 |
@Override |
313 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
314 |
} |
315 |
|
316 |
} |
317 |
|
318 |
} |