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 26 * @summary Check if NullPointerException is thrown if the key passed 27 * to remove() is null. 28 */ 29 30 import java.util.prefs.Preferences; 31 import java.util.prefs.AbstractPreferences; 32 import java.util.prefs.BackingStoreException; 33 34 public class RemoveNullKeyCheck { 35 36 private static boolean failed = false; 37 38 public static void main(String[] args) throws Exception { 39 checkPreferencesRemove(); 40 checkAbstractPreferencesRemove(); 41 if (failed) { 42 throw new RuntimeException("Expected NullPointerException " + 43 "not thrown"); 44 } 45 } 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 } 97 } 98 }