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 junit.framework.TestCase;
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.buffer.BufferedWriteFilter;
30  import org.apache.mina.filter.logging.LoggingFilter;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Tests {@link BufferedWriteFilter}.
36   *
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev$, $Date$
39   * @since MINA 2.0.0-M2
40   */
41  public class BufferedWriteFilterTest extends TestCase {
42      private final Logger logger = LoggerFactory
43              .getLogger(BufferedWriteFilterTest.class);
44  
45      public void testNonExpandableBuffer() throws Exception {
46          IoBuffer dest = IoBuffer.allocate(1);
47          assertEquals(false, dest.isAutoExpand());
48      }
49  
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)
58                      throws Exception {
59                  logger.debug("Filter closed !");
60                  assertEquals(3, counter);
61              }
62  
63              @Override
64              public void filterWrite(NextFilter nextFilter, IoSession session,
65                      WriteRequest writeRequest) throws Exception {
66                  logger.debug("New buffered message written !");
67                  counter++;
68                  try {
69                      IoBuffer buf = (IoBuffer) writeRequest.getMessage();
70                      if (counter == 3) {
71                          assertEquals(1, buf.limit());
72                          assertEquals(0, buf.get());
73                      } else {
74                          assertEquals(10, buf.limit());
75                      }
76                  } catch (Exception ex) {
77                      throw new AssertionError("Wrong message type");
78                  }
79              }
80  
81          });
82          sess.getFilterChain().addFirst("logger", new LoggingFilter());
83          BufferedWriteFilter bFilter = new BufferedWriteFilter(10);
84          sess.getFilterChain().addLast("buffer", bFilter);
85  
86          IoBuffer data = IoBuffer.allocate(1);
87          for (byte i = 0; i < 20; i++) {
88              data.put((byte) (0x30 + i));
89              data.flip();
90              sess.write(data);
91              data.clear();
92          }
93  
94          // Add one more byte to overflow the final buffer
95          data.put((byte) 0);
96          data.flip();
97          sess.write(data);
98          
99          // Flush the final byte
100         bFilter.flush(sess);
101         
102         sess.close(true);
103     }
104 }