StringRecord implements Record { public static String readString(final DataInput in) throws IOException { if (in.readBoolean()) { final int length = in.readInt(); if (length < 0) { throw new IOException("length of StringRecord is " + length); } final byte[] bytes = new byte[length]; in.readFully(bytes, 0, length); return decode(bytes); } return null; } StringRecord();  StringRecord(final String string);  StringRecord(final StringRecord utf8);  StringRecord(final byte[] utf8); byte[] getBytes(); int charAt(final int position); int find(final String what); int find(final String what, final int start); void set(final String string); void set(final byte[] utf8); void set(final StringRecord other); int getLength(); void set(final byte[] utf8, final int start, final int len); void append(final byte[] utf8, final int start, final int len); void clear(); String toString(); void read(final DataInput in); static void skip(final DataInput in); static void skipFully(final DataInput in, final int len); void write(final DataOutput out); @Override boolean equals(final Object obj); @Override int hashCode(); static String decode(final byte[] utf8); static String decode(final byte[] utf8, final int start, final int length); static String decode(final byte[] utf8, final int start, final int length, final boolean replace); static ByteBuffer encode(final String string); static ByteBuffer encode(final String string, final boolean replace); static String readString(final DataInput in); static int writeString(final DataOutput out, final String s); static void validateUTF8(final byte[] utf8); static void validateUTF8(final byte[] utf8, final int start, final int len); static int bytesToCodePoint(final ByteBuffer bytes); static int utf8Length(final String string);  }
@Test public void shouldReadProperInputs() { try { when(this.inputMock.readBoolean()).thenReturn(true); when(this.inputMock.readInt()).thenReturn(10); final String readString = StringRecord.readString(inputMock); assertThat(readString, is(not(nullValue()))); } catch (IOException ioe) { fail(ioe.getMessage()); } }
@Test public void shouldReadNegativeInputs() { try { when(this.inputMock.readBoolean()).thenReturn(true); when(this.inputMock.readInt()).thenReturn(-1); } catch(IOException ioe) { fail(ioe.getMessage()); } try { StringRecord.readString(inputMock); } catch(IOException ioe) { return; } fail("StringRecord.readString did not throw an IOException for negative length of string"); }