Use cases, contracts, testing
Use case
The use case for Buy Items version 1 indentified system operations (p 139)
- enterItem(upc, qty)
- endSale()
- makePayment()
Contract
The contract for enterItem(upc, quantity) (page 155) was:
Preconditions
- UPC is known to the system
Postconditions
- If a new sale, a Sale was created
- If a new sale, the new sale was associated with the POST
- A SalesLineItem was created
- The SalesLineitem was associated with the Sale
- SalesLineItem.quantity was set to quantity
- The SalesLineItem was associated with a ProductSpecification
based on UPC match
Collaboration diagram
The collaboration diagram for enterItem (page 302) is
EnterItem contract
The contract for this method in POST is
/**
* @pre Post.isKnown(upc)
* @post getSale().getLastLineItem().getQty() == qty
* @post getSale().getLastLineItem().getSpecification().getUpc() == upc
*/
public void enterItem(int upc, int qty);
Dependencies
- ProductSpecification does not rely on any other classes.
This can be tested first
- ProductCatalog relies on ProductSpecification but nothing else.
This can be tested next
- SalesLineItem relies on ProductSpecification.
This can be done next
- Sale relies on SalesLineItem and can be done next
- Finally, enterItem for POST can be tested
ProductSpecification contract
public class ProductSpecification {
/**
* @pre description != null
* @post upc == getUpc()
* @post description.equals(getDescription())
*/
public ProductSpecification(int upc, String description);
public int getUpc();
public String getDescription();
}
ProductSpecification implementation
public class ProductSpecification {
private String description;
private int upc;
/**
* @pre description != null
* @post upc == getUpc()
* @post description.equals(getDescription())
*/
public ProductSpecification(int upc, String description) {
this.upc = upc;
this.description = description;
}
public int getUpc() {
return upc;
}
public String getDescription() {
return description;
}
}
ProductSpecification tests
test test-1 {constructor} {
set spec [java::new ProductSpecification 300 "some description"]
set result [$spec getDescription]
} {some description}
test test-2 {constructor} {
set spec [java::new ProductSpecification 300 "some description"]
set result [$spec getUpc]
} {300}
ProductCatalog contract
public class ProductCatalog {
/**
* @pre isKnown(upc)
* @post return.getUpc() == upc
*/
public ProductSpecification getSpecification(int upc);
public boolean isKnown(int upc);
/**
* From manager use case
* @pre ! isKnown(upc)
* @post isKnown(upc)
* @post getSpecification(upc).getUpc() == upc
* @post getSpecification(upc).getDescription().equals(description)
*/
public void add(int upc, String description);
}
ProductCatalog implementation
import java.util.HashMap;
public class ProductCatalog {
private HashMap specs = new HashMap();
/**
* @pre isKnown(upc)
* @post return.getUpc() == upc
*/
public ProductSpecification getSpecification(int upc) {
Integer Upc = new Integer(upc);
return (ProductSpecification) specs.get(Upc);
}
public boolean isKnown(int upc) {
Integer Upc = new Integer(upc);
return specs.containsKey(Upc);
}
/**
* From manager use case
* @pre ! isKnown(upc)
* @post isKnown(upc)
* @post getSpecification(upc).getUpc() == upc
* @post getSpecification(upc).getDescription().equals(description)
*/
public void add(int upc, String description) {
ProductSpecification spec = new ProductSpecification(upc, description);
specs.put(new Integer(upc), spec);
}
}
ProductCatalog tests
############################################
#
# add/isKnown
#
test test-1a {add boundary case, no products} {
set catalog [java::new ProductCatalog]
$catalog isKnown 123
} {0}
test test-1b {add boundary case, 1 value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr
$catalog isKnown 123
} {1}
test test-1c {add boundary case, 1st value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr1
$catalog add 234 descr2
$catalog add 345 desrc3
$catalog isKnown 123
} {1}
test test-1d {add boundary case, 1st value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr1
$catalog add 234 descr2
$catalog add 345 desrc3
$catalog isKnown 123
} {1}
test test-1e {add boundary case, last value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr1
$catalog add 234 descr2
$catalog add 345 desrc3
$catalog isKnown 345
} {1}
test test-1f {add typical case, typical present value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr1
$catalog add 234 descr2
$catalog add 345 desrc3
$catalog add 456 desrc4
$catalog add 567 desrc5
$catalog isKnown 345
} {1}
test test-1g {add typical case, typical missing value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr1
$catalog add 234 descr2
$catalog add 345 desrc3
$catalog add 456 desrc4
$catalog add 567 desrc5
$catalog isKnown 876
} {0}
########################################################
#
# add/getProductSpec
#
test test-2a {add boundary case, 1 value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr
set spec [$catalog getSpecification 123]
set upc [$spec getUpc]
set descr [$spec getDescription]
set result "$upc $descr"
} {123 descr}
test test-2bb {add boundary case, 1st value} {
set catalog [java::new ProductCatalog]
$catalog add 123 descr1
$catalog add 234 descr2
$catalog add 345 desrc3
set spec [$catalog getSpecification 123]
set upc [$spec getUpc]
set descr [$spec getDescription]
set result "$upc $descr"
} {123 descr1}
Jan Newmarch (http://pandonia.canberra.edu.au)
jan@ise.canberra.edu.au
Last modified: Mon Sep 4 15:11:19 EST 2000
Copyright ©Jan Newmarch