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 Apache Mina Project (dev@mina.apache.org)
31   * @version $Rev: $, $Date:  $
32   */
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  }