    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.activeio;
    19
    20  import java.io.IOException;
    21  import java.net.URISyntaxException;
    22
    23  import junit.framework.TestCase;
    24
    25  import org.activeio.packet.ByteArrayPacket;
    26  import org.activeio.packet.EOSPacket;
    27
    28  /***
    29   */
    30  abstract public class ChannelRequestTestSupport extends TestCase implements RequestListener {
    31
    32      private ChannelServer server;
    33
    34      public void test() throws IOException {
    35          final RequestChannel channel = createClientRequestChannel();
    36          try {
    37              channel.start();
    38              sendRequest(channel, 1001);
    39              sendRequest(channel, 1002);
    40              sendRequest(channel, 1003);
    41              sendRequest(channel, 1004);
    42              sendRequest(channel, 1005);
    43          } finally {
    44              channel.dispose();
    45          }
    46      }
    47
    48      private void sendRequest(final RequestChannel channel, int packetSize) throws IOException {
    49          Packet request = new ByteArrayPacket(fill(new byte[packetSize],(byte)1));
    50          Packet response = channel.request(request, 1000 * 30*1000);
    51          assertNotNull(response);
    52          assertEquals(packetSize, response.remaining());
    53      }
    54
    55      private byte[] fill(byte[] bs, byte b) {
    56          for (int i = 0; i < bs.length; i++) {
    57              bs[i] = b;
    58          }
    59          return bs;
    60      }
    61
    62      public Packet onRequest(Packet request) {
    63          return new ByteArrayPacket(fill(new byte[request.remaining()],(byte)2));
    64      }
    65
    66      public void onRquestError(IOException error) {
    67          error.printStackTrace();
    68      }
    69
    70      protected void setUp() throws Exception {
    71          server = createChannelServer(this);
    72          server.start();
    73      }
    74
    75      protected void tearDown() throws Exception {
    76          server.dispose();
    77      }
    78
    79      abstract protected RequestChannel createClientRequestChannel() throws IOException;
    80      abstract protected ChannelServer createChannelServer(final RequestListener requestListener) throws IOException, URISyntaxException;}