Java tutorial
/* * Copyright 2011-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.lettuce.core.protocol; import static org.assertj.core.api.Assertions.assertThat; import java.nio.ByteBuffer; import java.util.Arrays; import org.junit.Test; import io.lettuce.core.codec.ByteArrayCodec; import io.lettuce.core.codec.Utf8StringCodec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; /** * @author Mark Paluch */ public class CommandArgsTest { private Utf8StringCodec codec = new Utf8StringCodec(); @Test public void getFirstIntegerShouldReturnNull() { CommandArgs<String, String> args = new CommandArgs<>(codec).add("foo"); assertThat(CommandArgsAccessor.getFirstInteger(args)).isNull(); } @Test public void getFirstIntegerShouldReturnFirstInteger() { CommandArgs<String, String> args = new CommandArgs<>(codec).add(1L).add(127).add(128).add(129).add(0) .add(-1); assertThat(CommandArgsAccessor.getFirstInteger(args)).isEqualTo(1L); } @Test public void getFirstIntegerShouldReturnFirstNegativeInteger() { CommandArgs<String, String> args = new CommandArgs<>(codec).add(-1L).add(-127).add(-128).add(-129); assertThat(CommandArgsAccessor.getFirstInteger(args)).isEqualTo(-1L); } @Test public void getFirstStringShouldReturnNull() { CommandArgs<String, String> args = new CommandArgs<>(codec).add(1); assertThat(CommandArgsAccessor.getFirstString(args)).isNull(); } @Test public void getFirstStringShouldReturnFirstString() { CommandArgs<String, String> args = new CommandArgs<>(codec).add("one").add("two"); assertThat(CommandArgsAccessor.getFirstString(args)).isEqualTo("one"); } @Test public void getFirstCharArrayShouldReturnCharArray() { CommandArgs<String, String> args = new CommandArgs<>(codec).add(1L).add("two".toCharArray()); assertThat(CommandArgsAccessor.getFirstCharArray(args)).isEqualTo("two".toCharArray()); } @Test public void getFirstCharArrayShouldReturnNull() { CommandArgs<String, String> args = new CommandArgs<>(codec).add(1L); assertThat(CommandArgsAccessor.getFirstCharArray(args)).isNull(); } @Test public void getFirstEncodedKeyShouldReturnNull() { CommandArgs<String, String> args = new CommandArgs<>(codec).add(1L); assertThat(CommandArgsAccessor.getFirstString(args)).isNull(); } @Test public void getFirstEncodedKeyShouldReturnFirstKey() { CommandArgs<String, String> args = new CommandArgs<>(codec).addKey("one").addKey("two"); assertThat(CommandArgsAccessor.encodeFirstKey(args)).isEqualTo(ByteBuffer.wrap("one".getBytes())); } @Test public void addValues() { CommandArgs<String, String> args = new CommandArgs<>(codec).addValues(Arrays.asList("1", "2")); ByteBuf buffer = Unpooled.buffer(); args.encode(buffer); ByteBuf expected = Unpooled.buffer(); expected.writeBytes(("$1\r\n" + "1\r\n" + "$1\r\n" + "2\r\n").getBytes()); assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII)); } @Test public void addByte() { CommandArgs<String, String> args = new CommandArgs<>(codec).add("one".getBytes()); ByteBuf buffer = Unpooled.buffer(); args.encode(buffer); ByteBuf expected = Unpooled.buffer(); expected.writeBytes(("$3\r\n" + "one\r\n").getBytes()); assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII)); } @Test public void addByteUsingByteCodec() { CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).add("one".getBytes()); ByteBuf buffer = Unpooled.buffer(); args.encode(buffer); ByteBuf expected = Unpooled.buffer(); expected.writeBytes(("$3\r\n" + "one\r\n").getBytes()); assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII)); } @Test public void addValueUsingByteCodec() { CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes()); ByteBuf buffer = Unpooled.buffer(); args.encode(buffer); ByteBuf expected = Unpooled.buffer(); expected.writeBytes(("$3\r\n" + "one\r\n").getBytes()); assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII)); } @Test public void addKeyUsingByteCodec() { CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes()); ByteBuf buffer = Unpooled.buffer(); args.encode(buffer); ByteBuf expected = Unpooled.buffer(); expected.writeBytes(("$3\r\n" + "one\r\n").getBytes()); assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII)); } }