changeset 751:da1ac89b3029

ValueCoderResolver - isAssignableFrom removed
author Devel 1
date Wed, 13 Dec 2017 11:49:31 +0100
parents 29f44148f245
children ca40a354ee98
files stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoderResolver.java stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderResolverTest.java stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java
diffstat 3 files changed, 51 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoderResolver.java	Wed Dec 13 11:27:42 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoderResolver.java	Wed Dec 13 11:49:31 2017 +0100
@@ -1,7 +1,8 @@
 package com.passus.st.reader.nc.option;
 
 import com.passus.data.ByteBuff;
-import com.passus.data.ByteString;
+import com.passus.data.ByteStringImpl;
+import com.passus.data.SliceByteString;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -21,14 +22,16 @@
     static final byte INT_CODE = 1;
     static final byte LONG_CODE = 2;
     static final byte STRING_CODE = 11;
-    static final byte BYTE_STRING_CODE = 12;
+    static final byte BYTE_STRING_IMPL_CODE = 12;
+    static final byte BYTE_STRING_SLICE_CODE = 13;
 
     {
         CODE_TO_CODER[NULL_CODE] = NULL_CODER;
         addCoder(INT_CODE, Integer.class, ValueCoders.INT_CODER);
         addCoder(LONG_CODE, Long.class, ValueCoders.LONG_CODER);
         addCoder(STRING_CODE, String.class, ValueCoders.STRING_NT_CODER);
-        addCoder(BYTE_STRING_CODE, ByteString.class, ValueCoders.BYTE_STRING_NT_CODER);
+        addCoder(BYTE_STRING_IMPL_CODE, ByteStringImpl.class, ValueCoders.BYTE_STRING_NT_CODER);
+        addCoder(BYTE_STRING_SLICE_CODE, SliceByteString.class, ValueCoders.BYTE_STRING_NT_CODER);
     }
 
     @Override
@@ -59,12 +62,6 @@
             return code;
         }
 
-        for (Map.Entry<Class, Byte> e : CLASS_TO_CODE.entrySet()) {
-            if (e.getKey().isAssignableFrom(cls)) {
-                return e.getValue();
-            }
-        }
-
         throw new IllegalArgumentException("Cannot encode object of type: " + cls.getSimpleName());
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderResolverTest.java	Wed Dec 13 11:49:31 2017 +0100
@@ -0,0 +1,38 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.passus.st.reader.nc.option;
+
+import com.passus.data.ByteStringImpl;
+import com.passus.st.reader.nc.option.ValueCoders.LongCoder;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class DefaultValueCoderResolverTest {
+
+    private final DefaultValueCoderResolver resolver = new DefaultValueCoderResolver();
+
+    @Test
+    public void testFindCode() {
+        assertEquals(DefaultValueCoderResolver.NULL_CODE, resolver.findCode(null));
+        assertEquals(DefaultValueCoderResolver.LONG_CODE, resolver.findCode(0L));
+        assertEquals(DefaultValueCoderResolver.BYTE_STRING_IMPL_CODE, resolver.findCode(new ByteStringImpl("")));
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void testFindCodeInvalid() {
+        resolver.findCode(new Thread());
+    }
+
+    @Test
+    public void testCodeToCoder() {
+        assertEquals(LongCoder.class, resolver.codeToCoder(DefaultValueCoderResolver.LONG_CODE).getClass());
+    }
+
+}
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java	Wed Dec 13 11:27:42 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java	Wed Dec 13 11:49:31 2017 +0100
@@ -1,7 +1,7 @@
 package com.passus.st.reader.nc.option;
 
 import com.passus.commons.AsciiUtils;
-import com.passus.data.ByteString;
+import com.passus.data.ByteStringImpl;
 import com.passus.data.HeapByteBuff;
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.Test;
@@ -13,29 +13,12 @@
 public class DefaultValueCoderTest {
 
     @Test
-    public void testConcreteClass_ByteBuff() {
+    public void testValidClass() {
         HeapByteBuff buff = new HeapByteBuff(8);
-        String s = "abc";
-
-        DefaultValueCoder.INSTANCE.encode(s, buff);
-        byte[] expected = {DefaultValueCoderResolver.STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL};
-        assertEquals(0, buff.startIndex());
-        assertEquals(5, buff.endIndex());
-        assertEquals(expected, buff.toArray());
-
-        Object obj = DefaultValueCoder.INSTANCE.decode(buff);
-        assertEquals(5, buff.startIndex());
-        assertEquals(5, buff.endIndex());
-        assertEquals(s, obj);
-    }
-
-    @Test
-    public void testAssignableClass_ByteBuff() {
-        HeapByteBuff buff = new HeapByteBuff(8);
-        ByteString bs = ByteString.create("abc");
+        ByteStringImpl bs = new ByteStringImpl("abc");
 
         DefaultValueCoder.INSTANCE.encode(bs, buff);
-        byte[] expected = {DefaultValueCoderResolver.BYTE_STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL};
+        byte[] expected = {DefaultValueCoderResolver.BYTE_STRING_IMPL_CODE, 'a', 'b', 'c', AsciiUtils.NUL};
         assertEquals(0, buff.startIndex());
         assertEquals(5, buff.endIndex());
         assertEquals(expected, buff.toArray());
@@ -47,14 +30,14 @@
     }
 
     @Test
-    public void testNull_ByteBuf() {
+    public void testNull() {
         HeapByteBuff buff = new HeapByteBuff(8);
 
         DefaultValueCoder.INSTANCE.encode(null, buff);
-        byte[] expected = {DefaultValueCoderResolver.NULL_CODE, 0, 0, 0, 0, 0, 0, 0};
+        byte[] expected = {DefaultValueCoderResolver.NULL_CODE};
         assertEquals(0, buff.startIndex());
         assertEquals(1, buff.endIndex());
-        assertEquals(expected, buff.buffer());
+        assertEquals(expected, buff.toArray());
 
         Object obj = DefaultValueCoder.INSTANCE.decode(buff);
         assertEquals(1, buff.startIndex());