test/jdk/java/lang/String/Formatted.java
author jlahoda
Mon, 21 Oct 2019 15:38:26 +0200
changeset 58713 ad69fd32778e
parent 55262 7d83cf1cfa74
child 59037 3d2575331a41
permissions -rw-r--r--
8226585: Improve javac messages for using a preview API Summary: Avoiding deprecation for removal for APIs associated with preview features, the features are marked with an annotation, and errors/warnings are produced for them based on the annotation. Reviewed-by: erikj, mcimadamore, alanb Contributed-by: joe.darcy@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55262
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     1
/*
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     4
 *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     8
 *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    13
 * accompanied this code).
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    14
 *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    18
 *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    21
 * questions.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    22
 */
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    23
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    24
/*
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    25
 * @test
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    26
 * bug 8203444
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    27
 * @summary Unit tests for instance versions of String#format
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 55262
diff changeset
    28
 * @compile --enable-preview -source 14 Formatted.java
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 55262
diff changeset
    29
 * @run main/othervm --enable-preview Formatted
55262
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    30
 */
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    31
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    32
import java.util.Locale;
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    33
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    34
public class Formatted {
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    35
    public static void main(String[] args) {
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    36
        test1();
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    37
    }
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    38
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    39
    /*
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    40
     * Test String#formatted(Object... args) functionality.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    41
     */
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    42
    static void test1() {
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    43
        check("formatted(Object... args)",
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    44
                "Test this %s".formatted("and that"),
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    45
                String.format("Test this %s", "and that"));
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    46
    }
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    47
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    48
    static void check(String test, String output, String expected) {
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    49
        if (output != expected && (output == null || !output.equals(expected))) {
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    50
            System.err.println("Testing " + test + ": unexpected result");
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    51
            System.err.println("Output: " + output);
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    52
            System.err.println("Expected: " + expected);
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    53
            throw new RuntimeException();
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    54
        }
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    55
    }
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents:
diff changeset
    56
}