From e9bdf144db11727c8b2d47b88897ff953947509a Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Tue, 4 Mar 2014 09:08:11 +0000 Subject: [PATCH] ICU-10721 Changed BreakIterator factory method to throw NPE always when specified locale is null. The API docs are also updated to clarify this. The spec is equivalent to JDK. X-SVN-Rev: 35317 --- .../src/com/ibm/icu/text/BreakIterator.java | 14 +++- .../icu/dev/test/rbbi/BreakIteratorTest.java | 65 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/BreakIterator.java b/icu4j/main/classes/core/src/com/ibm/icu/text/BreakIterator.java index f4e270e99ec..4425fab69c4 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/BreakIterator.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/BreakIterator.java @@ -581,6 +581,7 @@ public abstract class BreakIterator implements Cloneable * @param where A locale specifying the language of the text to be * analyzed. * @return An instance of BreakIterator that locates word boundaries. + * @throws NullPointerException if where is null. * @stable ICU 2.0 */ public static BreakIterator getWordInstance(Locale where) @@ -593,6 +594,7 @@ public abstract class BreakIterator implements Cloneable * @param where A locale specifying the language of the text to be * analyzed. * @return An instance of BreakIterator that locates word boundaries. + * @throws NullPointerException if where is null. * @stable ICU 3.2 */ public static BreakIterator getWordInstance(ULocale where) @@ -619,6 +621,7 @@ public abstract class BreakIterator implements Cloneable * @param where A Locale specifying the language of the text being broken. * @return A new instance of BreakIterator that locates legal * line-wrapping positions. + * @throws NullPointerException if where is null. * @stable ICU 2.0 */ public static BreakIterator getLineInstance(Locale where) @@ -632,6 +635,7 @@ public abstract class BreakIterator implements Cloneable * @param where A Locale specifying the language of the text being broken. * @return A new instance of BreakIterator that locates legal * line-wrapping positions. + * @throws NullPointerException if where is null. * @stable ICU 3.2 */ public static BreakIterator getLineInstance(ULocale where) @@ -658,6 +662,7 @@ public abstract class BreakIterator implements Cloneable * @param where A Locale specifying the language of the text being analyzed. * @return A new instance of BreakIterator that locates logical-character * boundaries. + * @throws NullPointerException if where is null. * @stable ICU 2.0 */ public static BreakIterator getCharacterInstance(Locale where) @@ -671,6 +676,7 @@ public abstract class BreakIterator implements Cloneable * @param where A Locale specifying the language of the text being analyzed. * @return A new instance of BreakIterator that locates logical-character * boundaries. + * @throws NullPointerException if where is null. * @stable ICU 3.2 */ public static BreakIterator getCharacterInstance(ULocale where) @@ -694,6 +700,7 @@ public abstract class BreakIterator implements Cloneable * Returns a new instance of BreakIterator that locates sentence boundaries. * @param where A Locale specifying the language of the text being analyzed. * @return A new instance of BreakIterator that locates sentence boundaries. + * @throws NullPointerException if where is null. * @stable ICU 2.0 */ public static BreakIterator getSentenceInstance(Locale where) @@ -705,6 +712,7 @@ public abstract class BreakIterator implements Cloneable * {@icu} Returns a new instance of BreakIterator that locates sentence boundaries. * @param where A Locale specifying the language of the text being analyzed. * @return A new instance of BreakIterator that locates sentence boundaries. + * @throws NullPointerException if where is null. * @stable ICU 3.2 */ public static BreakIterator getSentenceInstance(ULocale where) @@ -733,6 +741,7 @@ public abstract class BreakIterator implements Cloneable * please use Word Boundary iterator.{@link #getWordInstance} * @param where A Locale specifying the language of the text being analyzed. * @return A new instance of BreakIterator that locates title boundaries. + * @throws NullPointerException if where is null. * @stable ICU 2.0 */ public static BreakIterator getTitleInstance(Locale where) @@ -747,6 +756,7 @@ public abstract class BreakIterator implements Cloneable * please use Word Boundary iterator.{@link #getWordInstance} * @param where A Locale specifying the language of the text being analyzed. * @return A new instance of BreakIterator that locates title boundaries. + * @throws NullPointerException if where is null. * @stable ICU 3.2 s */ public static BreakIterator getTitleInstance(ULocale where) @@ -837,7 +847,9 @@ s */ */ @Deprecated public static BreakIterator getBreakInstance(ULocale where, int kind) { - + if (where == null) { + throw new NullPointerException("Specified locale is null"); + } if (iterCache[kind] != null) { BreakIteratorCache cache = (BreakIteratorCache)iterCache[kind].get(); if (cache != null) { diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java index 52a955fa38c..902aa1d961b 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2012, International Business Machines Corporation and * + * Copyright (C) 1996-2014, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -13,6 +13,7 @@ import java.util.Locale; import com.ibm.icu.dev.test.TestFmwk; import com.ibm.icu.text.BreakIterator; +import com.ibm.icu.util.ULocale; public class BreakIteratorTest extends TestFmwk { @@ -843,4 +844,66 @@ public class BreakIteratorTest extends TestFmwk errln("ERR: Failed to create an instance type: " + type + " / locale: " + loc + " / exception: " + e.getMessage()); } } + + /* + * Test case for Ticket#10721. BreakIterator factory method should throw NPE + * when specified locale is null. + */ + public void TestNullLocale() { + Locale loc = null; + ULocale uloc = null; + + @SuppressWarnings("unused") + BreakIterator brk; + + // Character + try { + brk = BreakIterator.getCharacterInstance(loc); + errln("getCharacterInstance((Locale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + try { + brk = BreakIterator.getCharacterInstance(uloc); + errln("getCharacterInstance((ULocale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + + // Line + try { + brk = BreakIterator.getLineInstance(loc); + errln("getLineInstance((Locale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + try { + brk = BreakIterator.getLineInstance(uloc); + errln("getLineInstance((ULocale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + + // Sentence + try { + brk = BreakIterator.getSentenceInstance(loc); + errln("getSentenceInstance((Locale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + try { + brk = BreakIterator.getSentenceInstance(uloc); + errln("getSentenceInstance((ULocale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + + // Title + try { + brk = BreakIterator.getTitleInstance(loc); + errln("getTitleInstance((Locale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + try { + brk = BreakIterator.getTitleInstance(uloc); + errln("getTitleInstance((ULocale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + + // Word + try { + brk = BreakIterator.getWordInstance(loc); + errln("getWordInstance((Locale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + try { + brk = BreakIterator.getWordInstance(uloc); + errln("getWordInstance((ULocale)null) did not throw NPE."); + } catch (NullPointerException e) { /* OK */ } + } } -- 2.40.0