|
| 1 | +/* |
| 2 | + * Copyright 2023 asyncer.io projects |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.asyncer.r2dbc.mysql.codec; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.Unpooled; |
| 21 | +import org.testcontainers.shaded.org.bouncycastle.util.encoders.Hex; |
| 22 | + |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.nio.charset.Charset; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.Arrays; |
| 27 | + |
| 28 | +/** |
| 29 | + * Unit tests for {@link ByteArrayInputStreamCodec}. |
| 30 | + */ |
| 31 | +public class ByteArrayInputStreamCodecTest implements CodecTestSupport<ByteArrayInputStream> { |
| 32 | + |
| 33 | + private final byte[][] rawData = { |
| 34 | + new byte[0], |
| 35 | + new byte[] { 0x7F }, |
| 36 | + new byte[] { 0x12, 34, 0x56, 78, (byte) 0x9A }, |
| 37 | + "Hello world!".getBytes(StandardCharsets.US_ASCII), |
| 38 | + new byte[] { (byte) 0xFE, (byte) 0xDC, (byte) 0xBA }, |
| 39 | + }; |
| 40 | + |
| 41 | + private final ByteArrayInputStream[] data = Arrays.stream(rawData) |
| 42 | + .map(ByteArrayInputStream::new) |
| 43 | + .toArray(ByteArrayInputStream[]::new); |
| 44 | + |
| 45 | + @Override |
| 46 | + public Codec<ByteArrayInputStream> getCodec() { |
| 47 | + return ByteArrayInputStreamCodec.INSTANCE; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public ByteArrayInputStream[] originParameters() { |
| 52 | + return data; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Object[] stringifyParameters() { |
| 57 | + return Arrays.stream(rawData) |
| 58 | + .map(bytes -> String.format("x'%s'", Hex.toHexString(bytes))) |
| 59 | + .toArray(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public ByteBuf[] binaryParameters(Charset charset) { |
| 64 | + return Arrays.stream(rawData) |
| 65 | + .map(Unpooled::wrappedBuffer) |
| 66 | + .toArray(ByteBuf[]::new); |
| 67 | + } |
| 68 | +} |
0 commit comments