< prev index next >

test/jdk/java/text/Normalizer/NormalizerAPITest.java

Print this page
rev 57619 : [mq]: 8174270
   1 /*
   2  * Copyright (c) 2018, 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 4221795
  27  * @summary Confirm Normalizer's fundamental behavior
  28  * @modules java.base/sun.text java.base/sun.text.normalizer
  29  * @library /java/text/testlib
  30  * @compile -XDignore.symbol.file NormalizerAPITest.java
  31  * @run main/timeout=30 NormalizerAPITest
  32  */
  33 
  34 import java.text.Normalizer;
  35 import java.nio.CharBuffer;
  36 
  37 
  38 /*
  39  * Tests around null/"" arguments for public methods.
  40  *
  41  * You may think that so elaborate testing for such a part is not necessary.
  42  * But I actually detected a bug by this program during my porting work.
  43  */
  44 public class NormalizerAPITest extends IntlTest {
  45 
  46     //
  47     // Shortcuts
  48     //
  49 
  50     /*
  51      * Normalization forms
  52      */
  53     static final Normalizer.Form NFC  = Normalizer.Form.NFC;
  54     static final Normalizer.Form NFD  = Normalizer.Form.NFD;
  55     static final Normalizer.Form NFKC = Normalizer.Form.NFKC;
  56     static final Normalizer.Form NFKD = Normalizer.Form.NFKD;
  57     static final Normalizer.Form[] forms = {NFC, NFD, NFKC, NFKD};
  58 
  59     static final Normalizer.Form NULL = null;
  60 
  61     /*
  62      * Option
  63      */
  64     static final int[] options = {
  65         0x00,
  66         sun.text.Normalizer.UNICODE_3_2,
  67         sun.text.normalizer.NormalizerBase.UNICODE_3_2,
  68         sun.text.normalizer.NormalizerBase.UNICODE_LATEST,
  69     };
  70 
  71     static final String nonNullStr = "testdata";
  72 
  73 
  74     public static void main(String[] args) throws Exception {
  75         new NormalizerAPITest().run(args);
  76     }
  77 
  78     /*
  79      * Check if normalize(null) throws NullPointerException as expected.
  80      */
  81     public void Test_NullPointerException_java_normalize() {
  82         boolean error = false;
  83 
  84         /* Check null as String to be normalized */
  85         for (int i = 0; i < forms.length; i++) {
  86             try {
  87                 String s = Normalizer.normalize(null, forms[i]);
  88                 error = true;


 302                            String.valueOf(outputData));
 303 
 304         check_CharSequence(new StringBuffer(original),
 305                            new StringBuffer(expected));
 306 
 307         check_CharSequence(new StringBuilder(original),
 308                            new StringBuilder(expected));
 309 
 310         check_CharSequence(CharBuffer.wrap(inputData),
 311                            CharBuffer.wrap(outputData));
 312     }
 313 
 314 
 315     void check_CharSequence(CharSequence in, CharSequence expected) {
 316         String out = Normalizer.normalize(in, NFD);
 317         if (!out.equals(expected.toString())) {
 318             errln("java.text.Normalizer.normalize(" +
 319                   in.getClass().getSimpleName() + ") failed.");
 320         }
 321         out = sun.text.Normalizer.normalize(in, NFD,
 322                              sun.text.normalizer.NormalizerBase.UNICODE_LATEST);
 323         if (!out.equals(expected.toString())) {
 324             errln("sun.text.Normalizer.normalize(" +
 325                   in.getClass().getSimpleName() + ") failed.");
 326         }
 327 
 328         if (!Normalizer.isNormalized(expected, NFD)) {
 329             errln("java.text.Normalizer.isNormalize(" +
 330                   in.getClass().getSimpleName() + ") failed.");
 331         }
 332         if (!sun.text.Normalizer.isNormalized(expected, NFD,
 333                            sun.text.normalizer.NormalizerBase.UNICODE_LATEST)) {
 334             errln("sun.text.Normalizer.isNormalize(" +
 335                   in.getClass().getSimpleName() + ") failed.");
 336         }
 337     }
 338 
 339     static final char[] inputData  = {'T', 's', 'c', 'h', 'u', '\u1e9b'};
 340     static final char[] outputData = {'T', 's', 'c', 'h', 'u', '\u017f', '\u0307'};
 341     static final String original   = String.valueOf(inputData);
 342     static final String expected   = String.valueOf(outputData);
 343 }
   1 /*
   2  * Copyright (c) 2018, 2020, 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 4221795 8174270
  27  * @summary Confirm Normalizer's fundamental behavior
  28  * @modules java.base/sun.text java.base/jdk.internal.icu.text
  29  * @library /java/text/testlib
  30  * @compile -XDignore.symbol.file NormalizerAPITest.java
  31  * @run main/timeout=30 NormalizerAPITest
  32  */
  33 
  34 import java.text.Normalizer;
  35 import java.nio.CharBuffer;
  36 
  37 
  38 /*
  39  * Tests around null/"" arguments for public methods.
  40  *
  41  * You may think that so elaborate testing for such a part is not necessary.
  42  * But I actually detected a bug by this program during my porting work.
  43  */
  44 public class NormalizerAPITest extends IntlTest {
  45 
  46     //
  47     // Shortcuts
  48     //
  49 
  50     /*
  51      * Normalization forms
  52      */
  53     static final Normalizer.Form NFC  = Normalizer.Form.NFC;
  54     static final Normalizer.Form NFD  = Normalizer.Form.NFD;
  55     static final Normalizer.Form NFKC = Normalizer.Form.NFKC;
  56     static final Normalizer.Form NFKD = Normalizer.Form.NFKD;
  57     static final Normalizer.Form[] forms = {NFC, NFD, NFKC, NFKD};
  58 
  59     static final Normalizer.Form NULL = null;
  60 
  61     /*
  62      * Option
  63      */
  64     static final int[] options = {
  65         0x00,
  66         sun.text.Normalizer.UNICODE_3_2,
  67         jdk.internal.icu.text.NormalizerBase.UNICODE_3_2,
  68         jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST,
  69     };
  70 
  71     static final String nonNullStr = "testdata";
  72 
  73 
  74     public static void main(String[] args) throws Exception {
  75         new NormalizerAPITest().run(args);
  76     }
  77 
  78     /*
  79      * Check if normalize(null) throws NullPointerException as expected.
  80      */
  81     public void Test_NullPointerException_java_normalize() {
  82         boolean error = false;
  83 
  84         /* Check null as String to be normalized */
  85         for (int i = 0; i < forms.length; i++) {
  86             try {
  87                 String s = Normalizer.normalize(null, forms[i]);
  88                 error = true;


 302                            String.valueOf(outputData));
 303 
 304         check_CharSequence(new StringBuffer(original),
 305                            new StringBuffer(expected));
 306 
 307         check_CharSequence(new StringBuilder(original),
 308                            new StringBuilder(expected));
 309 
 310         check_CharSequence(CharBuffer.wrap(inputData),
 311                            CharBuffer.wrap(outputData));
 312     }
 313 
 314 
 315     void check_CharSequence(CharSequence in, CharSequence expected) {
 316         String out = Normalizer.normalize(in, NFD);
 317         if (!out.equals(expected.toString())) {
 318             errln("java.text.Normalizer.normalize(" +
 319                   in.getClass().getSimpleName() + ") failed.");
 320         }
 321         out = sun.text.Normalizer.normalize(in, NFD,
 322                              jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST);
 323         if (!out.equals(expected.toString())) {
 324             errln("sun.text.Normalizer.normalize(" +
 325                   in.getClass().getSimpleName() + ") failed.");
 326         }
 327 
 328         if (!Normalizer.isNormalized(expected, NFD)) {
 329             errln("java.text.Normalizer.isNormalize(" +
 330                   in.getClass().getSimpleName() + ") failed.");
 331         }
 332         if (!sun.text.Normalizer.isNormalized(expected, NFD,
 333                            jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST)) {
 334             errln("sun.text.Normalizer.isNormalize(" +
 335                   in.getClass().getSimpleName() + ") failed.");
 336         }
 337     }
 338 
 339     static final char[] inputData  = {'T', 's', 'c', 'h', 'u', '\u1e9b'};
 340     static final char[] outputData = {'T', 's', 'c', 'h', 'u', '\u017f', '\u0307'};
 341     static final String original   = String.valueOf(inputData);
 342     static final String expected   = String.valueOf(outputData);
 343 }
< prev index next >