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 02.03.2005
021:
022: */package javax.swing.text;
023:
024: import java.awt.event.ActionEvent;
025: import javax.swing.AbstractAction;
026: import javax.swing.Action;
027: import javax.swing.SwingTestCase;
028:
029: public class TextActionTest extends SwingTestCase {
030: public static void main(final String[] args) {
031: junit.textui.TestRunner.run(TextActionTest.class);
032: }
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: }
038:
039: public void testAugmentList() {
040: String name1 = "name1";
041: String name2 = "name2";
042: String name3 = "name3";
043: String name5 = "name5";
044: Action action1 = new TextAction(name1) {
045: private static final long serialVersionUID = 1L;
046:
047: public void actionPerformed(final ActionEvent e) {
048: }
049: };
050: Action action2 = new AbstractAction(name2) {
051: private static final long serialVersionUID = 1L;
052:
053: public void actionPerformed(final ActionEvent e) {
054: }
055: };
056: Action action3 = new AbstractAction(name3) {
057: private static final long serialVersionUID = 1L;
058:
059: public void actionPerformed(final ActionEvent e) {
060: }
061: };
062: Action action4 = new AbstractAction(name1) {
063: private static final long serialVersionUID = 1L;
064:
065: public void actionPerformed(final ActionEvent e) {
066: }
067: };
068: Action action5 = new AbstractAction(name5) {
069: private static final long serialVersionUID = 1L;
070:
071: public void actionPerformed(final ActionEvent e) {
072: }
073: };
074: Action[] list1 = new Action[] { action1, action2 };
075: Action[] list2 = new Action[] { action3, action4 };
076: Action[] list3 = new Action[] { action4, action3 };
077: Action[] list4 = new Action[] { action2 };
078: Action[] list5 = new Action[] { action3, action4, action5 };
079: Action[] res = null;
080: res = TextAction.augmentList(list1, list2);
081: assertTrue("result's not null", res != null);
082: assertEquals("result's length ", 3, res.length);
083: assertEquals("resulted list item", action3, res[0]);
084: assertEquals("resulted list item", action2, res[1]);
085: assertEquals("resulted list item", action4, res[2]);
086: res = TextAction.augmentList(list2, list1);
087: assertTrue("result's not null", res != null);
088: assertEquals("result's length ", 3, res.length);
089: assertEquals("resulted list item", action3, res[0]);
090: assertEquals("resulted list item", action2, res[1]);
091: assertEquals("resulted list item", action1, res[2]);
092: res = TextAction.augmentList(list1, list3);
093: assertTrue("result's not null", res != null);
094: assertEquals("result's length ", 3, res.length);
095: assertEquals("resulted list item", action3, res[0]);
096: assertEquals("resulted list item", action2, res[1]);
097: assertEquals("resulted list item", action4, res[2]);
098: res = TextAction.augmentList(list3, list1);
099: assertTrue("result's not null", res != null);
100: assertEquals("result's length ", 3, res.length);
101: assertEquals("resulted list item", action3, res[0]);
102: assertEquals("resulted list item", action2, res[1]);
103: assertEquals("resulted list item", action1, res[2]);
104: res = TextAction.augmentList(list4, list5);
105: assertTrue("result's not null", res != null);
106: assertEquals("result's length ", 4, res.length);
107: assertEquals("resulted list item", action5, res[0]);
108: assertEquals("resulted list item", action3, res[1]);
109: assertEquals("resulted list item", action2, res[2]);
110: assertEquals("resulted list item", action4, res[3]);
111: }
112:
113: /*
114: * Class under test for void TextAction(String)
115: */
116: public void testTextActionString() {
117: String str1 = "string1";
118: String str2 = "string2";
119: TextAction action1 = new TextAction(str1) {
120: private static final long serialVersionUID = 1L;
121:
122: public void actionPerformed(final ActionEvent e) {
123: }
124: };
125: TextAction action2 = new TextAction(str2) {
126: private static final long serialVersionUID = 1L;
127:
128: public void actionPerformed(final ActionEvent e) {
129: }
130: };
131: TextAction action3 = new TextAction(null) {
132: private static final long serialVersionUID = 1L;
133:
134: public void actionPerformed(final ActionEvent e) {
135: }
136: };
137: assertEquals("Number of keys", 1, action1.getKeys().length);
138: assertEquals("name ", str1, action1.getValue(Action.NAME));
139: assertTrue("action is enabled initially", action1.isEnabled());
140: assertEquals("Number of keys", 1, action2.getKeys().length);
141: assertEquals("name ", str2, action2.getValue(Action.NAME));
142: assertTrue("action is enabled initially", action2.isEnabled());
143: assertEquals("Number of keys", 0, action3.getKeys().length);
144: assertNull("name ", action3.getValue(Action.NAME));
145: assertTrue("action is enabled initially", action3.isEnabled());
146: }
147:
148: public void testTextActionNullEvent() {
149: Action[] actions = new DefaultEditorKit().getActions();
150: for (int i = 0; i < actions.length; i++) {
151: try {
152: actions[i].actionPerformed(null);
153: } catch (NullPointerException e) {
154: fail("Null event is not supported in: "
155: + actions[i].getValue(Action.NAME));
156: }
157: }
158: }
159: }
|