add test: IdentifierTest

This commit is contained in:
free will committed 2021-03-29 16:04:31 +08:00
1 parent 186d7a48f7
commit e0f36a740e
3 files changed
+96 -28

No files matched your search

+19 -5
View File
@@ -114,6 +114,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* @param block
*/
public Identifier(Block block) throws ComponentException {
components = new IdentifierComponentContainer();
this.buildIdentifierByBlock(block);
}
@@ -122,8 +123,12 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
*
* @param identifierString
*/
public Identifier(String identifierString) {
this.buildIdentifierByString(identifierString);
public Identifier(String identifierString) throws ComponentException {
components = new IdentifierComponentContainer();
if(!this.buildIdentifierByString(identifierString)){
throw new ComponentException
("Identifier(String): identifierString does not starts with \"/\". ");
}
}
/**
@@ -132,6 +137,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* @param container
*/
public Identifier(IdentifierComponentContainer container) throws ComponentException {
components = new IdentifierComponentContainer();
this.buildIdentifierByComponents(container);
}
@@ -327,13 +333,13 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
}
/**
* 获取前 num 个名称组件构成的标识
* 获取前 num 个名称组件构成的新标识
*
* @param num
* @return
*/
public IdentifierComponentContainer getPrefix(int num) throws ComponentException {
return this.components.getPrefix(num);
public Identifier getPrefix(int num) throws ComponentException {
return new Identifier(this.components.getPrefix(num));
}
/**
@@ -353,4 +359,12 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
public boolean isValid() {
return this.components.length() > 0;
}
/**
* 标识后面新增一个名称组件
* @param component
*/
public void append(IdentifierComponent component){
this.components.addElement(component);
}
}
@@ -14,6 +14,7 @@ import util.ByteHelper;
import util.ByteHelperException;
import util.StringHelper;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
// @Description:
@@ -69,15 +70,11 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
public IdentifierComponent(String nameComponent) {
this.identifierBlock = new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(nameComponent.length() + 1));
byte[] value = new byte[0];
try {
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
} catch (VlIntException e) {
e.printStackTrace();
}
int len=nameComponent.getBytes(StandardCharsets.UTF_8).length;
this.identifierBlock.setLength(new VlInt(len + 1));
byte[] value = new byte[len+1];
value[0] = MarkerString;
System.arraycopy(nameComponent.getBytes(), 0, value, 1, nameComponent.getBytes().length);
System.arraycopy(nameComponent.getBytes(StandardCharsets.UTF_8), 0, value, 1, nameComponent.getBytes().length);
this.identifierBlock.setValue(value);
this.marker = MarkerString;
this.stringValue = nameComponent;
@@ -93,12 +90,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
byte[] uint64Array=Encoder.buildNonNegativeIntegerArr(intValue);
this.identifierBlock.setLength(new VlInt( uint64Array.length+ 1));
byte[] value = new byte[0];
try {
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
} catch (VlIntException e) {
e.printStackTrace();
}
byte[] value = new byte[uint64Array.length+1];
value[0] = MarkerNonNegativeInteger;
System.arraycopy(uint64Array, 0, value, 1, uint64Array.length);
this.identifierBlock.setValue(value);
@@ -115,12 +107,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
this.identifierBlock = new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(arr.length + 1));
byte[] value = new byte[0];
try {
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
} catch (VlIntException e) {
e.printStackTrace();
}
byte[] value = new byte[arr.length+1];
value[0] = MarkerByteArray;
System.arraycopy(arr, 0, value, 1, arr.length);
this.identifierBlock.setValue(value);
@@ -309,6 +296,11 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
@Override
public boolean wireDecode(Block block) throws ComponentException {
try {
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvIdentifierComponent))) {
return false;
}
this.identifierBlock = new Block(block);
//todo:越界问题
@@ -357,12 +349,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
public String toUri() {
if (isString()) {
return StringHelper.escape(this.stringValue);
} else if (isNonNegativeInteger()) {
} else if(isByteArray()){
return StringHelper.escape(new String(this.byteArrayValue));
}else{
// return StringHelper.FormatUint(this.intValue,10);
// todo: 需要对long类型按照指定编码规则编码成字符串
return "";
} else {
return StringHelper.escape(new String(this.byteArrayValue));
}
}
}