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