test/jdk/java/lang/String/TranslateEscapes.java
changeset 55261 dff30b1557ee
child 58713 ad69fd32778e
equal deleted inserted replaced
55260:cc0f117f4405 55261:dff30b1557ee
       
     1 /*
       
     2  * Copyright (c) 2019, 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 8223780
       
    27  * @summary This exercises String#translateEscapes patterns and limits.
       
    28  * @run main TranslateEscapes
       
    29  */
       
    30 
       
    31 public class TranslateEscapes {
       
    32     public static void main(String... arg) {
       
    33         test1();
       
    34         test2();
       
    35         test3();
       
    36     }
       
    37 
       
    38     /*
       
    39      * Standard escapes.
       
    40      */
       
    41     static void test1() {
       
    42         verifyEscape("b", '\b');
       
    43         verifyEscape("f", '\f');
       
    44         verifyEscape("n", '\n');
       
    45         verifyEscape("r", '\r');
       
    46         verifyEscape("t", '\t');
       
    47         verifyEscape("'", '\'');
       
    48         verifyEscape("\"", '\"');
       
    49         verifyEscape("\\", '\\');
       
    50     }
       
    51 
       
    52     /*
       
    53      * Octal escapes.
       
    54      */
       
    55     static void test2() {
       
    56         verifyOctalEscape("0", 0);
       
    57         verifyOctalEscape("3", 03);
       
    58         verifyOctalEscape("7", 07);
       
    59         verifyOctalEscape("07", 07);
       
    60         verifyOctalEscape("17", 017);
       
    61         verifyOctalEscape("27", 027);
       
    62         verifyOctalEscape("37", 037);
       
    63         verifyOctalEscape("377", 0377);
       
    64 
       
    65         verifyOctalEscape("777", 077);
       
    66         verifyOctalEscape("78", 07);
       
    67     }
       
    68 
       
    69     /*
       
    70      * Exceptions.
       
    71      */
       
    72     static void test3() {
       
    73         exceptionThrown("+");
       
    74         exceptionThrown("\n");
       
    75     }
       
    76 
       
    77     static void verifyEscape(String string, char ch) {
       
    78         String escapes = "\\" + string;
       
    79         if (escapes.translateEscapes().charAt(0) != ch) {
       
    80             System.err.format("\"%s\" not escape \"%s\"'%n", string, escapes);
       
    81             throw new RuntimeException();
       
    82         }
       
    83     }
       
    84 
       
    85     static void verifyOctalEscape(String string, int octal) {
       
    86         String escapes = "\\" + string;
       
    87         if (escapes.translateEscapes().charAt(0) != octal) {
       
    88             System.err.format("\"%s\" not octal %o%n", string, octal);
       
    89             throw new RuntimeException();
       
    90         }
       
    91     }
       
    92 
       
    93     static void exceptionThrown(String string) {
       
    94         String escapes = "\\" + string;
       
    95         try {
       
    96             escapes.translateEscapes();
       
    97             System.err.format("escape not thrown for %s%n", string);
       
    98             throw new RuntimeException();
       
    99 
       
   100         } catch (IllegalArgumentException ex) {
       
   101             // okay
       
   102         }
       
   103     }
       
   104 }