From 2d4a57beeb1d09786b795e6ddee4542eba07d7f1 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Mon, 29 Mar 2021 17:22:25 +0800 Subject: [PATCH] add test: IdentifierWrapper --- src/main/java/component/Identifier.java | 1 - .../java/component/IdentifierComponent.java | 4 +- .../java/component/IdentifierWrapper.java | 24 ++- src/test/java/component/IdentifierTest.java | 1 - .../java/component/IdentifierWrapperTest.java | 170 ++++++++++++++++++ 5 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 src/test/java/component/IdentifierWrapperTest.java diff --git a/src/main/java/component/Identifier.java b/src/main/java/component/Identifier.java index 689d4e1..944961d 100644 --- a/src/main/java/component/Identifier.java +++ b/src/main/java/component/Identifier.java @@ -263,7 +263,6 @@ public class Identifier implements TlvComponentBase, IEncodingAble { } // 解析子 TLV - if (!block.parseSubElements()) { return false; } diff --git a/src/main/java/component/IdentifierComponent.java b/src/main/java/component/IdentifierComponent.java index a289391..407f822 100644 --- a/src/main/java/component/IdentifierComponent.java +++ b/src/main/java/component/IdentifierComponent.java @@ -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() { diff --git a/src/main/java/component/IdentifierWrapper.java b/src/main/java/component/IdentifierWrapper.java index 1dc2320..7d1270d 100644 --- a/src/main/java/component/IdentifierWrapper.java +++ b/src/main/java/component/IdentifierWrapper.java @@ -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 { * // */ 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 { * // */ 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"); + } } /** diff --git a/src/test/java/component/IdentifierTest.java b/src/test/java/component/IdentifierTest.java index f55af71..a079f28 100644 --- a/src/test/java/component/IdentifierTest.java +++ b/src/test/java/component/IdentifierTest.java @@ -1,7 +1,6 @@ package component; import org.junit.Test; -import util.ByteHelper; import util.StringHelper; /* diff --git a/src/test/java/component/IdentifierWrapperTest.java b/src/test/java/component/IdentifierWrapperTest.java new file mode 100644 index 0000000..7abfbb9 --- /dev/null +++ b/src/test/java/component/IdentifierWrapperTest.java @@ -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()); + } + } +}