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