001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.filter.buffer; 021 022import static org.junit.Assert.assertEquals; 023 024import org.apache.mina.core.buffer.IoBuffer; 025import org.apache.mina.core.filterchain.IoFilterAdapter; 026import org.apache.mina.core.session.DummySession; 027import org.apache.mina.core.session.IoSession; 028import org.apache.mina.core.write.WriteRequest; 029import org.apache.mina.filter.logging.LoggingFilter; 030import org.junit.Test; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Tests {@link BufferedWriteFilter}. 036 * 037 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 038 * @since MINA 2.0.0-M2 039 */ 040public class BufferedWriteFilterTest { 041 static final Logger LOGGER = LoggerFactory.getLogger(BufferedWriteFilterTest.class); 042 043 @Test 044 public void testNonExpandableBuffer() throws Exception { 045 IoBuffer dest = IoBuffer.allocate(1); 046 assertEquals(false, dest.isAutoExpand()); 047 } 048 049 @Test 050 public void testBasicBuffering() { 051 DummySession sess = new DummySession(); 052 sess.getFilterChain().addFirst("peer", new IoFilterAdapter() { 053 054 private int counter; 055 056 @Override 057 public void filterClose(NextFilter nextFilter, IoSession session) throws Exception { 058 LOGGER.debug("Filter closed !"); 059 assertEquals(3, counter); 060 } 061 062 @Override 063 public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) 064 throws Exception { 065 LOGGER.debug("New buffered message written !"); 066 counter++; 067 try { 068 IoBuffer buf = (IoBuffer) writeRequest.getMessage(); 069 if (counter == 3) { 070 assertEquals(1, buf.limit()); 071 assertEquals(0, buf.get()); 072 } else { 073 assertEquals(10, buf.limit()); 074 } 075 } catch (Exception ex) { 076 throw new AssertionError("Wrong message type"); 077 } 078 } 079 080 }); 081 sess.getFilterChain().addFirst("logger", new LoggingFilter()); 082 BufferedWriteFilter bFilter = new BufferedWriteFilter(10); 083 sess.getFilterChain().addLast("buffer", bFilter); 084 085 IoBuffer data = IoBuffer.allocate(1); 086 for (byte i = 0; i < 20; i++) { 087 data.put((byte) (0x30 + i)); 088 data.flip(); 089 sess.write(data); 090 data.clear(); 091 } 092 093 // Add one more byte to overflow the final buffer 094 data.put((byte) 0); 095 data.flip(); 096 sess.write(data); 097 098 // Flush the final byte 099 bFilter.flush(sess); 100 101 sess.close(true); 102 } 103}