1 /* 2 * Copyright (c) 2018, 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 4221795 8032446 27 * @summary Confirm that java.text.Normalizer and sun.text.Normalize are 28 * thread-safe. 29 * @modules java.base/sun.text java.base/sun.text.normalizer 30 * @compile -XDignore.symbol.file ThreadSafeTest.java 31 * @run main/othervm -esa ThreadSafeTest 5 10 32 */ 33 34 // Usage: java ThreadSafeTest [threadsFactor [duration]] 35 public class ThreadSafeTest { 36 37 static volatile boolean runrun = true; 38 static volatile boolean error = false; 39 40 public static void main(String[] args) throws Exception { 41 int threadsFactor = 5; 42 if (args.length > 0) { 43 threadsFactor = Math.max(Integer.parseInt(args[0]), 5); 44 } 45 int duration = 180; 46 if (args.length > 1) { 47 duration = Math.max(5, Integer.parseInt(args[1])); 48 } 49 int nProcessors = Runtime.getRuntime().availableProcessors(); 50 int nTasks = nProcessors * threadsFactor; 51 Thread[] tasks = new Thread[nTasks]; 52 53 System.out.println("Testing with " + nTasks + " threads on " + 54 nProcessors + " processors for " + duration + 55 " seconds."); 56 57 for (int i = 0; i < nTasks; i++) { 58 tasks[i] = new Thread(new Worker()); 59 } 60 for (int i = 0; i < nTasks; i++) { 61 tasks[i].start(); 62 } 63 64 try { 65 for (int i = 0; runrun && i < duration; i++) { 66 Thread.sleep(1000); // 1 second 67 } 68 runrun = false; 69 for (int i = 0; i < nTasks; i++) { 70 tasks[i].join(); 71 } 72 } 73 catch (InterruptedException e) { 74 } 75 76 if (error) { 77 throw new RuntimeException("Normalizer is not thread-safe."); 78 } 79 } 80 81 static void testJavaNormalize(int num, java.text.Normalizer.Form form) { 82 String got = java.text.Normalizer.normalize(data[num][0], form); 83 if (!got.equals(data[num][1])) { 84 System.err.println("java.text.Normalizer.normalize(" + 85 form.toString() + ") failed."); 86 error = true; 87 } 88 } 89 90 static void testSunNormalize(int num, java.text.Normalizer.Form form, 91 int option) { 92 String got = sun.text.Normalizer.normalize(data[num][0], form, option); 93 if (!got.equals(data[num][1])) { 94 System.err.println("sun.text.Normalizer.normalize(" + 95 form.toString() + ", " + 96 Integer.toHexString(option) + ") failed."); 97 error = true; 98 } 99 } 100 101 static void testIsNormalized(int num, java.text.Normalizer.Form form) { 102 boolean normalized = java.text.Normalizer.isNormalized(data[num][1], form); 103 if (!normalized) { 104 System.err.println("java.text.Normalizer.isNormalized(" + 105 form.toString() + ") failed."); 106 error = true; 107 } 108 } 109 110 static class Worker implements Runnable { 111 public void run() { 112 while (runrun) { 113 testJavaNormalize(0, java.text.Normalizer.Form.NFKC); 114 testSunNormalize(1, java.text.Normalizer.Form.NFC, 115 sun.text.Normalizer.UNICODE_3_2); 116 testJavaNormalize(2, java.text.Normalizer.Form.NFKD); 117 testSunNormalize(3, java.text.Normalizer.Form.NFC, 118 sun.text.normalizer.NormalizerBase.UNICODE_LATEST); 119 testJavaNormalize(4, java.text.Normalizer.Form.NFD); 120 121 testIsNormalized(0, java.text.Normalizer.Form.NFKC); 122 testIsNormalized(2, java.text.Normalizer.Form.NFKD); 123 testIsNormalized(4, java.text.Normalizer.Form.NFD); 124 125 if (error) { 126 runrun = false; 127 return; 128 } 129 } 130 } 131 } 132 133 static final String[][] data = { 134 /* From: To: */ 135 {"A\u0300\u0316", "\u00C0\u0316"}, 136 {"\u0071\u0307\u0323\u0072", "\u0071\u0323\u0307\u0072"}, 137 {"\u0f77", "\u0fb2\u0f71\u0f80"}, 138 {"D\u0307\u0328\u0323", "\u1e0c\u0328\u0307"}, 139 {"\u0f71\u0f72\u0f73\u0f74\u0f75", 140 "\u0F71\u0F71\u0F71\u0F72\u0F72\u0F74\u0F74"}, 141 }; 142 }