kopia lustrzana https://github.com/felHR85/UsbSerial
added unit tests for binary mode
rodzic
d837762591
commit
b2222b0c27
|
@ -4,6 +4,7 @@ import com.felhr.utils.ProtocolBuffer;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
|
@ -23,6 +24,16 @@ public class ProtocolBufferTest extends TestCase {
|
|||
"10,N",
|
||||
",WPTNME*32\r\n"};
|
||||
|
||||
private final byte[] rawPacket = new byte[]{0x21, 0x3b, 0x20, 0x40};
|
||||
private final byte[] twoRawPackets = new byte[]{0x21, 0x3b, 0x20, 0x40, 0x4a, 0x20, 0x40};
|
||||
private final byte[] splitRawPacket = new byte[]{0x21, 0x3b, 0x20, 0x40, 0x4a};
|
||||
|
||||
private final byte[][] verySplitRawPacket = {
|
||||
new byte[]{0x21},
|
||||
new byte[]{0x3b},
|
||||
new byte[]{0x20},
|
||||
new byte[]{0x40}};
|
||||
|
||||
private ProtocolBuffer protocolBuffer;
|
||||
private final String modeText = ProtocolBuffer.TEXT;
|
||||
private final String modeBinary = ProtocolBuffer.BINARY;
|
||||
|
@ -35,7 +46,7 @@ public class ProtocolBufferTest extends TestCase {
|
|||
|
||||
boolean hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
String nextCommand = protocolBuffer.nextCommand();
|
||||
String nextCommand = protocolBuffer.nextTextCommand();
|
||||
assertEquals(onePacket, nextCommand);
|
||||
}
|
||||
|
||||
|
@ -48,7 +59,7 @@ public class ProtocolBufferTest extends TestCase {
|
|||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
while(protocolBuffer.hasMoreCommands()){
|
||||
builder.append(protocolBuffer.nextCommand());
|
||||
builder.append(protocolBuffer.nextTextCommand());
|
||||
}
|
||||
assertEquals(twoPackets, builder.toString());
|
||||
}
|
||||
|
@ -71,18 +82,18 @@ public class ProtocolBufferTest extends TestCase {
|
|||
|
||||
boolean hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
String nextCommand = protocolBuffer.nextCommand();
|
||||
String nextCommand = protocolBuffer.nextTextCommand();
|
||||
assertEquals("$GPAAM,A,A,0.10,N,WPTNME*32\r\n", nextCommand);
|
||||
|
||||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertFalse(hasMoreData);
|
||||
|
||||
nextCommand = protocolBuffer.nextCommand();
|
||||
nextCommand = protocolBuffer.nextTextCommand();
|
||||
assertNull(nextCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNMEA5(){
|
||||
public void testVerySplit(){
|
||||
protocolBuffer = new ProtocolBuffer(modeText);
|
||||
protocolBuffer.setDelimiter("\r\n");
|
||||
|
||||
|
@ -118,7 +129,86 @@ public class ProtocolBufferTest extends TestCase {
|
|||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
|
||||
String command = protocolBuffer.nextCommand();
|
||||
String command = protocolBuffer.nextTextCommand();
|
||||
assertEquals("$GPAAM,A,A,0.10,N,WPTNME*32\r\n", command);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawPacket(){
|
||||
protocolBuffer = new ProtocolBuffer(modeBinary);
|
||||
protocolBuffer.setDelimiter(new byte[]{0x20, 0x40});
|
||||
protocolBuffer.appendData(rawPacket);
|
||||
|
||||
boolean hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
|
||||
byte[] command = protocolBuffer.nextBinaryCommand();
|
||||
Assert.assertArrayEquals(command, rawPacket);
|
||||
|
||||
command = protocolBuffer.nextBinaryCommand();
|
||||
assertNull(command);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoRawPackets(){
|
||||
protocolBuffer = new ProtocolBuffer(modeBinary);
|
||||
protocolBuffer.setDelimiter(new byte[]{0x20, 0x40});
|
||||
protocolBuffer.appendData(twoRawPackets);
|
||||
|
||||
boolean hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
|
||||
byte[] command1 = protocolBuffer.nextBinaryCommand();
|
||||
Assert.assertArrayEquals(command1, new byte[]{0x21, 0x3b, 0x20, 0x40});
|
||||
|
||||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
|
||||
byte[] command2 = protocolBuffer.nextBinaryCommand();
|
||||
Assert.assertArrayEquals(command2, new byte[]{0x4a, 0x20, 0x40});
|
||||
|
||||
command2 = protocolBuffer.nextBinaryCommand();
|
||||
assertNull(command2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSplitRawPacket(){
|
||||
protocolBuffer = new ProtocolBuffer(modeBinary);
|
||||
protocolBuffer.setDelimiter(new byte[]{0x20, 0x40});
|
||||
protocolBuffer.appendData(splitRawPacket);
|
||||
|
||||
boolean hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
|
||||
byte[] command1 = protocolBuffer.nextBinaryCommand();
|
||||
Assert.assertArrayEquals(command1, new byte[]{0x21, 0x3b, 0x20, 0x40});
|
||||
|
||||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertFalse(hasMoreData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerySplitRawPacket(){
|
||||
protocolBuffer = new ProtocolBuffer(modeBinary);
|
||||
protocolBuffer.setDelimiter(new byte[]{0x20, 0x40});
|
||||
|
||||
protocolBuffer.appendData(verySplitRawPacket[0]);
|
||||
boolean hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertFalse(hasMoreData);
|
||||
|
||||
protocolBuffer.appendData(verySplitRawPacket[1]);
|
||||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertFalse(hasMoreData);
|
||||
|
||||
protocolBuffer.appendData(verySplitRawPacket[2]);
|
||||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertFalse(hasMoreData);
|
||||
|
||||
protocolBuffer.appendData(verySplitRawPacket[3]);
|
||||
hasMoreData = protocolBuffer.hasMoreCommands();
|
||||
assertTrue(hasMoreData);
|
||||
|
||||
byte[] command = protocolBuffer.nextBinaryCommand();
|
||||
Assert.assertArrayEquals(command, new byte[]{0x21, 0x3b, 0x20, 0x40});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
package com.felhr.utils;
|
||||
|
||||
import com.annimon.stream.IntStream;
|
||||
import com.annimon.stream.function.IntPredicate;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ProtocolBuffer {
|
||||
|
||||
public static final String BINARY = "binary.";
|
||||
public static final String BINARY = "binary";
|
||||
public static final String TEXT = "text";
|
||||
|
||||
private String mode;
|
||||
|
@ -14,14 +18,14 @@ public class ProtocolBuffer {
|
|||
private static final int DEFAULT_BUFFER_SIZE = 16 * 1024;
|
||||
|
||||
private byte[] rawBuffer;
|
||||
private int bufferPointer = 0;
|
||||
|
||||
private byte[] separator;
|
||||
private String delimiter;
|
||||
private StringBuilder stringBuffer;
|
||||
|
||||
private List<String> commands = new ArrayList<>();
|
||||
|
||||
private String regex;
|
||||
private List<byte[]> rawCommands = new ArrayList<>();
|
||||
|
||||
public ProtocolBuffer(String mode){
|
||||
this.mode = mode;
|
||||
|
@ -45,8 +49,8 @@ public class ProtocolBuffer {
|
|||
this.delimiter = delimiter;
|
||||
}
|
||||
|
||||
public void setSeparator(byte[] separator){
|
||||
this.separator = separator;
|
||||
public void setDelimiter(byte[] delimiter){
|
||||
this.separator = delimiter;
|
||||
}
|
||||
|
||||
public void appendData(byte[] data){
|
||||
|
@ -76,19 +80,74 @@ public class ProtocolBuffer {
|
|||
e.printStackTrace();
|
||||
}
|
||||
}else if(mode.equals(BINARY)){
|
||||
//TODO:!!
|
||||
appendRawData(data);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasMoreCommands(){
|
||||
return commands.size() > 0;
|
||||
if(mode.equals(TEXT)) {
|
||||
return commands.size() > 0;
|
||||
}else {
|
||||
return rawCommands.size() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
public String nextCommand(){
|
||||
public String nextTextCommand(){
|
||||
if(commands.size() > 0){
|
||||
return commands.remove(0);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] nextBinaryCommand(){
|
||||
if(rawCommands.size() > 0){
|
||||
return rawCommands.remove(0);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void appendRawData(byte[] rawData){
|
||||
|
||||
System.arraycopy(rawData, 0, rawBuffer, bufferPointer, rawData.length);
|
||||
bufferPointer += rawData.length;
|
||||
|
||||
SeparatorPredicate predicate = new SeparatorPredicate();
|
||||
int[] indexes =
|
||||
IntStream.range(0, bufferPointer)
|
||||
.filter(predicate)
|
||||
.toArray();
|
||||
|
||||
int prevIndex = 0;
|
||||
for(Integer i : indexes){
|
||||
byte[] command = Arrays.copyOfRange(rawBuffer, prevIndex, i + separator.length);
|
||||
rawCommands.add(command);
|
||||
prevIndex = i + separator.length;
|
||||
}
|
||||
|
||||
if(prevIndex < rawBuffer.length
|
||||
&& prevIndex > 0){
|
||||
byte[] tempBuffer = Arrays.copyOfRange(rawBuffer, prevIndex, rawBuffer.length);
|
||||
bufferPointer = 0;
|
||||
System.arraycopy(tempBuffer, 0, rawBuffer, bufferPointer, rawData.length);
|
||||
bufferPointer += rawData.length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class SeparatorPredicate implements IntPredicate{
|
||||
@Override
|
||||
public boolean test(int value) {
|
||||
if(rawBuffer[value] == separator[0]){
|
||||
for(int i=1;i<=separator.length-1;i++){
|
||||
if(rawBuffer[value + i] != separator[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue