Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/listener
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24740/input/javasrc/biz/xsoftware/examples/listener
Added Files:
LegacySystemListener.java package.html TestExample.java
LegacySystem.java SysUnderTest.java
Log Message:
original commit of mocklib2 that will become mocklib3
--- NEW FILE: package.html ---
<HTML>
<BODY>
Examples which mock out listeners to test for events under the correct circumstances.
</BODY>
</HTML>
--- 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.listener;
import junit.framework.TestCase;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.mock.MockObject;
/**
* JUnit Suite of TestCases for demonstrating mocking out listeners to
* test for events under certain circumstances.
*
* @author Dean Hiller
*/
public class TestExample extends TestCase {
private MockObject mockLegacy;
private MockObject mockListener1;
private MockObject mockListener2;
private SysUnderTest sysUnderTest;
private LegacySystemListener legacyListener;
/**
* @showcode
*/
public TestExample(String name) {
super(name);
}
/**
* @showcode
*/
@Override
public void setUp() {
mockLegacy = MockObjectFactory.createMock(LegacySystem.class);
mockListener1 = MockObjectFactory.createMock(LegacySystemListener.class);
mockListener2 = MockObjectFactory.createMock(LegacySystemListener.class);
sysUnderTest = new SysUnderTest((LegacySystem)mockLegacy);
Object[] params = mockLegacy.expect("setLegacyListener").getAllParams();
legacyListener = (LegacySystemListener)params[0];
}
@Override
public void tearDown() {
}
/**
* Tests that when the legacy system fires and event to it's only
* legacy listener, all listeners on the sysUnderTest that were added
* receive the event. No matter what the threading model, this test
* will pass since expect call will wait until the event happens. If
* the event never happens, the expectCall will timeout and throw an
* ExpectFailedException claiming it never got the event.
*
* @showcode
* @see SysUnderTest#addLegacyListener
* @see LegacySystemListener#legacyEventOccurred
*/
public void testBasicListener() {
sysUnderTest.addLegacyListener((LegacySystemListener)mockListener1);
sysUnderTest.addLegacyListener((LegacySystemListener)mockListener2);
//have legacy system fire event to it's one and only listener
legacyListener.legacyEventOccurred(5);
mockListener1.expect("legacyEventOccurred");
mockListener2.expect("legacyEventOccurred");
}
}
--- NEW FILE: LegacySystemListener.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.listener;
import java.util.EventListener;
/**
* Listener interface that listens for legacy events.
*
* @author Dean Hiller
*/
public interface LegacySystemListener extends EventListener {
public void legacyEventOccurred(int i);
}
--- NEW FILE: SysUnderTest.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.listener;
import javax.swing.event.EventListenerList;
/**
* The SysUnderTest here is an adapter to a legacy system. The SysUnderTest
* might be on a different thread or the same thread. For events from
* a legacy system, the SysUnderTest alot of the time may be on a different
* thread listening on some socket to the legacy system.
*
* @author Dean Hiller
*/
public class SysUnderTest {
private EventListenerList listenerList = new EventListenerList();
private LegacySystem legacySys;
public SysUnderTest(LegacySystem sys) {
legacySys = sys;
legacySys.setLegacyListener(new LegacyListener());
}
private class LegacyListener implements LegacySystemListener {
public void legacyEventOccurred(int i) {
fireLegacyEvent(i);
}
}
public void addLegacyListener(LegacySystemListener l) {
listenerList.add(LegacySystemListener.class, l);
}
public void removeLegacyListener(LegacySystemListener l) {
listenerList.remove(LegacySystemListener.class, l);
}
protected void fireLegacyEvent(int num) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==LegacySystemListener.class) {
// Lazily create the event:
((LegacySystemListener)listeners[i+1]).legacyEventOccurred(num);
}
}
}
}
--- NEW FILE: LegacySystem.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.listener;
/**
* Interface to what would normally be a real LegacySystemAdapter.
* @author Dean Hiller
*/
public interface LegacySystem {
/**
* @param listener
*/
void setLegacyListener(LegacySystemListener listener);
}
|