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 org.apache.regexp.RE;
030:
031: import java.awt.Component;
032:
033: import java.util.Iterator;
034: import java.util.Vector;
035:
036: import javax.swing.JList;
037:
038: /**
039: * Test class for testing <code>java.swing.JList</code> components. The following criteria are
040: * tested by this class:
041: *
042: * <ul>
043: * <li>
044: * <b>selected indices</b> - testes if the selected indices of the tested list are correct
045: * </li>
046: * </ul>
047: *
048: *
049: * @author Christoph Sauer
050: */
051: public class JListTest extends JComponentTest {
052: /** The indices to be tested represented by a string seperated with ',' */
053: private String targetSelectedIndicesString;
054:
055: /** The indices to be tested represented by an int array */
056: private int[] targetSelectedIndices;
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 selectedIndicesParam the selected indices as comma seperated values
069: */
070: public JListTest(Environment env, String componentName,
071: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
072: boolean isEnabled, String selectedIndicesParam) {
073: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
074: isEnabled);
075: setTargetSelectedIndicesString(selectedIndicesParam);
076: }
077:
078: /**
079: * Creates a new test with the values of the given component and default values. The target
080: * text is not a regular expression
081: *
082: * @param env the environment.
083: * @param components the components instance
084: * @param component the component to test. Must be of type <code>javax.swing.JList</code>.
085: */
086: public JListTest(Environment env, Components components,
087: Component component) {
088: super (env, components, component);
089:
090: // setting the values to be verified to initial ( in GUI currently selected )values
091: int[] selectedIndicesArray = ((JList) component)
092: .getSelectedIndices();
093: String selectedIndices = "";
094:
095: if (selectedIndicesArray.length > 0) {
096: selectedIndices += selectedIndicesArray[0];
097:
098: for (int i = 1; i < selectedIndicesArray.length; i++) {
099: selectedIndices += ",";
100: selectedIndices += selectedIndicesArray[i];
101: }
102: }
103:
104: setTargetSelectedIndicesString(selectedIndices);
105: }
106:
107: /**
108: * Creates a new test with default values and no environment. The environment should be defined
109: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
110: * before environment instances will be accessed.
111: */
112: public JListTest() {
113: this (null, "", false, false, false, true, "");
114: }
115:
116: /**
117: * Specifies the expected selected indices string. This string must only contain comma
118: * serperated integers.
119: *
120: * @param selectedIndicesParam DOCUMENT ME!
121: */
122: public void setTargetSelectedIndicesString(
123: String selectedIndicesParam) {
124: this .targetSelectedIndicesString = selectedIndicesParam;
125: }
126:
127: /**
128: * DOCUMENT ME!
129: *
130: * @return the expected selected indices string.
131: */
132: public String getTargetSelectedIndicesString() {
133: return targetSelectedIndicesString;
134: }
135:
136: /**
137: * @see jacareto.struct.StructureElement#getElementName()
138: */
139: public String getElementName() {
140: return language.getString("Tests.JListTest.Name");
141: }
142:
143: /**
144: * @see jacareto.struct.StructureElement#getElementDescription()
145: */
146: public String getElementDescription() {
147: return language.getString("Tests.JListTest.Description");
148: }
149:
150: /**
151: * @see jacareto.struct.StructureElement#toShortString()
152: */
153: public String toShortString() {
154: return getElementName();
155: }
156:
157: /**
158: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
159: */
160: public boolean evaluate(Components components) {
161: setLastIgnored(isIgnoring());
162: setLastResult(false);
163: setLastCorrected(false);
164:
165: JList jl = null;
166:
167: // get the list component with the componentName
168: Component component = components
169: .getComponent(getComponentName());
170:
171: if (component == null) {
172: setEvaluationMessage(language
173: .getString("Tests.Test.Failure.NoComponent"));
174:
175: return false;
176: }
177:
178: if (!(component instanceof JList)) {
179: setEvaluationMessage(language
180: .getString("Tests.Test.Failure.WrongComponentType"));
181:
182: return false;
183: }
184:
185: jl = (JList) component;
186:
187: if (doTest(jl)) {
188: boolean result = true;
189: Iterator childIter = childrenIterator();
190:
191: while (childIter.hasNext() && result) {
192: result &= evaluateChild((Test) childIter.next(),
193: components);
194: }
195:
196: setLastResult(result);
197:
198: return result;
199: }
200:
201: if (isCorrecting()) {
202: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
203: correct(jl);
204: setLastCorrected(true);
205: }
206:
207: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
208: return false;
209: }
210:
211: /**
212: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
213: */
214: protected boolean doTest(Component component) {
215: Vector selectedIndicesTemp = new Vector();
216: String first = null;
217: String last = null;
218:
219: // first test if focus state is correct
220: if (super .doTest(component)) {
221: JList jl = (JList) component;
222:
223: //checking if string is correct
224: try {
225: // this RE matches (int)(,int)* and blanks
226: // note that carriage returns are not matched
227: RE regexp = new RE(
228: "([:blank:]*([0-9]+)([:blank:]*,[:blank:]*([0-9]+))*[:blank:]*|[:blank:]*)");
229: regexp.match(targetSelectedIndicesString);
230:
231: String insideParens = regexp.getParen(1);
232:
233: if (!targetSelectedIndicesString.equals(insideParens)) {
234: setEvaluationMessage(language
235: .getString("Tests.JListTest.Failure.WrongSyntax"));
236:
237: return false;
238: }
239: } catch (Throwable t) {
240: t.printStackTrace();
241: }
242:
243: //generate 'target selected indices' array from string
244: //parsing text field content
245: String tmp = targetSelectedIndicesString;
246:
247: while (!tmp.equals("")) {
248: if ((tmp.indexOf(",")) != -1) { //number will follow...
249: first = tmp.substring(0, tmp.indexOf(","));
250:
251: if ((tmp.indexOf(",") + 1) <= (tmp.length() - 1)) {
252: //coma is followed by a number...
253: last = tmp.substring(tmp.indexOf(",") + 1);
254: } else {
255: //coma is last character...
256: last = "";
257: }
258:
259: selectedIndicesTemp.add(first);
260: tmp = last;
261: } else //last number...
262: {
263: selectedIndicesTemp.add(tmp);
264:
265: //...we can terminate the loop
266: tmp = "";
267: }
268: }
269:
270: // generating an array out of the help vector
271: targetSelectedIndices = new int[selectedIndicesTemp.size()];
272:
273: for (int i = 0; i < selectedIndicesTemp.size(); i++) {
274: try {
275: targetSelectedIndices[i] = Integer
276: .parseInt(((String) selectedIndicesTemp
277: .get(i)));
278: } catch (NumberFormatException e) {
279: setEvaluationMessage(language
280: .getString("Tests.JListTest.Failure.WrongSyntax"));
281:
282: return false;
283: }
284: }
285:
286: //matching indices ( performing the test )
287: int[] currentlySelectedIndices = jl.getSelectedIndices();
288: boolean check = false;
289:
290: //if lenghth of arrays doesn't match they can't be identical
291: if (currentlySelectedIndices.length == targetSelectedIndices.length) {
292: check = true;
293:
294: for (int i = 0; (check == true)
295: && (i < targetSelectedIndices.length); i++) {
296: check = (currentlySelectedIndices[i] == targetSelectedIndices[i]);
297: }
298: }
299:
300: if (!check) {
301: String currentlySelectedIndicesString = "";
302:
303: for (int i = 0; i < currentlySelectedIndices.length; i++) {
304: currentlySelectedIndicesString += currentlySelectedIndices[i];
305:
306: if (!(i == (currentlySelectedIndices.length - 1))) {
307: currentlySelectedIndicesString += ",";
308: }
309: }
310:
311: setEvaluationMessage(language
312: .getString("Tests.JListTest.Failure.WrongIndices")
313: + "\n"
314: + language
315: .getString("Tests.Test.Failure.Expected")
316: + ": "
317: + targetSelectedIndicesString
318: + "\n"
319: + language
320: .getString("Tests.Test.Failure.Detected")
321: + ": " + currentlySelectedIndicesString);
322:
323: return false;
324: }
325: } else {
326: return false;
327: }
328:
329: return true;
330: }
331:
332: /**
333: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
334: */
335: protected void correct(Component component) {
336: super .correct(component);
337:
338: JList jl = (JList) component;
339:
340: if (targetSelectedIndices == null) {
341: int[] a = {};
342: jl.setSelectedIndices(a);
343: } else {
344: jl.setSelectedIndices(targetSelectedIndices);
345: }
346:
347: appendToEvaluationMessage("\n"
348: + getElementName()
349: + ": "
350: + language
351: .getString("Tests.JListTest.Correct.SelectedIndices"));
352: }
353: }
|