1 19 package org.apache.mina.example.haiku; 20 21 import java.util.Collections ; 22 import java.util.List ; 23 24 import org.apache.mina.common.IoFilter; 25 import org.apache.mina.common.IoSession; 26 import org.jmock.Mock; 27 import org.jmock.MockObjectTestCase; 28 29 33 public class ToHaikuIoFilterTest extends MockObjectTestCase { 34 private IoFilter filter; 35 36 @Override 37 protected void setUp() throws Exception { 38 super.setUp(); 39 40 filter = new ToHaikuIoFilter(); 41 } 42 43 public void testThreeStringsMakesAHaiku() throws Exception { 44 Mock list = mock(List .class); 45 list.expects(once()).method("add").with(eq("two")).will( 46 returnValue(true)); 47 list.expects(once()).method("add").with(eq("three")).will( 48 returnValue(true)); 49 list.expects(once()).method("toArray").with(isA(String [].class)).will( 50 returnValue(new String [] { "one", "two", "three" })); 51 list.expects(exactly(2)).method("size").will( 52 onConsecutiveCalls(returnValue(2), returnValue(3))); 53 54 Mock session = mock(IoSession.class); 55 session.expects(exactly(3)).method("getAttribute").with(eq("phrases")) 56 .will( 57 onConsecutiveCalls(returnValue(null), returnValue(list 58 .proxy()), returnValue(list.proxy()), 59 returnValue(list.proxy()))); 60 session.expects(exactly(1)).method("setAttribute").with(eq("phrases"), 61 eq(Collections.emptyList())); 62 session.expects(exactly(1)).method("removeAttribute").with( 63 eq("phrases")); 64 65 IoSession sessionProxy = (IoSession) session.proxy(); 66 67 Mock nextFilter = mock(IoFilter.NextFilter.class); 68 nextFilter.expects(once()).method("messageReceived").with( 69 eq(sessionProxy), eq(new Haiku("one", "two", "three"))); 70 71 IoFilter.NextFilter nextFilterProxy = (IoFilter.NextFilter) nextFilter 72 .proxy(); 73 74 filter.messageReceived(nextFilterProxy, sessionProxy, "one"); 75 filter.messageReceived(nextFilterProxy, sessionProxy, "two"); 76 filter.messageReceived(nextFilterProxy, sessionProxy, "three"); 77 } 78 79 } 80 | Popular Tags |