Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/examples/mock2/basic
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5534/input/javasrc/biz/xsoftware/examples/mock2/basic
Added Files:
TestExample.java SysUnderTest.java CreditAuthorizationSvc.java
PurchaseException.java GiftCardAccountSvc.java
Log Message:
change test package and design.xml to follow the design
--- NEW FILE: PurchaseException.java ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.mock2.basic;
/**
* An Exception the SysUnderTest(WebStore) generates when a purchase goes
* bad.
* @author Dean Hiller
*/
public class PurchaseException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param e
*/
public PurchaseException(Throwable e) {
super(e);
}
}
--- NEW FILE: GiftCardAccountSvc.java ---
/*
* Created on Jul 3, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.mock2.basic;
/**
* Interface to what would normally be a real GiftCarAccountSvc.
* @author Dean Hiller
*/
public interface GiftCardAccountSvc {
public void putMoneyOnCard(int cardId, double money);
public void takeMoneyFromCard(int cardId, double money);
}
--- NEW FILE: TestExample.java ---
package biz.xsoftware.examples.mock2.basic;
import junit.framework.TestCase;
import biz.xsoftware.mock2.CalledMethod;
import biz.xsoftware.mock2.MockObject;
import biz.xsoftware.mock2.MockObjectFactory;
public class TestExample extends TestCase{
private SysUnderTest sysUnderTest;
private MockObject mockCreditSvc;
private MockObject mockGiftSvc;
public TestExample(String name) {
super(name);
}
public void setUp() {
mockCreditSvc = MockObjectFactory.createMock(CreditAuthorizationSvc.class);
mockGiftSvc = MockObjectFactory.createMock(GiftCardAccountSvc.class);
CreditAuthorizationSvc credit = (CreditAuthorizationSvc)mockCreditSvc;
GiftCardAccountSvc gift = (GiftCardAccountSvc)mockGiftSvc;
sysUnderTest = new SysUnderTest(credit, gift);
}
public void testBasicSysUnderTest() {
String user = "user1";
double amount = 340.99;
CalledMethod authorize=mockCreditSvc.expect("authorize");
sysUnderTest.purchase("itemA", user, amount);
mockCreditSvc.verify();
assertEquals("user should be the same",user,authorize.getParameters()[0]);
assertEquals("Amount should have been the same", new Double(amount), authorize.getParameters()[1]);
}
}
--- NEW FILE: CreditAuthorizationSvc.java ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.mock2.basic;
/**
* Interface to what would normally be a real Credit Authorization Service.
*
* @author Dean Hiller
*/
public interface CreditAuthorizationSvc {
/**
* @param user
* @param usDollars
*/
public void authorize(String user, double usDollars);
public void takeMoney(String user, double usDollars);
public void returnMoney(String user, double usDollars);
}
--- NEW FILE: SysUnderTest.java ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.mock2.basic;
/**
* The SysUnderTest here is a WebStore or something which needs
* to authorize purchases.
*/
public class SysUnderTest {
private CreditAuthorizationSvc creditSvc;
private GiftCardAccountSvc giftSvc;
/**
* @showcode
*/
public SysUnderTest(CreditAuthorizationSvc c, GiftCardAccountSvc g) {
creditSvc = c;
giftSvc = g;
}
/**
* @showcode
*/
public void purchase(String item, String user, double usDollars) {
try {
creditSvc.authorize(user, usDollars);
} catch(RuntimeException e) {
throw new PurchaseException(e);
}
//if authorize succeeded, go delete an item from the
//Inventory as we successfully sold it.
}
/**
* @showcode
*/
public void buyGiftCard(String user, double usDollars) {
creditSvc.takeMoney(user, usDollars);
try {
giftSvc.putMoneyOnCard(456, usDollars);
} catch(Throwable e) {
creditSvc.returnMoney(user, usDollars);
throw new PurchaseException(e);
}
return;
}
}
|