001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.test;
025:
026: import jacareto.comp.Components;
027: import jacareto.system.Environment;
028:
029: import java.awt.Component;
030:
031: import java.util.Iterator;
032:
033: import javax.swing.JMenuItem;
034:
035: /**
036: * Test class for testing <code>java.swing.JMenuItem</code> components. The following criteria are
037: * tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>is armed</b> - testes if the menu item is "armed"
042: * </li>
043: * </ul>
044: *
045: *
046: * @author uak, Christoph Sauer
047: * @version 1.0
048: */
049: public class JMenuItemTest extends AbstractButtonTest {
050: /** Contains whether the tested menu item is expected to be "armed" */
051: private boolean isItemArmed;
052:
053: /**
054: * Creates a new test with the specified values.
055: *
056: * @param env env the environment
057: * @param componentName the name of the component
058: * @param isIgnoring if the test result should be ignored
059: * @param isCorrecting if the values of the component should be corrected when the test has
060: * failed
061: * @param hasFocus if the component has the focus
062: * @param isEnabled if the component is set enabled
063: * @param btnText the text on the button
064: * @param isArmed if the the component is "armed"
065: */
066: public JMenuItemTest(Environment env, String componentName,
067: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
068: boolean isEnabled, String btnText, boolean isArmed) {
069: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
070: isEnabled, btnText);
071: setItemArmed(isArmed);
072: }
073:
074: /**
075: * Creates a new test with the values of the given component and default values.
076: *
077: * @param env the environment.
078: * @param components the components instance
079: * @param component the component to test. Must be of type
080: * <code>javax.swing.AbstractButton</code>.
081: */
082: public JMenuItemTest(Environment env, Components components,
083: Component component) {
084: super (env, components, component);
085: setItemArmed(((JMenuItem) component).isArmed());
086: }
087:
088: /**
089: * Creates a new test with default values and no environment. The environment should be defined
090: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
091: * before environment instances will be accessed.
092: */
093: public JMenuItemTest() {
094: this (null, "", false, false, false, true, "", false);
095: }
096:
097: /**
098: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
099: */
100: public boolean evaluate(Components components) {
101: JMenuItem menuItem;
102:
103: setLastIgnored(isIgnoring());
104: setLastResult(false);
105: setLastCorrected(false);
106:
107: // get the pane with the componentName
108: Component component = components
109: .getComponent(getComponentName());
110:
111: if (component == null) {
112: setEvaluationMessage(language
113: .getString("Tests.Test.Failure.NoComponent"));
114:
115: return false;
116: }
117:
118: if (!(component instanceof JMenuItem)) {
119: setEvaluationMessage(language
120: .getString("Tests.Test.Failure.WrongComponentType"));
121:
122: return false;
123: }
124:
125: menuItem = (JMenuItem) component;
126:
127: if (doTest(menuItem)) {
128: boolean result = true;
129: Iterator childIter = childrenIterator();
130:
131: while (childIter.hasNext() && result) {
132: result &= evaluateChild((Test) childIter.next(),
133: components);
134: }
135:
136: setLastResult(result);
137:
138: return result;
139: }
140:
141: if (isCorrecting()) {
142: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
143: correct(menuItem);
144: setLastCorrected(true);
145: }
146:
147: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
148: return false;
149: }
150:
151: /**
152: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
153: */
154: protected boolean doTest(Component component) {
155: if (super .doTest(component)) {
156: JMenuItem menuItem = (JMenuItem) component;
157:
158: if (menuItem.isArmed() != isItemArmed()) {
159: setEvaluationMessage(language
160: .getString("Tests.JMenuItemTest.Failure.WrongItemState")
161: + "\n"
162: + language
163: .getString("Tests.Test.Failure.Expected")
164: + ": "
165: + isItemArmed()
166: + "\n"
167: + language
168: .getString("Tests.Test.Failure.Detected")
169: + ": " + menuItem.isArmed());
170:
171: return false;
172: }
173: } else {
174: return false;
175: }
176:
177: return true;
178: }
179:
180: /**
181: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
182: */
183: protected void correct(Component component) {
184: super .correct(component);
185:
186: JMenuItem menuItem = (JMenuItem) component;
187: menuItem.setArmed(isItemArmed);
188: appendToEvaluationMessage("\n"
189: + getElementName()
190: + ": "
191: + language
192: .getString("Tests.JMenuItemTest.Correct.Armed"));
193: }
194:
195: /**
196: * @see jacareto.struct.StructureElement#getElementName()
197: */
198: public String getElementName() {
199: return language.getString("Tests.JMenuItemTest.Name");
200: }
201:
202: /**
203: * @see jacareto.struct.StructureElement#getElementDescription()
204: */
205: public String getElementDescription() {
206: return language.getString("Tests.JMenuItemTest.Description");
207: }
208:
209: /**
210: * @see jacareto.struct.StructureElement#toShortString()
211: */
212: public String toShortString() {
213: return getElementName();
214: }
215:
216: /**
217: * DOCUMENT ME!
218: *
219: * @return <code>true</code> if the tested component is expected to be enabled, if not
220: * <code>false</code>.
221: */
222: public boolean isItemArmed() {
223: return isItemArmed;
224: }
225:
226: /**
227: * DOCUMENT ME!
228: *
229: * @param b if the tested menu item is expected to be "armed"
230: */
231: public void setItemArmed(boolean b) {
232: isItemArmed = b;
233: }
234: }
|