~trond-norbye/gearmanij/client

« back to all changes in this revision

Viewing changes to test/gearmanij/client/GearmanServerRequestTest.java

  • Committer: elambert
  • Date: 2009-05-25 17:51:41 UTC
  • Revision ID: elambert-20090525175141-c5os5qie17o37zyo
-hardened GearmanServerRequest against invalid inputs
-new GearmanServerRequest tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import java.io.IOException;
4
4
import java.net.InetSocketAddress;
5
5
import java.net.ServerSocket;
 
6
import java.nio.ByteBuffer;
6
7
import java.nio.channels.SelectableChannel;
 
8
import java.util.Arrays;
7
9
 
8
10
import junit.framework.Assert;
9
11
import gearmanij.Constants;
98
100
  }
99
101
 
100
102
  @Test
101
 
  public void getRequestAsByteBufferTest() {
102
 
    //TODO implement me
 
103
  public void getRequestAsByteBufferTest() throws IOException {
 
104
    GearmanEchoResponseHandler handler = new GearmanEchoResponseHandler();
 
105
    Packet request = new Packet(PacketMagic.REQ,PacketType.ECHO_REQ,new byte [0]);
 
106
    GearmanServerRequest gsr = new GearmanServerRequest(handler,request);
 
107
    ByteBuffer bb = gsr.getRequestAsByteBuffer();
 
108
    ByteBuffer rb = request.toByteBuffer();
 
109
    Assert.assertEquals("limit of byte buffer return by getRequestAsByteBuffer does not match that of request packet.",rb.limit(), bb.limit());
 
110
    byte [] bba = new byte [bb.limit()];
 
111
    byte [] rba = new byte [rb.limit()];
 
112
    bb.get(bba, 0, bb.limit());
 
113
    rb.get(rba, 0, rb.limit());
 
114
    Assert.assertTrue("contents of byte buffer returned by getRequestAsByteBuffer does not match content of request.", Arrays.equals(rba, bba));
103
115
  }
104
116
  
105
117
  @Test 
106
118
  public void addResponsePacketTest() {
107
 
    //TODO implement me    
 
119
    GearmanEchoResponseHandler handler = new GearmanEchoResponseHandler();
 
120
    Packet request = new Packet(PacketMagic.REQ,PacketType.ECHO_REQ,new byte [0]);
 
121
    GearmanServerRequest gsr = new GearmanServerRequest(handler,request);
 
122
    
 
123
    try {
 
124
      gsr.addResponsePacket(null);
 
125
      Assert.fail("Attempt to add a null repsonse to  GearmanServerRequest did not result an exception being raised");
 
126
    } catch (Throwable t) {
 
127
      Assert.assertTrue("Attempt to add a null repsonse to  GearmanServerRequest resulted in an unexpected exception being raised", t instanceof IllegalArgumentException);
 
128
    }
 
129
    try {
 
130
      gsr.addResponsePacket(request);
 
131
      Assert.fail("Attempt to add an invalid repsonse packet to  GearmanServerRequest did not result an exception being raised");
 
132
    } catch (Throwable t) {
 
133
      Assert.assertTrue("Attempt to iadd nvalid repsonse packet to  GearmanServerRequest resulted in an unexpected exception being raised", t instanceof IllegalArgumentException);
 
134
    }
108
135
  }
109
136
  
110
137
  @Test
111
138
  public void getHandlerTest() {
112
 
    //TODO implement me    
 
139
    GearmanEchoResponseHandler handler = new GearmanEchoResponseHandler();
 
140
    Packet request = new Packet(PacketMagic.REQ,PacketType.ECHO_REQ,new byte [0]);
 
141
    GearmanServerRequest gsr = new GearmanServerRequest(handler,request);
 
142
    Assert.assertEquals("handler returned by getHandler does not match handler passed into constructor.",handler, gsr.getHandler());
113
143
  }
114
144
  
115
145
  @Test