001 /*
002 * Created on Jun 28, 2004
003 *
004 * To change the template for this generated file go to
005 * Window - Preferences - Java - Code Generation - Code and Comments
006 */
007 package biz.xsoftware.examples.listener;
008
009 import junit.framework.TestCase;
010 import biz.xsoftware.mock.CalledMethod;
011 import biz.xsoftware.mock.MockObjectFactory;
012 import biz.xsoftware.mock.MockObject;
013
014 /**
015 * This example has the following tests
016 *
017 * testBasicListener
018 *
019 * Tests when a user is added to the system, we get an event
020 * and tests when he is deleted, we get an event.
021 *
022 * testAddUserTwice
023 *
024 * This test that when I add a user that is already added, we do
025 * not fire a second event telling client systems the user was
026 * added again which would be a lie
027 *
028 * @author Dean Hiller
029 */
030 public class TestExample extends TestCase {
031
032 private MockObject mockListener;
033 private UserService sysUnderTest;
034
035
036 /**
037 * @showcode
038 */
039 public TestExample(String name) {
040 super(name);
041 }
042 /**
043 * @showcode
044 */
045 @Override
046 public void setUp() {
047 mockListener = MockObjectFactory.createMock(UserListener.class);
048
049 sysUnderTest = new UserServiceImpl();
050 }
051 @Override
052 public void tearDown() {
053
054 }
055
056 /**
057 * Tests when a user is added to the system, we get an event
058 * and tests when he is deleted, we get an event.
059 *
060 * @showcode
061 * @see UserServiceImpl#addLegacyListener
062 * @see LegacySystemListener#legacyEventOccurred
063 */
064 public void testBasicListener() {
065 sysUnderTest.addUserListener((UserListener)mockListener);
066
067 String user = "abc";
068 sysUnderTest.addUser(user);
069
070 CalledMethod method = mockListener.expect("userAdded");
071 String actualUser = (String)method.getAllParams()[0];
072 assertEquals(user, actualUser);
073
074 sysUnderTest.deleteUser(user);
075 String actualUser2 = (String)method.getAllParams()[0];
076 assertEquals(user, actualUser2);
077 }
078
079 /**
080 * This test that when I add a user that is already added, we do
081 * not fire a second event telling client systems the user was
082 * added again which would be a lie
083 */
084 public void testAddUserTwice() {
085 sysUnderTest.addUserListener((UserListener)mockListener);
086
087 String user = "abc";
088 sysUnderTest.addUser(user);
089
090 CalledMethod method = mockListener.expect("userAdded");
091 String actualUser = (String)method.getAllParams()[0];
092 assertEquals(user, actualUser);
093
094 try {
095 sysUnderTest.addUser(user);
096 fail("Should have thrown IllegalArgumentException and did not");
097 }catch(IllegalArgumentException e) {
098 }
099
100 mockListener.expect(MockObject.NONE);
101 }
102 }
|