001/*
002 * Copyright (c) 2005 Jens Schou, Staffan Gustafsson, Bjorn Lanneskog, 
003 * Einar Pehrson and Sebastian Kekkonen
004 *
005 * This file is part of
006 * CleanSheets Extension for Test Cases
007 *
008 * CleanSheets Extension for Test Cases is free software; you can
009 * redistribute it and/or modify it under the terms of the GNU General Public
010 * License as published by the Free Software Foundation; either version 2 of
011 * the License, or (at your option) any later version.
012 *
013 * CleanSheets Extension for Test Cases is distributed in the hope that
014 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
015 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with CleanSheets Extension for Test Cases; if not, write to the
020 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021 * Boston, MA  02111-1307  USA
022 */
023package csheets.ext.test;
024
025import java.io.Serializable;
026import java.util.HashSet;
027import java.util.Iterator;
028import java.util.Set;
029
030import csheets.core.IllegalValueTypeException;
031import csheets.core.Value;
032import csheets.core.formula.Formula;
033
034/**
035 * Contains the information for a single test case, i.e. a single test case
036 * parameter for each of the precedents.
037 * @author Staffan Gustafsson
038 * @author Jens Schou
039 * @author Einar Pehrson
040 */
041public class TestCase extends Formula implements Serializable {
042
043        /** The unique version identifier used for serialization */
044        private static final long serialVersionUID = -6135481035045505755L;
045
046        /** The validation states of a test case. */
047        public enum ValidationState {
048
049                /** The test case was validated successfully. */
050                VALID,
051
052                /** The test case failed. */
053                REJECTED,
054
055                /** The test case has not yet been validated. */
056                PENDING
057        };
058
059        /** The validation state of the test case. */
060        private ValidationState validationState = ValidationState.PENDING;
061
062        /** The parameters of the test case */
063        private Set<TestCaseParam> params = new HashSet<TestCaseParam>();
064
065        /** The value received when the test case was evaluated */
066        private Value value;
067
068        /**
069         * Creates a new test case for the given cell in the given spreadsheet.
070         * The value of the test case is calculated by replacing the references in
071         * the cell's formula with the given set of test case parameters.
072         * @param cell the cell for which the test case is to be created
073         * @param params the set of test case parameters to use
074         */
075        public TestCase(TestableCell cell, Set<TestCaseParam> params) {
076                super(cell, new TestCaseBuilder(params).getExpression(cell.getFormula()));
077
078                // Stores members
079                this.params = params;
080
081                // Evaluates the test case
082                try {
083                        this.value = super.evaluate();
084                } catch (IllegalValueTypeException e) {
085                        this.value = new Value(e);
086                }
087        }
088
089        public TestableCell getCell() {
090                return (TestableCell)super.getCell();
091        }
092
093        public Value evaluate() {
094                return value;
095        }
096
097        /**
098         *
099         * @return the set of test case parameters that make up the test case
100         */
101        public Set<TestCaseParam> getParams(){
102                return params;
103        }
104
105        /**
106         *
107         * @return Returns the ValidationState of the TestCase.
108         */
109        public ValidationState getValidationState() {
110                return validationState;
111        }
112
113        /**
114         *
115         * @param state Sets the ValidationState of the TestCase to the param's state.
116         */
117        public void setValidationState(ValidationState state) {
118                this.validationState = state;
119        }
120
121        /**
122         * Test if the TestCase is based on a specified TestCaseParam.
123         * @param param The TestCaseParam we want to know it the TestCase uses
124         * @return true if the TestCase uses param, otherwise false
125         */
126        public boolean hasParam(TestCaseParam param) {
127                Iterator<TestCaseParam> it = params.iterator();
128                while(it.hasNext()) {
129                        if(it.next().equals(param)) return true;
130                }
131                return false;
132        }
133
134        /**
135         * Returns whether the other object has the same parameters
136         * and cell as this has.
137         * @param other the object to check for equality
138         * @return true if the objects are equal
139         */
140        public boolean equals(Object other){
141                if (!(other instanceof TestCase) || other == null)
142                        return false;
143                TestCase otherTC = (TestCase)other;
144                return params.equals(otherTC.getParams()) && getCell().equals(otherTC.getCell());
145        }
146
147        /**
148         * Returns the hash code of the test case.
149         * @return the hash code of the test case.
150         */
151        public int hashCode() {
152                return getCell().hashCode() + params.hashCode();
153        }
154}