jdk/test/java/awt/font/NumericShaper/MTTest.java
author peytoia
Thu, 12 Nov 2009 18:59:19 +0900
changeset 4280 74c4e0c5d936
child 5447 30d843beb284
permissions -rw-r--r--
6842557: NumericShaper needs to be updated for Unicode 5.1 support 6843181: NumericShaper is not thread-safe 6900137: Typo in API Doc for NumericShaper Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4280
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     1
/*
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     2
 * Copyright (c) 2009 Sun Microsystems, Inc.  All Rights Reserved.
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     4
 *
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     8
 *
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    13
 * accompanied this code).
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    14
 *
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    18
 *
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    21
 * have any questions.
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    22
 */
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    23
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    24
/*
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    25
 * @test
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    26
 * @bug 6843181
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    27
 * @summary Confirm that NumericShaper is thread-safe.
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    28
 * @run main/timeout=300/othervm MTTest
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    29
 */
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    30
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    31
import java.awt.font.NumericShaper;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    32
import java.util.Arrays;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    33
import java.util.EnumSet;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    34
import static java.awt.font.NumericShaper.*;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    35
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    36
public class MTTest {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    37
    static volatile boolean runrun = true;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    38
    static volatile boolean err = false;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    39
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    40
    final static String text = "-123 (English) 456.00 (Arabic) \u0641\u0642\u0643 -789 (Thai) \u0e01\u0e33 01.23";
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    41
    static char[] t1, t2;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    42
    static NumericShaper ns1, ns2, ns3, ns4;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    43
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    44
    public static void main(String[] args) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    45
        System.out.println("   original: " + text);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    46
        ns1 = getContextualShaper(EnumSet.of(Range.ARABIC), Range.ARABIC);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    47
        t1 = text.toCharArray();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    48
        ns1.shape(t1, 0, t1.length);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    49
        System.out.println("expected t1: " + String.valueOf(t1));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    50
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    51
        ns2 = getContextualShaper(EnumSet.of(Range.THAI), Range.THAI);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    52
        t2 = text.toCharArray();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    53
        ns2.shape(t2, 0, t2.length);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    54
        System.out.println("expected t2: " + String.valueOf(t2));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    55
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    56
        ns3 = getContextualShaper(ARABIC, ARABIC);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    57
        ns4 = getContextualShaper(THAI, THAI);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    58
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    59
        Thread th1 = new Thread(new Work(ns1, t1));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    60
        Thread th2 = new Thread(new Work(ns2, t2));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    61
        Thread th3 = new Thread(new Work(ns1, t1));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    62
        Thread th4 = new Thread(new Work(ns2, t2));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    63
        Thread th5 = new Thread(new Work(ns3, t1));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    64
        Thread th6 = new Thread(new Work(ns4, t2));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    65
        Thread th7 = new Thread(new Work(ns3, t1));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    66
        Thread th8 = new Thread(new Work(ns4, t2));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    67
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    68
        th1.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    69
        th2.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    70
        th3.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    71
        th4.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    72
        th5.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    73
        th6.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    74
        th7.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    75
        th8.start();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    76
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    77
        try {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    78
            for (int i = 0; runrun && i < 180; i++) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    79
                Thread.sleep(1000); // 1 seconds
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    80
            }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    81
            runrun = false;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    82
            th1.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    83
            th2.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    84
            th3.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    85
            th4.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    86
            th5.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    87
            th6.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    88
            th7.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    89
            th8.join();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    90
        }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    91
        catch (InterruptedException e) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    92
        }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    93
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    94
        if (err) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    95
            throw new RuntimeException("Thread-safe test failed.");
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    96
        }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    97
    }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    98
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
    99
    private static class Work implements Runnable {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   100
        NumericShaper ns;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   101
        char[] expectedText;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   102
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   103
        Work(NumericShaper ns, char[] expectedText) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   104
            this.ns = ns;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   105
            this.expectedText = expectedText;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   106
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   107
        }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   108
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   109
        public void run() {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   110
            int count = 0;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   111
            while (runrun) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   112
                char[] t = text.toCharArray();
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   113
                try {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   114
                    count++;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   115
                    ns.shape(t, 0, t.length);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   116
                } catch (Exception e) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   117
                    System.err.println("Error: Unexpected exception: " + e);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   118
                    runrun = false;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   119
                    err = true;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   120
                    return;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   121
                }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   122
                if (!Arrays.equals(t, expectedText)) {
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   123
                    System.err.println("Error: shape() returned unexpected value: ");
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   124
                    System.err.println("count = " + count);
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   125
                    System.err.println("   expected: " + String.valueOf(expectedText));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   126
                    System.err.println("        got: " + String.valueOf(t));
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   127
                    runrun = false;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   128
                    err = true;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   129
                    return;
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   130
                }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   131
            }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   132
        }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   133
    }
74c4e0c5d936 6842557: NumericShaper needs to be updated for Unicode 5.1 support
peytoia
parents:
diff changeset
   134
}