jdk/test/java/util/prefs/RemoveNullKeyCheck.java
changeset 12681 ebee74a1fd8c
parent 12552 06a22aed06d1
child 14694 6d6d54dc0ae1
equal deleted inserted replaced
12680:289f842c0175 12681:ebee74a1fd8c
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 7160242
    25  * @bug 7160242 7165118
    26  * @summary Check if NullPointerException is thrown if the key passed
    26  * @summary Check if NullPointerException is thrown if the key passed
    27  *          to remove() is null.
    27  *          to remove() is null.
    28  */
    28  */
    29 
    29 
    30 import java.util.prefs.Preferences;
    30 import java.util.prefs.Preferences;
       
    31 import java.util.prefs.AbstractPreferences;
       
    32 import java.util.prefs.BackingStoreException;
    31 
    33 
    32 public class RemoveNullKeyCheck {
    34 public class RemoveNullKeyCheck {
    33 
    35 
       
    36     private static boolean failed = false;
       
    37 
    34     public static void main(String[] args) throws Exception {
    38     public static void main(String[] args) throws Exception {
    35        try {
    39         checkPreferencesRemove();
    36            Preferences node = Preferences.userRoot().node("N1");
    40         checkAbstractPreferencesRemove();
    37            node.remove(null);
    41         if (failed) {
    38            throw new RuntimeException("Expected NullPointerException " +
    42             throw new RuntimeException("Expected NullPointerException " +
    39                                       "not thrown");
    43                                        "not thrown");
    40        } catch (NullPointerException npe) {
    44         }
    41            System.out.println("NullPointerException thrown");
    45     }
    42        }
    46 
       
    47     public static void checkPreferencesRemove() {
       
    48         try {
       
    49             Preferences node = Preferences.userRoot().node("N1");
       
    50             node.remove(null);
       
    51             failed = true;
       
    52         } catch (NullPointerException npe) {
       
    53         }
       
    54     }
       
    55 
       
    56     public static void checkAbstractPreferencesRemove() {
       
    57 
       
    58         Preferences abstrPrefs = new AbstractPreferences(null, "") {
       
    59             @Override
       
    60             protected void putSpi(String key, String value) {
       
    61             }
       
    62             @Override
       
    63             protected String getSpi(String key) {
       
    64                 return null;
       
    65             }
       
    66             @Override
       
    67             protected void removeSpi(String key) {
       
    68             }
       
    69             @Override
       
    70             protected void removeNodeSpi() throws BackingStoreException {
       
    71             }
       
    72             @Override
       
    73             protected String[] keysSpi() throws BackingStoreException {
       
    74                 return new String[0];
       
    75             }
       
    76             @Override
       
    77             protected String[] childrenNamesSpi() throws BackingStoreException {
       
    78                 return new String[0];
       
    79             }
       
    80             @Override
       
    81             protected AbstractPreferences childSpi(String name) {
       
    82                 return null;
       
    83             }
       
    84             @Override
       
    85             protected void syncSpi() throws BackingStoreException {
       
    86             }
       
    87             @Override
       
    88             protected void flushSpi() throws BackingStoreException {
       
    89             }
       
    90         };
       
    91 
       
    92         try {
       
    93             abstrPrefs.remove(null);
       
    94             failed = true;
       
    95         } catch(NullPointerException npe) {
       
    96         }
    43     }
    97     }
    44 }
    98 }