Path: blob/master/test/jdk/java/text/Normalizer/NormalizerAPITest.java
41152 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4221795 817427026* @summary Confirm Normalizer's fundamental behavior27* @modules java.base/sun.text java.base/jdk.internal.icu.text28* @library /java/text/testlib29* @compile -XDignore.symbol.file NormalizerAPITest.java30* @run main/timeout=30 NormalizerAPITest31*/3233import java.text.Normalizer;34import java.nio.CharBuffer;353637/*38* Tests around null/"" arguments for public methods.39*40* You may think that so elaborate testing for such a part is not necessary.41* But I actually detected a bug by this program during my porting work.42*/43public class NormalizerAPITest extends IntlTest {4445//46// Shortcuts47//4849/*50* Normalization forms51*/52static final Normalizer.Form NFC = Normalizer.Form.NFC;53static final Normalizer.Form NFD = Normalizer.Form.NFD;54static final Normalizer.Form NFKC = Normalizer.Form.NFKC;55static final Normalizer.Form NFKD = Normalizer.Form.NFKD;56static final Normalizer.Form[] forms = {NFC, NFD, NFKC, NFKD};5758static final Normalizer.Form NULL = null;5960/*61* Option62*/63static final int[] options = {640x00,65sun.text.Normalizer.UNICODE_3_2,66jdk.internal.icu.text.NormalizerBase.UNICODE_3_2,67jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST,68};6970static final String nonNullStr = "testdata";717273public static void main(String[] args) throws Exception {74new NormalizerAPITest().run(args);75}7677/*78* Check if normalize(null) throws NullPointerException as expected.79*/80public void Test_NullPointerException_java_normalize() {81boolean error = false;8283/* Check null as String to be normalized */84for (int i = 0; i < forms.length; i++) {85try {86String s = Normalizer.normalize(null, forms[i]);87error = true;88}89catch (NullPointerException e) {90}91}9293/* Check null as a Normalization form */94try {95String s = Normalizer.normalize(nonNullStr, NULL);96error = true;97}98catch (NullPointerException e) {99}100101if (error) {102errln("normalize(null) should throw NullPointerException.");103}104}105106/*107* Check if normalize(null) throws NullPointerException as expected.108*/109public void Test_NullPointerException_sun_normalize() {110boolean error = false;111112for (int j = 0; j < options.length; j++) {113for (int i = 0; i < forms.length; i++) {114/* Check null as a String to be normalized */115try {116String s = sun.text.Normalizer.normalize(null, forms[i], options[j]);117error = true;118}119catch (NullPointerException e) {120}121}122123/* Check null as a Normalization form */124try {125String s = sun.text.Normalizer.normalize(nonNullStr, NULL, options[j]);126error = true;127}128catch (NullPointerException e) {129}130}131132if (error) {133errln("normalize(null) should throw NullPointerException.");134}135}136137/*138* Check if isNormalized(null) throws NullPointerException as expected.139*/140public void Test_NullPointerException_java_isNormalized() {141boolean error = false;142143for (int i = 0; i < forms.length; i++) {144try {145/* Check null as a String to be scanned */146boolean b = Normalizer.isNormalized(null, forms[i]);147error = true;148}149catch (NullPointerException e) {150}151}152153/* Check null as a String to be scanned */154try {155boolean b = Normalizer.isNormalized(nonNullStr, NULL);156error = true;157}158159catch (NullPointerException e) {160}161if (error) {162errln("isNormalized(null) should throw NullPointerException.");163}164}165166/*167* Check if isNormalized(null) throws NullPointerException as expected.168*/169public void Test_NullPointerException_sun_isNormalized() {170boolean error = false;171172for (int j = 0; j < options.length; j++) {173for (int i = 0; i < forms.length; i++) {174try {175/* Check null as a String to be scanned */176boolean b = sun.text.Normalizer.isNormalized(null, forms[i], options[j]);177error = true;178}179catch (NullPointerException e) {180}181}182183/* Check null as a String to be scanned */184try {185boolean b = sun.text.Normalizer.isNormalized(nonNullStr, NULL, options[j]);186error = true;187}188catch (NullPointerException e) {189}190}191192if (error) {193errln("isNormalized(null) should throw NullPointerException.");194}195}196197/*198* Check if isNormalized("") doesn't throw NullPointerException and returns199* "" as expected.200*/201public void Test_No_NullPointerException_java_normalize() {202boolean error = false;203204for (int i = 0; i < forms.length; i++) {205try {206String s = Normalizer.normalize("", forms[i]);207if (!s.equals("")) {208error = true;209}210}211catch (NullPointerException e) {212error = true;213}214}215216if (error) {217errln("normalize() for String(\"\") should return \"\".");218}219}220221/*222* Check if isNormalized("") doesn't throw NullPointerException and returns223* "" as expected.224*/225public void Test_No_NullPointerException_sun_normalize() {226boolean error = false;227228for (int j = 0; j < options.length; j++) {229for (int i = 0; i < forms.length; i++) {230try {231String s = sun.text.Normalizer.normalize("", forms[i], options[j]);232if (!s.equals("")) {233error = true;234}235}236catch (NullPointerException e) {237error = true;238}239}240}241if (error) {242errln("normalize() for String(\"\") should return \"\".");243}244}245246/*247* Check if isNormalized("") doesn't throw NullPointerException and returns248* "" as expected.249*/250public void Test_No_NullPointerException_java_isNormalized() {251boolean error = false;252253for (int i = 0; i < forms.length; i++) {254try {255boolean b = Normalizer.isNormalized("", forms[i]);256if (!b) {257error = true;258}259}260catch (NullPointerException e) {261error = true;262}263}264if (error) {265errln("isNormalized() for String(\"\") should not return true.");266}267}268269/*270* Check if isNormalized("") doesn't throw NullPointerException and returns271* "" as expected.272*/273public void Test_No_NullPointerException_sun_isNormalized() {274boolean error = false;275276for (int j = 0; j < options.length; j++) {277for (int i = 0; i < forms.length; i++) {278try {279boolean b = sun.text.Normalizer.isNormalized("", forms[i], options[j]);280if (!b) {281error = true;282}283}284catch (NullPointerException e) {285error = true;286}287}288}289if (error) {290errln("isNormalized() for String(\"\") should not return true.");291}292}293294/*295* Check if normalize() and isNormalized() work as expected for every296* known class which implement CharSequence Interface.297*/298public void Test_CharSequence() {299300check_CharSequence(String.valueOf(inputData),301String.valueOf(outputData));302303check_CharSequence(new StringBuffer(original),304new StringBuffer(expected));305306check_CharSequence(new StringBuilder(original),307new StringBuilder(expected));308309check_CharSequence(CharBuffer.wrap(inputData),310CharBuffer.wrap(outputData));311}312313314void check_CharSequence(CharSequence in, CharSequence expected) {315String out = Normalizer.normalize(in, NFD);316if (!out.equals(expected.toString())) {317errln("java.text.Normalizer.normalize(" +318in.getClass().getSimpleName() + ") failed.");319}320out = sun.text.Normalizer.normalize(in, NFD,321jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST);322if (!out.equals(expected.toString())) {323errln("sun.text.Normalizer.normalize(" +324in.getClass().getSimpleName() + ") failed.");325}326327if (!Normalizer.isNormalized(expected, NFD)) {328errln("java.text.Normalizer.isNormalize(" +329in.getClass().getSimpleName() + ") failed.");330}331if (!sun.text.Normalizer.isNormalized(expected, NFD,332jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST)) {333errln("sun.text.Normalizer.isNormalize(" +334in.getClass().getSimpleName() + ") failed.");335}336}337338static final char[] inputData = {'T', 's', 'c', 'h', 'u', '\u1e9b'};339static final char[] outputData = {'T', 's', 'c', 'h', 'u', '\u017f', '\u0307'};340static final String original = String.valueOf(inputData);341static final String expected = String.valueOf(outputData);342}343344345