001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.mina.example.haiku; 020 021import java.util.Collections; 022import java.util.List; 023 024import org.apache.mina.core.filterchain.IoFilter; 025import org.apache.mina.core.session.IoSession; 026import org.jmock.Mock; 027import org.jmock.MockObjectTestCase; 028 029/** 030 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 031 */ 032public class ToHaikuIoFilterTest extends MockObjectTestCase { 033 private IoFilter filter; 034 035 @Override 036 protected void setUp() throws Exception { 037 super.setUp(); 038 039 filter = new ToHaikuIoFilter(); 040 } 041 042 public void testThreeStringsMakesAHaiku() throws Exception { 043 Mock list = mock(List.class); 044 list.expects(once()).method("add").with(eq("two")).will( 045 returnValue(true)); 046 list.expects(once()).method("add").with(eq("three")).will( 047 returnValue(true)); 048 list.expects(once()).method("toArray").with(isA(String[].class)).will( 049 returnValue(new String[] { "one", "two", "three" })); 050 list.expects(exactly(2)).method("size").will( 051 onConsecutiveCalls(returnValue(2), returnValue(3))); 052 053 Mock session = mock(IoSession.class); 054 session.expects(exactly(3)).method("getAttribute").with(eq("phrases")) 055 .will( 056 onConsecutiveCalls(returnValue(null), returnValue(list 057 .proxy()), returnValue(list.proxy()), 058 returnValue(list.proxy()))); 059 session.expects(exactly(1)).method("setAttribute").with(eq("phrases"), 060 eq(Collections.emptyList())); 061 session.expects(exactly(1)).method("removeAttribute").with( 062 eq("phrases")); 063 064 IoSession sessionProxy = (IoSession) session.proxy(); 065 066 Mock nextFilter = mock(IoFilter.NextFilter.class); 067 nextFilter.expects(once()).method("messageReceived").with( 068 eq(sessionProxy), eq(new Haiku("one", "two", "three"))); 069 070 IoFilter.NextFilter nextFilterProxy = (IoFilter.NextFilter) nextFilter 071 .proxy(); 072 073 filter.messageReceived(nextFilterProxy, sessionProxy, "one"); 074 filter.messageReceived(nextFilterProxy, sessionProxy, "two"); 075 filter.messageReceived(nextFilterProxy, sessionProxy, "three"); 076 } 077 078}