@Test public void testCanSerialiseDeSerialiseCorrectlyTypeValueOnly() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); typeSubTypeValue.setValue("testValue"); typeSubTypeValue.setType("testType"); byte[] bytes = serialiser.serialise(typeSubTypeValue); String serialisedForm = new String(bytes); assertEquals("testType\0\0testValue", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(bytes); assertEquals(typeSubTypeValue.getType(), deSerialisedTypeSubTypeValue.getType()); assertNull(deSerialisedTypeSubTypeValue.getSubType()); assertEquals(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }
@Override public TypeSubTypeValue deserialise(final byte[] bytes) throws SerialisationException { int lastDelimiter = 0; TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); for (int i = 0; i < bytes.length; i++) { if (bytes[i] == ByteArrayEscapeUtils.DELIMITER) { if (i > 0) { try { typeSubTypeValue.setType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Type from TypeSubTypeValue Object", e); if (i > lastDelimiter) { try { typeSubTypeValue.setSubType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the SubType from TypeSubTypeValue Object", e); typeSubTypeValue.setValue(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, bytes.length), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Value from TypeSubTypeValue Object", e);
@Override public int compareTo(final TypeSubTypeValue typeSubTypeValue) { if (null == typeSubTypeValue) { return 1; } int i = stringComparator.compare(type, typeSubTypeValue.getType()); if (i != 0) { return i; } i = stringComparator.compare(subType, typeSubTypeValue.getSubType()); if (i != 0) { return i; } return stringComparator.compare(value, typeSubTypeValue.getValue()); } }
@Override public Pair<TypeSubTypeValue, byte[]>[] getHistoricSerialisationPairs() { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); typeSubTypeValue.setType("testType"); typeSubTypeValue.setSubType("testSubType"); return new Pair[]{ new Pair(typeSubTypeValue, new byte[]{116, 101, 115, 116, 84, 121, 112, 101, 0, 116, 101, 115, 116, 83, 117, 98, 84, 121, 112, 101, 0}) }; } }
@Test public void testHashCodeAndEqualsMethodTreatsEmptyStringAsNull() { // Given TypeSubTypeValue typeSubTypeValueEmptyStrings = new TypeSubTypeValue("", "", "X"); TypeSubTypeValue typeSubTypeValueNullStrings = new TypeSubTypeValue(null, null, "X"); // When Boolean equalsResult = typeSubTypeValueEmptyStrings.equals(typeSubTypeValueNullStrings); // Then assertTrue(equalsResult); assertEquals(typeSubTypeValueEmptyStrings.hashCode(), typeSubTypeValueNullStrings.hashCode()); } }
@Override public TypeSubTypeValue deserialiseEmpty() { return new TypeSubTypeValue(); } }
@Test public void testComparisonsAreAsExpected() { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue("a", "b", "c"); assertEquals(0, typeSubTypeValue.compareTo(new TypeSubTypeValue("a", "b", "c"))); assertTrue(typeSubTypeValue.compareTo(null) > 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue()) > 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue("1", "b", "c")) > 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue("a", "a", "c")) > 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue("a", "b", "a")) > 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue("b", "a", "c")) < 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue("a", "c", "c")) < 0); assertTrue(typeSubTypeValue.compareTo(new TypeSubTypeValue("a", "b", "d")) < 0); }
@Override public byte[] serialise(final TypeSubTypeValue typeSubTypeValue) throws SerialisationException { String type = typeSubTypeValue.getType(); String subType = typeSubTypeValue.getSubType(); String value = typeSubTypeValue.getValue(); if ((null == type || type.isEmpty()) && (null == subType || subType.isEmpty()) && (null == value || value.isEmpty())) { throw new SerialisationException("TypeSubTypeValue passed to serialiser is blank");
@Override public TypeSubTypeValue apply(final Object value) { return new TypeSubTypeValue(null, null, null != value ? value.toString() : null); } }
@Test public void testCanSerialiseDeSerialiseCorrectly() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue("testType", "testSubType", "testValue"); byte[] bytes = serialiser.serialise(typeSubTypeValue); String serialisedForm = new String(bytes); assertEquals("testType\0testSubType\0testValue", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(bytes); assertEquals(typeSubTypeValue.getType(), deSerialisedTypeSubTypeValue.getType()); assertEquals(typeSubTypeValue.getSubType(), deSerialisedTypeSubTypeValue.getSubType()); assertEquals(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }
@Override public Object[] serialise(final TypeSubTypeValue object) throws SerialisationException { if (null != object) { return new Object[]{object.getType(), object.getSubType(), object.getValue()}; } return new Object[]{null, null, null}; }
@Override public TypeSubTypeValue deserialise(final byte[] bytes) throws SerialisationException { int lastDelimiter = 0; TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); for (int i = 0; i < bytes.length; i++) { if (bytes[i] == ByteArrayEscapeUtils.DELIMITER) { if (i > 0) { try { typeSubTypeValue.setType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Type from TypeSubTypeValue Object", e); if (i > lastDelimiter) { try { typeSubTypeValue.setSubType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the SubType from TypeSubTypeValue Object", e); typeSubTypeValue.setValue(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, bytes.length), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Value from TypeSubTypeValue Object", e);
@Override public void shouldDeserialiseEmpty() throws SerialisationException { // When final TypeSubTypeValue value = serialiser.deserialiseEmpty(); // Then assertEquals(new TypeSubTypeValue(), value); }
@Test public void testCanSerialiseDeSerialiseCorrectlySubTypeOnly() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); typeSubTypeValue.setSubType("testSubType"); byte[] bytes = serialiser.serialise(typeSubTypeValue); String serialisedForm = new String(bytes); assertEquals("\0testSubType\0", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(bytes); assertNull(deSerialisedTypeSubTypeValue.getType()); assertEquals(typeSubTypeValue.getSubType(), deSerialisedTypeSubTypeValue.getSubType()); assertNull(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }
@Override public int compareTo(final TypeSubTypeValue typeSubTypeValue) { if (null == typeSubTypeValue) { return 1; } int i = stringComparator.compare(type, typeSubTypeValue.getType()); if (i != 0) { return i; } i = stringComparator.compare(subType, typeSubTypeValue.getSubType()); if (i != 0) { return i; } return stringComparator.compare(value, typeSubTypeValue.getValue()); } }
@Test public void shouldConvertObjectToTypeSubTypeValue() { // Given final ToTypeSubTypeValue function = new ToTypeSubTypeValue(); final Object value = 1L; // When final TypeSubTypeValue result = function.apply(value); // Then assertEquals(new TypeSubTypeValue(null, null, value.toString()), result); }
@Test public void testCanSerialiseDeSerialiseCorrectlyTypeSubTypeOnly() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); typeSubTypeValue.setType("testType"); typeSubTypeValue.setSubType("testSubType"); byte[] bytes = serialiser.serialise(typeSubTypeValue); String serialisedForm = new String(bytes); assertEquals("testType\0testSubType\0", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(bytes); assertEquals(typeSubTypeValue.getType(), deSerialisedTypeSubTypeValue.getType()); assertEquals(typeSubTypeValue.getSubType(), deSerialisedTypeSubTypeValue.getSubType()); assertNull(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }
@Override public void write(final Kryo kryo, final Output output, final TypeSubTypeValue typeSubTypeValue) { output.writeString(typeSubTypeValue.getType()); output.writeString(typeSubTypeValue.getSubType()); output.writeString(typeSubTypeValue.getValue()); }
@Test public void shouldConvertStringToTypeSubTypeValue() { // Given final ToTypeSubTypeValue function = new ToTypeSubTypeValue(); final Object value = "value1"; // When final TypeSubTypeValue result = function.apply(value); // Then assertEquals(new TypeSubTypeValue(null, null, value.toString()), result); }
@Test public void testCanSerialiseDeSerialiseCorrectlySubTypeValueOnly() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue(); typeSubTypeValue.setValue("testValue"); typeSubTypeValue.setSubType("testSubType"); byte[] bytes = serialiser.serialise(typeSubTypeValue); String serialisedForm = new String(bytes); assertEquals("\0testSubType\0testValue", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(bytes); assertNull(deSerialisedTypeSubTypeValue.getType()); assertEquals(typeSubTypeValue.getSubType(), deSerialisedTypeSubTypeValue.getSubType()); assertEquals(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }