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
42              .getLogger(BufferedWriteFilterTest.class);
43  
44      @Test
45      public void testNonExpandableBuffer() throws Exception {
46          IoBuffer dest = IoBuffer.allocate(1);
47          assertEquals(false, dest.isAutoExpand());
48      }
49  
50      @Test
51      public void testBasicBuffering() {
52          DummySession sess = new DummySession();
53          sess.getFilterChain().addFirst("peer", new IoFilterAdapter() {
54  
55              private int counter;
56  
57              @Override
58              public void filterClose(NextFilter nextFilter, IoSession session)
59                      throws Exception {
60                  LOGGER.debug("Filter closed !");
61                  assertEquals(3, counter);
62              }
63  
64              @Override
65              public void filterWrite(NextFilter nextFilter, IoSession session,
66                      WriteRequest writeRequest) throws Exception {
67                  LOGGER.debug("New buffered message written !");
68                  counter++;
69                  try {
70                      IoBuffer buf = (IoBuffer) writeRequest.getMessage();
71                      if (counter == 3) {
72                          assertEquals(1, buf.limit());
73                          assertEquals(0, buf.get());
74                      } else {
75                          assertEquals(10, buf.limit());
76                      }
77                  } catch (Exception ex) {
78                      throw new AssertionError("Wrong message type");
79                  }
80              }
81  
82          });
83          sess.getFilterChain().addFirst("logger", new LoggingFilter());
84          BufferedWriteFilter bFilter = new BufferedWriteFilter(10);
85          sess.getFilterChain().addLast("buffer", bFilter);
86  
87          IoBuffer data = IoBuffer.allocate(1);
88          for (byte i = 0; i < 20; i++) {
89              data.put((byte) (0x30 + i));
90              data.flip();
91              sess.write(data);
92              data.clear();
93          }
94  
95          // Add one more byte to overflow the final buffer
96          data.put((byte) 0);
97          data.flip();
98          sess.write(data);
99          
100         // Flush the final byte
101         bFilter.flush(sess);
102         
103         sess.close(true);
104     }
105 }