1 /* 2 * Copyright (c) 2009, 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 6860438 26 @summary Tests various MultiUIDefaults methods 27 @author Peter Zhelezniakov 28 @run main Test6860438 29 */ 30 31 import java.util.Enumeration; 32 import java.util.Map.Entry; 33 import java.util.Set; 34 import javax.swing.UIManager; 35 36 public class Test6860438 37 { 38 static final String KEY = "Test6860438.key"; 39 static final String VALUE = "Test6860438.value"; 40 check(Object key, Object value, boolean present, int size)41 void check(Object key, Object value, boolean present, int size) { 42 check(UIManager.get(key) == value, "UIManager.get()"); 43 check(UIManager.getDefaults().size() == size, "MultiUIDefaults.size()"); 44 45 checkEnumeration(UIManager.getDefaults().keys(), 46 key, present, "MultiUIDefaults.keys()"); 47 checkEnumeration(UIManager.getDefaults().elements(), 48 value, present, "MultiUIDefaults.elements()"); 49 50 // check MultiUIDefaults.entrySet() 51 boolean found = false; 52 Set<Entry<Object, Object>> entries = UIManager.getDefaults().entrySet(); 53 for (Entry<Object, Object> e: entries) { 54 if (e.getKey() == key) { 55 check(e.getValue() == value, "MultiUIDefaults.entrySet()"); 56 found = true; 57 } 58 } 59 check(found == present, "MultiUIDefaults.entrySet()"); 60 } 61 checkEnumeration(Enumeration<Object> e, Object elem, boolean present, String error)62 void checkEnumeration(Enumeration<Object> e, Object elem, 63 boolean present, String error) { 64 boolean found = false; 65 while (e.hasMoreElements()) { 66 if (e.nextElement() == elem) { 67 found = true; 68 } 69 } 70 check(found == present, error); 71 } 72 check(boolean condition, String methodName)73 void check(boolean condition, String methodName) { 74 if (! condition) { 75 throw new RuntimeException(methodName + " failed"); 76 } 77 } 78 test()79 void test() { 80 int size = UIManager.getDefaults().size(); 81 82 // create a new value, size increases 83 UIManager.getLookAndFeelDefaults().put(KEY, VALUE); 84 check(KEY, VALUE, true, size + 1); 85 86 // override the value, size remains the same 87 UIManager.put(KEY, VALUE); 88 check(KEY, VALUE, true, size + 1); 89 90 // remove the value, size decreases 91 UIManager.getDefaults().remove(KEY); 92 check(KEY, null, false, size); 93 } 94 main(String[] args)95 public static void main(String[] args) { 96 new Test6860438().test(); 97 } 98 } 99