    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.codec.textline;
    21
    22  import java.net.SocketAddress;
    23  import java.nio.charset.Charset;
    24
    25  import junit.framework.Assert;
    26  import junit.framework.TestCase;
    27
    28  import org.apache.mina.common.ByteBuffer;
    29  import org.apache.mina.common.IoFilterChain;
    30  import org.apache.mina.common.IoHandler;
    31  import org.apache.mina.common.IoServiceConfig;
    32  import org.apache.mina.common.IoSession;
    33  import org.apache.mina.common.IoService;
    34  import org.apache.mina.common.IoSessionConfig;
    35  import org.apache.mina.common.TransportType;
    36  import org.apache.mina.common.WriteFuture;
    37  import org.apache.mina.common.support.BaseIoSession;
    38  import org.apache.mina.filter.codec.support.SimpleProtocolEncoderOutput;
    39
    40  /**
    41   * Tests {@link TextLineEncoder}.
    42   *
    43   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    44   * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $
    45   */
    46  public class TextLineEncoderTest extends TestCase {
    47      public static void main(String[] args) {
    48          junit.textui.TestRunner.run(TextLineEncoderTest.class);
    49      }
    50
    51      public void testEncode() throws Exception {
    52          TextLineEncoder encoder = new TextLineEncoder(Charset.forName("UTF-8"),
    53                  LineDelimiter.WINDOWS);
    54          IoSession session = new DummySession();
    55          SimpleProtocolEncoderOutput out = new SimpleProtocolEncoderOutput() {
    56              @Override
    57              protected WriteFuture doFlush(ByteBuffer buf) {
    58                  return null;
    59              }
    60          };
    61
    62          encoder.encode(session, "ABC", out);
    63          Assert.assertEquals(1, out.getBufferQueue().size());
    64          ByteBuffer buf = out.getBufferQueue().poll();
    65          Assert.assertEquals(5, buf.remaining());
    66          Assert.assertEquals('A', buf.get());
    67          Assert.assertEquals('B', buf.get());
    68          Assert.assertEquals('C', buf.get());
    69          Assert.assertEquals('\r', buf.get());
    70          Assert.assertEquals('\n', buf.get());
    71      }
    72
    73      private static class DummySession extends BaseIoSession {
    74          @Override
    75          protected void updateTrafficMask() {
    76          }
    77
    78          public IoService getService() {
    79              return null;
    80          }
    81
    82          public IoServiceConfig getServiceConfig() {
    83              return null;
    84          }
    85
    86          public IoHandler getHandler() {
    87              return null;
    88          }
    89
    90          public IoFilterChain getFilterChain() {
    91              return null;
    92          }
    93
    94          public TransportType getTransportType() {
    95              return null;
    96          }
    97
    98          public SocketAddress getRemoteAddress() {
    99              return null;
    100         }
    101
    102         public SocketAddress getLocalAddress() {
    103             return null;
    104         }
    105
    106         public int getScheduledWriteRequests() {
    107             return 0;
    108         }
    109
    110         public IoSessionConfig getConfig() {
    111             return null;
    112         }
    113
    114         public SocketAddress getServiceAddress() {
    115             return null;
    116         }
    117
    118         public int getScheduledWriteBytes() {
    119             return 0;
    120         }
    121     }
    122 }