1 19 20 package org.netbeans.spi.project.support.ant.ui; 21 22 import java.beans.PropertyChangeListener ; 23 import java.util.Map ; 24 import javax.swing.JTextField ; 25 import javax.swing.JToggleButton ; 26 import javax.swing.text.Document ; 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 name) { 34 super(name); 35 } 36 37 public void testString() throws Exception { 38 39 String PROP_NAME = "testText"; 41 String ORIGINAL_VALUE = "originalValue"; 42 String NEW_VALUE = "newValue"; 43 44 EditableProperties ep = new EditableProperties(); 46 PropertyEvaluator evaluator = new PlainPropertyEvaluator( ep ); 47 StoreGroup sg = new StoreGroup(); 48 49 ep.setProperty( PROP_NAME, ORIGINAL_VALUE ); 51 Document doc = sg.createStringDocument( evaluator, PROP_NAME ); 52 JTextField jtf = new JTextField (); 53 jtf.setDocument( doc ); 54 assertEquals( "JTextField has to have correct value", ORIGINAL_VALUE, jtf.getText() ); 55 56 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 { 64 65 67 String [][] TEST_PROPERTIES = new String [][] { 68 new String [] { "propTrue", "true", "false", "true" }, 69 new String [] { "propFalse", "false", "true", "false" }, 70 new String [] { "propYes", "yes", "no", "true" }, 71 new String [] { "propNo", "no", "yes", "false"}, 72 new String [] { "propOn", "on", "off", "true" }, 73 new String [] { "propOff", "off", "on", "false" }, 74 }; 75 76 JToggleButton.ToggleButtonModel [] models = new JToggleButton.ToggleButtonModel [ TEST_PROPERTIES.length ]; 77 JToggleButton.ToggleButtonModel [] inverseModels = new JToggleButton.ToggleButtonModel [ TEST_PROPERTIES.length ]; 78 JToggleButton buttons[] = new JToggleButton [ TEST_PROPERTIES.length ]; 79 JToggleButton inverseButtons[] = new JToggleButton [ TEST_PROPERTIES.length ]; 80 81 EditableProperties ep = new EditableProperties(); 83 PropertyEvaluator evaluator = new PlainPropertyEvaluator( ep ); 84 StoreGroup sg = new StoreGroup(); 85 StoreGroup inverseSg = new StoreGroup(); 86 87 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 (); 95 buttons[i].setModel( models[i] ); 96 inverseButtons[i] = new JToggleButton (); 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 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 { 121 122 String PROP_BOOLEAN = "boolean"; 124 String PROP_STRING = "string"; 125 126 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 e = null; 138 139 try { 140 sg.createToggleButtonModel( evaluator, PROP_BOOLEAN ); 141 } 142 catch ( IllegalArgumentException 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 iea ) { 152 e = iea; 153 } 154 assertNotNull( "Exception has to be throen", e ); 155 156 157 } 158 159 162 public void testIssue57797 () throws Exception { 163 String PROP_NAME_A = "propertyA"; 164 String PROP_NAME_B = "propertyB"; 165 String ORIGINAL_A_VALUE = "original_A_Value"; 166 String ORIGINAL_B_VALUE = "original_B_Value"; 167 String 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 doc1 = sg.createStringDocument( evaluator, PROP_NAME_A ); 176 Document doc2 = sg.createStringDocument( evaluator, PROP_NAME_B ); 177 JTextField jtf1 = new JTextField (); 178 jtf1.setDocument ( doc1 ); 179 JTextField jtf2 = new JTextField (); 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 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 getProperty(String prop) { 202 return properties.getProperty( prop ); 203 } 204 205 public String evaluate(String text) { 206 return text; 207 } 208 209 public void removePropertyChangeListener(PropertyChangeListener listener) { 210 } 212 213 public void addPropertyChangeListener(PropertyChangeListener listener) { 214 } 216 217 public Map <String ,String > getProperties() { 218 return properties; 219 } 220 221 } 222 223 224 } 225 | Popular Tags |