|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.util; |
| 15 | + |
| 16 | +import java.nio.ByteBuffer; |
| 17 | +import java.nio.charset.Charset; |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.Arguments; |
| 23 | +import org.junit.jupiter.params.provider.MethodSource; |
| 24 | + |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | +import static org.hamcrest.Matchers.is; |
| 28 | + |
| 29 | +// @checkstyle-disable-check : AvoidEscapedUnicodeCharactersCheck |
| 30 | +public class CharsetStringBuilderTest |
| 31 | +{ |
| 32 | + public static Stream<Arguments> tests() |
| 33 | + { |
| 34 | + return Stream.of( |
| 35 | + Arguments.of("Hello World \uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-16 Æ\tÿ!!!", StandardCharsets.UTF_16), |
| 36 | + Arguments.of("Hello World \uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-8 Æ\tÿ!!!", StandardCharsets.UTF_8), |
| 37 | + Arguments.of("Now is the time for all good men to test US_ASCII \r\n\t!", StandardCharsets.US_ASCII), |
| 38 | + Arguments.of("How Now Brown Cow. Test iso 8859 Æ\tÿ!", StandardCharsets.ISO_8859_1) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + @ParameterizedTest |
| 43 | + @MethodSource("tests") |
| 44 | + public void testBuilder(String test, Charset charset) throws Exception |
| 45 | + { |
| 46 | + byte[] bytes = test.getBytes(charset); |
| 47 | + |
| 48 | + CharsetStringBuilder builder = CharsetStringBuilder.forCharset(charset); |
| 49 | + |
| 50 | + builder.append(bytes); |
| 51 | + assertThat(builder.build(), equalTo(test)); |
| 52 | + |
| 53 | + for (byte b : bytes) |
| 54 | + builder.append(b); |
| 55 | + assertThat(builder.build(), equalTo(test)); |
| 56 | + |
| 57 | + builder.append(bytes[0]); |
| 58 | + builder.append(bytes, 1, bytes.length - 1); |
| 59 | + assertThat(builder.build(), equalTo(test)); |
| 60 | + } |
| 61 | + |
| 62 | + public static Stream<Charset> charsets() |
| 63 | + { |
| 64 | + return Stream.of( |
| 65 | + StandardCharsets.UTF_8, |
| 66 | + StandardCharsets.ISO_8859_1, |
| 67 | + StandardCharsets.US_ASCII, |
| 68 | + StandardCharsets.UTF_16 |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest |
| 73 | + @MethodSource("charsets") |
| 74 | + public void testBasicApi(Charset charset) throws Exception |
| 75 | + { |
| 76 | + CharsetStringBuilder builder = CharsetStringBuilder.forCharset(charset); |
| 77 | + ByteBuffer encoded = charset.encode("1"); |
| 78 | + while (encoded.hasRemaining()) |
| 79 | + builder.append(encoded.get()); |
| 80 | + |
| 81 | + builder.append('2'); |
| 82 | + |
| 83 | + builder.append(charset.encode("34")); |
| 84 | + |
| 85 | + encoded = charset.encode("abc"); |
| 86 | + int offset = encoded.remaining(); |
| 87 | + encoded = charset.encode("abc56"); |
| 88 | + int length = encoded.remaining() - offset; |
| 89 | + encoded = charset.encode("abc56xyz"); |
| 90 | + byte[] bytes = new byte[1028]; |
| 91 | + encoded.get(bytes, 0, encoded.remaining()); |
| 92 | + builder.append(bytes, offset, length); |
| 93 | + |
| 94 | + encoded = charset.encode("abc78xyz"); |
| 95 | + encoded.position(offset); |
| 96 | + encoded.limit(offset + length); |
| 97 | + builder.append(encoded); |
| 98 | + |
| 99 | + builder.append("9A", 0, 2); |
| 100 | + builder.append("xyzBCpqy", 3, 2); |
| 101 | + |
| 102 | + assertThat(builder.build(), is("123456789ABC")); |
| 103 | + } |
| 104 | +} |
0 commit comments