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 12:19:00 +0900 (금, 13  7월 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              protected WriteFuture doFlush(ByteBuffer buf) {
57                  return null;
58              }
59          };
60  
61          encoder.encode(session, "ABC", out);
62          Assert.assertEquals(1, out.getBufferQueue().size());
63          ByteBuffer buf = (ByteBuffer) out.getBufferQueue().pop();
64          Assert.assertEquals(5, buf.remaining());
65          Assert.assertEquals('A', buf.get());
66          Assert.assertEquals('B', buf.get());
67          Assert.assertEquals('C', buf.get());
68          Assert.assertEquals('\r', buf.get());
69          Assert.assertEquals('\n', buf.get());
70      }
71  
72      private static class DummySession extends BaseIoSession {
73          protected void updateTrafficMask() {
74          }
75  
76          public IoService getService() {
77              return null;
78          }
79  
80          public IoServiceConfig getServiceConfig() {
81              return null;
82          }
83  
84          public IoHandler getHandler() {
85              return null;
86          }
87  
88          public IoFilterChain getFilterChain() {
89              return null;
90          }
91  
92          public TransportType getTransportType() {
93              return null;
94          }
95  
96          public SocketAddress getRemoteAddress() {
97              return null;
98          }
99  
100         public SocketAddress getLocalAddress() {
101             return null;
102         }
103 
104         public int getScheduledWriteRequests() {
105             return 0;
106         }
107 
108         public IoSessionConfig getConfig() {
109             return null;
110         }
111 
112         public SocketAddress getServiceAddress() {
113             return null;
114         }
115 
116         public int getScheduledWriteBytes() {
117             return 0;
118         }
119     }
120 }