Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/basic
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24740/input/javasrc/biz/xsoftware/examples/basic
Added Files:
package.html SysUnderTest.java PurchaseException.java
GiftCardAccountSvc.java TestExample.java
CreditAuthorizationSvc.java
Log Message:
original commit of mocklib2 that will become mocklib3
--- NEW FILE: TestExample.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.basic;
import junit.framework.TestCase;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.mock.MockObject;
/**
* This is the class actually holding the test Examples for you to look at.
*
* @author Dean Hiller
*/
public class TestExample extends TestCase {
private SysUnderTest sysUnderTest;
private MockObject mockCreditSvc;
private MockObject mockGiftSvc;
/**
* @param name The name of the test
* @showcode
*/
public TestExample(String name) {
super(name);
}
/**
* @showcode
* @see junit.framework.TestCase#setUp()
*/
@Override
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);
}
/**
* Test that when we purchase an item from the SysUnderTest
* that the CreditAuthorization Svc is used to verify the
* customer has that amount of money on his credit card.
*
* @see SysUnderTest#purchase
* @showcode
*/
public void testBasicSysUnderTest() {
String user = "user1";
double amount = 340.99;
sysUnderTest.purchase("itemA", user, amount);
Object[] paramsToAuthorize = mockCreditSvc.expect("authorize").getAllParams();
assertEquals("User should be the same", user, paramsToAuthorize[0]);
assertEquals("Amount should have been the same", new Double(amount), paramsToAuthorize[1]);
}
/**
* In this test we test that a failure from CreditAuthSvc
* results in the correct StoreUnderTest behavior which in this
* instance is to throw an exception to the user.
*
* @see SysUnderTest#purchase
* @showcode
*/
public void testFailureOfAuthorization() {
mockCreditSvc.addThrowException("authorize", new RuntimeException("test robustness of StoreUnderTest"));
String user = "user1";
double amount = 340.99;
try {
sysUnderTest.purchase("itemA", user, amount);
fail("Expecting exactly the IllegalStateException for some odd reason(didn't want to define my own)");
} catch(PurchaseException e) {}
}
/**
* Test that
* <ol>
* <li>Money is taken from users credit account</li>
* <li>Put money on card, but cause failure</li>
* <li>Assert that money is put back in users credit account</li>
* </ol>
*
* @see SysUnderTest#buyGiftCard
* @showcode
*/
public void testFailureOfBuyingGiftCard() {
mockGiftSvc.addThrowException("putMoneyOnCard", new RuntimeException("cause failure in subsystem"));
String user = "userXXX";
double amount = 40.01;
try {
sysUnderTest.buyGiftCard(user, amount);
fail("Should have thrown exception");
} catch(PurchaseException e) {}
//make sure takeMoney then return money was called which would happen
//in a failure case. Also could have made takeMoney ignored instead so
//we didn't test the takeMoney and just tested returnMoney.
CalledMethod[] methods = mockCreditSvc.expect("takeMoney", "returnMoney");
CalledMethod takeMoney = methods[0];
assertEquals("Took from correct user", user, takeMoney.getParameter(0));
assertEquals("Took 40 dollars", new Double(amount), takeMoney.getParameter(1));
CalledMethod returnMoney = methods[1];
assertEquals("Gave money back to correct user", user, returnMoney.getParameter(0));
assertEquals("amount put back on card is correct", new Double(amount), returnMoney.getParameter(1));
}
}
--- 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.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;
}
}
--- NEW FILE: package.html ---
<HTML>
<BODY>
Basic Example using a WebStore(See bottom of page for description).
<br/><br/>
Basic Example using a WebStore(SysUnderTest) to test the following features of the webstore
<OL>
<LI><a href="TestExample.html#testBasicSysUnderTest()">testBasicSysUnderTest</a>
- Tests that when purchase is made, amount is authorized with a credit service</LI>
<LI><a href="TestExample.html#testFailureOfAuthorization()">testFailureOfAuthorization</a>
- Tests that if authorization fails, SysUnderTest throws a PurchaseException to indicate it failed</LI>
<LI><a href="TestExample.html#testFailureOfBuyingGiftCard()">testFailureOfBuyingGiftCard</a>
- A more complicated failure that tests when putting money on a gift card through the GiftCard Svc fails, the money is returned to the credit account</LI>
</OL>
</BODY>
</HTML>
--- 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.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: 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.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: 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.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);
}
|