Package net.rubyeye.xmemcached.test.unittest.commands.binary

Source Code of net.rubyeye.xmemcached.test.unittest.commands.binary.BinaryAppendPrependCommandUnitTest

package net.rubyeye.xmemcached.test.unittest.commands.binary;

import java.nio.ByteBuffer;

import net.rubyeye.xmemcached.command.Command;
import net.rubyeye.xmemcached.command.binary.OpCode;
import net.rubyeye.xmemcached.utils.ByteUtils;

public class BinaryAppendPrependCommandUnitTest extends
    BaseBinaryCommandUnitTest {
  String key = "hello";
  byte[] keyBytes = ByteUtils.getBytes(this.key);
  String value = "!";
  boolean noreply = false;

  public void testAppendEncodeAndDecode() {

    Command command = this.commandFactory.createAppendCommand(this.key,
        this.keyBytes, this.value, this.noreply, this.transcoder);
    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(30, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    assertEquals(OpCode.APPEND.fieldValue(), opCode);

    ByteBuffer buffer = constructResponse(OpCode.APPEND.fieldValue(),
        (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 1L, null, null,
        null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer = constructResponse(OpCode.APPEND.fieldValue(), (short) 0,
        (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 1L, null, null, null);
    command = this.commandFactory.createAppendCommand(this.key, this.keyBytes, this.value,
        this.noreply, this.transcoder);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }

  public void testPrependEncodeAndDecode() {

    Command command = this.commandFactory.createPrependCommand(this.key,
        this.keyBytes, this.value, this.noreply, this.transcoder);
    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(30, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    assertEquals(OpCode.PREPEND.fieldValue(), opCode);

    ByteBuffer buffer = constructResponse(OpCode.PREPEND.fieldValue(),
        (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 1L, null, null,
        null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer = constructResponse(OpCode.PREPEND.fieldValue(), (short) 0,
        (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 1L, null, null, null);
    command = this.commandFactory.createPrependCommand(this.key, this.keyBytes,
        this.value, this.noreply, this.transcoder);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }
}
TOP

Related Classes of net.rubyeye.xmemcached.test.unittest.commands.binary.BinaryAppendPrependCommandUnitTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.