JComboBoxTest.java :  » Testing » jacareto » jacareto » test » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » test » JComboBoxTest.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Created on 13.05.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package jacareto.test;


import jacareto.comp.Components;
import jacareto.system.Environment;

import java.awt.Component;

import java.util.Iterator;

import javax.swing.JComboBox;

/**
 * Test class for testing <code>java.swing.JComboBox</code> components.  The following criteria are
 * tested by this class:
 * 
 * <ul>
 * <li>
 * <b>selected index</b> - testes if the selected index of the tested combo box is correct
 * </li>
 * </ul>
 * 
 *
 * @author carola
 */
public class JComboBoxTest extends JComponentTest {
    /** Contains the selected index the tested component should have */
    private int selectedIndex;

    /**
     * Creates a new test with the specified values.
     *
     * @param env env the environment
     * @param componentName the name of the component
     * @param isIgnoring if the test result should be ignored
     * @param isCorrecting if the values of the component should be corrected when the test has
     *        failed
     * @param hasFocus if the component has the focus
     * @param isEnabled if the component is set enabled
     * @param selectedIndex the selected index of the combo box
     */
    public JComboBoxTest (Environment env, String componentName, boolean isIgnoring,
        boolean isCorrecting, boolean hasFocus, boolean isEnabled, int selectedIndex) {
        super(env, componentName, isIgnoring, isCorrecting, hasFocus, isEnabled);
        setSelectedIndex (selectedIndex);
    }

    /**
     * Creates a new test with the values of the given component and default values.
     *
     * @param env the environment.
     * @param components the components instance
     * @param component the component to test. Must be of type <code>javax.swing.JComboBox</code>.
     */
    public JComboBoxTest (Environment env, Components components, Component component) {
        super(env, components, component);
        setSelectedIndex (((JComboBox) component).getSelectedIndex ());
    }

    /**
     * Creates a new test with default values and no environment. The environment should be defined
     * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
     * before environment instances will be accessed.
     */
    public JComboBoxTest () {
        this(null, "", false, false, false, true, 0);
    }

    /**
     * Specifies the expected selected index
     *
     * @param selectedIndex DOCUMENT ME!
     */
    public void setSelectedIndex (int selectedIndex) {
        this.selectedIndex = selectedIndex;
    }

    /**
     * DOCUMENT ME!
     *
     * @return the expected selected index
     */
    public int getSelectedIndex () {
        return selectedIndex;
    }

    /**
     * @see jacareto.struct.StructureElement#getElementName()
     */
    public String getElementName () {
        return language.getString ("Tests.JComboBoxTest.Name");
    }

    /**
     * @see jacareto.struct.StructureElement#getElementDescription()
     */
    public String getElementDescription () {
        return language.getString ("Tests.JComboBoxTest.Description");
    }

    /**
     * @see jacareto.struct.StructureElement#toShortString()
     */
    public String toShortString () {
        return getElementName ();
    }

    /**
     * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
     */
    public boolean evaluate (Components components) {
        JComboBox combo = null;
        setLastIgnored (isIgnoring ());
        setLastResult (false);
        setLastCorrected (false);

        // get the combo with the componentName
        Component component = components.getComponent (getComponentName ());

        if (component == null) {
            setEvaluationMessage (language.getString ("Tests.Test.Failure.NoComponent"));

            return false;
        }

        if (! (component instanceof JComboBox)) {
            setEvaluationMessage (language.getString ("Tests.Test.Failure.WrongComponentType"));

            return false;
        }

        combo = (JComboBox) component;

        if (doTest (combo)) {
            boolean result = true;
            Iterator childIter = childrenIterator ();

            while (childIter.hasNext () && result) {
                result &= evaluateChild ((Test) childIter.next (), components);
            }

            setLastResult (result);

            return result;
        }

        if (isCorrecting ()) {
            //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
            correct (combo);
            setLastCorrected (true);
        }

        //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
        return false;
    }

    /**
     * @see jacareto.test.JComponentTest#doTest(Component)
     */
    protected boolean doTest (Component component) {
        if (super.doTest (component)) {
            JComboBox jcb = (JComboBox) component;

            if (jcb.getSelectedIndex () != getSelectedIndex ()) {
                setEvaluationMessage (language.getString ("Tests.JComboBoxTest.Failure.WrongIndex") +
                    "\n" + language.getString ("Tests.Test.Failure.Expected") + ": " +
                    getSelectedIndex () + "\n" +
                    language.getString ("Tests.Test.Failure.Detected") + ": " +
                    jcb.getSelectedIndex ());

                return false;
            }
        } else {
            return false;
        }

        return true;
    }

    /**
     * @see jacareto.test.JComponentTest#correct(Component)
     */
    protected void correct (Component component) {
        super.correct (component);

        JComboBox jcb = (JComboBox) component;

        // is the selection indice to be set existing in the gui list?
        if ((getSelectedIndex () >= 0) && (getSelectedIndex () < jcb.getItemCount ())) {
            jcb.setSelectedIndex (getSelectedIndex ());
            appendToEvaluationMessage ("\n" + getElementName () + ": " +
                language.getString ("Tests.JComboBoxTest.Correct.SelectedIndex"));
        } else {
            appendToEvaluationMessage ("\n" + getElementName () + ": " +
                language.getString ("Tests.JComboBoxTest.Correct.SelectedIndexFailed"));
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.