Path: blob/master/test/jdk/java/lang/Character/UnicodeBlock/CheckBlocks.java
41153 views
/*1* Copyright (c) 2007, 2019, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* @test27* @bug 4830803 4886934 6565620 6959267 7070436 7198195 8032446 8072600 820277128* 822143129* @summary Check that the UnicodeBlock forName() method works as expected30* and block ranges are correct for all Unicode characters.31* @library /lib/testlibrary/java/lang32* @run main CheckBlocks33* @author John O'Conner34*/3536import java.lang.Character.UnicodeBlock;37import java.lang.reflect.Field;38import java.io.BufferedReader;39import java.io.File;40import java.io.FileReader;41import java.util.HashSet;42import java.util.Locale;4344public class CheckBlocks {4546static boolean err = false;47static Class<?> clazzUnicodeBlock;4849public static void main(String[] args) throws Exception {50generateBlockList();5152try {53clazzUnicodeBlock = Class.forName("java.lang.Character$UnicodeBlock");54} catch (ClassNotFoundException e) {55throw new RuntimeException("Class.forName(\"java.lang.Character$UnicodeBlock\") failed.");56}5758for (Block blk : blocks) {59test4830803_1(blk);60test4830803_2();61test4886934(blk);62}6364test8202771();6566if (err) {67throw new RuntimeException("Failed");68} else {69System.out.println("Passed");70}71}7273/**74* Check that the UnicodeBlock forName() method works as expected.75*/76private static void test4830803_1(Block blk) throws Exception {7778/*79* Try 3 forms of block name in the forName() method. Each form should80* produce the same expected block.81*/82String blkName = blk.getName();8384// For backward compatibility85switch (blkName) {86case "COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS":87blkName = "COMBINING_MARKS_FOR_SYMBOLS";88System.out.println("*** COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS"89+ " is replaced with COMBINING_MARKS_FOR_SYMBOLS"90+ " for backward compatibility.");91break;92case "GREEK_AND_COPTIC":93blkName = "GREEK";94System.out.println("*** GREEK_AND_COPTIC is replaced with GREEK"95+ " for backward compatibility.");96break;97case "CYRILLIC_SUPPLEMENT":98blkName = "CYRILLIC_SUPPLEMENTARY";99System.out.println("*** CYRILLIC_SUPPLEMENT is replaced with"100+ " CYRILLIC_SUPPLEMENTARY for backward compatibility.");101break;102default:103break;104}105106String expectedBlock = null;107try {108expectedBlock = clazzUnicodeBlock.getField(blkName).getName();109} catch (NoSuchFieldException | SecurityException e) {110System.err.println("Error: " + blkName + " was not found.");111err = true;112return;113}114115String canonicalBlockName = blk.getOriginalName();116String idBlockName = expectedBlock;117String regexBlockName = toRegExString(canonicalBlockName);118119if (regexBlockName == null) {120System.err.println("Error: Block name which was processed with regex was null.");121err = true;122return;123}124125if (!expectedBlock.equals(UnicodeBlock.forName(canonicalBlockName).toString())) {126System.err.println("Error #1: UnicodeBlock.forName(\"" +127canonicalBlockName + "\") returned wrong value.\n\tGot: " +128UnicodeBlock.forName(canonicalBlockName) +129"\n\tExpected: " + expectedBlock);130err = true;131}132133if (!expectedBlock.equals(UnicodeBlock.forName(idBlockName).toString())) {134System.err.println("Error #2: UnicodeBlock.forName(\"" +135idBlockName + "\") returned wrong value.\n\tGot: " +136UnicodeBlock.forName(idBlockName) +137"\n\tExpected: " + expectedBlock);138err = true;139}140141if (!expectedBlock.equals(UnicodeBlock.forName(regexBlockName).toString())) {142System.err.println("Error #3: UnicodeBlock.forName(\"" +143regexBlockName + "\") returned wrong value.\n\tGot: " +144UnicodeBlock.forName(regexBlockName) +145"\n\tExpected: " + expectedBlock);146err = true;147}148}149150/**151* now try a bad block name. This should produce an IAE.152*/153private static void test4830803_2() {154boolean threwExpected = false;155156try {157UnicodeBlock block = UnicodeBlock.forName("notdefined");158}159catch(IllegalArgumentException e) {160threwExpected = true;161}162163if (threwExpected == false) {164System.err.println("Error: UnicodeBlock.forName(\"notdefined\") should throw IllegalArgumentException.");165err = true;166}167}168169/**170* Convert the argument to a block name form used by the regex package.171* That is, remove all spaces.172*/173private static String toRegExString(String str) {174String[] tokens = null;175StringBuilder retStr = new StringBuilder();176try {177tokens = str.split(" ");178}179catch(java.util.regex.PatternSyntaxException e) {180return null;181}182for(int x=0; x < tokens.length; ++x) {183retStr.append(tokens[x]);184}185return retStr.toString();186}187188private static void test4886934(Block blk) {189String blkName = blk.getName();190String blkOrigName = blk.getOriginalName();191UnicodeBlock block;192String blockName;193194// For backward compatibility195switch (blkName) {196case "COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS":197blkName = "COMBINING_MARKS_FOR_SYMBOLS";198System.out.println("*** COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS"199+ " is replaced with COMBINING_MARKS_FOR_SYMBOLS"200+ " for backward compatibility.");201break;202case "GREEK_AND_COPTIC":203blkName = "GREEK";204System.out.println("*** GREEK_AND_COPTIC is replaced with GREEK"205+ " for backward compatibility.");206break;207case "CYRILLIC_SUPPLEMENT":208blkName = "CYRILLIC_SUPPLEMENTARY";209System.out.println("*** CYRILLIC_SUPPLEMENT is replaced with"210+ " CYRILLIC_SUPPLEMENTARY for backward compatibility.");211break;212default:213break;214}215216for (int ch = blk.getBegin(); ch <= blk.getEnd(); ch++) {217block = UnicodeBlock.of(ch);218if (block == null) {219System.err.println("Error: The block for " + blkName220+ " is missing. Please check java.lang.Character.UnicodeBlock.");221err = true;222break;223}224blockName = block.toString();225if (!blockName.equals(blkName)) {226System.err.println("Error: Character(0x"227+ Integer.toHexString(ch).toUpperCase()228+ ") should be in \"" + blkName + "\" block "229+ "(Block name is \"" + blkOrigName + "\")"230+ " but found in \"" + blockName + "\" block.");231err = true;232}233}234}235236/**237* Check if every Field of Character.UnicodeBlock is a valid Unicode Block.238*/239private static void test8202771() {240Field[] fields = clazzUnicodeBlock.getFields();241242for (Field f : fields) {243// Handle Deprecated field "SURROGATES_AREA".244if (f.getAnnotation(Deprecated.class) != null) {245continue;246}247248String blkName = f.getName();249switch (blkName) {250case "COMBINING_MARKS_FOR_SYMBOLS":251validateBlock("COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS");252break;253case "GREEK":254validateBlock("GREEK_AND_COPTIC");255break;256case "CYRILLIC_SUPPLEMENTARY":257validateBlock("CYRILLIC_SUPPLEMENT");258break;259default:260validateBlock(blkName);261break;262}263}264}265266private static void validateBlock(String blkName) {267for (Block block : blocks) {268String blockName = block.getName();269if (blockName.equals(blkName)) {270return;271}272}273err = true;274System.err.println(blkName + " is not a valid Unicode Block.");275}276277// List of all Unicode blocks, their start, and end codepoints.278public static HashSet<Block> blocks = new HashSet<>();279280private static void generateBlockList() throws Exception {281File blockData = UCDFiles.BLOCKS.toFile();282try (BufferedReader f = new BufferedReader(new FileReader(blockData))) {283String line;284while ((line = f.readLine()) != null) {285if (line.length() == 0 || line.charAt(0) == '#') {286continue;287}288289int index1 = line.indexOf('.');290int begin = Integer.parseInt(line.substring(0, index1), 16);291int index2 = line.indexOf(';');292int end = Integer.parseInt(line.substring(index1 + 2, index2), 16);293String name = line.substring(index2 + 1).trim();294295System.out.println(" Adding a Block(" + Integer.toHexString(begin) + ", " + Integer.toHexString(end)296+ ", " + name + ")");297blocks.add(new Block(begin, end, name));298}299}300}301}302303class Block {304305public Block() {306blockBegin = 0;307blockEnd = 0;308blockName = null;309}310311public Block(int begin, int end, String name) {312blockBegin = begin;313blockEnd = end;314blockName = name.replaceAll("[ -]", "_").toUpperCase(Locale.ENGLISH);315originalBlockName = name;316}317318public int getBegin() {319return blockBegin;320}321322public int getEnd() {323return blockEnd;324}325326public String getName() {327return blockName;328}329330public String getOriginalName() {331return originalBlockName;332}333334@Override335public boolean equals(Object obj) {336if (obj == null) return false;337if (!(obj instanceof Block)) return false;338339Block other = (Block)obj;340return other.blockBegin == blockBegin &&341other.blockEnd == blockEnd &&342other.blockName.equals(blockName) &&343other.originalBlockName.equals(originalBlockName);344}345int blockBegin, blockEnd;346String blockName, originalBlockName;347}348349350