/*
* $Id: sqlunit-book.xml,v 1.100 2006/04/30 22:25:54 spal Exp $ (1)
* $Source: /cvsroot/sqlunit/sqlunit/docs/sqlunit-book.xml,v $
* SQLUnit - a test harness for unit testing database stored procedures.(2)
* Copyright (C) 2003 The SQLUnit Team
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package net.sourceforge.sqlunit.matchers; (3)
import java.util.Map; (4)
import net.sourceforge.sqlunit.IErrorCodes;
import net.sourceforge.sqlunit.IMatcher;
import net.sourceforge.sqlunit.SQLUnitException;
/** (5)
* The PercentageRangeMatcher is an implementation of the IMatcher interface
* used to define rulesets for matching columns in SQLUnit. This matcher
* will accept a percentage tolerance value and check to see that the target
* is within (+/-) tolerance percent of the source.
* Arguments:
* pc-tolerance : a percentage tolerance value.
* @author Sujit Pal (spal@users.sourceforge.net)
* @version $Revision: 1.100 $
*/
public class PercentageRangeMatcher implements IMatcher { (6)
/**
* Default constructor as per contract with IMatcher.
*/
public PercentageRangeMatcher() {;}
/**
* Returns true if the value of the target is withing (+/-) a specified
* tolerance value of the source. Note that in this case, the source,
* target and tolerance must all be numeric values.
* @param source the String representing the source to be matched.
* @param target the String representing the target to be matched.
* @param args a Map of name value pairs of arguments passed in.
*/
public boolean isEqual(String source, String target, Map args)
throws SQLUnitException { (7)
String aTolerance = (String) args.get("pc-tolerance"); (8)
if (aTolerance == null) {
throw new SQLUnitException(IErrorCodes.MATCHER_EXCEPTION,
new String[] {this.getClass().getName(),
"Value for key 'pc-tolerance' is NULL"});
}
// is tolerance a float?
float iTolerance = 0;
try {
iTolerance = Float.parseFloat(aTolerance);
} catch (NumberFormatException e) {
throw new SQLUnitException(IErrorCodes.MATCHER_EXCEPTION,
new String[] {this.getClass().getName(),
"Value of key 'pc-tolerance' is not a FLOAT"});
}
// cannot have the tolerance exceed 100
if (iTolerance > 100.0) {
throw new SQLUnitException(IErrorCodes.MATCHER_EXCEPTION,
new String[] {this.getClass().getName(),
"Value of key 'pc-tolerance' must be between 0 and 100"});
}
// is the source a float? (9)
float iSource = 0;
try {
iSource = Float.parseFloat(source);
} catch (NumberFormatException e) {
throw new SQLUnitException(IErrorCodes.MATCHER_EXCEPTION,
new String[] {this.getClass().getName(),
"Value of 'source' is not a FLOAT"});
}
// is the target an integer?
float iTarget = 0;
try {
iTarget = Float.parseFloat(target);
} catch (NumberFormatException e) {
throw new SQLUnitException(IErrorCodes.MATCHER_EXCEPTION,
new String[] {this.getClass().getName(),
"Value of 'target' is not a FLOAT"});
}
// return the match (10)
return ((iTarget >= (iSource - (iTolerance * 100))) &&
(iTarget <= (iSource + (iTolerance * 100))));
}
}
|