mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: IdentifierWrapper
This commit is contained in:
1 parent
e0f36a740e
commit
2d4a57beeb
5 files changed
+189
-11
No files matched your search
@@ -263,7 +263,6 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
|
||||
if (!block.parseSubElements()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -154,7 +154,9 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent(Block block) throws ComponentException {
|
||||
this.wireDecode(block);
|
||||
if(!this.wireDecode(block)){
|
||||
throw new ComponentException("IdentifierComponent(Block block): block invalid");
|
||||
}
|
||||
}
|
||||
|
||||
public byte getMarker() {
|
||||
|
||||
@@ -10,11 +10,10 @@ import encoding.*;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
private Identifier identifier;
|
||||
private VlInt tlvType;
|
||||
private Identifier identifier=new Identifier();
|
||||
private VlInt tlvType=new VlInt(TLV.TlvInvalid);
|
||||
|
||||
public IdentifierWrapper() {
|
||||
identifier = new Identifier();
|
||||
}
|
||||
|
||||
public VlInt getTlvType() {
|
||||
@@ -41,6 +40,11 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
}
|
||||
}
|
||||
|
||||
public IdentifierWrapper(VlInt tlvType,Identifier identifier){
|
||||
this.tlvType=tlvType;
|
||||
this.identifier=identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义标识类型,根据标识的字符串标识创建一个自定义的标识
|
||||
*
|
||||
@@ -52,7 +56,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
* // <Identifier>
|
||||
*/
|
||||
public IdentifierWrapper(VlInt tlvType, String identifierString) throws ComponentException {
|
||||
if (TLV.isValidIdentifierType(tlvType)) {
|
||||
if (!TLV.isValidIdentifierType(tlvType)) {
|
||||
throw new ComponentException("IdentifierWrapper(VlInt tlvType,String identifierString): "
|
||||
+ "tlvType invalid");
|
||||
}
|
||||
@@ -71,7 +75,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
* // <Identifier>
|
||||
*/
|
||||
public IdentifierWrapper(VlInt tlvType, IdentifierComponentContainer container) throws ComponentException {
|
||||
if (TLV.isValidIdentifierType(tlvType)) {
|
||||
if (!TLV.isValidIdentifierType(tlvType)) {
|
||||
throw new ComponentException("IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container): "
|
||||
+ "tlvType invalid");
|
||||
}
|
||||
@@ -87,11 +91,15 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper(VlInt tlvType, Block block) throws ComponentException {
|
||||
if (TLV.isValidIdentifierType(tlvType)) {
|
||||
// todo: 报错
|
||||
if (!TLV.isValidIdentifierType(tlvType)) {
|
||||
throw new ComponentException("IdentifierWrapper(VlInt tlvType,Block block): "
|
||||
+ "tlvType invalid");
|
||||
}
|
||||
this.tlvType = tlvType;
|
||||
this.identifier.buildIdentifierByBlock(block);
|
||||
if(!this.identifier.buildIdentifierByBlock(block)){
|
||||
throw new ComponentException
|
||||
("IdentifierWrapper(VlInt tlvType, Block block): Identifier block is valid");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package component;
|
||||
|
||||
import org.junit.Test;
|
||||
import util.ByteHelper;
|
||||
import util.StringHelper;
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
import org.junit.Test;
|
||||
import util.ByteHelper;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:04 2021/3/29
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IdentifierWrapperTest {
|
||||
|
||||
@Test
|
||||
public void TestCreateCustomIdentifierByString() throws ComponentException {
|
||||
String str="/min/pku/王锋";
|
||||
VlInt tlvType=new VlInt(199);
|
||||
IdentifierWrapper got=new IdentifierWrapper(tlvType,str);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateCustomIdentifierByComponents() throws ComponentException {
|
||||
IdentifierComponentContainer components=new IdentifierComponentContainer();
|
||||
String[] componentStrings="/min/pku/王锋".split("/");
|
||||
// System.out.println(componentStrings.toString());
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
System.out.println(componentStrings[i]);
|
||||
components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
}
|
||||
VlInt tlvType=new VlInt(110);
|
||||
IdentifierWrapper got=new IdentifierWrapper(tlvType,components);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateCustomIdentifierByBlock() throws BlockException, ComponentException {
|
||||
byte[] buf=new byte[8000];
|
||||
buf[0]=100;
|
||||
buf[1]= ByteHelper.uint8ToByte(241);
|
||||
System.arraycopy(ByteHelper.uint16ToByteArray(7996),0,buf,2,2);
|
||||
VlInt tlvType=new VlInt(103);
|
||||
Block block=new Block(new VlInt(101),new VlInt(8000),buf,true);
|
||||
IdentifierWrapper got=new IdentifierWrapper(tlvType,block);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateCommonIdentifierByString() throws ComponentException {
|
||||
String str="/min/pku/王锋";
|
||||
IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateCommonIdentifierByComponents() throws ComponentException {
|
||||
IdentifierComponentContainer components=new IdentifierComponentContainer();
|
||||
String[] componentStrings="/min/pku/王锋".split("/");
|
||||
// System.out.println(componentStrings.toString());
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
System.out.println(componentStrings[i]);
|
||||
components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
}
|
||||
IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByComponents(components);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateContentDataIdentifierByStringg() throws ComponentException {
|
||||
String str="/min/pku/王锋";
|
||||
IdentifierWrapper got=new IdentifierWrapper().createContentDataIdentifierByString(str);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateContentDataIdentifierByComponents() throws ComponentException {
|
||||
IdentifierComponentContainer components=new IdentifierComponentContainer();
|
||||
String[] componentStrings="/min/pku/王锋".split("/");
|
||||
// System.out.println(componentStrings.toString());
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
System.out.println(componentStrings[i]);
|
||||
components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
}
|
||||
IdentifierWrapper got=new IdentifierWrapper().createContentDataIdentifierByComponents(components);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestIdentifierWrapper_WireEncode() throws ComponentException, EncoderException, BlockException {
|
||||
byte[] buf=new byte[8000];
|
||||
buf[0]=100;
|
||||
buf[1]= ByteHelper.uint8ToByte(241);
|
||||
System.arraycopy(ByteHelper.uint16ToByteArray(7996),0,buf,2,2);
|
||||
|
||||
VlInt tlvType=new VlInt(103);
|
||||
Block block=new Block(new VlInt(101),new VlInt(8000),buf,true);
|
||||
IdentifierWrapper got=new IdentifierWrapper(tlvType,block);
|
||||
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
|
||||
Encoder encoder=new Encoder();
|
||||
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
|
||||
got.wireEncode(encoder);
|
||||
byte[] rawBuf=encoder.getBuffer();
|
||||
Block rawBlock=new Block(rawBuf,false);
|
||||
IdentifierWrapper gt=new IdentifierWrapper();
|
||||
gt.wireDecode(rawBlock);
|
||||
System.out.println(gt.isValid());
|
||||
System.out.println(gt.toUri());
|
||||
System.out.println(gt.getIdentifier().size());
|
||||
System.out.println(gt.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateContentDataIdentifierByBlock() throws BlockException, ComponentException {
|
||||
byte[] buf=new byte[8000];
|
||||
buf[0]=100;
|
||||
buf[1]= ByteHelper.uint8ToByte(241);
|
||||
System.arraycopy(ByteHelper.uint16ToByteArray(7996),0,buf,2,2);
|
||||
VlInt tlvType=new VlInt(103);
|
||||
Block block=new Block(new VlInt(101),new VlInt(8000),buf,true);
|
||||
IdentifierWrapper got=new IdentifierWrapper().createContentDataIdentifierByBlock(block);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateInterestDataIdentifierByStringg() throws ComponentException {
|
||||
String str="/min/pku/王";
|
||||
IdentifierWrapper got=new IdentifierWrapper().createContentInterestIdentifierByString(str);
|
||||
System.out.println(got.isValid());
|
||||
System.out.println(got.toUri());
|
||||
System.out.println(got.getIdentifier().size());
|
||||
System.out.println(got.getTlvType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateIdentifierWrapperByTypeAndIdentifier() throws ComponentException {
|
||||
Identifier identifier=new Identifier("/min/pku/王");
|
||||
for (int i = 100; i < 200; i++) {
|
||||
IdentifierWrapper got=new IdentifierWrapper(new VlInt(i),identifier);
|
||||
System.out.println(got.toUri());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user