@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenUnknownPrimitiveTypeSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeUnknown\" primitiveType=\"XXXX\"/>" + "</types>"; parseTestXmlWithMap("/types/type", testXmlString); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenUnknownPresenceSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTyeUnknown\" presence=\"XXXXX\" primitiveType=\"char\"/>" + "</types>"; parseTestXmlWithMap("/types/type", testXmlString); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenConstantPresenceButNoDataSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypePresenceConst\" primitiveType=\"char\" presence=\"constant\"></type>" + "</types>"; parseTestXmlWithMap("/types/type", testXmlString); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenNoPrimitiveTypeSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType\"/>" + "</types>"; parseTestXmlWithMap("/types/type", testXmlString); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenNoNameSpecified() throws Exception { final String testXmlString = "<types>" + " <type primitiveType=\"char\"/>" + "</types>"; parseTestXmlWithMap("/types/type", testXmlString); }
@Test public void shouldReturnCorrectSemanticTypeForType() throws Exception { final String semanticType = "char"; final String testXmlString = "<types>" + " <type name=\"testType\" primitiveType=\"char\" semanticType=\"" + semanticType + "\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat(map.get("testType").semanticType(), is(semanticType)); }
@Test public void shouldReturnDefaultMaxValueWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefaultCharMaxValue\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(((EncodedDataType)map.get("testTypeDefaultCharMaxValue")).maxValue()); }
@Test public void shouldReturnCorrectDescriptionForType() throws Exception { final String desc = "basic description attribute of a type element"; final String testXmlString = "<types>" + " <type name=\"testTypeDescription\" primitiveType=\"char\" description=\"" + desc + "\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat(map.get("testTypeDescription").description(), is(desc)); }
@Test public void shouldReturnDefaultMinValueWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefaultCharMinValue\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(((EncodedDataType)map.get("testTypeDefaultCharMinValue")).minValue()); }
@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 shouldReturnDefaultNullValueWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefaultCharNullValue\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(((EncodedDataType)map.get("testTypeDefaultCharNullValue")).nullValue()); }
@Test public void shouldReturnNullOnNoDescriptionSet() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeNoDescription\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); final String description = map.get("testTypeNoDescription").description(); Assert.assertNull(description); }
@Test public void shouldReturnNullWhenSemanticTypeNotSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(map.get("testType").semanticType()); }
@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 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))); }
@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 shouldHandleMultipleTypes() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType1\" presence=\"required\" primitiveType=\"char\" length=\"1\" " + "variableLength=\"false\"/>" + " <type name=\"testType2\" presence=\"required\" primitiveType=\"int8\" length=\"1\" " + "variableLength=\"false\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); // assert that testType is in map and name of Type is correct assertThat(map.size(), is(2)); assertThat(map.get("testType1").name(), is("testType1")); assertThat(map.get("testType2").name(), is("testType2")); }
@Test public void shouldUseAppropriatePresence() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefault\" primitiveType=\"char\"/>" + " <type name=\"testTypeRequired\" presence=\"required\" primitiveType=\"char\"/>" + " <type name=\"testTypeOptional\" presence=\"optional\" primitiveType=\"char\"/>" + " <type name=\"testTypeConstant\" presence=\"constant\" primitiveType=\"char\">A</type>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat(map.get("testTypeDefault").presence(), is(Presence.REQUIRED)); assertThat(map.get("testTypeRequired").presence(), is(Presence.REQUIRED)); assertThat(map.get("testTypeOptional").presence(), is(Presence.OPTIONAL)); assertThat(map.get("testTypeConstant").presence(), is(Presence.CONSTANT)); }
@Test public void shouldSetAppropriateDefaultsWhenNoneSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); // assert that testType is in map and name of Type is correct assertThat(map.get("testType").name(), is("testType")); // assert defaults for length, variableLength and presence final Type t = map.get("testType"); assertThat(t.presence(), is(Presence.REQUIRED)); final EncodedDataType d = (EncodedDataType)t; assertThat(d.length(), is(1)); assertThat(d.isVariableLength(), is(false)); }