1/* 2 * Copyright (c) 2010, 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 6843181 6943963 27 * @summary Confirm that NumericShaper is thread-safe. 28 * @run main/timeout=300/othervm MTTest 29 */ 30 31import java.awt.font.NumericShaper; 32import java.util.Arrays; 33import java.util.EnumSet; 34import static java.awt.font.NumericShaper.*; 35 36public class MTTest { 37 static volatile boolean runrun = true; 38 static volatile boolean err = false; 39 40 final static String text = "-123 (English) 456.00 (Arabic) \u0641\u0642\u0643 -456 (Thai) \u0e01\u0e33 01.23"; 41 final static char[] expected1 = "-123 (English) 456.00 (Arabic) \u0641\u0642\u0643 -\u06f4\u06f5\u06f6 (Thai) \u0e01\u0e33 \u0e50\u0e51.\u0e52\u0e53".toCharArray(); // for EASTERN_ARABIC 42 final static char[] expected2 = "-123 (English) 456.00 (Arabic) \u0641\u0642\u0643 -\u0664\u0665\u0666 (Thai) \u0e01\u0e33 \u0e50\u0e51.\u0e52\u0e53".toCharArray(); // for ARABIC 43 44 static NumericShaper ns1, ns2, ns3, ns4; 45 46 public static void main(String[] args) { 47 System.out.println("original: " + text); 48 ns1 = getContextualShaper(EnumSet.of(Range.EASTERN_ARABIC, Range.THAI), 49 Range.EUROPEAN); 50 ns2 = getContextualShaper(EnumSet.of(Range.ARABIC, Range.THAI), 51 Range.EUROPEAN); 52 System.out.println("expected for Eastern-Arabic & Thai: " + 53 String.valueOf(expected1)); 54 System.out.println("expected for Arabic & Thai: " + 55 String.valueOf(expected2)); 56 57 ns3 = getContextualShaper(EASTERN_ARABIC|THAI, EUROPEAN); 58 ns4 = getContextualShaper(ARABIC|THAI, EUROPEAN); 59 60 Thread th1 = new Thread(new Work(ns1, expected1)); 61 Thread th2 = new Thread(new Work(ns2, expected2)); 62 Thread th3 = new Thread(new Work(ns1, expected1)); 63 Thread th4 = new Thread(new Work(ns2, expected2)); 64 Thread th5 = new Thread(new Work(ns3, expected1)); 65 Thread th6 = new Thread(new Work(ns4, expected2)); 66 Thread th7 = new Thread(new Work(ns3, expected1)); 67 Thread th8 = new Thread(new Work(ns4, expected2)); 68 69 th1.start(); 70 th2.start(); 71 th3.start(); 72 th4.start(); 73 th5.start(); 74 th6.start(); 75 th7.start(); 76 th8.start(); 77 78 try { 79 for (int i = 0; runrun && i < 180; i++) { 80 Thread.sleep(1000); // 1 seconds 81 } 82 runrun = false; 83 th1.join(); 84 th2.join(); 85 th3.join(); 86 th4.join(); 87 th5.join(); 88 th6.join(); 89 th7.join(); 90 th8.join(); 91 } 92 catch (InterruptedException e) { 93 } 94 95 if (err) { 96 throw new RuntimeException("Thread-safe test failed."); 97 } 98 } 99 100 private static class Work implements Runnable { 101 NumericShaper ns; 102 char[] expectedText; 103 104 Work(NumericShaper ns, char[] expectedText) { 105 this.ns = ns; 106 this.expectedText = expectedText; 107 108 } 109 110 public void run() { 111 int count = 0; 112 while (runrun) { 113 char[] t = text.toCharArray(); 114 count++; 115 try { 116 ns.shape(t, 0, t.length); 117 } catch (Exception e) { 118 System.err.println("Error: Unexpected exception: " + e); 119 runrun = false; 120 err = true; 121 return; 122 } 123 if (!Arrays.equals(t, expectedText)) { 124 System.err.println("Error: shape() returned unexpected value: "); 125 System.err.println("count = " + count); 126 System.err.println(" expected: " + String.valueOf(expectedText)); 127 System.err.println(" got: " + String.valueOf(t)); 128 runrun = false; 129 err = true; 130 return; 131 } 132 } 133 } 134 } 135} 136