Adding ByteBufferAssert to aide in testing

This commit is contained in:
Joakim Erdfelt 2012-06-19 14:38:02 -07:00
parent 30901917eb
commit 03a1b68f38
5 changed files with 44 additions and 23 deletions

View File

@ -31,6 +31,11 @@ public abstract class ControlFrame extends BaseFrame
public void setPayload(ByteBuffer payload)
{
this.payload = payload;
if (payload.position() != 0)
{
// Make buffer ready for reading?
payload.flip();
}
setPayloadLength(payload.array().length);
}
}

View File

@ -34,22 +34,9 @@ public class PingFrame extends ControlFrame
return OpCode.PING;
}
public String getPayloadAsString()
{
if (hasPayload())
{
ByteBuffer payload = getPayload();
return payload.asCharBuffer().toString();
}
else
{
return "<n/a>";
}
}
@Override
public String toString()
{
return String.format("%s ping, payload[has=%b, string=%s]",super.toString(),hasPayload(),getPayloadAsString());
return String.format("%s ping, has-payload=%b",super.toString(),hasPayload());
}
}

View File

@ -0,0 +1,30 @@
package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import org.junit.Assert;
public class ByteBufferAssert
{
public static void assertEquals(String message, byte[] expected, byte[] actual)
{
Assert.assertThat(message + " byte[].length",actual.length,is(expected.length));
int len = expected.length;
for (int i = 0; i < len; i++)
{
Assert.assertThat(message + " byte[" + i + "]",actual[i],is(expected[i]));
}
}
public static void assertEquals(String message, String expectedString, ByteBuffer actualBuffer)
{
int len = actualBuffer.remaining();
byte actual[] = new byte[len];
actualBuffer.get(actual, 0, len);
byte expected[] = expectedString.getBytes();
assertEquals(message, expected, actual);
}
}

View File

@ -2,7 +2,7 @@ package org.eclipse.jetty.websocket.parser;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.Debug;
import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.frames.PingFrame;
import org.junit.Test;
@ -11,14 +11,10 @@ public class PingParserTest
@Test
public void testBasicPingParsing()
{
Debug.enableDebugLogging(Parser.class);
ByteBuffer buf = ByteBuffer.allocate(16);
buf.put(new byte[]
{ (byte)0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
Debug.dumpState(buf);
buf.flip();
Debug.dumpState(buf);
Parser parser = new Parser();
FrameParseCapture capture = new FrameParseCapture();
@ -27,5 +23,7 @@ public class PingParserTest
capture.assertNoErrors();
capture.assertHasFrame(PingFrame.class,1);
PingFrame ping = (PingFrame)capture.getFrames().get(0);
ByteBufferAssert.assertEquals("PingFrame.payload","Hello",ping.getPayload());
}
}

View File

@ -1,9 +1,10 @@
package org.eclipse.jetty.websocket.parser;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.frames.BinaryFrame;
import org.eclipse.jetty.websocket.frames.PingFrame;
import org.eclipse.jetty.websocket.frames.PongFrame;
@ -175,7 +176,7 @@ public class RFC6455ExamplesParserTest
capture.assertHasFrame(PingFrame.class,1);
PingFrame ping = (PingFrame)capture.getFrames().get(0);
Assert.assertThat("PingFrame.data",ping.getPayload().toString(),is("Hello"));
ByteBufferAssert.assertEquals("PingFrame.payload","Hello",ping.getPayload());
}
@Test