View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.mina.example.haiku;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.mina.core.filterchain.IoFilter;
25  import org.apache.mina.core.session.IoSession;
26  import org.jmock.Mock;
27  import org.jmock.MockObjectTestCase;
28  
29  /**
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public class ToHaikuIoFilterTest extends MockObjectTestCase {
33      private IoFilter filter;
34  
35      @Override
36      protected void setUp() throws Exception {
37          super.setUp();
38  
39          filter = new ToHaikuIoFilter();
40      }
41  
42      public void testThreeStringsMakesAHaiku() throws Exception {
43          Mock list = mock(List.class);
44          list.expects(once()).method("add").with(eq("two")).will(
45                  returnValue(true));
46          list.expects(once()).method("add").with(eq("three")).will(
47                  returnValue(true));
48          list.expects(once()).method("toArray").with(isA(String[].class)).will(
49                  returnValue(new String[] { "one", "two", "three" }));
50          list.expects(exactly(2)).method("size").will(
51                  onConsecutiveCalls(returnValue(2), returnValue(3)));
52  
53          Mock session = mock(IoSession.class);
54          session.expects(exactly(3)).method("getAttribute").with(eq("phrases"))
55                  .will(
56                          onConsecutiveCalls(returnValue(null), returnValue(list
57                                  .proxy()), returnValue(list.proxy()),
58                                  returnValue(list.proxy())));
59          session.expects(exactly(1)).method("setAttribute").with(eq("phrases"),
60                  eq(Collections.emptyList()));
61          session.expects(exactly(1)).method("removeAttribute").with(
62                  eq("phrases"));
63  
64          IoSession sessionProxy = (IoSession) session.proxy();
65  
66          Mock nextFilter = mock(IoFilter.NextFilter.class);
67          nextFilter.expects(once()).method("messageReceived").with(
68                  eq(sessionProxy), eq(new Haiku("one", "two", "three")));
69  
70          IoFilter.NextFilter nextFilterProxy = (IoFilter.NextFilter) nextFilter
71                  .proxy();
72  
73          filter.messageReceived(nextFilterProxy, sessionProxy, "one");
74          filter.messageReceived(nextFilterProxy, sessionProxy, "two");
75          filter.messageReceived(nextFilterProxy, sessionProxy, "three");
76      }
77  
78  }