test/jdk/build/translations/VerifyTranslations.java
author herrick
Thu, 10 Jan 2019 13:13:56 -0500
branchJDK-8200758-branch
changeset 57105 3409e81fb3cb
parent 51207 fc24da4898f1
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50590
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     1
/*
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     4
 *
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     8
 *
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    13
 * accompanied this code).
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    14
 *
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    18
 *
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    21
 * questions.
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    22
 */
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    23
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    24
import java.io.BufferedInputStream;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    25
import java.io.FileInputStream;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    26
import java.io.IOException;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    27
import java.io.PrintWriter;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    28
import java.io.StringWriter;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    29
import java.util.Set;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    30
import java.util.regex.Matcher;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    31
import java.util.regex.Pattern;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    32
import java.util.zip.ZipEntry;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    33
import java.util.zip.ZipInputStream;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    34
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    35
/**
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    36
 * @test
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    37
 * @requires release.implementor == "Oracle Corporation"
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    38
 * @modules jdk.jlink/jdk.tools.jimage
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    39
 * @summary Oracle builds of OpenJDK should only contain english, chinese and
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    40
 *          japanese translations
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    41
 */
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    42
public class VerifyTranslations {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    43
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    44
    /**
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    45
     * The set of translations we want to see in an Oracle built image
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    46
     */
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    47
    private static final Set<String> VALID_TRANSLATION_SUFFIXES = Set.of(
51207
fc24da4898f1 8207243: Fix translation filtering to also support zh_HK and zh_TW
erikj
parents: 50590
diff changeset
    48
            "_en", "_en_US", "_en_US_POSIX", "_ja", "_zh_CN"
50590
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    49
    );
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    50
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    51
    /**
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    52
     * This regexp will not match locales with 3 letter lang strings because
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    53
     * doing so would trigger a ton of false positives all over the source
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    54
     * tree. This is ok for now but is a potential future flaw in the test.
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    55
     */
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    56
    private static final String BASE_LOCALE_REGEXP
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    57
            = "(_[a-z]{2}(_[A-Z][a-z]{3})?(_([A-Z]{2})|([0-9]{3}))?(_[a-zA-Z]+)?)";
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    58
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    59
    public static void main(String[] args) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    60
        String jdkPath = System.getProperty("test.jdk");
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    61
        String modulesFile = jdkPath + "/lib/modules";
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    62
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    63
        // Run jimage tool to extract list of all classes and resources in the jdk
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    64
        StringWriter output = new StringWriter();
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    65
        jdk.tools.jimage.Main.run(new String[] { "list", modulesFile }, new PrintWriter(output));
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    66
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    67
        Pattern classesLocalePattern = Pattern.compile(BASE_LOCALE_REGEXP + "\\.(class|properties)");
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    68
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    69
        boolean failed = false;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    70
        String module = "";
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    71
        for (String line : output.toString().split("\n")) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    72
            if (line.startsWith("Module: ")) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    73
                module = line.substring(8).trim();
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    74
            }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    75
            // We do not filter resources in jdk.localedata
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    76
            if (!module.equals("jdk.localedata")) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    77
                Matcher matcher = classesLocalePattern.matcher(line);
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    78
                if (matcher.find()) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    79
                    if (!VALID_TRANSLATION_SUFFIXES.contains(matcher.group(1))) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    80
                        System.out.println("Unsupported translation found in lib/modules: "
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    81
                                + module + "/" + line.trim());
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    82
                        failed = true;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    83
                    }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    84
                }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    85
            }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    86
        }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    87
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    88
        // Check all files in src.zip
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    89
        Pattern sourceLocalePattern = Pattern.compile(BASE_LOCALE_REGEXP + "\\.java");
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    90
        String srcZip = jdkPath + "/lib/src.zip";
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    91
        try (ZipInputStream srcZipInput = new ZipInputStream(
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    92
                new BufferedInputStream(new FileInputStream(srcZip)))) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    93
            ZipEntry entry;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    94
            while ((entry = srcZipInput.getNextEntry()) != null) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    95
                String name = entry.getName();
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    96
                if (!name.startsWith("jdk.localedata")) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    97
                    Matcher matcher = sourceLocalePattern.matcher(name);
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    98
                    if (matcher.find()) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
    99
                        if (!VALID_TRANSLATION_SUFFIXES.contains(matcher.group(1))) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   100
                            System.out.println("Unsupported translation found in lib/src.zip: " + name);
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   101
                            failed = true;
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   102
                        }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   103
                    }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   104
                }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   105
            }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   106
        } catch (IOException e) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   107
            throw new RuntimeException(e);
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   108
        }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   109
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   110
        if (failed) {
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   111
            throw new RuntimeException("lib/modules contains unsupported translations");
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   112
        }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   113
    }
5fa19bad622d 8204973: Add build support for filtering translations
erikj
parents:
diff changeset
   114
}