001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Sergey Burlak
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Graphics;
027: import java.awt.Insets;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030: import java.io.ByteArrayOutputStream;
031: import java.io.PrintStream;
032: import java.net.URL;
033: import java.net.URLClassLoader;
034: import java.security.AccessController;
035: import java.security.PrivilegedAction;
036: import java.util.Hashtable;
037: import java.util.List;
038: import java.util.Locale;
039: import java.util.ResourceBundle;
040: import java.util.Vector;
041: import javax.swing.border.Border;
042: import javax.swing.plaf.ColorUIResource;
043: import javax.swing.plaf.InputMapUIResource;
044: import javax.swing.plaf.basic.BasicLookAndFeel;
045:
046: public class UIDefaultsTest extends SwingTestCase {
047: UIDefaults uiDefaults;
048:
049: @Override
050: public void setUp() throws Exception {
051: super .setUp();
052: uiDefaults = new UIDefaults();
053: }
054:
055: @Override
056: public void tearDown() throws Exception {
057: uiDefaults = null;
058: super .tearDown();
059: }
060:
061: public void testGetBoolean() {
062: uiDefaults.put("TRUE", Boolean.TRUE);
063: assertEquals(1, uiDefaults.size());
064: assertEquals(Boolean.TRUE.booleanValue(), uiDefaults
065: .getBoolean("TRUE"));
066: uiDefaults.put("TRUE", "a");
067: uiDefaults.getBoolean("TRUE");
068: }
069:
070: public void testGetInt() {
071: Integer num = new Integer("10");
072: uiDefaults.put("1", num);
073: assertEquals(1, uiDefaults.size());
074: assertEquals(num.intValue(), uiDefaults.getInt("1"));
075: uiDefaults.put("1", "a");
076: uiDefaults.getInt("1");
077: }
078:
079: public void testGetString() {
080: String str = "10";
081: uiDefaults.put("string", str);
082: assertEquals(1, uiDefaults.size());
083: assertEquals(str, uiDefaults.getString("string"));
084: }
085:
086: public void testGetIcon() {
087: Icon ic = new Icon() {
088: public int getIconHeight() {
089: return 0;
090: }
091:
092: public int getIconWidth() {
093: return 0;
094: }
095:
096: public void paintIcon(final Component c, final Graphics g,
097: final int x, final int y) {
098: }
099: };
100: uiDefaults.put("ic", ic);
101: assertEquals(1, uiDefaults.size());
102: assertEquals(ic, uiDefaults.getIcon("ic"));
103: }
104:
105: public void testGetBorder() {
106: Border b = new Border() {
107: public void paintBorder(final Component c,
108: final Graphics g, final int x, final int y,
109: final int w, final int h) {
110: }
111:
112: public Insets getBorderInsets(final Component c) {
113: return null;
114: }
115:
116: public boolean isBorderOpaque() {
117: return false;
118: }
119: };
120: uiDefaults.put("b", b);
121: assertEquals(1, uiDefaults.size());
122: assertEquals(b, uiDefaults.getBorder("b"));
123: }
124:
125: public void testGetInsets() {
126: Insets ins = new Insets(0, 0, 0, 0);
127: uiDefaults.put("ins", ins);
128: assertEquals(1, uiDefaults.size());
129: assertEquals(ins, uiDefaults.getInsets("ins"));
130: }
131:
132: public void testGetFont() {
133: Font f = new Font("Arial", Font.BOLD, 12);
134: uiDefaults.put("f", f);
135: assertEquals(1, uiDefaults.size());
136: assertEquals(f, uiDefaults.getFont("f"));
137: }
138:
139: public void testGetDimension() {
140: Dimension dim = new Dimension();
141: uiDefaults.put("d", dim);
142: assertEquals(1, uiDefaults.size());
143: assertEquals(dim, uiDefaults.getDimension("d"));
144: }
145:
146: public void testGetColor() {
147: Color c = Color.RED;
148: uiDefaults.put("c", c);
149: assertEquals(1, uiDefaults.size());
150: assertEquals(c, uiDefaults.getColor("c"));
151: }
152:
153: public void testProxyLazyValue() {
154: uiDefaults.put("1", new UIDefaults.ProxyLazyValue(
155: "java.lang.String", new Object[] { "aaa" }));
156: propertyChangeController = new PropertyChangeController();
157: uiDefaults.addPropertyChangeListener(propertyChangeController);
158: assertEquals(1, uiDefaults.size());
159: assertEquals("aaa", uiDefaults.get("1"));
160: assertEquals(1, uiDefaults.size());
161: assertFalse(propertyChangeController.isChanged());
162: uiDefaults.put("2", new UIDefaults.ProxyLazyValue(
163: "java.lang.String"));
164: assertEquals(2, uiDefaults.size());
165: assertEquals("aaa", uiDefaults.get("1"));
166: assertEquals("", uiDefaults.get("2"));
167: assertNull(uiDefaults.getIcon("2"));
168: uiDefaults.put("3", new UIDefaults.ProxyLazyValue(
169: "java.lang.Integer", "decode", new String[] { "43" }));
170: assertEquals(3, uiDefaults.size());
171: assertEquals("aaa", uiDefaults.get("1"));
172: assertEquals("", uiDefaults.get("2"));
173: assertEquals(new Integer("43"), uiDefaults.get("3"));
174: assertEquals(43, uiDefaults.getInt("3"));
175: uiDefaults.put("4", new UIDefaults.ProxyLazyValue(
176: "java.lang.String", "toUpperCase"));
177: assertEquals(4, uiDefaults.size());
178: assertEquals("aaa", uiDefaults.get("1"));
179: assertEquals("", uiDefaults.get("2"));
180: assertEquals(new Integer("43"), uiDefaults.get("3"));
181: assertNull(uiDefaults.get("4"));
182: }
183:
184: public void testProxyLazyValueWithThreadClassLoader()
185: throws Exception {
186: // Regression test for HARMONY-3420
187:
188: final ClassLoader classLoader = new ArrayClassLoader();
189:
190: class ThreadGetUIClass extends Thread {
191: private UIDefaults.ProxyLazyValue plv = new UIDefaults.ProxyLazyValue(
192: "MyLabelUI");
193: private Object result;
194:
195: public ThreadGetUIClass() {
196: setContextClassLoader(classLoader);
197: }
198:
199: public void run() {
200: result = plv.createValue(null);
201: }
202:
203: public Object getResult() {
204: while (true) {
205: try {
206: join();
207: return result;
208: } catch (InterruptedException e) {
209: // Ignored.
210: }
211: }
212: }
213: }
214: ;
215:
216: ThreadGetUIClass thread = new ThreadGetUIClass();
217: thread.start();
218: Object result = thread.getResult();
219: assertNotNull(result);
220: assertEquals(result.getClass(), classLoader
221: .loadClass("MyLabelUI"));
222: }
223:
224: public void testGetUI() {
225: setBasicLF();
226: final String uiClassName = "javax.swing.plaf.basic.BasicButtonUI";
227: uiDefaults.put("ButtonUI", uiClassName);
228: assertNull(uiDefaults.get(uiClassName));
229: JButton button = new JButton();
230: assertTrue(uiDefaults.getUI(button).getClass().getName()
231: .equals(uiClassName));
232: Object classObj = uiDefaults.get(uiClassName);
233: assertNotNull(classObj);
234: Object methodObj = uiDefaults.get(classObj);
235: assertNotNull(methodObj);
236: }
237:
238: public void testGetUIWithClassLoader() throws Exception {
239: // Regression test for HARMONY-3385
240:
241: uiDefaults.put("LabelUI", "MyLabelUI");
242: Class<? extends JComponent> cls = (Class<? extends JComponent>) new ArrayClassLoader()
243: .loadClass("MyJLabel");
244: JComponent component = cls.newInstance();
245: assertNotNull(uiDefaults.getUI(component));
246: }
247:
248: public void testGetUIClass() throws ClassNotFoundException {
249: setBasicLF();
250: final String uiClassName = "javax.swing.plaf.basic.BasicButtonUI";
251: uiDefaults.put("ButtonUI", uiClassName);
252: assertEquals(Class.forName(uiClassName), uiDefaults
253: .getUIClass("ButtonUI"));
254: }
255:
256: public void testGetUIClassWithClassLoader() throws Exception {
257: setBasicLF();
258: final String uiClassName = "javax.swing.plaf.basic.BasicButtonUI";
259: uiDefaults.put("ButtonUI", uiClassName);
260: assertEquals(Class.forName(uiClassName), uiDefaults.getUIClass(
261: "ButtonUI", null));
262: AccessController.doPrivileged(new PrivilegedAction<Object>() {
263: public Object run() {
264: try {
265: assertEquals(Class.forName(uiClassName), uiDefaults
266: .getUIClass("ButtonUI", new URLClassLoader(
267: new URL[0])));
268: } catch (ClassNotFoundException e) {
269: }
270: return null;
271: }
272: });
273: }
274:
275: public void testGetUIClassWithThreadClassLoader() throws Exception {
276: // Regression test for HARMONY-3398
277:
278: final ClassLoader classLoader = new ArrayClassLoader();
279:
280: class ThreadGetUIClass extends Thread {
281: private UIDefaults uid = new UIDefaults(new Object[] {
282: "LabelUI", "MyLabelUI" });
283: private Class result;
284:
285: public ThreadGetUIClass() {
286: setContextClassLoader(classLoader);
287: }
288:
289: public void run() {
290: result = uid.getUIClass("LabelUI");
291: }
292:
293: public Class getResult() {
294: while (true) {
295: try {
296: join();
297: return result;
298: } catch (InterruptedException e) {
299: // Ignored.
300: }
301: }
302: }
303: }
304: ;
305:
306: ThreadGetUIClass thread = new ThreadGetUIClass();
307: thread.start();
308: Class result = thread.getResult();
309: assertNotNull(result);
310: assertEquals(result, classLoader.loadClass("MyLabelUI"));
311: }
312:
313: public void testGetValueFromResourceBundle() {
314: String bundleName = "javax.swing.TestBundle";
315: assertNull(uiDefaults.get("OptionPane.okButtonText"));
316: propertyChangeController = new PropertyChangeController();
317: uiDefaults.addPropertyChangeListener(propertyChangeController);
318: uiDefaults.addResourceBundle(bundleName);
319: assertFalse(propertyChangeController.isChanged());
320: ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
321: assertEquals(bundle.getString("OptionPane.okButtonText"),
322: uiDefaults.get("OptionPane.okButtonText"));
323: propertyChangeController.reset();
324: uiDefaults.removeResourceBundle(bundleName);
325: assertFalse(propertyChangeController.isChanged());
326: }
327:
328: public void testInputMap() {
329: uiDefaults.put("1", new UIDefaults.LazyInputMap(new Object[] {
330: "1", "11", "2", "22", "3", "33", "4", "44" }));
331: assertTrue(uiDefaults.get("1") instanceof InputMapUIResource);
332: InputMapUIResource map = (InputMapUIResource) uiDefaults
333: .get("1");
334: assertEquals(4, map.allKeys().length);
335: }
336:
337: public void testCreateUIDefaultsTable() {
338: assertEquals(0, uiDefaults.size());
339: UIDefaults uid = new UIDefaults(new Object[] { "1", "1v", "2",
340: "2v", "3", "3v", "4", "4v" });
341: assertEquals(4, uid.size());
342: assertEquals("1v", uid.get("1"));
343: assertEquals("3v", uid.getString("3"));
344: uiDefaults.put("key", null);
345: assertNull(uiDefaults.get("key"));
346: }
347:
348: public void testErrorGetUI() {
349: final PrintStream orig = System.err;
350: final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
351: final PrintStream errStream = new PrintStream(byteStream);
352: AccessController.doPrivileged(new PrivilegedAction<Object>() {
353: public Object run() {
354: System.setErr(errStream);
355: return null;
356: }
357: });
358: uiDefaults.put("ButtonUI", "plaf.windows.WWWWWinButtonUI");
359: JButton button = new JButton();
360: uiDefaults.getUI(button);
361: String error = byteStream.toString();
362: assertTrue(error.indexOf("failed") > 0);
363: AccessController.doPrivileged(new PrivilegedAction<Object>() {
364: public Object run() {
365: System.setErr(orig);
366: return null;
367: }
368: });
369: }
370:
371: public void testLocale() {
372: assertTrue(uiDefaults.getDefaultLocale() == Locale.getDefault());
373: propertyChangeController = new PropertyChangeController();
374: uiDefaults.addPropertyChangeListener(propertyChangeController);
375: uiDefaults.setDefaultLocale(Locale.UK);
376: assertFalse(propertyChangeController.isChanged());
377: String bundleName = "javax.swing.TestBundle";
378: assertNull(uiDefaults.get("OptionPane.titleText"));
379: uiDefaults.addResourceBundle(bundleName);
380: assertEquals(ResourceBundle
381: .getBundle(bundleName, Locale.GERMAN).getString(
382: "OptionPane.titleText"), uiDefaults.getString(
383: "OptionPane.titleText", Locale.GERMAN));
384: assertEquals(ResourceBundle.getBundle(bundleName,
385: Locale.ITALIAN).getString("OptionPane.titleText"),
386: uiDefaults.getString("OptionPane.titleText",
387: Locale.ITALIAN));
388: assertEquals(ResourceBundle
389: .getBundle(bundleName, Locale.KOREAN).getString(
390: "OptionPane.titleText"), uiDefaults.getString(
391: "OptionPane.titleText", Locale.KOREAN));
392: }
393:
394: public void testPropertyListeners() {
395: final List<Object> count = new Vector<Object>();
396: PropertyChangeListener listener = new PropertyChangeListener() {
397: public void propertyChange(final PropertyChangeEvent pce) {
398: count.add(new Object());
399: }
400: };
401: uiDefaults.put("1", "1v");
402: assertEquals(0, uiDefaults.getPropertyChangeListeners().length);
403: uiDefaults.addPropertyChangeListener(listener);
404: assertEquals(1, uiDefaults.getPropertyChangeListeners().length);
405: assertEquals(0, count.size());
406: uiDefaults.put("2", "2v");
407: assertEquals(1, count.size());
408: uiDefaults.put("2", "2v");
409: assertEquals(1, count.size());
410: uiDefaults.removePropertyChangeListener(listener);
411: assertEquals(0, uiDefaults.getPropertyChangeListeners().length);
412: }
413:
414: public void testPutDefaultsChangeEvent() {
415: propertyChangeController = new PropertyChangeController();
416: uiDefaults.addPropertyChangeListener(propertyChangeController);
417: uiDefaults.putDefaults(new Object[] { "1", "11", "2", "22",
418: "3", "33", "4", "44" });
419: assertTrue(propertyChangeController.isChanged("UIDefaults"));
420: }
421:
422: public void testCreateProxyLazyValueWithUIResourceAsParameter() {
423: uiDefaults
424: .put(
425: "1",
426: new UIDefaults.ProxyLazyValue(
427: "javax.swing.plaf.BorderUIResource$LineBorderUIResource",
428: new Object[] { new ColorUIResource(255,
429: 255, 0) }));
430: assertEquals(1, uiDefaults.size());
431: assertNotNull(uiDefaults.get("1"));
432: assertTrue(uiDefaults.get("1") instanceof Border);
433: }
434:
435: public void testPut() throws Exception {
436: propertyChangeController = new PropertyChangeController();
437: uiDefaults.addPropertyChangeListener(propertyChangeController);
438: assertNull(uiDefaults.put("1", "val1"));
439: assertEquals(1, uiDefaults.size());
440: assertTrue(propertyChangeController.isChanged("1"));
441: propertyChangeController.reset();
442: assertNull(uiDefaults.put("2", "val2"));
443: assertEquals(2, uiDefaults.size());
444: assertTrue(propertyChangeController.isChanged("2"));
445: propertyChangeController.reset();
446: assertEquals("val2", uiDefaults.put("2", "val2"));
447: assertEquals(2, uiDefaults.size());
448: assertFalse(propertyChangeController.isChanged("2"));
449: propertyChangeController.reset();
450: assertEquals("val2", uiDefaults.put("2", null));
451: assertTrue(propertyChangeController.isChanged("2"));
452: assertEquals(1, uiDefaults.size());
453: propertyChangeController.reset();
454: uiDefaults.put("nokey", null);
455: assertTrue(propertyChangeController.isChanged("nokey"));
456: assertEquals(1, uiDefaults.size());
457: }
458:
459: private void setBasicLF() {
460: try {
461: UIManager.setLookAndFeel(new BasicLookAndFeel() {
462: private static final long serialVersionUID = 1L;
463:
464: @Override
465: public boolean isNativeLookAndFeel() {
466: return true;
467: }
468:
469: @Override
470: public boolean isSupportedLookAndFeel() {
471: return true;
472: }
473:
474: @Override
475: public String getDescription() {
476: return null;
477: }
478:
479: @Override
480: public String getID() {
481: return null;
482: }
483:
484: @Override
485: public String getName() {
486: return null;
487: }
488: });
489: } catch (UnsupportedLookAndFeelException e) {
490: e.printStackTrace();
491: }
492: }
493:
494: /**
495: * Special classloader for classloader-based tests.
496: */
497: private static class ArrayClassLoader extends ClassLoader {
498:
499: private static Hashtable<String, byte[]> table = new Hashtable<String, byte[]>();
500:
501: static {
502: table.put("MyJLabel", new byte[] {
503: /*
504: * public class MyJLabel extends JLabel {
505: * public String getUIClassID() {
506: * return "LabelUI";
507: * }
508: * }
509: */
510: -54, -2, -70, -66, 0, 0, 0, 49, 0, 17, 10, 0, 4, 0, 13, 8,
511: 0, 14, 7, 0, 15, 7, 0, 16, 1, 0, 6, 60, 105, 110,
512: 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67,
513: 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78,
514: 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0,
515: 12, 103, 101, 116, 85, 73, 67, 108, 97, 115, 115,
516: 73, 68, 1, 0, 20, 40, 41, 76, 106, 97, 118, 97, 47,
517: 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103,
518: 59, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105,
519: 108, 101, 1, 0, 13, 77, 121, 74, 76, 97, 98, 101,
520: 108, 46, 106, 97, 118, 97, 12, 0, 5, 0, 6, 1, 0, 7,
521: 76, 97, 98, 101, 108, 85, 73, 1, 0, 8, 77, 121, 74,
522: 76, 97, 98, 101, 108, 1, 0, 18, 106, 97, 118, 97,
523: 120, 47, 115, 119, 105, 110, 103, 47, 74, 76, 97,
524: 98, 101, 108, 0, 33, 0, 3, 0, 4, 0, 0, 0, 0, 0, 2,
525: 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0, 29, 0, 1, 0,
526: 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0,
527: 8, 0, 0, 0, 6, 0, 1, 0, 0, 0, 3, 0, 1, 0, 9, 0, 10,
528: 0, 1, 0, 7, 0, 0, 0, 27, 0, 1, 0, 1, 0, 0, 0, 3,
529: 18, 2, -80, 0, 0, 0, 1, 0, 8, 0, 0, 0, 6, 0, 1, 0,
530: 0, 0, 5, 0, 1, 0, 11, 0, 0, 0, 2, 0, 12 });
531:
532: table.put("MyLabelUI",
533: new byte[] {
534: /*
535: * public class MyLabelUI extends LabelUI {
536: * public static ComponentUI createUI(JComponent c) {
537: * return new MyLabelUI();
538: * }
539: * }
540: */
541: -54, -2, -70, -66, 0, 0, 0, 49, 0, 16, 10, 0, 4, 0,
542: 13, 7, 0, 14, 10, 0, 2, 0, 13, 7, 0, 15, 1,
543: 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3,
544: 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1,
545: 0, 15, 76, 105, 110, 101, 78, 117, 109, 98,
546: 101, 114, 84, 97, 98, 108, 101, 1, 0, 8,
547: 99, 114, 101, 97, 116, 101, 85, 73, 1, 0,
548: 56, 40, 76, 106, 97, 118, 97, 120, 47, 115,
549: 119, 105, 110, 103, 47, 74, 67, 111, 109,
550: 112, 111, 110, 101, 110, 116, 59, 41, 76,
551: 106, 97, 118, 97, 120, 47, 115, 119, 105,
552: 110, 103, 47, 112, 108, 97, 102, 47, 67,
553: 111, 109, 112, 111, 110, 101, 110, 116, 85,
554: 73, 59, 1, 0, 10, 83, 111, 117, 114, 99,
555: 101, 70, 105, 108, 101, 1, 0, 14, 77, 121,
556: 76, 97, 98, 101, 108, 85, 73, 46, 106, 97,
557: 118, 97, 12, 0, 5, 0, 6, 1, 0, 9, 77, 121,
558: 76, 97, 98, 101, 108, 85, 73, 1, 0, 24,
559: 106, 97, 118, 97, 120, 47, 115, 119, 105,
560: 110, 103, 47, 112, 108, 97, 102, 47, 76,
561: 97, 98, 101, 108, 85, 73, 0, 33, 0, 2, 0,
562: 4, 0, 0, 0, 0, 0, 2, 0, 1, 0, 5, 0, 6, 0,
563: 1, 0, 7, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0,
564: 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 8, 0,
565: 0, 0, 6, 0, 1, 0, 0, 0, 5, 0, 9, 0, 9, 0,
566: 10, 0, 1, 0, 7, 0, 0, 0, 32, 0, 2, 0, 1, 0,
567: 0, 0, 8, -69, 0, 2, 89, -73, 0, 3, -80, 0,
568: 0, 0, 1, 0, 8, 0, 0, 0, 6, 0, 1, 0, 0, 0,
569: 7, 0, 1, 0, 11, 0, 0, 0, 2, 0, 12 });
570: }
571:
572: protected Class findClass(String name)
573: throws ClassNotFoundException {
574: byte[] bytes = table.get(name);
575:
576: if (bytes == null) {
577: throw new ClassNotFoundException(name);
578: }
579: return defineClass(name, bytes, 0, bytes.length);
580: }
581: }
582: }
|