1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html#License
3/*
4 *******************************************************************************
5 * Copyright (C) 2008, International Business Machines Corporation and         *
6 * others. All Rights Reserved.                                                *
7 *******************************************************************************
8 */
9package com.ibm.icu.dev.test.lang;
10
11import java.util.LinkedList;
12import java.util.List;
13import java.util.ListIterator;
14
15import org.junit.Test;
16import org.junit.runner.RunWith;
17import org.junit.runners.JUnit4;
18
19import com.ibm.icu.dev.test.TestFmwk;
20import com.ibm.icu.lang.UCharacter;
21
22/**
23 * @author aheninger
24 *
25 */
26@RunWith(JUnit4.class)
27public class UCharacterThreadTest extends TestFmwk {
28  // constructor -----------------------------------------------------------
29
30    /**
31    * Private constructor to prevent initialisation
32    */
33    public UCharacterThreadTest()
34    {
35    }
36
37      // public methods --------------------------------------------------------
38
39    //
40    //  Test multi-threaded parallel calls to UCharacter.getName(codePoint)
41    //  Regression test for ticket 6264.
42    //
43    @Test
44    public void TestUCharactersGetName() throws InterruptedException {
45        List threads = new LinkedList();
46        for(int t=0; t<20; t++) {
47          int codePoint = 47 + t;
48          String correctName = UCharacter.getName(codePoint);
49          GetNameThread thread = new GetNameThread(codePoint, correctName);
50          thread.start();
51          threads.add(thread);
52        }
53        ListIterator i = threads.listIterator();
54        while (i.hasNext()) {
55            GetNameThread thread = (GetNameThread)i.next();
56            thread.join();
57            if (!thread.correctName.equals(thread.actualName)) {
58                errln("FAIL, expected \"" + thread.correctName + "\", got \"" + thread.actualName + "\"");
59            }
60        }
61      }
62
63      private static class GetNameThread extends Thread {
64        private final int codePoint;
65        private final String correctName;
66        private String actualName;
67
68        GetNameThread(int codePoint, String correctName) {
69           this.codePoint = codePoint;
70           this.correctName = correctName;
71        }
72
73        @Override
74        public void run() {
75          for(int i=0; i<10000; i++) {
76            actualName = UCharacter.getName(codePoint);
77            if (!correctName.equals(actualName)) {
78              break;
79            }
80          }
81        }
82      }
83}
84