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: /*
025: * Created on 13.05.2003
026: *
027: * To change the template for this generated file go to
028: * Window>Preferences>Java>Code Generation>Code and Comments
029: */
030: package jacareto.test;
031:
032: import jacareto.comp.Components;
033: import jacareto.system.Environment;
034:
035: import java.awt.Component;
036:
037: import java.util.Iterator;
038:
039: import javax.swing.JComboBox;
040:
041: /**
042: * Test class for testing <code>java.swing.JComboBox</code> components. The following criteria are
043: * tested by this class:
044: *
045: * <ul>
046: * <li>
047: * <b>selected index</b> - testes if the selected index of the tested combo box is correct
048: * </li>
049: * </ul>
050: *
051: *
052: * @author carola
053: */
054: public class JComboBoxTest extends JComponentTest {
055: /** Contains the selected index the tested component should have */
056: private int selectedIndex;
057:
058: /**
059: * Creates a new test with the specified values.
060: *
061: * @param env env the environment
062: * @param componentName the name of the component
063: * @param isIgnoring if the test result should be ignored
064: * @param isCorrecting if the values of the component should be corrected when the test has
065: * failed
066: * @param hasFocus if the component has the focus
067: * @param isEnabled if the component is set enabled
068: * @param selectedIndex the selected index of the combo box
069: */
070: public JComboBoxTest(Environment env, String componentName,
071: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
072: boolean isEnabled, int selectedIndex) {
073: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
074: isEnabled);
075: setSelectedIndex(selectedIndex);
076: }
077:
078: /**
079: * Creates a new test with the values of the given component and default values.
080: *
081: * @param env the environment.
082: * @param components the components instance
083: * @param component the component to test. Must be of type <code>javax.swing.JComboBox</code>.
084: */
085: public JComboBoxTest(Environment env, Components components,
086: Component component) {
087: super (env, components, component);
088: setSelectedIndex(((JComboBox) component).getSelectedIndex());
089: }
090:
091: /**
092: * Creates a new test with default values and no environment. The environment should be defined
093: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
094: * before environment instances will be accessed.
095: */
096: public JComboBoxTest() {
097: this (null, "", false, false, false, true, 0);
098: }
099:
100: /**
101: * Specifies the expected selected index
102: *
103: * @param selectedIndex DOCUMENT ME!
104: */
105: public void setSelectedIndex(int selectedIndex) {
106: this .selectedIndex = selectedIndex;
107: }
108:
109: /**
110: * DOCUMENT ME!
111: *
112: * @return the expected selected index
113: */
114: public int getSelectedIndex() {
115: return selectedIndex;
116: }
117:
118: /**
119: * @see jacareto.struct.StructureElement#getElementName()
120: */
121: public String getElementName() {
122: return language.getString("Tests.JComboBoxTest.Name");
123: }
124:
125: /**
126: * @see jacareto.struct.StructureElement#getElementDescription()
127: */
128: public String getElementDescription() {
129: return language.getString("Tests.JComboBoxTest.Description");
130: }
131:
132: /**
133: * @see jacareto.struct.StructureElement#toShortString()
134: */
135: public String toShortString() {
136: return getElementName();
137: }
138:
139: /**
140: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
141: */
142: public boolean evaluate(Components components) {
143: JComboBox combo = null;
144: setLastIgnored(isIgnoring());
145: setLastResult(false);
146: setLastCorrected(false);
147:
148: // get the combo with the componentName
149: Component component = components
150: .getComponent(getComponentName());
151:
152: if (component == null) {
153: setEvaluationMessage(language
154: .getString("Tests.Test.Failure.NoComponent"));
155:
156: return false;
157: }
158:
159: if (!(component instanceof JComboBox)) {
160: setEvaluationMessage(language
161: .getString("Tests.Test.Failure.WrongComponentType"));
162:
163: return false;
164: }
165:
166: combo = (JComboBox) component;
167:
168: if (doTest(combo)) {
169: boolean result = true;
170: Iterator childIter = childrenIterator();
171:
172: while (childIter.hasNext() && result) {
173: result &= evaluateChild((Test) childIter.next(),
174: components);
175: }
176:
177: setLastResult(result);
178:
179: return result;
180: }
181:
182: if (isCorrecting()) {
183: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
184: correct(combo);
185: setLastCorrected(true);
186: }
187:
188: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
189: return false;
190: }
191:
192: /**
193: * @see jacareto.test.JComponentTest#doTest(Component)
194: */
195: protected boolean doTest(Component component) {
196: if (super .doTest(component)) {
197: JComboBox jcb = (JComboBox) component;
198:
199: if (jcb.getSelectedIndex() != getSelectedIndex()) {
200: setEvaluationMessage(language
201: .getString("Tests.JComboBoxTest.Failure.WrongIndex")
202: + "\n"
203: + language
204: .getString("Tests.Test.Failure.Expected")
205: + ": "
206: + getSelectedIndex()
207: + "\n"
208: + language
209: .getString("Tests.Test.Failure.Detected")
210: + ": " + jcb.getSelectedIndex());
211:
212: return false;
213: }
214: } else {
215: return false;
216: }
217:
218: return true;
219: }
220:
221: /**
222: * @see jacareto.test.JComponentTest#correct(Component)
223: */
224: protected void correct(Component component) {
225: super .correct(component);
226:
227: JComboBox jcb = (JComboBox) component;
228:
229: // is the selection indice to be set existing in the gui list?
230: if ((getSelectedIndex() >= 0)
231: && (getSelectedIndex() < jcb.getItemCount())) {
232: jcb.setSelectedIndex(getSelectedIndex());
233: appendToEvaluationMessage("\n"
234: + getElementName()
235: + ": "
236: + language
237: .getString("Tests.JComboBoxTest.Correct.SelectedIndex"));
238: } else {
239: appendToEvaluationMessage("\n"
240: + getElementName()
241: + ": "
242: + language
243: .getString("Tests.JComboBoxTest.Correct.SelectedIndexFailed"));
244: }
245: }
246: }
|