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 30.09.2004
021:
022: */package javax.swing;
023:
024: import java.awt.event.ActionEvent;
025: import java.beans.PropertyChangeListener;
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.io.Serializable;
033:
034: public class ActionMapTest extends SwingTestCase {
035: private static class ActionProxy implements Action, Serializable {
036: private static final long serialVersionUID = 1L;
037:
038: public String name = "";
039:
040: public ActionProxy() {
041: }
042:
043: public ActionProxy(final String name) {
044: this .name = name;
045: }
046:
047: public boolean isEnabled() {
048: return true;
049: }
050:
051: public void setEnabled(final boolean enabled) {
052: }
053:
054: public void addPropertyChangeListener(
055: final PropertyChangeListener listener) {
056: }
057:
058: public void removePropertyChangeListener(
059: final PropertyChangeListener listener) {
060: }
061:
062: public Object getValue(final String valueName) {
063: return null;
064: }
065:
066: public void putValue(final String valueName, final Object value) {
067: }
068:
069: public void actionPerformed(final ActionEvent event) {
070: }
071:
072: private void writeObject(final ObjectOutputStream outStream)
073: throws IOException {
074: outStream.defaultWriteObject();
075: }
076:
077: private void readObject(final ObjectInputStream inStream)
078: throws IOException, ClassNotFoundException {
079: inStream.defaultReadObject();
080: }
081: }
082:
083: protected ActionMap map;
084:
085: protected boolean find(final Object[] array, final Object value) {
086: boolean found = false;
087: if (array != null) {
088: for (int i = 0; i < array.length; i++) {
089: if (array[i].equals(value)) {
090: found = true;
091: break;
092: }
093: }
094: }
095: return found;
096: }
097:
098: /*
099: * @see TestCase#setUp()
100: */
101: @Override
102: protected void setUp() throws Exception {
103: super .setUp();
104: map = new ActionMap();
105: }
106:
107: public void testPut() {
108: Action action1 = new ActionProxy();
109: Action action2 = new ActionProxy();
110: map.put("1", action1);
111: map.put("2", action2);
112: assertTrue(map.get("1") == action1);
113: assertTrue(map.get("2") == action2);
114: map.put("2", action1);
115: map.put("1", action2);
116: assertTrue(map.get("1") == action2);
117: assertTrue(map.get("2") == action1);
118: map.put("1", null);
119: map.put("2", null);
120: assertTrue(map.size() == 0);
121: }
122:
123: public void testGet() {
124: Action action1 = new ActionProxy();
125: Action action2 = new ActionProxy();
126: Action action3 = new ActionProxy();
127: assertNull(map.get("1"));
128: assertNull(map.get("2"));
129: map.put("1", action1);
130: map.put("2", action1);
131: assertTrue(map.get("1") == action1);
132: assertTrue(map.get("2") == action1);
133: map.put("1", action2);
134: assertTrue(map.get("1") == action2);
135: map.put("1", null);
136: assertNull(map.get("1"));
137: map.clear();
138: ActionMap childMap = new ActionMap();
139: childMap.setParent(map);
140: map.put("1", action1);
141: childMap.put("2", action2);
142: assertTrue(childMap.get("1") == action1);
143: assertTrue(childMap.get("2") == action2);
144: map.put("2", action3);
145: assertTrue(childMap.get("2") == action2);
146: childMap.put("1", action3);
147: assertTrue(childMap.get("1") == action3);
148: }
149:
150: public void testSetGetParent() {
151: ActionMap parent1 = new ActionMap();
152: ActionMap parent2 = new ActionMap();
153: ActionMap parent3 = null;
154: assertNull(map.getParent());
155: map.setParent(parent1);
156: assertTrue(map.getParent() == parent1);
157: map.setParent(parent3);
158: assertTrue(map.getParent() == parent3);
159: map.setParent(parent2);
160: assertTrue(map.getParent() == parent2);
161: }
162:
163: public void testRemove() {
164: Action action1 = new ActionProxy();
165: Action action2 = new ActionProxy();
166: map.put("1", action1);
167: map.put("2", action2);
168: assertTrue(map.get("1") == action1);
169: assertTrue(map.get("2") == action2);
170: map.remove("2");
171: assertNull(map.get("2"));
172: assertTrue(map.get("1") == action1);
173: map.remove("1");
174: assertNull(map.get("1"));
175: assertTrue(map.size() == 0);
176: }
177:
178: public void testKeys() {
179: Action action1 = new ActionProxy();
180: Action action2 = new ActionProxy();
181: Object[] keys = map.keys();
182: assertTrue(map.size() == 0);
183: if (isHarmony()) {
184: assertTrue(keys != null);
185: assertTrue(keys.length == 0);
186: }
187: map.put("1", action1);
188: map.put("2", action2);
189: map.put("3", action1);
190: map.put("4", action2);
191: keys = map.keys();
192: assertTrue(keys != null && keys.length == 4);
193: assertTrue(find(keys, "1"));
194: assertTrue(find(keys, "2"));
195: assertTrue(find(keys, "3"));
196: assertTrue(find(keys, "4"));
197: }
198:
199: public void testAllKeys() {
200: ActionMap parent = new ActionMap();
201: Action action1 = new ActionProxy();
202: Action action2 = new ActionProxy();
203: Object[] keys = map.allKeys();
204: map.setParent(parent);
205: assertTrue(map.size() == 0);
206: if (isHarmony()) {
207: assertTrue(keys != null);
208: assertTrue(keys.length == 0);
209: }
210: parent.put("1", action1);
211: parent.put("2", action2);
212: parent.put("3", action1);
213: parent.put("4", action2);
214: map.put("3", action1);
215: map.put("4", action2);
216: map.put("5", action1);
217: map.put("6", action2);
218: keys = map.allKeys();
219: assertTrue(keys != null && keys.length == 6);
220: assertTrue(find(keys, "1"));
221: assertTrue(find(keys, "2"));
222: assertTrue(find(keys, "3"));
223: assertTrue(find(keys, "4"));
224: assertTrue(find(keys, "5"));
225: assertTrue(find(keys, "6"));
226: }
227:
228: public void testClear() {
229: Action action1 = new ActionProxy();
230: Action action2 = new ActionProxy();
231: assertTrue(map.size() == 0);
232: map.put("1", action1);
233: map.put("2", action2);
234: assertTrue(map.size() == 2);
235: map.clear();
236: assertTrue(map.size() == 0);
237: if (isHarmony()) {
238: assertTrue("keys", map.keys() != null);
239: assertTrue("keys", map.keys().length == 0);
240: }
241: map.put("1", action1);
242: assertTrue(map.size() == 1);
243: }
244:
245: public void testSize() {
246: Action action1 = new ActionProxy();
247: Action action2 = new ActionProxy();
248: assertTrue(map.size() == 0);
249: map.put("1", action1);
250: map.put("2", action2);
251: assertTrue(map.size() == 2);
252: map.put("3", action1);
253: map.put("4", action2);
254: assertTrue(map.size() == 4);
255: map.put("3", null);
256: map.put("4", null);
257: assertTrue(map.size() == 2);
258: }
259:
260: public void testWriteObject() throws Exception {
261: String name1 = "action1";
262: String name2 = "action2";
263: Action action1 = new ActionProxy(name1);
264: Action action2 = new ActionProxy(name2);
265: Object object1 = "object1";
266: Object object2 = "object2";
267: ActionMap parent = new ActionMap();
268: map.setParent(parent);
269: map.put(object1, action1);
270: map.put(object2, action2);
271: ByteArrayOutputStream fo = new ByteArrayOutputStream();
272: ObjectOutputStream so = new ObjectOutputStream(fo);
273: so.writeObject(map);
274: so.flush();
275: assertTrue(fo.size() > 0);
276: }
277:
278: public void testReadObject() throws Exception {
279: String name1 = "action1";
280: String name2 = "action2";
281: Action action1 = new ActionProxy(name1);
282: Action action2 = new ActionProxy(name2);
283: Object object1 = "object1";
284: Object object2 = "object2";
285: ActionMap parent = new ActionMap();
286: map.setParent(parent);
287: map.put(object1, action1);
288: map.put(object2, action2);
289: ByteArrayOutputStream fo = new ByteArrayOutputStream();
290: ObjectOutputStream so = new ObjectOutputStream(fo);
291: so.writeObject(map);
292: so.flush();
293: InputStream fi = new ByteArrayInputStream(fo.toByteArray());
294: ObjectInputStream si = new ObjectInputStream(fi);
295: ActionMap ressurectedMap = (ActionMap) si.readObject();
296: assertTrue(ressurectedMap.getParent() != null);
297: assertTrue(ressurectedMap.get(object1) instanceof ActionProxy);
298: assertTrue(((ActionProxy) ressurectedMap.get(object1)).name
299: .equals(name1));
300: assertTrue(ressurectedMap.get(object2) instanceof ActionProxy);
301: assertTrue(((ActionProxy) ressurectedMap.get(object2)).name
302: .equals(name2));
303: }
304: }
|