001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 16.12.2004
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.image.BufferedImage;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.io.ObjectInputStream;
032: import java.io.ObjectOutputStream;
033: import java.lang.reflect.InvocationTargetException;
034:
035: public class AbstractActionTest extends SwingTestCase {
036: protected boolean find(final Object[] array, final Object value) {
037: boolean found = false;
038: if (array != null) {
039: for (int i = 0; i < array.length; i++) {
040: if (array[i].equals(value)) {
041: found = true;
042: break;
043: }
044: }
045: }
046: return found;
047: }
048:
049: protected AbstractAction action;
050:
051: public static class ConcreteSerializableListener implements
052: PropertyChangeListener {
053: public ConcreteSerializableListener() {
054: }
055:
056: public ConcreteSerializableListener(final String name) {
057: }
058:
059: private void writeObject(final ObjectOutputStream outStream)
060: throws IOException {
061: outStream.defaultWriteObject();
062: }
063:
064: private void readObject(final ObjectInputStream inStream)
065: throws IOException, ClassNotFoundException {
066: inStream.defaultReadObject();
067: }
068:
069: public void propertyChange(PropertyChangeEvent event) {
070: }
071: };
072:
073: /*
074: * @see TestCase#setUp()
075: */
076: @Override
077: protected void setUp() throws Exception {
078: super .setUp();
079: action = new AbstractAction() {
080: private static final long serialVersionUID = 1L;
081:
082: public void actionPerformed(final ActionEvent event) {
083: }
084:
085: private void writeObject(final ObjectOutputStream outStream)
086: throws IOException {
087: }
088:
089: private void readObject(final ObjectInputStream inStream)
090: throws IOException, ClassNotFoundException {
091: }
092:
093: @Override
094: public Object clone() throws CloneNotSupportedException {
095: return super .clone();
096: }
097: };
098: }
099:
100: /*
101: * Class under test for void AbstractAction(String, Icon)
102: */
103: public void testAbstractActionStringIcon() {
104: Icon icon = new ImageIcon(new BufferedImage(20, 20,
105: BufferedImage.TYPE_BYTE_GRAY));
106: action = new AbstractAction("ActionName", icon) {
107: private static final long serialVersionUID = 1L;
108:
109: public void actionPerformed(final ActionEvent event) {
110: }
111: };
112: assertEquals("icon initialized properly", icon, action
113: .getValue(Action.SMALL_ICON));
114: assertEquals("name initialized properly", "ActionName", action
115: .getValue(Action.NAME));
116: assertTrue("enabled property init state is true", action
117: .isEnabled());
118: }
119:
120: /*
121: * Class under test for void AbstractAction(String)
122: */
123: public void testAbstractActionString() {
124: action = new AbstractAction("ActionName") {
125: private static final long serialVersionUID = 1L;
126:
127: public void actionPerformed(final ActionEvent event) {
128: }
129: };
130: assertNull("icon initialized properly", action
131: .getValue(Action.SMALL_ICON));
132: assertEquals("name initialized properly", "ActionName", action
133: .getValue(Action.NAME));
134: assertTrue("enabled property init state is true", action
135: .isEnabled());
136: }
137:
138: /*
139: * Class under test for void AbstractAction()
140: */
141: public void testAbstractAction() {
142: action = new AbstractAction() {
143: private static final long serialVersionUID = 1L;
144:
145: public void actionPerformed(final ActionEvent event) {
146: }
147: };
148: assertNull("icon initialized properly", action
149: .getValue(Action.SMALL_ICON));
150: assertNull("name initialized properly", action
151: .getValue(Action.NAME));
152: assertTrue("enabled property init state is true", action
153: .isEnabled());
154: }
155:
156: public void testPutValue() {
157: String name1 = "name1";
158: String name2 = "name2";
159: String value1 = "value1";
160: String value2 = "value2";
161: PropertyChangeController changeListener = new PropertyChangeController();
162: assertNull("value is not stored initially", action
163: .getValue(name1));
164: assertNull("value is not stored initially", action
165: .getValue(name2));
166: action.addPropertyChangeListener(changeListener);
167: action.putValue(name1, value1);
168: assertEquals("value is stored properly", value1, action
169: .getValue(name1));
170: changeListener.checkLastPropertyFired(action, name1, null,
171: value1);
172: changeListener.reset();
173: action.putValue(name2, value2);
174: assertEquals("value is stored properly", value1, action
175: .getValue(name1));
176: assertEquals("value is stored properly", value2, action
177: .getValue(name2));
178: changeListener.checkLastPropertyFired(action, name2, null,
179: value2);
180: changeListener.reset();
181: action.putValue(name1, null);
182: assertNull("value is stored properly", action.getValue(name1));
183: assertEquals("value is stored properly", value2, action
184: .getValue(name2));
185: changeListener.checkLastPropertyFired(action, name1, value1,
186: null);
187: }
188:
189: public void testGetValue() {
190: String name1 = "name1";
191: String name2 = "name2";
192: String value1 = "value1";
193: String value2 = "value2";
194: assertNull("value is not stored initially", action
195: .getValue(name1));
196: assertNull("value is not stored initially", action
197: .getValue(name2));
198: action.putValue(name1, value1);
199: assertEquals("value is stored properly", value1, action
200: .getValue(name1));
201: action.putValue(name2, value2);
202: assertEquals("value is stored properly", value1, action
203: .getValue(name1));
204: assertEquals("value is stored properly", value2, action
205: .getValue(name2));
206: action.putValue(name1, null);
207: assertNull("value is stored properly", action.getValue(name1));
208: assertEquals("value is stored properly", value2, action
209: .getValue(name2));
210: }
211:
212: public void testGetKeys() {
213: String name1 = "name1";
214: String name2 = "name2";
215: String name3 = "name3";
216: String name4 = "name4";
217: String value1 = "value1";
218: String value2 = "value2";
219: String value3 = "value3";
220: String value4 = "value4";
221: Object[] keys = action.getKeys();
222: assertTrue(
223: "with default constructor the initial number of keys is 0",
224: keys == null || keys.length == 0);
225: action.putValue(name1, value1);
226: action.putValue(name2, value2);
227: action.putValue(name3, value3);
228: action.putValue(name4, value4);
229: keys = action.getKeys();
230: assertTrue("the number of keys is correct", keys != null
231: && keys.length == 4);
232: assertTrue(find(keys, name1));
233: assertTrue(find(keys, name2));
234: assertTrue(find(keys, name3));
235: assertTrue(find(keys, name4));
236: }
237:
238: /*
239: * Class under test for Object clone()
240: */
241: public void testClone() throws IllegalArgumentException,
242: SecurityException, IllegalAccessException,
243: InvocationTargetException, NoSuchMethodException {
244: AbstractAction actionClone = null;
245: String name1 = "name1";
246: String name2 = "name2";
247: String value1 = "value1";
248: String value2 = "value2";
249: PropertyChangeController changeListener = new PropertyChangeController();
250: action.addPropertyChangeListener(changeListener);
251: action.putValue(name1, value1);
252: action.putValue(name2, value2);
253: actionClone = (AbstractAction) action.getClass().getMethod(
254: "clone").invoke(action);
255: assertEquals("values in table coincide ", action
256: .getValue(name1), actionClone.getValue(name1));
257: assertEquals("values in table coincide ", action
258: .getValue(name2), actionClone.getValue(name2));
259: assertEquals("listeners coincide ", action
260: .getPropertyChangeListeners().length, actionClone
261: .getPropertyChangeListeners().length);
262: assertEquals("listeners coincide ", action
263: .getPropertyChangeListeners()[0], actionClone
264: .getPropertyChangeListeners()[0]);
265: assertTrue("listeners coincide ", action
266: .getPropertyChangeListeners()[0] == actionClone
267: .getPropertyChangeListeners()[0]);
268: }
269:
270: public void testRemovePropertyChangeListener() {
271: PropertyChangeController changeListener1 = new PropertyChangeController();
272: PropertyChangeController changeListener2 = new PropertyChangeController();
273: PropertyChangeController changeListener3 = new PropertyChangeController();
274: PropertyChangeController changeListener4 = new PropertyChangeController();
275: PropertyChangeListener[] listenersArray = null;
276: action.addPropertyChangeListener(changeListener1);
277: action.addPropertyChangeListener(changeListener2);
278: listenersArray = action.getPropertyChangeListeners();
279: assertTrue("listeners added successfully",
280: listenersArray.length == 2);
281: action.removePropertyChangeListener(changeListener1);
282: listenersArray = action.getPropertyChangeListeners();
283: assertTrue("listener is removed successfully",
284: listenersArray.length == 1);
285: assertEquals("it was the right listener that was removed",
286: listenersArray[0], changeListener2);
287: action.removePropertyChangeListener(changeListener2);
288: listenersArray = action.getPropertyChangeListeners();
289: assertTrue("listener is removed successfully",
290: listenersArray.length == 0);
291: action.addPropertyChangeListener(changeListener1);
292: action.addPropertyChangeListener(changeListener2);
293: action.addPropertyChangeListener(changeListener3);
294: action.addPropertyChangeListener(changeListener4);
295: listenersArray = action.getPropertyChangeListeners();
296: assertTrue("listeners added successfully",
297: listenersArray.length == 4);
298: action.removePropertyChangeListener(changeListener3);
299: listenersArray = action.getPropertyChangeListeners();
300: assertTrue("listener is removed successfully",
301: listenersArray.length == 3);
302: assertTrue("it was the right listener that was removed",
303: changeListener3.findMe(listenersArray) == 0);
304: }
305:
306: public void testAddPropertyChangeListener() {
307: PropertyChangeController changeListener1 = new PropertyChangeController();
308: PropertyChangeController changeListener2 = new PropertyChangeController();
309: PropertyChangeListener[] listenersArray = null;
310: action.addPropertyChangeListener(changeListener1);
311: listenersArray = action.getPropertyChangeListeners();
312: assertTrue(listenersArray.length == 1);
313: assertTrue(changeListener1.findMe(listenersArray) > 0);
314: action.addPropertyChangeListener(changeListener2);
315: listenersArray = action.getPropertyChangeListeners();
316: assertTrue(listenersArray.length == 2);
317: assertTrue(changeListener1.findMe(listenersArray) > 0);
318: assertTrue(changeListener2.findMe(listenersArray) > 0);
319: action.addPropertyChangeListener(changeListener2);
320: listenersArray = action.getPropertyChangeListeners();
321: assertTrue(listenersArray.length == 3);
322: }
323:
324: public void testGetPropertyChangeListeners() {
325: PropertyChangeController changeListener1 = new PropertyChangeController();
326: PropertyChangeController changeListener2 = new PropertyChangeController();
327: PropertyChangeListener[] listenersArray = null;
328: listenersArray = action.getPropertyChangeListeners();
329: assertTrue(listenersArray.length == 0);
330: action.addPropertyChangeListener(changeListener1);
331: listenersArray = action.getPropertyChangeListeners();
332: assertTrue(listenersArray.length == 1);
333: assertTrue(changeListener1.findMe(listenersArray) == 1);
334: action.addPropertyChangeListener(changeListener2);
335: listenersArray = action.getPropertyChangeListeners();
336: assertTrue(listenersArray.length == 2);
337: assertTrue(changeListener1.findMe(listenersArray) == 1);
338: assertTrue(changeListener2.findMe(listenersArray) == 1);
339: }
340:
341: /*
342: * this method is being tested by testSetEnabled()
343: */
344: public void testFirePropertyChange() {
345: }
346:
347: public void testSetEnabled() {
348: class PropertyChangeListenerFalse implements
349: PropertyChangeListener {
350: public boolean isChanged = false;
351:
352: public void propertyChange(final PropertyChangeEvent evt) {
353: if ("enabled".equals(evt.getPropertyName())
354: && (evt.getNewValue() != null)) {
355: assertTrue(evt.getNewValue().equals(Boolean.FALSE));
356: assertTrue(evt.getOldValue().equals(Boolean.TRUE));
357: isChanged = true;
358: }
359: }
360: }
361: ;
362: class PropertyChangeListenerTrue implements
363: PropertyChangeListener {
364: public boolean isChanged = false;
365:
366: public void propertyChange(final PropertyChangeEvent evt) {
367: if ("enabled".equals(evt.getPropertyName())
368: && (evt.getNewValue() != null)) {
369: assertTrue(evt.getNewValue().equals(Boolean.TRUE));
370: assertTrue(evt.getOldValue().equals(Boolean.FALSE));
371: isChanged = true;
372: }
373: }
374: }
375: ;
376: PropertyChangeListenerFalse falseListener = new PropertyChangeListenerFalse();
377: PropertyChangeListenerTrue trueListener = new PropertyChangeListenerTrue();
378: action.addPropertyChangeListener(trueListener);
379: action.setEnabled(true);
380: assertFalse("state changing event's not fired",
381: trueListener.isChanged);
382: assertTrue("state's not changed", action.isEnabled());
383: action.removePropertyChangeListener(trueListener);
384: action.addPropertyChangeListener(falseListener);
385: action.setEnabled(false);
386: assertTrue("state changing event's fired",
387: falseListener.isChanged);
388: assertFalse("state's changed", action.isEnabled());
389: action.removePropertyChangeListener(falseListener);
390: action.addPropertyChangeListener(trueListener);
391: action.setEnabled(true);
392: assertTrue("state changing event's fired",
393: trueListener.isChanged);
394: assertTrue("state's changed", action.isEnabled());
395: }
396:
397: public void testIsEnabled() {
398: assertTrue("action is enabled initially", action.isEnabled());
399: action.setEnabled(false);
400: assertFalse("action is disabled", action.isEnabled());
401: action.setEnabled(true);
402: assertTrue("action is enabled", action.isEnabled());
403: }
404:
405: public void testWriteObject() throws Exception {
406: String name1 = "name1";
407: String name2 = "name2";
408: String value1 = "value1";
409: String value2 = "value2";
410: ConcreteSerializableListener changeListener = new ConcreteSerializableListener();
411: action.addPropertyChangeListener(changeListener);
412: action.putValue(name1, value1);
413: action.putValue(name2, value2);
414: ByteArrayOutputStream output = new ByteArrayOutputStream();
415: ObjectOutputStream so = new ObjectOutputStream(output);
416: so.writeObject(action);
417: so.flush();
418: assertTrue(output.size() > 1);
419: }
420:
421: public void testReadObject() throws Exception {
422: String name1 = "name1";
423: String name2 = "name2";
424: String value1 = "value1";
425: String value2 = "value2";
426: ConcreteSerializableListener changeListener = new ConcreteSerializableListener();
427: action.addPropertyChangeListener(changeListener);
428: action.putValue(name1, value1);
429: action.putValue(name2, value2);
430: ByteArrayOutputStream output = new ByteArrayOutputStream();
431: ObjectOutputStream so = new ObjectOutputStream(output);
432: so.writeObject(action);
433: so.flush();
434: assertTrue(output.size() > 1);
435: ObjectInputStream si = new ObjectInputStream(
436: new ByteArrayInputStream(output.toByteArray()));
437: AbstractAction ressurectedAction = (AbstractAction) si
438: .readObject();
439: assertEquals("values in table coincide ", action
440: .getValue(name1), ressurectedAction.getValue(name1));
441: assertEquals("values in table coincide ", action
442: .getValue(name2), ressurectedAction.getValue(name2));
443: assertEquals("no listeners resurrected ", 0, ressurectedAction
444: .getPropertyChangeListeners().length);
445: }
446: }
|