@Test
  public void decodesTheConstantValueAsIs() throws IOException {
    int initialPosition = bytesIn.getPosition();

    bytesIn.setBuffer(encoded);
    it.decodeConstant(in);

    assertThat(bytesIn.getPosition(), is(initialPosition + encoded.length));
  }
  @Test
  public void decodesTheEmptyConstant() throws IOException {
    it = new ConstantType("");

    int initialPosition = bytesIn.getPosition();

    bytesIn.setBuffer(new byte[] {});
    it.decodeConstant(in);

    assertThat(bytesIn.getPosition(), is(initialPosition));
  }
  @Before
  public void setUp() {
    it = new ConstantType("foo");

    decoded = it.getStringValue().getBytes();
    encoded = it.getStringValue().getBytes();

    bytesOut = new ByteArrayOutputStream();
    out = new DataOutputStream(bytesOut);
    bytesIn = new ReusableByteArrayInputStream();
    in = new DataInputStream(bytesIn);
  }
  @Test
  public void doesNotMoveInputStreamIfItDoesntReadTheConstantValue() throws IOException {
    // There should be more length bytes.
    byte[] badlyEncoded = "bar".getBytes();
    bytesIn.setBuffer(badlyEncoded);

    int initialPosition = bytesIn.getPosition();
    boolean raised = false;

    try {
      it.decodeConstant(in);
    } catch (ConstantType.DecodedWrongValueException e) {
      raised = true;
    }

    assertTrue(raised);
    assertThat(bytesIn.getPosition(), is(initialPosition));
  }
  @Test
  public void doesNotMoveInputStreamOnShortRead() throws IOException {
    // There should be more length bytes.
    byte[] shortEncoded = "fo".getBytes();
    bytesIn.setBuffer(shortEncoded);

    int initialPosition = bytesIn.getPosition();
    boolean raised = false;

    try {
      it.decodeConstant(in);
    } catch (EOFException ioe) {
      raised = true;
    }

    assertTrue(raised);
    assertThat(bytesIn.getPosition(), is(initialPosition));
  }
 @Test
 public void encodesTheConstantValueAsIs() throws IOException {
   it.encodeConstant(out);
   assertThat(bytesOut.toByteArray(), is(encoded));
 }
 @Test
 public void isConstant() {
   assertThat(it.isConstant(), is(true));
 }
 @Test
 public void isWellFormed() {
   assertThat(it.isWellFormed(), is(true));
 }
 @Test
 public void hasASolitaryValue() {
   assertThat(it.getStringValue(), is("foo"));
   assertThat(it.getValue(), is("foo".getBytes()));
 }
 @Test
 public void isByteAligned() {
   assertThat(it.isByteAligned(), is(true));
   assertThat(it.isInternallyByteAligned(), is(true));
 }
 @Test
 public void isAsWideAsTheUTF8EncodingOfTheStringPassedAtConstruction() {
   assertThat(new ConstantType("f\u00f6\u00f6").getBitWidth(), is(40));
   assertThat(it.isWholeByteSized(), is(true));
 }
 @Test
 public void isFixedWidth() {
   assertThat(it.isFixedWidth(), is(true));
   assertThat(it.isStreamDecodeable(), is(true));
   assertThat(it.isDecodeable(), is(true));
 }
 @Test
 public void isPackable() {
   assertThat(it.isPackable(), is(true));
 }
 @Test
 public void isNotNameable() {
   assertThat(it.isNameable(), is(false));
 }
 @Test
 public void encodesTheEmptyConstant() throws IOException {
   it = new ConstantType("");
   it.encodeConstant(out);
   assertThat(bytesOut.toByteArray(), is(new byte[] {}));
 }