mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
05.38.2014 4a0d4ef8f61cb9576bded5b0f98cecee0bb92a82
ByteStringBuilder.java:
Added ByteStringBuilder(ByteSequence) and setByte(int, byte).

ByteStringBuilderTestCase.java:
Added tests.
2 files modified
148 ■■■■■ changed files
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java 34 ●●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java 114 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java
@@ -285,6 +285,19 @@
    }
    /**
     * Creates a new byte string builder with the content of the provided
     * ByteSequence. Its capacity is set to the length of the provided
     * ByteSequence.
     *
     * @param bs
     *            The ByteSequence to copy
     */
    public ByteStringBuilder(final ByteSequence bs) {
        this(bs.length());
        bs.copyTo(this);
    }
    /**
     * Appends the provided byte to this byte string builder.
     *
     * @param b
@@ -923,6 +936,27 @@
    }
    /**
     * Sets the byte value at the specified index.
     * <p>
     * An index ranges from zero to {@code length() - 1}. The first byte value
     * of the sequence is at index zero, the next at index one, and so on, as
     * for array indexing.
     *
     * @param index
     *            The index of the byte to be set.
     * @param b
     *            The byte to set on this byte string builder.
     * @throws IndexOutOfBoundsException
     *             If the index argument is negative or not less than length().
     */
    public void setByte(final int index, final byte b) {
        if (index >= length || index < 0) {
            throw new IndexOutOfBoundsException();
        }
        buffer[index] = b;
    }
    /**
     * Sets the length of this byte string builder.
     * <p>
     * If the <code>newLength</code> argument is less than the current length,
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
@@ -293,4 +293,118 @@
                    _(0x00), _(0x00), _(0x00), _(0x84), _(0xFF), _(0xFF),
                    _(0xFF), _(0xFF) } }, };
    }
    @Test
    public void testCopyCtor() {
        final ByteStringBuilder builder = new ByteStringBuilder(400);
        builder.append("this is a ByteString");
        final ByteString orig = builder.toByteString();
        final ByteString copy = new ByteStringBuilder(orig).toByteString();
        Assert.assertEquals(copy, orig);
        Assert.assertEquals(copy.length(), builder.length());
    }
    @Test
    public void testSetByte() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.append("this is a ByteString");
        builder.setByte(2, _('a'));
        builder.setByte(3, _('t'));
        Assert.assertEquals(builder.toByteString().toString(), "that is a ByteString");
    }
    @Test(expectedExceptions = { IndexOutOfBoundsException.class })
    public void testSetByteAtInvalidLowerIndex() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.setByte(-1, _(0));
    }
    @Test(expectedExceptions = { IndexOutOfBoundsException.class })
    public void testSetByteAtInvalidUpperIndex() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.setByte(builder.length(), _(0));
    }
    @Test
    public void testSetLength() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.append("this is a ByteString");
        builder.setLength(builder.length() - 16);
        Assert.assertEquals(builder.toString(), "this");
        builder.setLength(builder.length() + 1);
        Assert.assertEquals(builder.toString(), "this\u0000");
    }
    @Test(expectedExceptions = { IndexOutOfBoundsException.class })
    public void testSetInvalidLength() {
        new ByteStringBuilder().setLength(-1);
    }
    @Test
    public void testAppendNullCharArray() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.append((char[]) null);
        Assert.assertTrue(builder.isEmpty());
    }
    @Test
    public void testAppendNullString() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.append((String) null);
        Assert.assertTrue(builder.isEmpty());
    }
    @Test
    public void testAppendNonAsciiCharArray() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.append(new char[] { 'œ', 'Œ' });
        Assert.assertEquals(builder.toString(), "œŒ");
    }
    @Test
    public void testAppendNonAsciiString() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        builder.append("œŒ");
        Assert.assertEquals(builder.toString(), "œŒ");
    }
    @Test
    public void testByteStringBuilderCompareTo() {
        final ByteString orig = ByteString.valueOf("this is a ByteString");
        final ByteStringBuilder builder = new ByteStringBuilder(orig);
        Assert.assertEquals(builder.compareTo(builder), 0);
        Assert.assertEquals(builder.compareTo(orig), 0);
        Assert.assertEquals(orig.compareTo(builder), 0);
    }
    @Test
    public void testSubSequenceCompareTo() {
        final ByteString orig = ByteString.valueOf("this is a ByteString");
        final ByteStringBuilder builder = new ByteStringBuilder(orig);
        final ByteSequence subSequence = builder.subSequence(0, 4);
        Assert.assertEquals(subSequence.compareTo(subSequence), 0);
        Assert.assertTrue(subSequence.compareTo(orig) < 0);
        Assert.assertTrue(orig.compareTo(subSequence) > 0);
    }
    @Test
    public void testSubSequenceEqualsAndHashCode() {
        final ByteString orig = ByteString.valueOf("this is a ByteString");
        final ByteStringBuilder builder = new ByteStringBuilder(orig);
        final ByteSequence subSequence = builder.subSequence(0, builder.length());
        final ByteSequence subSequence2 = builder.subSequence(0, builder.length());
        Assert.assertTrue(subSequence.hashCode() != 0);
        Assert.assertTrue(subSequence.equals(subSequence));
        Assert.assertTrue(subSequence.equals(subSequence2));
        Assert.assertTrue(subSequence.equals(builder));
        Assert.assertFalse(subSequence.equals(null));
    }
    @Test
    public void testSubSequenceIsEmpty() {
        final ByteStringBuilder builder = new ByteStringBuilder();
        Assert.assertTrue(builder.subSequence(0, builder.length()).isEmpty());
        builder.append("This is a ByteString");
        Assert.assertFalse(builder.subSequence(0, builder.length()).isEmpty());
    }
}