8196759: Move two java/text/Normalizer tests into OpenJDK
authorxyin
Tue, 27 Feb 2018 23:11:26 -0800
changeset 49000 a406a9c451a0
parent 48999 5a37d5e9c2e3
child 49001 ce06058197a4
8196759: Move two java/text/Normalizer tests into OpenJDK Reviewed-by: naoto
test/jdk/java/text/Normalizer/NormalizerAPITest.java
test/jdk/java/text/Normalizer/ThreadSafeTest.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/text/Normalizer/NormalizerAPITest.java	Tue Feb 27 23:11:26 2018 -0800
@@ -0,0 +1,343 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4221795
+ * @summary Confirm Normalizer's fundamental behavior
+ * @modules java.base/sun.text java.base/sun.text.normalizer
+ * @library /java/text/testlib
+ * @compile -XDignore.symbol.file NormalizerAPITest.java
+ * @run main/timeout=30 NormalizerAPITest
+ */
+
+import java.text.Normalizer;
+import java.nio.CharBuffer;
+
+
+/*
+ * Tests around null/"" arguments for public methods.
+ *
+ * You may think that so elaborate testing for such a part is not necessary.
+ * But I actually detected a bug by this program during my porting work.
+ */
+public class NormalizerAPITest extends IntlTest {
+
+    //
+    // Shortcuts
+    //
+
+    /*
+     * Normalization forms
+     */
+    static final Normalizer.Form NFC  = Normalizer.Form.NFC;
+    static final Normalizer.Form NFD  = Normalizer.Form.NFD;
+    static final Normalizer.Form NFKC = Normalizer.Form.NFKC;
+    static final Normalizer.Form NFKD = Normalizer.Form.NFKD;
+    static final Normalizer.Form[] forms = {NFC, NFD, NFKC, NFKD};
+
+    static final Normalizer.Form NULL = null;
+
+    /*
+     * Option
+     */
+    static final int[] options = {
+        0x00,
+        sun.text.Normalizer.UNICODE_3_2,
+        sun.text.normalizer.NormalizerBase.UNICODE_3_2,
+        sun.text.normalizer.NormalizerBase.UNICODE_LATEST,
+    };
+
+    static final String nonNullStr = "testdata";
+
+
+    public static void main(String[] args) throws Exception {
+        new NormalizerAPITest().run(args);
+    }
+
+    /*
+     * Check if normalize(null) throws NullPointerException as expected.
+     */
+    void Test_NullPointerException_java_normalize() {
+        boolean error = false;
+
+        /* Check null as String to be normalized */
+        for (int i = 0; i < forms.length; i++) {
+            try {
+                String s = Normalizer.normalize(null, forms[i]);
+                error = true;
+            }
+            catch (NullPointerException e) {
+            }
+        }
+
+        /* Check null as a Normalization form */
+        try {
+            String s = Normalizer.normalize(nonNullStr, NULL);
+            error = true;
+        }
+        catch (NullPointerException e) {
+        }
+
+        if (error) {
+             errln("normalize(null) should throw NullPointerException.");
+        }
+    }
+
+    /*
+     * Check if normalize(null) throws NullPointerException as expected.
+     */
+    void Test_NullPointerException_sun_normalize() {
+        boolean error = false;
+
+        for (int j = 0; j < options.length; j++) {
+            for (int i = 0; i < forms.length; i++) {
+                /* Check null as a String to be normalized */
+                try {
+                    String s = sun.text.Normalizer.normalize(null, forms[i], options[j]);
+                    error = true;
+                }
+                catch (NullPointerException e) {
+                }
+            }
+
+            /* Check null as a Normalization form */
+            try {
+                String s = sun.text.Normalizer.normalize(nonNullStr, NULL, options[j]);
+                error = true;
+            }
+            catch (NullPointerException e) {
+            }
+        }
+
+        if (error) {
+             errln("normalize(null) should throw NullPointerException.");
+        }
+    }
+
+    /*
+     * Check if isNormalized(null) throws NullPointerException as expected.
+     */
+    void Test_NullPointerException_java_isNormalized() {
+        boolean error = false;
+
+        for (int i = 0; i < forms.length; i++) {
+            try {
+                /* Check null as a String to be scanned */
+                boolean b = Normalizer.isNormalized(null, forms[i]);
+                error = true;
+            }
+            catch (NullPointerException e) {
+            }
+        }
+
+        /* Check null as a String to be scanned */
+        try {
+            boolean b = Normalizer.isNormalized(nonNullStr, NULL);
+            error = true;
+        }
+
+        catch (NullPointerException e) {
+        }
+        if (error) {
+             errln("isNormalized(null) should throw NullPointerException.");
+        }
+    }
+
+    /*
+     * Check if isNormalized(null) throws NullPointerException as expected.
+     */
+    void Test_NullPointerException_sun_isNormalized() {
+        boolean error = false;
+
+        for (int j = 0; j < options.length; j++) {
+            for (int i = 0; i < forms.length; i++) {
+                try {
+                    /* Check null as a String to be scanned */
+                    boolean b = sun.text.Normalizer.isNormalized(null, forms[i], options[j]);
+                    error = true;
+                }
+                catch (NullPointerException e) {
+                }
+            }
+
+            /* Check null as a String to be scanned */
+            try {
+                boolean b = sun.text.Normalizer.isNormalized(nonNullStr, NULL, options[j]);
+                error = true;
+            }
+            catch (NullPointerException e) {
+            }
+        }
+
+        if (error) {
+             errln("isNormalized(null) should throw NullPointerException.");
+        }
+    }
+
+    /*
+     * Check if isNormalized("") doesn't throw NullPointerException and returns
+     * "" as expected.
+     */
+    void Test_No_NullPointerException_java_normalize() {
+        boolean error = false;
+
+        for (int i = 0; i < forms.length; i++) {
+            try {
+                String s = Normalizer.normalize("", forms[i]);
+                if (!s.equals("")) {
+                    error = true;
+                }
+            }
+            catch (NullPointerException e) {
+                error = true;
+            }
+        }
+
+        if (error) {
+             errln("normalize() for String(\"\") should return \"\".");
+        }
+    }
+
+    /*
+     * Check if isNormalized("") doesn't throw NullPointerException and returns
+     * "" as expected.
+     */
+    void Test_No_NullPointerException_sun_normalize() {
+        boolean error = false;
+
+        for (int j = 0; j < options.length; j++) {
+            for (int i = 0; i < forms.length; i++) {
+                try {
+                    String s = sun.text.Normalizer.normalize("", forms[i], options[j]);
+                    if (!s.equals("")) {
+                        error = true;
+                    }
+                }
+                catch (NullPointerException e) {
+                    error = true;
+                }
+            }
+        }
+        if (error) {
+             errln("normalize() for String(\"\") should return \"\".");
+        }
+    }
+
+    /*
+     * Check if isNormalized("") doesn't throw NullPointerException and returns
+     * "" as expected.
+     */
+    void Test_No_NullPointerException_java_isNormalized() {
+        boolean error = false;
+
+        for (int i = 0; i < forms.length; i++) {
+            try {
+                boolean b = Normalizer.isNormalized("", forms[i]);
+                if (!b) {
+                    error = true;
+                }
+            }
+            catch (NullPointerException e) {
+                error = true;
+            }
+        }
+        if (error) {
+             errln("isNormalized() for String(\"\") should not return true.");
+        }
+    }
+
+    /*
+     * Check if isNormalized("") doesn't throw NullPointerException and returns
+     * "" as expected.
+     */
+    void Test_No_NullPointerException_sun_isNormalized() {
+        boolean error = false;
+
+        for (int j = 0; j < options.length; j++) {
+            for (int i = 0; i < forms.length; i++) {
+                try {
+                    boolean b = sun.text.Normalizer.isNormalized("", forms[i], options[j]);
+                    if (!b) {
+                        error = true;
+                    }
+                }
+                catch (NullPointerException e) {
+                    error = true;
+                }
+            }
+        }
+        if (error) {
+             errln("isNormalized() for String(\"\") should not return true.");
+        }
+    }
+
+    /*
+     * Check if normalize() and isNormalized() work as expected for every
+     * known class which implement CharSequence Interface.
+     */
+    void Test_CharSequence() {
+
+        check_CharSequence(String.valueOf(inputData),
+                           String.valueOf(outputData));
+
+        check_CharSequence(new StringBuffer(original),
+                           new StringBuffer(expected));
+
+        check_CharSequence(new StringBuilder(original),
+                           new StringBuilder(expected));
+
+        check_CharSequence(CharBuffer.wrap(inputData),
+                           CharBuffer.wrap(outputData));
+    }
+
+
+    void check_CharSequence(CharSequence in, CharSequence expected) {
+        String out = Normalizer.normalize(in, NFD);
+        if (!out.equals(expected.toString())) {
+            errln("java.text.Normalizer.normalize(" +
+                  in.getClass().getSimpleName() + ") failed.");
+        }
+        out = sun.text.Normalizer.normalize(in, NFD,
+                             sun.text.normalizer.NormalizerBase.UNICODE_LATEST);
+        if (!out.equals(expected.toString())) {
+            errln("sun.text.Normalizer.normalize(" +
+                  in.getClass().getSimpleName() + ") failed.");
+        }
+
+        if (!Normalizer.isNormalized(expected, NFD)) {
+            errln("java.text.Normalizer.isNormalize(" +
+                  in.getClass().getSimpleName() + ") failed.");
+        }
+        if (!sun.text.Normalizer.isNormalized(expected, NFD,
+                           sun.text.normalizer.NormalizerBase.UNICODE_LATEST)) {
+            errln("sun.text.Normalizer.isNormalize(" +
+                  in.getClass().getSimpleName() + ") failed.");
+        }
+    }
+
+    static final char[] inputData  = {'T', 's', 'c', 'h', 'u', '\u1e9b'};
+    static final char[] outputData = {'T', 's', 'c', 'h', 'u', '\u017f', '\u0307'};
+    static final String original   = String.valueOf(inputData);
+    static final String expected   = String.valueOf(outputData);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/text/Normalizer/ThreadSafeTest.java	Tue Feb 27 23:11:26 2018 -0800
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4221795 8032446
+ * @summary Confirm that java.text.Normalizer and sun.text.Normalize are
+ * thread-safe.
+ * @modules java.base/sun.text java.base/sun.text.normalizer
+ * @compile -XDignore.symbol.file ThreadSafeTest.java
+ * @run main/othervm -esa ThreadSafeTest 5 10
+ */
+
+// Usage: java ThreadSafeTest [threadsFactor [duration]]
+public class ThreadSafeTest {
+
+    static volatile boolean runrun = true;
+    static volatile boolean error = false;
+
+    public static void main(String[] args) throws Exception {
+        int threadsFactor = 5;
+        if (args.length > 0) {
+            threadsFactor = Math.max(Integer.parseInt(args[0]), 5);
+        }
+        int duration = 180;
+        if (args.length > 1) {
+            duration = Math.max(5, Integer.parseInt(args[1]));
+        }
+        int nProcessors = Runtime.getRuntime().availableProcessors();
+        int nTasks = nProcessors * threadsFactor;
+        Thread[] tasks = new Thread[nTasks];
+
+        System.out.println("Testing with " + nTasks + " threads on " +
+                           nProcessors + " processors for " + duration +
+                           " seconds.");
+
+        for (int i = 0; i < nTasks; i++) {
+            tasks[i] = new Thread(new Worker());
+        }
+        for (int i = 0; i < nTasks; i++) {
+            tasks[i].start();
+        }
+
+        try {
+            for (int i = 0; runrun && i < duration; i++) {
+                Thread.sleep(1000); // 1 second
+            }
+            runrun = false;
+            for (int i = 0; i < nTasks; i++) {
+                tasks[i].join();
+            }
+        }
+        catch (InterruptedException e) {
+        }
+
+        if (error) {
+            throw new RuntimeException("Normalizer is not thread-safe.");
+        }
+    }
+
+    static void testJavaNormalize(int num, java.text.Normalizer.Form form) {
+        String got = java.text.Normalizer.normalize(data[num][0], form);
+        if (!got.equals(data[num][1])) {
+            System.err.println("java.text.Normalizer.normalize(" +
+                               form.toString() + ") failed.");
+            error = true;
+        }
+    }
+
+    static void testSunNormalize(int num, java.text.Normalizer.Form form,
+                                 int option) {
+        String got = sun.text.Normalizer.normalize(data[num][0], form, option);
+        if (!got.equals(data[num][1])) {
+            System.err.println("sun.text.Normalizer.normalize(" +
+                               form.toString() + ", " +
+                               Integer.toHexString(option) + ") failed.");
+            error = true;
+        }
+    }
+
+    static void testIsNormalized(int num, java.text.Normalizer.Form form) {
+        boolean normalized = java.text.Normalizer.isNormalized(data[num][1], form);
+        if (!normalized) {
+            System.err.println("java.text.Normalizer.isNormalized(" +
+                               form.toString() + ") failed.");
+            error = true;
+        }
+    }
+
+    static class Worker implements Runnable {
+        public void run() {
+            while (runrun) {
+                testJavaNormalize(0, java.text.Normalizer.Form.NFKC);
+                testSunNormalize(1, java.text.Normalizer.Form.NFC,
+                                 sun.text.Normalizer.UNICODE_3_2);
+                testJavaNormalize(2, java.text.Normalizer.Form.NFKD);
+                testSunNormalize(3, java.text.Normalizer.Form.NFC,
+                            sun.text.normalizer.NormalizerBase.UNICODE_LATEST);
+                testJavaNormalize(4, java.text.Normalizer.Form.NFD);
+
+                testIsNormalized(0, java.text.Normalizer.Form.NFKC);
+                testIsNormalized(2, java.text.Normalizer.Form.NFKD);
+                testIsNormalized(4, java.text.Normalizer.Form.NFD);
+
+                if (error) {
+                    runrun = false;
+                    return;
+                }
+            }
+        }
+    }
+
+    static final String[][] data = {
+     /*      From:                             To:           */
+      {"A\u0300\u0316",                  "\u00C0\u0316"},
+      {"\u0071\u0307\u0323\u0072",       "\u0071\u0323\u0307\u0072"},
+      {"\u0f77",                         "\u0fb2\u0f71\u0f80"},
+      {"D\u0307\u0328\u0323",            "\u1e0c\u0328\u0307"},
+      {"\u0f71\u0f72\u0f73\u0f74\u0f75",
+                 "\u0F71\u0F71\u0F71\u0F72\u0F72\u0F74\u0F74"},
+    };
+}