|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8337506 |
| 27 | + * @summary Verify Command Line arguments are not mapped with |
| 28 | + * "best-fit" mappings on Windows |
| 29 | + * @requires (os.family == "windows") |
| 30 | + * @library /test/lib |
| 31 | + * @run junit DisableBestFitMappingTest |
| 32 | + */ |
| 33 | +import java.nio.ByteBuffer; |
| 34 | +import java.nio.charset.Charset; |
| 35 | +import java.nio.charset.CharsetEncoder; |
| 36 | +import java.util.stream.Stream; |
| 37 | +import jdk.test.lib.process.ProcessTools; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.Arguments; |
| 42 | +import org.junit.jupiter.params.provider.MethodSource; |
| 43 | + |
| 44 | +import static org.junit.jupiter.api.Assertions.*; |
| 45 | +import static org.junit.jupiter.api.Assumptions.*; |
| 46 | + |
| 47 | +public class DisableBestFitMappingTest { |
| 48 | + private static final CharsetEncoder NATIVE_ENC = |
| 49 | + Charset.forName(System.getProperty("native.encoding")).newEncoder(); |
| 50 | + private static final String REPLACEMENT = |
| 51 | + NATIVE_ENC.charset().decode(ByteBuffer.wrap(NATIVE_ENC.replacement())).toString(); |
| 52 | + private static final int EXIT_SUCCESS = 0; |
| 53 | + private static final int EXIT_FAILURE = -1; |
| 54 | + |
| 55 | + static Stream<Arguments> CMD_ARGS() { |
| 56 | + return Stream.of( |
| 57 | + Arguments.of("aa\uff02 \uff02bb", "aa" + REPLACEMENT + " " + REPLACEMENT + "bb"), |
| 58 | + Arguments.of("aa\uff01bb", "aa" + REPLACEMENT + "bb"), |
| 59 | + Arguments.of("aa\u221ebb", "aa" + REPLACEMENT + "bb") |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @MethodSource("CMD_ARGS") |
| 65 | + void testDisableBestFitMapping(String arg, String expected) throws Exception { |
| 66 | + // Only execute if the arg cannot be encoded |
| 67 | + assumeFalse(NATIVE_ENC.canEncode(arg), |
| 68 | + "native.encoding (%s) can encode the argument '%s'. Test ignored." |
| 69 | + .formatted(NATIVE_ENC.charset(), arg)); |
| 70 | + |
| 71 | + var result= ProcessTools.executeTestJava( |
| 72 | + DisableBestFitMappingTest.class.getSimpleName(), arg, expected); |
| 73 | + result.asLines().forEach(System.out::println); |
| 74 | + assertEquals(EXIT_SUCCESS, result.getExitValue(), |
| 75 | + "Disabling best-fit mapping failed"); |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String... args) { |
| 79 | + System.out.println(args[0]); |
| 80 | + System.out.println(args[1]); |
| 81 | + System.exit(args[0].equals(args[1]) ? EXIT_SUCCESS : EXIT_FAILURE); |
| 82 | + } |
| 83 | +} |
0 commit comments