blob: e8b5bb4eb82604f77be88c7d6ec34177d3579d59 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package org.traccar;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Assert;
import org.traccar.model.Command;
import javax.xml.bind.DatatypeConverter;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
public class ProtocolEncoderTest {
private String concatenateStrings(String... strings) {
StringBuilder builder = new StringBuilder();
for (String s : strings) {
builder.append(s);
}
return builder.toString();
}
protected ChannelBuffer binary(String... data) {
return binary(ByteOrder.BIG_ENDIAN, data);
}
protected ChannelBuffer binary(ByteOrder endianness, String... data) {
return ChannelBuffers.wrappedBuffer(
endianness, DatatypeConverter.parseHexBinary(concatenateStrings(data)));
}
protected String text(String... data) {
return concatenateStrings(data);
}
protected ChannelBuffer buffer(String... data) {
return ChannelBuffers.copiedBuffer(concatenateStrings(data), Charset.defaultCharset());
}
protected void verifyCommand(
BaseProtocolEncoder encoder, Command command, ChannelBuffer expected) throws Exception {
verifyDecodedCommand(encoder.encodeCommand(command), expected);
}
private void verifyDecodedCommand(Object decodedObject, ChannelBuffer expected) {
Assert.assertNotNull("command is null", decodedObject);
Assert.assertTrue("not a buffer", decodedObject instanceof ChannelBuffer);
Assert.assertEquals(ChannelBuffers.hexDump(expected), ChannelBuffers.hexDump((ChannelBuffer) decodedObject));
}
}
|