QuoteEngine implements IQuoteEngine { @Override public void fireLevel1Quote(final ILevel1Quote quote) { synchronized (level1ListenerMap) { List<Level1QuoteListener> listeners = level1ListenerMap.get(quote.getTicker()); if (listeners == null) { return; } for (final Level1QuoteListener listener : listeners) { try { synchronized (listeners) { Thread thread = new Thread(new Runnable() { public void run() { listener.quoteRecieved(quote); } }); thread.start(); } } catch (Exception ex) { } } } } QuoteEngine(); void addErrorListener(ErrorListener listener); void removeErrorListener(ErrorListener listener); void fireErrorEvent(QuoteError error); void subscribeLevel1(Ticker ticker, Level1QuoteListener listener); void unsubscribeLevel1(Ticker ticker, Level1QuoteListener listener); @Override void fireLevel1Quote(final ILevel1Quote quote); @Override void fireMarketDepthQuote(ILevel2Quote quote); void subscribeMarketDepth(Ticker ticker, Level2QuoteListener listener); void unsubscribeMarketDepth(Ticker ticker, Level2QuoteListener listener);  }
@Test public void testFireLevel1Quote() throws Exception { QuoteEngine quoteEngine = createNewQuoteEngine(); final ILevel1Quote mockQuote = mock( ILevel1Quote.class ); final Map<Ticker, List<Level1QuoteListener>> listenerMap = new HashMap<>(); final Ticker ticker = new StockTicker( "ABC" ); final Level1QuoteListener mockListener = mock( Level1QuoteListener.class ); final List<Level1QuoteListener> listenerList = new ArrayList<>(); listenerList.add( mockListener ); listenerMap.put(ticker, listenerList); quoteEngine.level1ListenerMap = listenerMap; when(mockQuote.getTicker()).thenReturn(ticker); quoteEngine.fireLevel1Quote(mockQuote); Thread.sleep(1000); verify(mockListener).quoteRecieved(mockQuote); }
@Test public void testFireLevel1Quote_ThrowsException() { QuoteEngine quoteEngine = createNewQuoteEngine(); final ILevel1Quote mockQuote = mock( ILevel1Quote.class ); final Map mockLevel1ListenerMap = mock( Map.class ); final Ticker ticker = new StockTicker( "ABC" ); final Level1QuoteListener mockListener = mock( Level1QuoteListener.class ); final List<Level1QuoteListener> mockListenerList = new ArrayList<Level1QuoteListener>(); mockListenerList.add( mockListener ); quoteEngine.level1ListenerMap = mockLevel1ListenerMap; when(mockQuote.getTicker()).thenReturn(ticker); when(mockLevel1ListenerMap.get(ticker)).thenReturn(mockListenerList); doThrow(new IllegalStateException("bogus")).when(mockListener).quoteRecieved(mockQuote); try { quoteEngine.fireLevel1Quote(mockQuote); } catch( Exception ex ) { fail(); } }
@Test public void testFireLevel1Quote_NoListeners() { QuoteEngine quoteEngine = createNewQuoteEngine(); final ILevel1Quote mockQuote = mockery.mock( ILevel1Quote.class ); final Map mockLevel1ListenerMap = mockery.mock( Map.class ); final Ticker ticker = new StockTicker( "ABC" ); quoteEngine.level1ListenerMap = mockLevel1ListenerMap; mockery.checking( new Expectations() {{ one(mockQuote).getTicker(); will(returnValue(ticker)); one(mockLevel1ListenerMap).get(ticker); will(returnValue(null)); }}); quoteEngine.fireLevel1Quote(mockQuote); mockery.assertIsSatisfied(); }