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.directory.mitosis.service.protocol.codec;
21  
22  
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.util.Queue;
26  import java.util.concurrent.LinkedBlockingQueue;
27  
28  import junit.framework.Assert;
29  
30  import org.apache.directory.mitosis.service.protocol.message.BaseMessage;
31  import org.apache.directory.server.core.DefaultDirectoryService;
32  import org.apache.mina.common.ByteBuffer;
33  import org.apache.mina.common.IoFilterChain;
34  import org.apache.mina.common.IoHandler;
35  import org.apache.mina.common.IoService;
36  import org.apache.mina.common.IoServiceConfig;
37  import org.apache.mina.common.IoSession;
38  import org.apache.mina.common.IoSessionConfig;
39  import org.apache.mina.common.TransportType;
40  import org.apache.mina.common.WriteFuture;
41  import org.apache.mina.common.support.BaseIoSession;
42  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
43  import org.apache.mina.filter.codec.demux.MessageDecoder;
44  import org.apache.mina.filter.codec.demux.MessageEncoder;
45  import org.apache.mina.filter.codec.support.SimpleProtocolEncoderOutput;
46  import org.junit.BeforeClass;
47  import org.junit.Test;
48  
49  
50  public abstract class AbstractMessageCodecTest
51  {
52      private final BaseMessage message;
53      private final MessageEncoder encoder;
54      private final MessageDecoder decoder;
55  
56  
57      private static DefaultDirectoryService service;
58  
59      @BeforeClass
60      public static void setUp()
61      {
62          service = new DefaultDirectoryService();
63      }
64      
65      
66      protected AbstractMessageCodecTest( BaseMessage message, MessageEncoder encoder, MessageDecoder decoder )
67      {
68          if ( message == null )
69          {
70              throw new NullPointerException( "message" );
71          }
72          if ( encoder == null )
73          {
74              throw new NullPointerException( "encoder" );
75          }
76          if ( decoder == null )
77          {
78              throw new NullPointerException( "decoder" );
79          }
80  
81          this.message = message;
82          this.encoder = encoder;
83          this.decoder = decoder;
84      }
85  
86  
87      @Test 
88      public void testMessageCodec() throws Exception
89      {
90          SimpleProtocolEncoderOutput encoderOut = new SimpleProtocolEncoderOutput()
91          {
92              protected WriteFuture doFlush( ByteBuffer buf )
93              {
94                  return null;
95              }
96  
97          };
98          
99          IoSession session = new  DummySession();
100         
101         session.setAttribute( "registries", service.getRegistries() );
102         encoder.encode( session, message, encoderOut );
103         ByteBuffer buf = encoderOut.getBufferQueue().poll();
104 
105         buf.mark();
106         Assert.assertTrue( decoder.decodable( null, buf ) == MessageDecoder.OK );
107         buf.reset();
108 
109         ProtocolDecoderOutputImpl decoderOut = new ProtocolDecoderOutputImpl();
110         decoder.decode( session, buf, decoderOut );
111 
112         Assert.assertTrue( compare( message, ( BaseMessage ) decoderOut.messages.poll() ) );
113     }
114 
115 
116     protected boolean compare( BaseMessage expected, BaseMessage actual )
117     {
118         return expected.equals( actual );
119     }
120 
121     private class ProtocolDecoderOutputImpl implements ProtocolDecoderOutput
122     {
123         private final Queue<Object> messages = new LinkedBlockingQueue<Object>();
124 
125 
126         public void flush()
127         {
128         }
129 
130 
131         public void write( Object message )
132         {
133             messages.add( message );
134         }
135     }
136 
137 
138     protected static class DummySession extends BaseIoSession
139     {
140         Object message;
141 
142 
143         @Override
144         public WriteFuture write( Object message )
145         {
146             this.message = message;
147 
148             return super.write( message );
149         }
150 
151 
152         protected Object getMessage()
153         {
154             return message;
155         }
156 
157 
158         protected void updateTrafficMask()
159         {
160             // Do nothing.
161         }
162 
163 
164         public IoService getService()
165         {
166             return null;
167         }
168 
169 
170         public IoHandler getHandler()
171         {
172             return null;
173         }
174 
175 
176         public IoFilterChain getFilterChain()
177         {
178             return null;
179         }
180 
181 
182         public TransportType getTransportType()
183         {
184             return null;
185         }
186 
187 
188         public SocketAddress getRemoteAddress()
189         {
190             return new InetSocketAddress( 10088 );
191         }
192 
193 
194         public SocketAddress getLocalAddress()
195         {
196             return null;
197         }
198 
199 
200         public IoSessionConfig getConfig()
201         {
202             return null;
203         }
204 
205 
206         public int getScheduledWriteRequests()
207         {
208             return 0;
209         }
210 
211 
212         public SocketAddress getServiceAddress()
213         {
214             return null;
215         }
216 
217 
218         public IoServiceConfig getServiceConfig()
219         {
220             return null;
221         }
222 
223 
224         public int getScheduledWriteBytes()
225         {
226             return 0;
227         }
228     }
229 }