primitiveValue = PrimitiveValue.parse(nodeValue, primitiveType, characterEncoding); primitiveValue = PrimitiveValue.parse(nodeValue, length, characterEncoding); primitiveValue = PrimitiveValue.parse(nodeValue, valueLength, characterEncoding); primitiveValue = PrimitiveValue.parse(nodeValue, length, characterEncoding);
/** * Construct a ValidValue given the XML node and the encodingType. * * @param node that contains the validValue * @param encodingType for the enum */ public ValidValue(final Node node, final PrimitiveType encodingType) { name = getAttributeValue(node, "name"); description = getAttributeValueOrNull(node, "description"); value = PrimitiveValue.parse(node.getFirstChild().getNodeValue(), encodingType); sinceVersion = Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")); deprecated = Integer.parseInt(getAttributeValue(node, "deprecated", "0")); checkForValidName(node, name); }
/** * Construct a Choice given the XML node and the encodingType * * @param node that contains the validValue * @param encodingType for the enum */ public Choice(final Node node, final PrimitiveType encodingType) { name = getAttributeValue(node, "name"); description = getAttributeValueOrNull(node, "description"); value = PrimitiveValue.parse(node.getFirstChild().getNodeValue(), encodingType); sinceVersion = Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")); deprecated = Integer.parseInt(getAttributeValue(node, "deprecated", "0")); // choice values are bit positions (0, 1, 2, 3, 4, etc.) from LSB to MSB if (value.longValue() >= (encodingType.size() * 8)) { throw new IllegalArgumentException("Choice value out of bounds: " + value.longValue()); } checkForValidName(node, name); }
@Test public void shouldReturnCorrectConstantStringWhenSpecified() throws Exception { final String strConst = "string constant"; final String testXmlString = "<types>" + " <type name=\"testTypeConstString\" primitiveType=\"char\" presence=\"constant\" " + "length=\"" + strConst.length() + "\"" + ">" + strConst + "</type>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat((((EncodedDataType)map.get("testTypeConstString")).constVal()), is(parse(strConst, strConst.length(), "US-ASCII"))); }
@Test public void shouldReturnCorrectMinValueWhenSpecified() throws Exception { final String minVal = "10"; final String testXmlString = "<types>" + " <type name=\"testTypeInt8MinValue\" primitiveType=\"int8\" minValue=\"" + minVal + "\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat((((EncodedDataType)map.get("testTypeInt8MinValue")).minValue()), is(parse(minVal, PrimitiveType.INT8))); }
@Test public void shouldReturnCorrectMaxValueWhenSpecified() throws Exception { final String maxVal = "10"; final String testXmlString = "<types>" + " <type name=\"testTypeInt8MaxValue\" primitiveType=\"int8\" maxValue=\"" + maxVal + "\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat((((EncodedDataType)map.get("testTypeInt8MaxValue")).maxValue()), is(parse(maxVal, PrimitiveType.INT8))); }
@Test public void shouldReturnCorrectPresenceConstantWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypePresenceConst\" primitiveType=\"char\" presence=\"constant\">F</type>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); final String expectedString = "F"; final PrimitiveValue expectedValue = parse(expectedString, PrimitiveType.CHAR); assertThat((((EncodedDataType)map.get("testTypePresenceConst")).constVal()), is(expectedValue)); }
@Test public void shouldReturnCorrectNullValueWhenSpecified() throws Exception { final String nullVal = "10"; final String testXmlString = "<types>" + " <type name=\"testTypeInt8NullValue\" primitiveType=\"int8\" presence=\"optional\" nullValue=\"" + nullVal + "\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat((((EncodedDataType)map.get("testTypeInt8NullValue")).nullValue()), is(parse(nullVal, PrimitiveType.INT8))); }
nullValue = PrimitiveValue.parse(nullValueStr, encodingType);
@Test public void shouldHandleCharEnumEncodingType() throws Exception { final String testXmlString = "<types>" + "<enum name=\"mixed\" encodingType=\"char\">" + " <validValue name=\"Cee\">C</validValue>" + " <validValue name=\"One\">1</validValue>" + " <validValue name=\"Two\">2</validValue>" + " <validValue name=\"Eee\">E</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("mixed"); assertThat(e.encodingType(), is(PrimitiveType.CHAR)); assertThat(e.getValidValue("Cee").primitiveValue(), is(PrimitiveValue.parse("C", PrimitiveType.CHAR))); assertThat(e.getValidValue("One").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.CHAR))); assertThat(e.getValidValue("Two").primitiveValue(), is(PrimitiveValue.parse("2", PrimitiveType.CHAR))); assertThat(e.getValidValue("Eee").primitiveValue(), is(PrimitiveValue.parse("E", PrimitiveType.CHAR))); }
@Test public void shouldHandleCompositeHasNullableType() throws Exception { final String nullValStr = "9223372036854775807"; final String testXmlString = "<types>" + "<composite name=\"PRICENULL\" description=\"Price NULL\" semanticType=\"Price\">" + " <type name=\"mantissa\" description=\"mantissa\" presence=\"optional\" nullValue=\"" + nullValStr + "\" primitiveType=\"int64\"/>" + " <type name=\"exponent\" description=\"exponent\" presence=\"constant\" primitiveType=\"int8\">" + "-7</type>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType c = (CompositeType)map.get("PRICENULL"); final EncodedDataType mantissa = (EncodedDataType)c.getType("mantissa"); assertThat(mantissa.nullValue(), is(PrimitiveValue.parse(nullValStr, PrimitiveType.INT64))); }
@Test public void shouldHandleOptionalBooleanEnumType() throws Exception { final String nullValueStr = "255"; final String testXmlString = "<types>" + "<enum name=\"optionalBoolean\" encodingType=\"uint8\" presence=\"optional\"" + " nullValue=\"" + nullValueStr + "\" semanticType=\"Boolean\">" + " <validValue name=\"False\">0</validValue>" + " <validValue name=\"True\">1</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("optionalBoolean"); assertThat(e.name(), is("optionalBoolean")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.validValues().size(), is(2)); assertThat(e.getValidValue("True").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getValidValue("False").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); assertThat(e.nullValue(), is(PrimitiveValue.parse(nullValueStr, PrimitiveType.UINT8))); }
@Test public void shouldHandleBinarySetType() throws Exception { final String testXmlString = "<types>" + "<set name=\"biOp\" encodingType=\"uint8\">" + " <choice name=\"Bit0\" description=\"Bit 0\">0</choice>" + " <choice name=\"Bit1\" description=\"Bit 1\">1</choice>" + "</set>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/set", testXmlString); final SetType e = (SetType)map.get("biOp"); assertThat(e.name(), is("biOp")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.choices().size(), is(2)); assertThat(e.getChoice("Bit1").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getChoice("Bit0").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); }
@Test public void shouldHandleBinaryEnumType() throws Exception { final String testXmlString = "<types>" + "<enum name=\"biOp\" encodingType=\"uint8\">" + " <validValue name=\"off\" description=\"switch is off\">0</validValue>" + " <validValue name=\"on\" description=\"switch is on\">1</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("biOp"); assertThat(e.name(), is("biOp")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.validValues().size(), is(2)); assertThat(e.getValidValue("on").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getValidValue("off").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); }
@Test public void shouldHandleBooleanEnumType() throws Exception { final String testXmlString = "<types>" + "<enum name=\"Boolean\" encodingType=\"uint8\" semanticType=\"Boolean\">" + " <validValue name=\"False\">0</validValue>" + " <validValue name=\"True\">1</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("Boolean"); assertThat(e.name(), is("Boolean")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.validValues().size(), is(2)); assertThat(e.getValidValue("True").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getValidValue("False").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); }
@Test public void shouldHandleDecimal64CompositeType() throws Exception { final String testXmlString = "<types>" + "<composite name=\"decimal64\">" + " <type name=\"mantissa\" primitiveType=\"int64\"/>" + " <type name=\"exponent\" primitiveType=\"int8\" presence=\"constant\">-2</type>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType decimal64 = (CompositeType)map.get("decimal64"); assertThat(decimal64.name(), is("decimal64")); final EncodedDataType mantissa = (EncodedDataType)decimal64.getType("mantissa"); final EncodedDataType exponent = (EncodedDataType)decimal64.getType("exponent"); assertThat(mantissa.primitiveType(), is(PrimitiveType.INT64)); assertThat(exponent.primitiveType(), is(PrimitiveType.INT8)); assertThat(exponent.presence(), is(Presence.CONSTANT)); assertThat(exponent.constVal(), is(PrimitiveValue.parse("-2", PrimitiveType.INT8))); assertThat(decimal64.encodedLength(), is(8)); }
@Test public void shouldHandleDecimal32CompositeType() throws Exception { final String testXmlString = "<types>" + "<composite name=\"decimal32\">" + " <type name=\"mantissa\" primitiveType=\"int32\"/>" + " <type name=\"exponent\" primitiveType=\"int8\" presence=\"constant\">-2</type>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType decimal32 = (CompositeType)map.get("decimal32"); assertThat(decimal32.name(), is("decimal32")); final EncodedDataType mantissa = (EncodedDataType)decimal32.getType("mantissa"); final EncodedDataType exponent = (EncodedDataType)decimal32.getType("exponent"); assertThat(mantissa.primitiveType(), is(PrimitiveType.INT32)); assertThat(exponent.primitiveType(), is(PrimitiveType.INT8)); assertThat(exponent.presence(), is(Presence.CONSTANT)); assertThat(exponent.constVal(), is(PrimitiveValue.parse("-2", PrimitiveType.INT8))); assertThat(decimal32.encodedLength(), is(4)); }
/** * Construct a ValidValue given the XML node and the encodingType. * * @param node that contains the validValue * @param encodingType for the enum */ public ValidValue(final Node node, final PrimitiveType encodingType) { name = getAttributeValue(node, "name"); description = getAttributeValueOrNull(node, "description"); value = PrimitiveValue.parse(node.getFirstChild().getNodeValue(), encodingType); sinceVersion = Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")); checkForValidName(node, name); }
/** * Construct a ValidValue given the XML node and the encodingType. * * @param node that contains the validValue * @param encodingType for the enum */ public ValidValue(final Node node, final PrimitiveType encodingType) { name = getAttributeValue(node, "name"); description = getAttributeValueOrNull(node, "description"); value = PrimitiveValue.parse(node.getFirstChild().getNodeValue(), encodingType); sinceVersion = Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")); deprecated = Integer.parseInt(getAttributeValue(node, "deprecated", "0")); checkForValidName(node, name); }