1 /*
   2  * Copyright (c) 2011, 2014, 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 7051769 8038092
  27  * @summary verify that Bidi.toString() returns the corect result.
  28  *     The second run is intended to test lazy SharedSectets init for 8038092
  29  * @run main Bug7051769
  30  * @run main/othervm -DpreloadBidi=true Bug7051769
  31  */
  32 import java.awt.font.*;
  33 import java.text.*;
  34 import java.util.*;
  35 
  36 public class Bug7051769 {
  37 
  38     static {
  39         if (System.getProperty("preloadBidi", "").equals("true")) {
  40             // Make sure the SharedSecret is lazily initialized correctly
  41             try {
  42                 Class.forName("sun.text.bidi.BidiBase");
  43                 System.out.println("BidiBase class has been pre-loaded.");
  44             } catch (ClassNotFoundException e) {
  45                 System.out.println("BidiBase class could not be pre-loaded.");
  46             }
  47         }
  48     }
  49 
  50     private static boolean err = false;
  51 
  52     public static void main(String[] args) {
  53         testNumericShaping();
  54 
  55         if (err) {
  56             throw new RuntimeException("Failed");
  57         } else {
  58             System.out.println("Passed.");
  59         }
  60     }
  61 
  62     private static void testNumericShaping() {
  63         Map attrNS = new HashMap();
  64         attrNS.put(TextAttribute.NUMERIC_SHAPING,
  65                    NumericShaper.getContextualShaper(NumericShaper.ARABIC));
  66         attrNS.put(TextAttribute.RUN_DIRECTION,
  67                    TextAttribute.RUN_DIRECTION_RTL);
  68 
  69         String text = "\u0623\u0643\u062a\u0648\u0628\u0631 10";
  70         String expected = "sun.text.bidi.BidiBase[dir: 2 baselevel: 1 length: 9 runs: [1 1 1 1 1 1 1 2 2] text: [0x623 0x643 0x62a 0x648 0x628 0x631 0x20 0x661 0x660]]";
  71 
  72         AttributedString as = new AttributedString(text, attrNS);
  73         AttributedCharacterIterator itr = as.getIterator();
  74         itr.last();
  75         itr.next();
  76         Bidi bidi = new Bidi(itr);
  77         String got = bidi.toString();
  78 
  79         if (!got.equals(expected)) {
  80             err = true;
  81             System.err.println("Wrong toString() output: " +
  82                                "\n\tExpected=" + expected +
  83                                "\n\tGot=" + got);
  84         }
  85     }
  86 
  87 }