|
| 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 8325945 |
| 27 | + * @summary Test abridged VM String printing |
| 28 | + * @library /test/lib |
| 29 | + * @build jdk.test.whitebox.WhiteBox |
| 30 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 31 | + * @run main/othervm -Xbootclasspath/a:. -XX:+WhiteBoxAPI StringPrinting |
| 32 | + */ |
| 33 | + |
| 34 | +import jdk.test.whitebox.WhiteBox; |
| 35 | + |
| 36 | +public class StringPrinting { |
| 37 | + |
| 38 | + private static final WhiteBox WB = WhiteBox.getWhiteBox(); |
| 39 | + |
| 40 | + static void checkEqual(String s1, String s2) { |
| 41 | + if (!s1.equals(s2)) { |
| 42 | + throw new RuntimeException("Different strings: " + s1 + " vs " + s2); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + static void checkEqual(int len1, int len2) { |
| 47 | + if (len1 != len2) { |
| 48 | + throw new RuntimeException("Different lengths: " + len1 + " vs " + len2); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + // Modified string format is "xxx ... (N characters ommitted) ... xxx" (abridged) |
| 54 | + final String elipse = " ... "; |
| 55 | + final String text = " characters ommitted)"; |
| 56 | + final String abridged = "\" (abridged) "; |
| 57 | + |
| 58 | + // Define a set of maxLengths for ease of inspection in the test outout |
| 59 | + // Note: maxLength must be >= 2 |
| 60 | + int[] maxLengths = new int[] { 2, 3, 16, 256, 512 }; |
| 61 | + for (int maxLength : maxLengths) { |
| 62 | + // Test string lengths around maxLength and "much" bigger |
| 63 | + // than maxLength |
| 64 | + int[] strLengths = new int[] { maxLength - 1, |
| 65 | + maxLength, |
| 66 | + maxLength + 1, |
| 67 | + 2 * maxLength |
| 68 | + }; |
| 69 | + for (int length : strLengths) { |
| 70 | + System.out.println("Testing string length " + length + " with maxLength " + maxLength); |
| 71 | + String s = "x".repeat(length); |
| 72 | + String r = WB.printString(s, maxLength); |
| 73 | + if (length <= maxLength) { |
| 74 | + // Strip off the double-quotes that surround the string |
| 75 | + if (r.charAt(0) == '\"' && r.charAt(r.length() - 1) == '\"') { |
| 76 | + r = r.substring(1, r.length() - 1); |
| 77 | + } else { |
| 78 | + throw new RuntimeException("String was not quoted as expected: " + r); |
| 79 | + } |
| 80 | + checkEqual(s, r); |
| 81 | + } else { |
| 82 | + // Strip off leading double-quote |
| 83 | + if (r.charAt(0) == '\"') { |
| 84 | + r = r.substring(1, r.length()); |
| 85 | + } else { |
| 86 | + throw new RuntimeException("String was not quoted as expected: " + r); |
| 87 | + } |
| 88 | + |
| 89 | + // Strip off abridged |
| 90 | + if (r.endsWith(abridged)) { |
| 91 | + r = r.substring(0, r.length() - abridged.length()); |
| 92 | + } else { |
| 93 | + throw new RuntimeException("String was not marked abridged as expected: " + r); |
| 94 | + } |
| 95 | + |
| 96 | + // Now extract the two "halves" |
| 97 | + int elipseStart = r.indexOf(elipse); |
| 98 | + String firstHalf = r.substring(0, elipseStart); |
| 99 | + int secondElipseStart = r.lastIndexOf(elipse); |
| 100 | + String secondHalf = r.substring(secondElipseStart + elipse.length(), r.length()); |
| 101 | + |
| 102 | + System.out.println("S1: >" + firstHalf + "<"); |
| 103 | + System.out.println("S2: >" + secondHalf + "<"); |
| 104 | + |
| 105 | + checkEqual(firstHalf.length(), maxLength / 2); |
| 106 | + checkEqual(secondHalf.length(), maxLength /2); |
| 107 | + |
| 108 | + // Now check number of characters ommitted |
| 109 | + String tail = r.substring(r.indexOf("("), r.length()); |
| 110 | + int numberEnd = tail.indexOf(" "); |
| 111 | + String nChars = tail.substring(1, numberEnd); |
| 112 | + System.out.println("N: >" + nChars + "<"); |
| 113 | + |
| 114 | + // Now add all the bits back together to get the expected full length |
| 115 | + int fullLength = maxLength / 2 + elipse.length() + 1 /* for ( */ |
| 116 | + + nChars.length() + text.length() + elipse.length() + maxLength / 2; |
| 117 | + checkEqual(r.length(), fullLength); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments