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.JMenuBar;
034:
035: /**
036: * Test class for testing <code>java.swing.JMenuBar</code> components. The following criteria are
037: * tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>menu element count</b> - testes if the number of menus in the tested menu bar is correct
042: * </li>
043: * </ul>
044: *
045: *
046: * @author uak, Christoph Sauer
047: */
048: public class JMenuBarTest extends JComponentTest {
049: /** Cotains the expected number of menus in the menu bar */
050: private int menuElementCount;
051:
052: /**
053: * Creates a new test with the specified values.
054: *
055: * @param env env the environment
056: * @param componentName the name of the component
057: * @param isIgnoring if the test result should be ignored
058: * @param isCorrecting if the values of the component should be corrected when the test has
059: * failed
060: * @param hasFocus if the component has the focus
061: * @param isEnabled if the component is set enabled
062: * @param elemCount the number of menus in the menu bar
063: */
064: public JMenuBarTest(Environment env, String componentName,
065: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
066: boolean isEnabled, int elemCount) {
067: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
068: isEnabled);
069: setMenuElementCount(elemCount);
070: }
071:
072: /**
073: * Creates a new test with the values of the given component and default values.
074: *
075: * @param env the environment.
076: * @param components the components instance
077: * @param component the component to test. Must be of type <code>javax.swing.JMenuBar</code>.
078: */
079: public JMenuBarTest(Environment env, Components components,
080: Component component) {
081: super (env, components, component);
082: setMenuElementCount(((JMenuBar) component).getMenuCount());
083: }
084:
085: /**
086: * Creates a new test with default values and no environment. The environment should be defined
087: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
088: * before environment instances will be accessed.
089: */
090: public JMenuBarTest() {
091: this (null, "", false, false, false, true, 0);
092: }
093:
094: /**
095: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
096: */
097: public boolean evaluate(Components components) {
098: setLastIgnored(isIgnoring());
099: setLastResult(false);
100: setLastCorrected(false);
101:
102: JMenuBar menuBar;
103:
104: // get the menu bar with the componentName
105: Component component = components
106: .getComponent(getComponentName());
107:
108: if (component == null) {
109: setEvaluationMessage(language
110: .getString("Tests.Test.Failure.NoComponent"));
111:
112: return false;
113: }
114:
115: if (!(component instanceof JMenuBar)) {
116: setEvaluationMessage(language
117: .getString("Tests.Test.Failure.WrongComponentType"));
118:
119: return false;
120: }
121:
122: menuBar = (JMenuBar) component;
123:
124: if (doTest(menuBar)) {
125: boolean result = true;
126: Iterator childIter = childrenIterator();
127:
128: while (childIter.hasNext() && result) {
129: result &= evaluateChild((Test) childIter.next(),
130: components);
131: }
132:
133: setLastResult(result);
134:
135: return result;
136: }
137:
138: // correct the menu items if wanted
139: if (isCorrecting()) {
140: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
141: correct(menuBar);
142: setLastCorrected(true);
143: }
144:
145: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
146: return false;
147: }
148:
149: /**
150: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
151: */
152: protected boolean doTest(Component component) {
153: if (super .doTest(component)) {
154: JMenuBar menuBar = (JMenuBar) component;
155:
156: if (menuBar.getMenuCount() != getMenuElementCount()) {
157: setEvaluationMessage(language
158: .getString("Tests.JMenuBarTest.Failure.WrongElementCount")
159: + "\n"
160: + language
161: .getString("Tests.Test.Failure.Expected")
162: + ": "
163: + getMenuElementCount()
164: + "\n"
165: + language
166: .getString("Tests.Test.Failure.Detected")
167: + ": " + menuBar.getMenuCount());
168:
169: return false;
170: }
171: } else {
172: return false;
173: }
174:
175: return true;
176: }
177:
178: /**
179: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
180: */
181: protected void correct(Component component) {
182: super .correct(component);
183:
184: JMenuBar menuBar = (JMenuBar) component;
185:
186: if (menuBar.getMenuCount() != getMenuElementCount()) {
187: setEvaluationMessage("\n"
188: + getElementName()
189: + ": "
190: + language
191: .getString("Tests.JMenuBarTest.Correct.MenuCountFailure"));
192: }
193: }
194:
195: /**
196: * @see jacareto.struct.StructureElement#getElementName()
197: */
198: public String getElementName() {
199: return language.getString("Tests.JMenuBarTest.Name");
200: }
201:
202: /**
203: * @see jacareto.struct.StructureElement#getElementDescription()
204: */
205: public String getElementDescription() {
206: return language.getString("Tests.JMenuBarTest.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 The number of expected menus in the menu bar
220: */
221: public int getMenuElementCount() {
222: return menuElementCount;
223: }
224:
225: /**
226: * DOCUMENT ME!
227: *
228: * @param i the number of expected menus in the menu bar
229: */
230: public void setMenuElementCount(int i) {
231: menuElementCount = i;
232: }
233: }
|