]> granicus.if.org Git - icu/commitdiff
ICU-10721 Changed BreakIterator factory method to throw NPE always when specified...
authorYoshito Umaoka <y.umaoka@gmail.com>
Tue, 4 Mar 2014 09:08:11 +0000 (09:08 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Tue, 4 Mar 2014 09:08:11 +0000 (09:08 +0000)
X-SVN-Rev: 35317

icu4j/main/classes/core/src/com/ibm/icu/text/BreakIterator.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/BreakIteratorTest.java

index f4e270e99ecc0601fee2088315ca96a18fdb0fa0..4425fab69c42950cbca0cc4776019b056593b5a3 100644 (file)
@@ -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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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 <code>where</code> 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) {
index 52a955fa38c0fe548fdd0d87b4a99ab377c405b6..902aa1d961bd8221eba5634fb45d4e9dd171f421 100644 (file)
@@ -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 */ }
+    }
 }