jdk/test/java/awt/font/NumericShaper/MTTest.java
changeset 4280 74c4e0c5d936
child 5447 30d843beb284
equal deleted inserted replaced
4279:67bbd3f37f62 4280:74c4e0c5d936
       
     1 /*
       
     2  * Copyright (c) 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6843181
       
    27  * @summary Confirm that NumericShaper is thread-safe.
       
    28  * @run main/timeout=300/othervm MTTest
       
    29  */
       
    30 
       
    31 import java.awt.font.NumericShaper;
       
    32 import java.util.Arrays;
       
    33 import java.util.EnumSet;
       
    34 import static java.awt.font.NumericShaper.*;
       
    35 
       
    36 public 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 -789 (Thai) \u0e01\u0e33 01.23";
       
    41     static char[] t1, t2;
       
    42     static NumericShaper ns1, ns2, ns3, ns4;
       
    43 
       
    44     public static void main(String[] args) {
       
    45         System.out.println("   original: " + text);
       
    46         ns1 = getContextualShaper(EnumSet.of(Range.ARABIC), Range.ARABIC);
       
    47         t1 = text.toCharArray();
       
    48         ns1.shape(t1, 0, t1.length);
       
    49         System.out.println("expected t1: " + String.valueOf(t1));
       
    50 
       
    51         ns2 = getContextualShaper(EnumSet.of(Range.THAI), Range.THAI);
       
    52         t2 = text.toCharArray();
       
    53         ns2.shape(t2, 0, t2.length);
       
    54         System.out.println("expected t2: " + String.valueOf(t2));
       
    55 
       
    56         ns3 = getContextualShaper(ARABIC, ARABIC);
       
    57         ns4 = getContextualShaper(THAI, THAI);
       
    58 
       
    59         Thread th1 = new Thread(new Work(ns1, t1));
       
    60         Thread th2 = new Thread(new Work(ns2, t2));
       
    61         Thread th3 = new Thread(new Work(ns1, t1));
       
    62         Thread th4 = new Thread(new Work(ns2, t2));
       
    63         Thread th5 = new Thread(new Work(ns3, t1));
       
    64         Thread th6 = new Thread(new Work(ns4, t2));
       
    65         Thread th7 = new Thread(new Work(ns3, t1));
       
    66         Thread th8 = new Thread(new Work(ns4, t2));
       
    67 
       
    68         th1.start();
       
    69         th2.start();
       
    70         th3.start();
       
    71         th4.start();
       
    72         th5.start();
       
    73         th6.start();
       
    74         th7.start();
       
    75         th8.start();
       
    76 
       
    77         try {
       
    78             for (int i = 0; runrun && i < 180; i++) {
       
    79                 Thread.sleep(1000); // 1 seconds
       
    80             }
       
    81             runrun = false;
       
    82             th1.join();
       
    83             th2.join();
       
    84             th3.join();
       
    85             th4.join();
       
    86             th5.join();
       
    87             th6.join();
       
    88             th7.join();
       
    89             th8.join();
       
    90         }
       
    91         catch (InterruptedException e) {
       
    92         }
       
    93 
       
    94         if (err) {
       
    95             throw new RuntimeException("Thread-safe test failed.");
       
    96         }
       
    97     }
       
    98 
       
    99     private static class Work implements Runnable {
       
   100         NumericShaper ns;
       
   101         char[] expectedText;
       
   102 
       
   103         Work(NumericShaper ns, char[] expectedText) {
       
   104             this.ns = ns;
       
   105             this.expectedText = expectedText;
       
   106 
       
   107         }
       
   108 
       
   109         public void run() {
       
   110             int count = 0;
       
   111             while (runrun) {
       
   112                 char[] t = text.toCharArray();
       
   113                 try {
       
   114                     count++;
       
   115                     ns.shape(t, 0, t.length);
       
   116                 } catch (Exception e) {
       
   117                     System.err.println("Error: Unexpected exception: " + e);
       
   118                     runrun = false;
       
   119                     err = true;
       
   120                     return;
       
   121                 }
       
   122                 if (!Arrays.equals(t, expectedText)) {
       
   123                     System.err.println("Error: shape() returned unexpected value: ");
       
   124                     System.err.println("count = " + count);
       
   125                     System.err.println("   expected: " + String.valueOf(expectedText));
       
   126                     System.err.println("        got: " + String.valueOf(t));
       
   127                     runrun = false;
       
   128                     err = true;
       
   129                     return;
       
   130                 }
       
   131             }
       
   132         }
       
   133     }
       
   134 }