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.buffer;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import org.apache.mina.core.buffer.IoBuffer;
25  import org.apache.mina.core.filterchain.IoFilterAdapter;
26  import org.apache.mina.core.session.DummySession;
27  import org.apache.mina.core.session.IoSession;
28  import org.apache.mina.core.write.WriteRequest;
29  import org.apache.mina.filter.logging.LoggingFilter;
30  import org.junit.Test;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Tests {@link BufferedWriteFilter}.
36   *
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   * @since MINA 2.0.0-M2
39   */
40  public class BufferedWriteFilterTest {
41      static final Logger LOGGER = LoggerFactory.getLogger(BufferedWriteFilterTest.class);
42  
43      @Test
44      public void testNonExpandableBuffer() throws Exception {
45          IoBuffer dest = IoBuffer.allocate(1);
46          assertEquals(false, dest.isAutoExpand());
47      }
48  
49      @Test
50      public void testBasicBuffering() {
51          DummySession sess = new DummySession();
52          sess.getFilterChain().addFirst("peer", new IoFilterAdapter() {
53  
54              private int counter;
55  
56              @Override
57              public void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
58                  if (LOGGER.isDebugEnabled()) {
59                      LOGGER.debug("Filter closed !");
60                  }
61                  
62                  assertEquals(3, counter);
63              }
64  
65              @Override
66              public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
67                      throws Exception {
68                  if (LOGGER.isDebugEnabled()) {
69                      LOGGER.debug("New buffered message written !");
70                  }
71                  
72                  counter++;
73                  try {
74                      IoBuffer buf = (IoBuffer) writeRequest.getMessage();
75                      if (counter == 3) {
76                          assertEquals(1, buf.limit());
77                          assertEquals(0, buf.get());
78                      } else {
79                          assertEquals(10, buf.limit());
80                      }
81                  } catch (Exception ex) {
82                      throw new AssertionError("Wrong message type");
83                  }
84              }
85  
86          });
87          sess.getFilterChain().addFirst("logger", new LoggingFilter());
88          BufferedWriteFilter bFilter = new BufferedWriteFilter(10);
89          sess.getFilterChain().addLast("buffer", bFilter);
90  
91          IoBuffer data = IoBuffer.allocate(1);
92          for (byte i = 0; i < 20; i++) {
93              data.put((byte) (0x30 + i));
94              data.flip();
95              sess.write(data);
96              data.clear();
97          }
98  
99          // Add one more byte to overflow the final buffer
100         data.put((byte) 0);
101         data.flip();
102         sess.write(data);
103 
104         // Flush the final byte
105         bFilter.flush(sess);
106 
107         sess.closeNow();
108     }
109 }