Path: blob/master/test/jdk/build/translations/VerifyTranslations.java
41145 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedInputStream;24import java.io.FileInputStream;25import java.io.IOException;26import java.io.PrintWriter;27import java.io.StringWriter;28import java.util.Set;29import java.util.regex.Matcher;30import java.util.regex.Pattern;31import java.util.zip.ZipEntry;32import java.util.zip.ZipInputStream;3334/**35* @test36* @requires release.implementor == "Oracle Corporation"37* @modules jdk.jlink/jdk.tools.jimage38* @summary Oracle builds of OpenJDK should only contain english, chinese and39* japanese translations40*/41public class VerifyTranslations {4243/**44* The set of translations we want to see in an Oracle built image45*/46private static final Set<String> VALID_TRANSLATION_SUFFIXES = Set.of(47"_en", "_en_US", "_en_US_POSIX", "_ja", "_zh_CN"48);4950/**51* This regexp will not match locales with 3 letter lang strings because52* doing so would trigger a ton of false positives all over the source53* tree. This is ok for now but is a potential future flaw in the test.54*/55private static final String BASE_LOCALE_REGEXP56= "(_[a-z]{2}(_[A-Z][a-z]{3})?(_([A-Z]{2})|([0-9]{3}))?(_[a-zA-Z]+)?)";5758public static void main(String[] args) {59String jdkPath = System.getProperty("test.jdk");60String modulesFile = jdkPath + "/lib/modules";6162// Run jimage tool to extract list of all classes and resources in the jdk63StringWriter output = new StringWriter();64jdk.tools.jimage.Main.run(new String[] { "list", modulesFile }, new PrintWriter(output));6566Pattern classesLocalePattern = Pattern.compile(BASE_LOCALE_REGEXP + "\\.(class|properties)");6768boolean failed = false;69String module = "";70for (String line : output.toString().split("\n")) {71if (line.startsWith("Module: ")) {72module = line.substring(8).trim();73}74// We do not filter resources in jdk.localedata75if (!module.equals("jdk.localedata")) {76Matcher matcher = classesLocalePattern.matcher(line);77if (matcher.find()) {78if (!VALID_TRANSLATION_SUFFIXES.contains(matcher.group(1))) {79System.out.println("Unsupported translation found in lib/modules: "80+ module + "/" + line.trim());81failed = true;82}83}84}85}8687// Check all files in src.zip88Pattern sourceLocalePattern = Pattern.compile(BASE_LOCALE_REGEXP + "\\.java");89String srcZip = jdkPath + "/lib/src.zip";90try (ZipInputStream srcZipInput = new ZipInputStream(91new BufferedInputStream(new FileInputStream(srcZip)))) {92ZipEntry entry;93while ((entry = srcZipInput.getNextEntry()) != null) {94String name = entry.getName();95if (!name.startsWith("jdk.localedata")) {96Matcher matcher = sourceLocalePattern.matcher(name);97if (matcher.find()) {98if (!VALID_TRANSLATION_SUFFIXES.contains(matcher.group(1))) {99System.out.println("Unsupported translation found in lib/src.zip: " + name);100failed = true;101}102}103}104}105} catch (IOException e) {106throw new RuntimeException(e);107}108109if (failed) {110throw new RuntimeException("lib/modules contains unsupported translations");111}112}113}114115116