001    /*
002     * Copyright (c) 2005 Jens Schou, Staffan Gustafsson, Björn 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     */
023    package csheets.ext.test;
024    
025    import java.util.Set;
026    
027    import csheets.core.Spreadsheet;
028    import csheets.ext.SpreadsheetExtension;
029    
030    /**
031     * An extension of a spreadsheet, with support for testable cells.
032     * @author Einar Pehrson
033     */
034    public class TestableSpreadsheet extends SpreadsheetExtension {
035    
036            /** The unique version identifier used for serialization */
037            private static final long serialVersionUID = -8144504629252776866L;
038    
039            /**
040             * Creates a testable spreadsheet for the given spreadsheet.
041             * @param spreadsheet the spreadsheet to extend
042             */
043            protected TestableSpreadsheet(Spreadsheet spreadsheet) {
044                    super(spreadsheet, TestExtension.NAME);
045            }
046    
047            /**
048             * Returns the testedness of the spreadsheet, i.e. the ratio of valid
049             * test cases to available test cases in all cells.
050             * @return a number between 0.0 and 1.0 denoting the level of testedness
051             */
052            public double getTestedness() {
053                    // Sums test cases
054                    double nAvailable = 0;
055                    double nValid = 0;
056    
057                    for (int row = 0; row < getRowCount(); row++)
058                            for (int column = 0; column < getColumnCount(); column++) {
059                                    TestableCell cell = (TestableCell)getCell(column, row);
060                                    if (cell.hasTestCases()) {
061                                            Set<TestCase> testCases = cell.getTestCases();
062                                            nAvailable += testCases.size();
063                                            for (TestCase testCase : testCases)
064                                                    if (testCase.getValidationState() == TestCase.ValidationState.VALID)
065                                                            nValid++;
066                                    }
067                            }
068    
069                    // Calculates and returns the testedness
070                    if (nAvailable > 0)
071                            return nValid / nAvailable;
072                    else
073                            return 0;
074            }
075    }