Programming with contracts.
The contracts are enabled and disabled by settings in a property file.
Default: If no property file is being found, all contracts are disabled.
The property file can be provided in some ways:
The properties file for the contracts contains the list of classes whose contracts have to be checked during runtime.
The format allows you to set the check for a class or for a package. Defined for a package the check option is defined recursively for all of the sub-packages and classes of this package. The format of a definition is:
GLOBAL={enabled, enabledByDefault, disabledByDefault, disabled}
<package name>={enabled, enabledByDefault, disabledByDefault, disabled}
<class name>={enabled, enabledByDefault, disabledByDefault, disabled}
Example:
GLOBAL=enabledByDefault
myPackage1=disabledByDefault
myPackage1.myPackage2.MyClass=enabled
The rules:
/*
* Copyright (c) 2001 SOFTWARE AG, All Rights Reserved.
*/
package com.softwareag.xtools.common.mycomponent;
// import Pre and Postconditions, copy this
import com.softwareag.common.instrumentation.contract.Precondition;
import com.softwareag.common.instrumentation.contract.Postcondition;
/**
** Demonstrate the usage of Pre and Postconditions.
**
** @author lem@softwareag.com
** @version 0.0.
**/
public class ContractUser{
// Just copy this two field declarations and adjust the class name
/**
** Enables/disables precondition testing due to a global setting.
**/
private static final boolean PRE_CHECK = Precondition.isEnabled(ContractUser.class);
/**
** Enables/disables postcondition testing due to a global setting.
**/
private static final boolean POST_CHECK = Postcondition.isEnabled(ContractUser.class);
/**
** My constructor gets some parameters as input. The pre and post tags will be added to the javadoc automatically,
** because the sagdoclet is used in the build process.
**
** @pre param1 != null
** @pre param1 in {"I like contracts", "Contracts are fine"}
**
** @param param1 A string that shows that the user likes contracts.
**/
public ContractUser(String param1){
// Precondition.check() throws a ViolatedPrecondition error with the String given, if the
// condition is false.
if (PRE_CHECK) Precondition.check("param1 is not null", param1 != null);
if (PRE_CHECK) Precondition.check("param1 has to be a kind of enthusiasm about contracts", param1.equals("I like contracts") || param1.equals("Contracts are fine"));
// No processing is performed because this is an example.
}
/**
** This method is designed to return null !!
** The user might learn this by reading the javadoc postcondition:
**
** @post result == null
**
** @return Almost always null. This is guaranteed by a post condition.
**/
public Object createNull(){
// Follow the convention and store your return value in "result".
Object result = null;
// This was not a difficult processing, now it will be checked, if this method can hold its promises:
// Postcondition.check() throws a ViolatedPostcondition error with the String given, if the
// condition is false.
if (POST_CHECK) Postcondition.check("Result is null", result == null);
return result;
}
} // end of example