This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 200691
Collapse All | Expand All

(-)d4dbd210ef90 (+277 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 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.J2SECustomPropertySaver
64
 * 
65
 * @author Petr Somol
66
 */
67
public class J2SECustomPropertySaverTest {
68
    
69
    public J2SECustomPropertySaverTest() {
70
    }
71
72
    @BeforeClass
73
    public static void setUpClass() throws Exception {
74
        MockServices.setServices(MockProjectExtenderPropertiesSaver.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 CustomPropertySavers to save all extended properties
92
        Project thisProjectMockup = null;
93
        for (J2SECustomPropertySaver saver : Lookup.getDefault().lookupAll(J2SECustomPropertySaver.class)) {
94
            saver.save(thisProjectMockup);
95
        }
96
    }
97
    
98
    /**
99
     * Test of save method, of class J2SECustomPropertySaver.
100
     */
101
    @Test
102
    public void testCentralizedPropertiesSave() {
103
        MockupCategory1Panel panel1 = new MockupCategory1Panel();
104
        MockupCategory2Panel panel2 = new MockupCategory2Panel();
105
        System.out.println("testCentralizedPropertiesSave():");
106
        assertEquals(storedProperties.size(), 0);
107
        invokeCentralizedSave();
108
        assertEquals(storedProperties.size(), 2); // empty document is not saved
109
        assertEquals(storedProperties.get("mock.property1"), "false"); 
110
        assertEquals(storedProperties.get("mock.property2"), "false"); 
111
        assertNull(storedProperties.get("mock.property3")); 
112
        System.out.println("OK"); 
113
        // simulate user invoked property change in first Category panel followed by click on OK button
114
        panel1.simulateUserButtonAction();
115
        invokeCentralizedSave();
116
        assertEquals(storedProperties.size(), 2); // empty document is not saved
117
        assertEquals(storedProperties.get("mock.property1"), "true"); 
118
        assertEquals(storedProperties.get("mock.property2"), "false"); 
119
        assertNull(storedProperties.get("mock.property3")); 
120
        System.out.println("OK"); 
121
        // simulate user invoked property change in second Category panel followed by click on OK button
122
        panel2.simulateUserButtonAction();
123
        invokeCentralizedSave();
124
        assertEquals(storedProperties.size(), 2); // empty document is not saved
125
        assertEquals(storedProperties.get("mock.property1"), "true"); 
126
        assertEquals(storedProperties.get("mock.property2"), "true"); 
127
        assertNull(storedProperties.get("mock.property3")); 
128
        System.out.println("OK"); 
129
        // simulate user invoked property change in first and second Category panel followed by click on OK button
130
        panel1.simulateUserButtonAction();
131
        panel2.simulateUserTextFieldAction("USER WROTE THIS"); 
132
        invokeCentralizedSave();
133
        assertEquals(storedProperties.size(), 3);
134
        assertEquals(storedProperties.get("mock.property1"), "false"); 
135
        assertEquals(storedProperties.get("mock.property2"), "true"); 
136
        assertEquals(storedProperties.get("mock.property3"), "USER WROTE THIS"); 
137
        System.out.println("OK"); 
138
    }
139
140
    /**
141
     * Extended properties centralized saver service.
142
     */
143
    public static final class MockProjectExtenderPropertiesSaver implements J2SECustomPropertySaver {
144
145
        @Override
146
        public void save(Project p) {
147
            // in real p would be used to lookup the correct ProjectExtenderProperties instance
148
            MockProjectExtenderProperties prop = MockProjectExtenderProperties.getInstanceIfExists();
149
            if(prop != null) {
150
                try {
151
                    prop.store();
152
                } catch (IOException ex) {
153
                    Exceptions.printStackTrace(ex);
154
                }
155
            }
156
        }
157
    }
158
    
159
    /**
160
     * Class managing properties specific to the project extension module.
161
     */
162
    public static final class MockProjectExtenderProperties {
163
164
        private static MockProjectExtenderProperties extProperties = null;
165
166
        public static final String PROPERTY1 = "mock.property1"; 
167
        public static final String PROPERTY2 = "mock.property2"; 
168
        public static final String PROPERTY3 = "mock.property3"; 
169
        private JToggleButton.ToggleButtonModel model1;
170
        private JToggleButton.ToggleButtonModel model2;
171
        private Document model3;
172
        private StoreGroup mockPropGroup = new StoreGroup();
173
174
        public JToggleButton.ToggleButtonModel getModel1() {
175
            return model1;
176
        }
177
        public JToggleButton.ToggleButtonModel getModel2() {
178
            return model2;
179
        }
180
        public Document getModel3() {
181
            return model3;
182
        }
183
184
        MockProjectExtenderProperties() {
185
            EditableProperties mockCentralProperties = new EditableProperties(true);
186
            MockPropertyEvaluator pe = new MockPropertyEvaluator(mockCentralProperties);
187
            model1 = mockPropGroup.createToggleButtonModel(pe, PROPERTY1);
188
            model2 = mockPropGroup.createToggleButtonModel(pe, PROPERTY2);
189
            model3 = mockPropGroup.createStringDocument(pe, PROPERTY3);
190
        }
191
        
192
        public static MockProjectExtenderProperties getInstanceIfExists() {
193
            if(extProperties != null) {
194
                return extProperties;
195
            }
196
            return null;
197
        }
198
        
199
        public static MockProjectExtenderProperties getInstance() {
200
            if(extProperties == null) {
201
                extProperties = new MockProjectExtenderProperties();
202
            }
203
            return extProperties;
204
        }
205
206
        /**
207
         * Method implementing actual saving of extended properties.
208
         */
209
        public void store() throws IOException {
210
            try {
211
                mockPropGroup.store(storedProperties);
212
                // in reality cache or save to *.properties file
213
                if(false) throw new IOException();
214
                
215
            } catch (IOException ioex) {}
216
        }   
217
    }
218
    
219
    public static final class MockupCategory1Panel extends JPanel {
220
        JToggleButton button = new JToggleButton();
221
        MockupCategory1Panel() {
222
            button.setModel(extProperties.getModel1());
223
        }
224
        public void simulateUserButtonAction() {
225
            button.doClick();
226
        }
227
    }
228
229
    public static final class MockupCategory2Panel extends JPanel {
230
        JToggleButton button = new JToggleButton();
231
        JTextField textField  = new JTextField();
232
        MockupCategory2Panel() {
233
            button.setModel(extProperties.getModel2());
234
            textField.setDocument(extProperties.getModel3());
235
        }
236
        public void simulateUserButtonAction() {
237
            button.doClick();
238
        }
239
        public void simulateUserTextFieldAction(String str) {
240
            textField.setText(str);
241
        }
242
    }
243
244
    public static final class MockPropertyEvaluator implements PropertyEvaluator {
245
246
        private final EditableProperties ep;
247
                
248
        MockPropertyEvaluator(EditableProperties ep) {
249
            this.ep = ep;
250
        }
251
        
252
        @Override
253
        public String getProperty(String prop) {
254
            return ep.getProperty(prop);
255
        }
256
257
        @Override
258
        public String evaluate(String text) {
259
            return null;
260
        }
261
262
        @Override
263
        public Map<String, String> getProperties() {
264
            return null;
265
        }
266
267
        @Override
268
        public void addPropertyChangeListener(PropertyChangeListener listener) {
269
        }
270
271
        @Override
272
        public void removePropertyChangeListener(PropertyChangeListener listener) {
273
        }
274
        
275
    }
276
    
277
}

Return to bug 200691