changeset 697:86e9b9d85b7f

OptionsReaderWriter
author Devel 1
date Mon, 27 Nov 2017 13:33:13 +0100
parents 60afd76e6ca8
children f248f2e25d10
files stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java stress-tester/src/test/java/com/passus/st/reader/nc/option/OptionsReaderWriterTest.java
diffstat 3 files changed, 84 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java	Mon Nov 27 12:29:29 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java	Mon Nov 27 13:33:13 2017 +0100
@@ -41,6 +41,16 @@
         addCoder(name, coder);
     }
 
+    static void removeOptionAndCoder(byte code) {
+        VALUE_CODERS.remove(OPTION_NAMES[code]);
+        OPTION_CODES.remove(OPTION_NAMES[code]);
+        OPTION_NAMES[code] = null;
+    }
+
+    static void removeCoder(String name) {
+        VALUE_CODERS.remove(name);
+    }
+
     public static String getOptionName(byte code) {
         return OPTION_NAMES[code];
     }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java	Mon Nov 27 12:29:29 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java	Mon Nov 27 13:33:13 2017 +0100
@@ -15,11 +15,14 @@
 
     public void writeOptions(List<Option> options, ByteBuff buff) {
         NC_HELPER.writeIntVLC(options.size(), buff);
-        
+
         for (Option option : options) {
             String name = option.getName();
             byte code = Options.getOptionCode(name);
             ValueCoder coder = Options.getValueCoder(name);
+            if (coder == null) {
+                coder = DefaultValueCoder.INSTANCE;
+            }
 
             buff.append(code);
             if (code == 0) {
@@ -32,7 +35,7 @@
     public List<Option> readOptions(ByteBuff buff) {
         int numOptions = NC_HELPER.readIntVLC(buff);
         List<Option> options = new ArrayList<>(numOptions);
-        
+
         for (int i = 0; i < numOptions; i++) {
             byte code = buff.read();
             String name;
@@ -41,12 +44,16 @@
             } else {
                 name = Options.getOptionName(code);
             }
+
             ValueCoder coder = Options.getValueCoder(name);
+            if (coder == null) {
+                coder = DefaultValueCoder.INSTANCE;
+            }
+
             Object value = coder.decode(buff);
-
             options.add(new Option(name, value));
         }
-        
+
         return options;
     }
 }
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/OptionsReaderWriterTest.java	Mon Nov 27 12:29:29 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/OptionsReaderWriterTest.java	Mon Nov 27 13:33:13 2017 +0100
@@ -1,5 +1,8 @@
 package com.passus.st.reader.nc.option;
 
+import com.passus.data.HeapByteBuff;
+import java.util.Arrays;
+import java.util.List;
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.Test;
 
@@ -9,16 +12,71 @@
  */
 public class OptionsReaderWriterTest {
 
+    final byte opCode = 127;
+    final String opName = "xyz";
+    final Option op = new Option(opName, 123);
+    final List<Option> options = Arrays.asList(op, op);
+    final OptionsReaderWriter rw = new OptionsReaderWriter();
+
     @Test
     public void testOptionWithCode() {
-        
-    }    
+        Options.addOptionAndCoder(opName, opCode, ValueCoders.INT_CODER);
+        try {
+            HeapByteBuff buff = new HeapByteBuff();
+            rw.writeOptions(options, buff);
+            byte[] result = buff.toArray();
+            byte[] expected = {
+                2,
+                127, 0, 0, 0, 123,
+                127, 0, 0, 0, 123
+            };
+            assertEquals(expected, result);
+
+            checkDecodedOptions(buff);
+        } finally {
+            Options.removeOptionAndCoder(opCode);
+        }
+    }
+
     @Test
     public void testOptionWithoutCode() {
-    }    
+        Options.addCoder(opName, ValueCoders.INT_CODER);
+        try {
+            HeapByteBuff buff = new HeapByteBuff();
+            rw.writeOptions(options, buff);
+            byte[] result = buff.toArray();
+            byte[] expected = {
+                2,
+                0, 'x', 'y', 'z', 0, 0, 0, 0, 123,
+                0, 'x', 'y', 'z', 0, 0, 0, 0, 123
+            };
+            assertEquals(expected, result);
+
+            checkDecodedOptions(buff);
+        } finally {
+            Options.removeCoder(opName);
+        }
+    }
 
     @Test
     public void testDefaultCoder() {
-        
-    }    
+        HeapByteBuff buff = new HeapByteBuff();
+        rw.writeOptions(options, buff);
+        byte[] result = buff.toArray();
+        byte[] expected = {
+            2,
+            0, 'x', 'y', 'z', 0, DefaultValueCoder.INT_CODE, 0, 0, 0, 123,
+            0, 'x', 'y', 'z', 0, DefaultValueCoder.INT_CODE, 0, 0, 0, 123
+        };
+        assertEquals(expected, result);
+
+        checkDecodedOptions(buff);
+    }
+
+    private void checkDecodedOptions(HeapByteBuff buff) {
+        List<Option> options = rw.readOptions(buff);
+        assertEquals(2, options.size());
+        assertEquals(opName, options.get(0).getName());
+        assertEquals(123, options.get(0).getValue());
+    }
 }