/** * {@inheritDoc} */ public AsciiSequenceView subSequence(final int start, final int end) { if (start < 0) { throw new StringIndexOutOfBoundsException("start=" + start); } if (end > length) { throw new StringIndexOutOfBoundsException("end=" + end); } if (end - start < 0) { throw new StringIndexOutOfBoundsException("start=" + start + " end=" + end); } return new AsciiSequenceView(buffer, offset + start, end - start); }
@Test public void shouldSubSequence() { final String data = "a little bit of ascii"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); final AsciiSequenceView subSequenceView = asciiSequenceView.subSequence(2, 8); assertThat(subSequenceView.toString(), is("little")); }
@Test(expected = StringIndexOutOfBoundsException.class) public void shouldThrowIndexOutOfBoundsExceptionWhenCharNotPresentAtGivenPosition() { final String data = "foo"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); asciiSequenceView.charAt(4); }
private void assertRequiredFieldsMessageFieldsAsViewDecoded( final Decoder decoder, final String currency, final String exchange, final String country) throws Exception { assertEquals(currency, getCurrencyFieldAsView(decoder).toString()); assertEquals(exchange, getExchangeFieldAsView(decoder).toString()); assertEquals(country, getCountryFieldAsView(decoder).toString()); }
@Test public void shouldCopyDataUnderTheView() { final String data = "a little bit of ascii"; final int targetBufferOffset = 56; final MutableDirectBuffer targetBuffer = new UnsafeBuffer(new byte[128]); buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); asciiSequenceView.getBytes(targetBuffer, targetBufferOffset); assertThat(targetBuffer.getStringWithoutLengthAscii(targetBufferOffset, data.length()), is(data)); }
@Test(expected = StringIndexOutOfBoundsException.class) public void shouldThrowExceptionWhenCharAtCalledWithNoBuffer() { asciiSequenceView.charAt(0); }
@Test public void shouldDecodeDifferentFieldTypes() throws Exception { final Decoder decoder = (Decoder)fieldsMessage.getConstructor().newInstance(); decode(EG_FIELDS_MESSAGE, decoder); assertRequiredFieldsMessageFieldsDecoded(decoder, "GBP", "XLON", "GB"); final char[] gbp = "GBP".toCharArray(); final char[] xlon = "XLON".toCharArray(); final char[] gb = "GB".toCharArray(); assertArrayEquals(gbp, getOptionalCurrencyField(decoder)); assertArrayEquals(xlon, getOptionalExchangeField(decoder)); assertArrayEquals(gb, getOptionalCountryField(decoder)); assertEquals("GBP", getOptionalCurrencyFieldAsString(decoder)); assertEquals("XLON", getOptionalExchangeFieldAsString(decoder)); assertEquals("GB", getOptionalCountryFieldAsString(decoder)); assertEquals("GBP", getOptionalCurrencyFieldAsView(decoder).toString()); assertEquals("XLON", getOptionalExchangeFieldAsView(decoder).toString()); assertEquals("GB", getOptionalCountryFieldAsView(decoder).toString()); assertValid(decoder); }
public static AsciiSequenceView getAsciiSequenceView(final Object value, final String name) throws Exception { final AsciiSequenceView view = new AsciiSequenceView(); value.getClass() .getMethod(name, AsciiSequenceView.class) .invoke(value, view); return view; } }
@Test public void shouldBeAbleToExtractStringsAsAsciiSequenceViewFromStringFields() throws Exception { final Decoder decoder = decodeHeartbeat(NO_OPTIONAL_MESSAGE); final AsciiSequenceView actual = getAsciiSequenceView(decoder, "onBehalfOfCompID"); assertEquals("abc", actual.toString()); assertThrows(() -> getAsciiSequenceView(decoder, "testReqID"), IllegalArgumentException.class, "No value for optional field: TestReqID"); }
/** * {@inheritDoc} */ public AsciiSequenceView subSequence(final int start, final int end) { if (start < 0) { throw new StringIndexOutOfBoundsException("start=" + start); } if (end > length) { throw new StringIndexOutOfBoundsException("end=" + end); } if (end - start < 0) { throw new StringIndexOutOfBoundsException("start=" + start + " end=" + end); } return new AsciiSequenceView(buffer, offset + start, end - start); }
@Test public void shouldBeAbleToGetChars() { final String data = "stringy"; buffer.putStringWithoutLengthAscii(INDEX, data); asciiSequenceView.wrap(buffer, INDEX, data.length()); assertThat(asciiSequenceView.charAt(0), is('s')); assertThat(asciiSequenceView.charAt(1), is('t')); assertThat(asciiSequenceView.charAt(2), is('r')); assertThat(asciiSequenceView.charAt(3), is('i')); assertThat(asciiSequenceView.charAt(4), is('n')); assertThat(asciiSequenceView.charAt(5), is('g')); assertThat(asciiSequenceView.charAt(6), is('y')); }