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   */
20  package org.apache.mina.filter.util;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.mina.core.filterchain.IoFilter;
28  import org.apache.mina.core.filterchain.IoFilterEvent;
29  import org.apache.mina.core.session.DummySession;
30  import org.apache.mina.core.session.IdleStatus;
31  import org.apache.mina.core.session.IoEventType;
32  import org.apache.mina.core.session.IoSession;
33  import org.apache.mina.core.write.DefaultWriteRequest;
34  import org.apache.mina.core.write.WriteRequest;
35  import org.easymock.EasyMock;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  /**
40   * Tests {@link CommonEventFilter}.
41   *
42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
43   */
44  
45  public class WrappingFilterTest {
46      private IoSession session;
47  
48      private IoFilter.NextFilter nextFilter;
49  
50      @Before
51      public void setUp() throws Exception {
52          /*
53           * Create the mocks.
54           */
55          session = new DummySession();
56          nextFilter = EasyMock.createMock(IoFilter.NextFilter.class);
57          //nextFilter = (IoFilter.NextFilter) mockNextFilter.getClass();
58      }
59  
60      @Test
61      public void testFilter() throws Exception {
62          MyWrappingFilter wrappingFilter = new MyWrappingFilter();
63  
64          /* record expectations */
65          Object message1 = "message one";
66          Object message2 = "message two";
67          WriteRequest writeRequest1 = new DefaultWriteRequest("test1");
68          WriteRequest writeRequest2 = new DefaultWriteRequest("test2");
69          Throwable cause = new Throwable("testing");
70  
71          nextFilter.sessionCreated(session);
72          nextFilter.sessionOpened(session);
73          nextFilter.sessionIdle(session, IdleStatus.READER_IDLE);
74          nextFilter.messageReceived(session, message1);
75          nextFilter.messageSent(session, writeRequest1);
76          nextFilter.messageSent(session, writeRequest2);
77          nextFilter.messageReceived(session, message2);
78          nextFilter.filterWrite(session, writeRequest1);
79          nextFilter.filterClose(session);
80          nextFilter.exceptionCaught(session, cause);
81          nextFilter.sessionClosed(session);
82  
83          /* replay */
84          EasyMock.replay(nextFilter);
85          wrappingFilter.sessionCreated(nextFilter, session);
86          wrappingFilter.sessionOpened(nextFilter, session);
87          wrappingFilter.sessionIdle(nextFilter, session, IdleStatus.READER_IDLE);
88          wrappingFilter.messageReceived(nextFilter, session, message1);
89          wrappingFilter.messageSent(nextFilter, session, writeRequest1);
90          wrappingFilter.messageSent(nextFilter, session, writeRequest2);
91          wrappingFilter.messageReceived(nextFilter, session, message2);
92          wrappingFilter.filterWrite(nextFilter, session, writeRequest1);
93          wrappingFilter.filterClose(nextFilter, session);
94          wrappingFilter.exceptionCaught(nextFilter, session, cause);
95          wrappingFilter.sessionClosed(nextFilter, session);
96  
97          /* verify */
98          EasyMock.verify(nextFilter);
99  
100         /* check event lists */
101         assertEquals(11, wrappingFilter.eventsBefore.size());
102         assertEquals(IoEventType.SESSION_CREATED, wrappingFilter.eventsBefore.get(0));
103         assertEquals(IoEventType.SESSION_OPENED, wrappingFilter.eventsBefore.get(1));
104         assertEquals(IoEventType.SESSION_IDLE, wrappingFilter.eventsBefore.get(2));
105         assertEquals(IoEventType.MESSAGE_RECEIVED, wrappingFilter.eventsBefore.get(3));
106         assertEquals(IoEventType.MESSAGE_SENT, wrappingFilter.eventsBefore.get(4));
107         assertEquals(IoEventType.MESSAGE_SENT, wrappingFilter.eventsBefore.get(5));
108         assertEquals(IoEventType.MESSAGE_RECEIVED, wrappingFilter.eventsBefore.get(6));
109         assertEquals(IoEventType.WRITE, wrappingFilter.eventsBefore.get(7));
110         assertEquals(IoEventType.CLOSE, wrappingFilter.eventsBefore.get(8));
111         assertEquals(IoEventType.EXCEPTION_CAUGHT, wrappingFilter.eventsBefore.get(9));
112         assertEquals(IoEventType.SESSION_CLOSED, wrappingFilter.eventsBefore.get(10));
113         assertEquals(wrappingFilter.eventsBefore, wrappingFilter.eventsAfter);
114     }
115 
116     private static class MyWrappingFilter extends CommonEventFilter {
117         List<IoEventType> eventsBefore = new ArrayList<IoEventType>();
118 
119         List<IoEventType> eventsAfter = new ArrayList<IoEventType>();
120 
121         /**
122          * Default constructor
123          */
124         public MyWrappingFilter() {
125             super();
126         }
127 
128         @Override
129         protected void filter(IoFilterEvent event) {
130             eventsBefore.add(event.getType());
131             event.fire();
132             eventsAfter.add(event.getType());
133         }
134     }
135 }