StringToString extends TypeConverterBase { @Override public byte[] from(Object value, int length, int decimals, boolean addPadding) { byte[] strBytes = null; if(value != null || this.defaultValue != null) { strBytes = (value != null ? (String)value : this.defaultValue).getBytes(this.charset); if (strBytes.length > length) { throw new TypeConverterException("Field to small for value: " + length + " < " + strBytes.length); } if (addPadding) { strBytes = padBytes(strBytes, length); } } else if(addPadding) { strBytes = new byte[length]; Arrays.fill(strBytes, this.nullFillerByte); } return strBytes; }  @Override void validate(Class<?> type, int size, int decimals); @Override Object to(byte[] bytes, int offset, int length, int decimals, boolean removePadding); @Override byte[] from(Object value, int length, int decimals, boolean addPadding);  }
@Test public void testFrom() throws Exception { assertArrayEquals("ab12".getBytes(StandardCharsets.UTF_8), typeConverter.from("ab12", 4, -1, true)); assertArrayEquals(" 12".getBytes(StandardCharsets.UTF_8), typeConverter.from("12", 4, -1, true)); }
@Test public void testFromZeroValue() throws Exception { assertArrayEquals(" ".getBytes(StandardCharsets.UTF_8), typeConverter.from("", 4, -1, true)); }
@Test public void testFromNullValue() throws Exception { assertArrayEquals(new byte[] { (byte)0, (byte)0 }, typeConverter.from(null, 2, -1, true)); }
@Test public void testFromOverflow() throws Exception { expectedEx.expect(TypeConverterException.class); expectedEx.expectMessage("Field to small for value"); byte[] bytes = typeConverter.from("12345", 4, -1, true); }