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.AbstractButton;
034:
035: /**
036: * Test class for testing <code>java.swing.AbstractButton</code> components. The following
037: * criteria are tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>button text</b> - testes if the text on the tested button is correct
042: * </li>
043: * </ul>
044: *
045: *
046: * @author <a href="mailto:frommer@rbg.informatik.tu-darmstadt.de">Juri Frommer</a>
047: * @version 1.0
048: */
049: public class AbstractButtonTest extends JComponentTest {
050: /** Contains the expected text of the tested component */
051: private String buttonText;
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: */
065: public AbstractButtonTest(Environment env, String componentName,
066: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
067: boolean isEnabled, String btnText) {
068: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
069: isEnabled);
070: setButtonText(btnText);
071: }
072:
073: /**
074: * Creates a new test with the values of the given component and default values.
075: *
076: * @param env the environment.
077: * @param components the components instance
078: * @param component the component to test. Must be of type
079: * <code>javax.swing.AbstractButton</code>.
080: */
081: public AbstractButtonTest(Environment env, Components components,
082: Component component) {
083: super (env, components, component);
084: setButtonText(((AbstractButton) component).getText());
085: }
086:
087: /**
088: * Creates a new test with default values and no environment. The environment should be defined
089: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
090: * before environment instances will be accessed.
091: */
092: public AbstractButtonTest() {
093: this (null, "", false, false, false, true, "");
094: }
095:
096: /**
097: * @see jacareto.struct.StructureElement#getElementName()
098: */
099: public String getElementName() {
100: return language.getString("Tests.AbstractButtonTest.Name");
101: }
102:
103: /**
104: * @see jacareto.struct.StructureElement#getElementDescription()
105: */
106: public String getElementDescription() {
107: return language
108: .getString("Tests.AbstractButtonTest.Description");
109: }
110:
111: /**
112: * @see jacareto.struct.StructureElement#toShortString()
113: */
114: public String toShortString() {
115: return getElementName();
116: }
117:
118: /**
119: * DOCUMENT ME!
120: *
121: * @return The expected text of the tested button
122: */
123: public String getButtonText() {
124: return buttonText;
125: }
126:
127: /**
128: * Specifies The expected text of the tested button
129: *
130: * @param string DOCUMENT ME!
131: */
132: public void setButtonText(String string) {
133: buttonText = string;
134: }
135:
136: /**
137: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
138: */
139: public boolean evaluate(Components components) {
140: AbstractButton button = null;
141:
142: setLastIgnored(isIgnoring());
143: setLastResult(false);
144: setLastCorrected(false);
145:
146: // get the button with the componentName
147: Component component = components
148: .getComponent(getComponentName());
149:
150: if (component == null) {
151: setEvaluationMessage(language
152: .getString("Tests.Test.Failure.NoComponent"));
153:
154: return false;
155: }
156:
157: if (!(component instanceof AbstractButton)) {
158: setEvaluationMessage(language
159: .getString("Tests.Test.Failure.WrongComponentType"));
160:
161: return false;
162: }
163:
164: button = (AbstractButton) component;
165:
166: if (doTest(button)) {
167: boolean result = true;
168: Iterator childIter = childrenIterator();
169:
170: while (childIter.hasNext() && result) {
171: result &= evaluateChild((Test) childIter.next(),
172: components);
173: }
174:
175: setLastResult(result);
176:
177: return result;
178: }
179:
180: if (isCorrecting()) {
181: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
182: correct(button);
183: setLastCorrected(true);
184: }
185:
186: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
187: return false;
188: }
189:
190: /**
191: * @see jacareto.test.JComponentTest#doTest(Component)
192: */
193: protected boolean doTest(Component component) {
194: if (super .doTest(component)) {
195: AbstractButton button = (AbstractButton) component;
196: String currentText = button.getText();
197: String targetText = getButtonText();
198:
199: if (!(button.getText().equals(getButtonText()))) {
200: setEvaluationMessage(language
201: .getString("Tests.AbstractButtonTest.Failure.WrongText")
202: + "\n"
203: + language
204: .getString("Tests.Test.Failure.Expected")
205: + ": "
206: + targetText
207: + "\n"
208: + language
209: .getString("Tests.Test.Failure.Detected")
210: + ": " + currentText);
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: AbstractButton button = (AbstractButton) component;
228: button.setText(getButtonText());
229: appendToEvaluationMessage("\n"
230: + getElementName()
231: + ": "
232: + language
233: .getString("Tests.AbstractButtonTest.Correct.Text"));
234: }
235: }
|