KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > ui > StoreGroupTest


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.netbeans.spi.project.support.ant.ui;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.swing.JTextField JavaDoc;
25 import javax.swing.JToggleButton JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.spi.project.support.ant.EditableProperties;
29 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
30
31 public class StoreGroupTest extends NbTestCase {
32
33     public StoreGroupTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public void testString() throws Exception JavaDoc {
38
39         // Test values
40
String JavaDoc PROP_NAME = "testText";
41         String JavaDoc ORIGINAL_VALUE = "originalValue";
42         String JavaDoc NEW_VALUE = "newValue";
43         
44         // Needed objects
45
EditableProperties ep = new EditableProperties();
46         PropertyEvaluator evaluator = new PlainPropertyEvaluator( ep );
47         StoreGroup sg = new StoreGroup();
48
49         // Test correct value of the model
50
ep.setProperty( PROP_NAME, ORIGINAL_VALUE );
51         Document JavaDoc doc = sg.createStringDocument( evaluator, PROP_NAME );
52         JTextField JavaDoc jtf = new JTextField JavaDoc();
53         jtf.setDocument( doc );
54         assertEquals( "JTextField has to have correct value", ORIGINAL_VALUE, jtf.getText() );
55         
56         // Test value is stored
57
jtf.setText( NEW_VALUE );
58         sg.store( ep );
59         assertEquals( "Value has to be set into the properties", NEW_VALUE, ep.getProperty( PROP_NAME ) );
60         
61     }
62     
63     public void testBooleans() throws Exception JavaDoc {
64         
65         // Test values
66

67         String JavaDoc[][] TEST_PROPERTIES = new String JavaDoc[][] {
68             new String JavaDoc[] { "propTrue", "true", "false", "true" },
69             new String JavaDoc[] { "propFalse", "false", "true", "false" },
70             new String JavaDoc[] { "propYes", "yes", "no", "true" },
71             new String JavaDoc[] { "propNo", "no", "yes", "false"},
72             new String JavaDoc[] { "propOn", "on", "off", "true" },
73             new String JavaDoc[] { "propOff", "off", "on", "false" },
74         };
75         
76         JToggleButton.ToggleButtonModel JavaDoc[] models = new JToggleButton.ToggleButtonModel JavaDoc[ TEST_PROPERTIES.length ];
77         JToggleButton.ToggleButtonModel JavaDoc[] inverseModels = new JToggleButton.ToggleButtonModel JavaDoc[ TEST_PROPERTIES.length ];
78         JToggleButton JavaDoc buttons[] = new JToggleButton JavaDoc[ TEST_PROPERTIES.length ];
79         JToggleButton JavaDoc inverseButtons[] = new JToggleButton JavaDoc[ TEST_PROPERTIES.length ];
80         
81         // Needed objects
82
EditableProperties ep = new EditableProperties();
83         PropertyEvaluator evaluator = new PlainPropertyEvaluator( ep );
84         StoreGroup sg = new StoreGroup();
85         StoreGroup inverseSg = new StoreGroup();
86         
87         // Test correct value of the model
88
for( int i = 0; i < TEST_PROPERTIES.length; i++ ) {
89             ep.setProperty( TEST_PROPERTIES[i][0], TEST_PROPERTIES[i][1] );
90         }
91         for( int i = 0; i < TEST_PROPERTIES.length; i++ ) {
92             models[i] = sg.createToggleButtonModel( evaluator, TEST_PROPERTIES[i][0] );
93             inverseModels[i] = inverseSg.createInverseToggleButtonModel( evaluator, TEST_PROPERTIES[i][0] );
94             buttons[i] = new JToggleButton JavaDoc();
95             buttons[i].setModel( models[i] );
96             inverseButtons[i] = new JToggleButton JavaDoc();
97             inverseButtons[i].setModel( inverseModels[i] );
98         }
99         for( int i = 0; i < TEST_PROPERTIES.length; i++ ) {
100             assertEquals( "Button [" + i + "] has to have correct value.", Boolean.valueOf( TEST_PROPERTIES[i][3] ).booleanValue(), buttons[i].isSelected() );
101             assertEquals( "InverseButton [" + i + "] has to have correct value.", !Boolean.valueOf( TEST_PROPERTIES[i][3] ).booleanValue(), inverseButtons[i].isSelected() );
102         }
103         
104         // Change value of all the buttons and test the correct property values
105
for( int i = 0; i < TEST_PROPERTIES.length; i++ ) {
106             buttons[i].setSelected( !buttons[i].isSelected() );
107             inverseButtons[i].setSelected( !inverseButtons[i].isSelected() );
108         }
109         
110         EditableProperties inverseEp = new EditableProperties();
111         sg.store( ep );
112         inverseSg.store( inverseEp );
113         for( int i = 0; i < TEST_PROPERTIES.length; i++ ) {
114             assertEquals( "Property [" + i + "] has to have correct value.", TEST_PROPERTIES[i][2], ep.getProperty( TEST_PROPERTIES[i][0] ) );
115             assertEquals( "Property [" + i + "] has to have correct value.", TEST_PROPERTIES[i][2], inverseEp.getProperty( TEST_PROPERTIES[i][0] ) );
116         }
117         
118     }
119     
120     public void testDuplicateModels() throws Exception JavaDoc {
121         
122         // Test values
123
String JavaDoc PROP_BOOLEAN = "boolean";
124         String JavaDoc PROP_STRING = "string";
125         
126         // Needed objects
127
EditableProperties ep = new EditableProperties();
128         PropertyEvaluator evaluator = new PlainPropertyEvaluator( ep );
129         StoreGroup sg = new StoreGroup();
130
131         ep.setProperty( PROP_BOOLEAN, "true" );
132         ep.setProperty( PROP_STRING, "text" );
133         
134         sg.createToggleButtonModel( evaluator, PROP_BOOLEAN );
135         sg.createStringDocument( evaluator, PROP_STRING );
136         
137         IllegalArgumentException JavaDoc e = null;
138         
139         try {
140             sg.createToggleButtonModel( evaluator, PROP_BOOLEAN );
141         }
142         catch ( IllegalArgumentException JavaDoc iea ) {
143             e = iea;
144         }
145         assertNotNull( "Exception has to be throen", e);
146         
147         e = null;
148         try {
149             sg.createStringDocument( evaluator, PROP_STRING );
150         }
151         catch ( IllegalArgumentException JavaDoc iea ) {
152             e = iea;
153         }
154         assertNotNull( "Exception has to be throen", e );
155         
156         
157     }
158     
159     /**
160      *#57797:dist.jar changed to hardcode 'dist' rather than '${dist.dir}'
161      */

162     public void testIssue57797 () throws Exception JavaDoc {
163         String JavaDoc PROP_NAME_A = "propertyA";
164         String JavaDoc PROP_NAME_B = "propertyB";
165         String JavaDoc ORIGINAL_A_VALUE = "original_A_Value";
166         String JavaDoc ORIGINAL_B_VALUE = "original_B_Value";
167         String JavaDoc NEW_A_VALUE = "new_A_Value";
168         
169         EditableProperties ep = new EditableProperties();
170         PropertyEvaluator evaluator = new PlainPropertyEvaluator( ep );
171         StoreGroup sg = new StoreGroup();
172
173         ep.setProperty( PROP_NAME_A, ORIGINAL_A_VALUE );
174         ep.setProperty( PROP_NAME_B, ORIGINAL_B_VALUE );
175         Document JavaDoc doc1 = sg.createStringDocument( evaluator, PROP_NAME_A );
176         Document JavaDoc doc2 = sg.createStringDocument( evaluator, PROP_NAME_B );
177         JTextField JavaDoc jtf1 = new JTextField JavaDoc ();
178         jtf1.setDocument ( doc1 );
179         JTextField JavaDoc jtf2 = new JTextField JavaDoc ();
180         jtf2.setDocument ( doc2 );
181         jtf1.setText( NEW_A_VALUE );
182         EditableProperties newEp = new EditableProperties ();
183         sg.store( newEp );
184         assertEquals( "Expected one new propery", 1, newEp.size());
185         assertEquals( "Value has to be set into the properties", NEW_A_VALUE, newEp.getProperty( PROP_NAME_A ) );
186     }
187     
188     
189     // Innerclasses ------------------------------------------------------------
190

191     
192     private static class PlainPropertyEvaluator implements PropertyEvaluator {
193         
194         private EditableProperties properties;
195         
196         PlainPropertyEvaluator( EditableProperties properties ) {
197             this.properties = properties;
198         }
199         
200         
201         public String JavaDoc getProperty(String JavaDoc prop) {
202             return properties.getProperty( prop );
203         }
204
205         public String JavaDoc evaluate(String JavaDoc text) {
206             return text;
207         }
208
209         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
210             // NOP
211
}
212
213         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
214             // NOP
215
}
216
217         public Map JavaDoc<String JavaDoc,String JavaDoc> getProperties() {
218             return properties;
219         }
220         
221     }
222     
223     
224 }
225
Popular Tags