|
Java example source code file (RedisEncoderTest.java)
The RedisEncoderTest.java Java example source code/* * Copyright 2016 The Netty Project * * The Netty Project licenses this file to you 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.netty.handler.codec.redis; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.List; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import static io.netty.handler.codec.redis.RedisCodecTestUtil.*; /** * Verifies the correct functionality of the {@link RedisEncoder}. */ public class RedisEncoderTest { private EmbeddedChannel channel; @Before public void setup() throws Exception { channel = new EmbeddedChannel(new RedisEncoder()); } @After public void teardown() throws Exception { assertFalse(channel.finish()); } @Test public void shouldEncodeSimpleString() { RedisMessage msg = new SimpleStringRedisMessage("simple"); boolean result = channel.writeOutbound(msg); assertThat(result, is(true)); ByteBuf written = readAll(channel); assertThat(bytesOf(written), is(bytesOf("+simple\r\n"))); written.release(); } @Test public void shouldEncodeError() { RedisMessage msg = new ErrorRedisMessage("error1"); boolean result = channel.writeOutbound(msg); assertThat(result, is(true)); ByteBuf written = readAll(channel); assertThat(bytesOf(written), is(equalTo(bytesOf("-error1\r\n")))); written.release(); } @Test public void shouldEncodeInteger() { RedisMessage msg = new IntegerRedisMessage(1234L); boolean result = channel.writeOutbound(msg); assertThat(result, is(true)); ByteBuf written = readAll(channel); assertThat(bytesOf(written), is(equalTo(bytesOf(":1234\r\n")))); written.release(); } @Test public void shouldEncodeBulkStringContent() { RedisMessage header = new BulkStringHeaderRedisMessage(16); RedisMessage body1 = new DefaultBulkStringRedisContent(byteBufOf("bulk\nstr").retain()); RedisMessage body2 = new DefaultLastBulkStringRedisContent(byteBufOf("ing\ntest").retain()); assertThat(channel.writeOutbound(header), is(true)); assertThat(channel.writeOutbound(body1), is(true)); assertThat(channel.writeOutbound(body2), is(true)); ByteBuf written = readAll(channel); assertThat(bytesOf(written), is(equalTo(bytesOf("$16\r\nbulk\nstring\ntest\r\n")))); written.release(); } @Test public void shouldEncodeFullBulkString() { ByteBuf bulkString = byteBufOf("bulk\nstring\ntest").retain(); int length = bulkString.readableBytes(); RedisMessage msg = new FullBulkStringRedisMessage(bulkString); boolean result = channel.writeOutbound(msg); assertThat(result, is(true)); ByteBuf written = readAll(channel); assertThat(bytesOf(written), is(equalTo(bytesOf("$" + length + "\r\nbulk\nstring\ntest\r\n")))); written.release(); } @Test public void shouldEncodeSimpleArray() { List<RedisMessage> children = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java RedisEncoderTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.