jdk/test/java/util/prefs/RemoveNullKeyCheck.java
author khazra
Wed, 09 May 2012 11:14:22 -0700
changeset 12681 ebee74a1fd8c
parent 12552 06a22aed06d1
child 14694 6d6d54dc0ae1
permissions -rw-r--r--
7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE Summary: Insert null argument check in AbstractPreferences.remove() Reviewed-by: dholmes, chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12552
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     1
/*
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     4
 *
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     8
 *
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    13
 * accompanied this code).
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    14
 *
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    18
 *
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    21
 * questions.
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    22
 */
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    23
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    24
/* @test
12681
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    25
 * @bug 7160242 7165118
12552
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    26
 * @summary Check if NullPointerException is thrown if the key passed
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    27
 *          to remove() is null.
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    28
 */
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    29
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    30
import java.util.prefs.Preferences;
12681
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    31
import java.util.prefs.AbstractPreferences;
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    32
import java.util.prefs.BackingStoreException;
12552
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    33
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    34
public class RemoveNullKeyCheck {
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    35
12681
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    36
    private static boolean failed = false;
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    37
12552
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
12681
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    39
        checkPreferencesRemove();
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    40
        checkAbstractPreferencesRemove();
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    41
        if (failed) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    42
            throw new RuntimeException("Expected NullPointerException " +
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    43
                                       "not thrown");
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    44
        }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    45
    }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    46
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    47
    public static void checkPreferencesRemove() {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    48
        try {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    49
            Preferences node = Preferences.userRoot().node("N1");
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    50
            node.remove(null);
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    51
            failed = true;
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    52
        } catch (NullPointerException npe) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    53
        }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    54
    }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    55
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    56
    public static void checkAbstractPreferencesRemove() {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    57
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    58
        Preferences abstrPrefs = new AbstractPreferences(null, "") {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    59
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    60
            protected void putSpi(String key, String value) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    61
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    62
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    63
            protected String getSpi(String key) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    64
                return null;
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    65
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    66
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    67
            protected void removeSpi(String key) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    68
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    69
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    70
            protected void removeNodeSpi() throws BackingStoreException {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    71
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    72
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    73
            protected String[] keysSpi() throws BackingStoreException {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    74
                return new String[0];
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    75
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    76
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    77
            protected String[] childrenNamesSpi() throws BackingStoreException {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    78
                return new String[0];
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    79
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    80
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    81
            protected AbstractPreferences childSpi(String name) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    82
                return null;
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    83
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    84
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    85
            protected void syncSpi() throws BackingStoreException {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    86
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    87
            @Override
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    88
            protected void flushSpi() throws BackingStoreException {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    89
            }
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    90
        };
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    91
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    92
        try {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    93
            abstrPrefs.remove(null);
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    94
            failed = true;
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    95
        } catch(NullPointerException npe) {
ebee74a1fd8c 7165118: (prefs) AbstractPreferences.remove(null) does not throw NPE
khazra
parents: 12552
diff changeset
    96
        }
12552
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    97
    }
06a22aed06d1 7160242: (prefs) Preferences.remove(null) does not throw NPE [macosx]
khazra
parents:
diff changeset
    98
}