001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.uihandler;
043:
044: import java.awt.HeadlessException;
045: import java.awt.Image;
046: import java.awt.Toolkit;
047: import java.awt.image.BufferedImage;
048: import java.awt.image.ColorModel;
049: import java.beans.BeanInfo;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.IOException;
053: import java.net.URL;
054: import java.text.MessageFormat;
055: import java.util.ResourceBundle;
056: import java.util.logging.Handler;
057: import java.util.logging.Level;
058: import junit.framework.TestCase;
059: import java.util.logging.LogRecord;
060: import org.openide.util.Utilities;
061:
062: /**
063: *
064: * @author Jaroslav Tulach
065: */
066: public class DecorableTest extends TestCase {
067: private static class Node implements Decorable {
068: private String name, displayName, base, shortDescription;
069:
070: public void setName(String n) {
071: name = n;
072: }
073:
074: public void setDisplayName(String n) {
075: displayName = n;
076: }
077:
078: public void setIconBaseWithExtension(String base) {
079: this .base = base;
080: }
081:
082: public void setShortDescription(String format) {
083: shortDescription = format;
084: }
085:
086: public String getName() {
087: return name;
088: }
089:
090: public String getDisplayName() {
091: return displayName;
092: }
093:
094: public Image getIcon(int type) {
095: URL u = getClass().getClassLoader().getResource(base);
096: assertNotNull("icon found", u);
097: return Toolkit.getDefaultToolkit().createImage(u);
098: }
099:
100: public String getHtmlDisplayName() {
101: return displayName;
102: }
103: }
104:
105: private static class UINode extends Node {
106: public static Node create(LogRecord r) {
107: Node n = new UINode();
108: LogRecords.decorate(r, n);
109: return n;
110: }
111: }
112:
113: public DecorableTest(String testName) {
114: super (testName);
115: }
116:
117: @Override
118: protected void setUp() throws Exception {
119: }
120:
121: @Override
122: protected void tearDown() throws Exception {
123: }
124:
125: public void testDisplayNameOfTheNode() throws Exception {
126: LogRecord r = new LogRecord(Level.INFO, "test_msg");
127: r
128: .setResourceBundleName("org.netbeans.lib.uihandler.TestBundle");
129: r.setResourceBundle(ResourceBundle
130: .getBundle("org.netbeans.lib.uihandler.TestBundle"));
131: r.setParameters(new Object[] { new Integer(1), "Ahoj" });
132:
133: Node n = UINode.create(r);
134: assertEquals("Name is taken from the message", "test_msg", n
135: .getName());
136:
137: if (!n.getDisplayName().matches(".*Ahoj.*1.*")) {
138: fail("wrong display name, shall contain Ahoj and 1: "
139: + n.getDisplayName());
140: }
141: assertSerializedWell(r, n);
142: }
143:
144: public void testIconOfTheNode() throws Exception {
145: LogRecord r = new LogRecord(Level.INFO, "icon_msg");
146: r
147: .setResourceBundleName("org.netbeans.lib.uihandler.TestBundle");
148: r.setResourceBundle(ResourceBundle
149: .getBundle("org.netbeans.lib.uihandler.TestBundle"));
150: r.setParameters(new Object[] { new Integer(1), "Ahoj" });
151:
152: Node n = UINode.create(r);
153: assertEquals("Name is taken from the message", "icon_msg", n
154: .getName());
155:
156: if (!n.getDisplayName().matches(".*Ahoj.*")) {
157: fail("wrong display name, shall contain Ahoj: "
158: + n.getDisplayName());
159: }
160:
161: Image img = n.getIcon(BeanInfo.ICON_COLOR_32x32);
162: assertNotNull("Some icon", img);
163: IconInfo imgReal = new IconInfo(img);
164: IconInfo template = new IconInfo(getClass().getResource(
165: "testicon.png"));
166: assertEquals("Icon from ICON_BASE used", template, imgReal);
167:
168: assertSerializedWell(r, n);
169: }
170:
171: public void testSomeNPE() throws Exception {
172: LogRecord r = new LogRecord(Level.FINE, "UI_ACTION_EDITOR");
173: Node n = UINode.create(r);
174: assertNotNull(n);
175: assertEquals("No name", "", n.getDisplayName());
176: assertNotNull(n.getName());
177: assertSerializedWell(r, n);
178: }
179:
180: public void testHasNonNullName() throws Exception {
181: LogRecord r = new LogRecord(Level.WARNING, null);
182: r.setThrown(new Exception());
183: Node n = UINode.create(r);
184: assertNotNull(n);
185: assertNotNull(n.getName());
186: assertSerializedWell(r, n);
187: }
188:
189: public void testHasNonNullNameWhenMessageIsGiven() throws Exception {
190: Exception my = new Exception("Ahoj");
191: LogRecord r = new LogRecord(Level.WARNING, my.getMessage());
192: r.setThrown(my);
193: Node n = UINode.create(r);
194: assertNotNull(n);
195: assertNotNull(n.getName());
196: assertSerializedWell(r, n);
197: }
198:
199: private static void assertSerializedWell(LogRecord r, Node n)
200: throws Exception {
201: assertNotNull("There is a log record", r);
202: ByteArrayOutputStream os = new ByteArrayOutputStream();
203: LogRecords.write(os, r);
204: os.close();
205:
206: {
207: ByteArrayInputStream is = new ByteArrayInputStream(os
208: .toByteArray());
209: class H extends Handler {
210: public LogRecord nr;
211:
212: public void publish(LogRecord arg0) {
213: assertNull("First call", nr);
214: nr = arg0;
215: }
216:
217: public void flush() {
218: }
219:
220: public void close() throws SecurityException {
221: }
222: }
223:
224: H handler = new H();
225: LogRecords.scan(is, handler);
226: LogRecord nr = handler.nr;
227: is.close();
228:
229: Node newNode = UINode.create(nr);
230:
231: assertEquals("name", n.getName(), newNode.getName());
232: assertEquals("displayName", n.getDisplayName(), newNode
233: .getDisplayName());
234: assertEquals("htmlName", n.getHtmlDisplayName(), newNode
235: .getHtmlDisplayName());
236: IconInfo old = new IconInfo(n
237: .getIcon(BeanInfo.ICON_COLOR_16x16));
238: assertEquals("16x16", old, new IconInfo(newNode
239: .getIcon(BeanInfo.ICON_COLOR_16x16)));
240: }
241: class H extends Handler {
242: LogRecord one;
243:
244: public void publish(LogRecord a) {
245: assertNull("This is first one: " + a, one);
246: one = a;
247: }
248:
249: public void flush() {
250: }
251:
252: public void close() throws SecurityException {
253: }
254: }
255:
256: H handler = new H();
257:
258: {
259: ByteArrayInputStream is = new ByteArrayInputStream(os
260: .toByteArray());
261: LogRecords.scan(is, handler);
262: is.close();
263:
264: Node newNode = UINode.create(handler.one);
265:
266: assertEquals("name", n.getName(), newNode.getName());
267: assertEquals("displayName", n.getDisplayName(), newNode
268: .getDisplayName());
269: assertEquals("htmlName", n.getHtmlDisplayName(), newNode
270: .getHtmlDisplayName());
271:
272: IconInfo old = new IconInfo(n
273: .getIcon(BeanInfo.ICON_COLOR_16x16));
274: assertEquals("16x16", old, new IconInfo(newNode
275: .getIcon(BeanInfo.ICON_COLOR_16x16)));
276: }
277: }
278:
279: static final java.awt.image.BufferedImage createBufferedImage(
280: int width, int height) {
281: if (Utilities.isMac()) {
282: return new BufferedImage(width, height,
283: BufferedImage.TYPE_INT_ARGB_PRE);
284: }
285:
286: ColorModel model = colorModel(java.awt.Transparency.TRANSLUCENT);
287: java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage(
288: model, model.createCompatibleWritableRaster(width,
289: height), model.isAlphaPremultiplied(), null);
290:
291: return buffImage;
292: }
293:
294: static private ColorModel colorModel(int transparency) {
295: ColorModel model;
296: try {
297: model = java.awt.GraphicsEnvironment
298: .getLocalGraphicsEnvironment()
299: .getDefaultScreenDevice().getDefaultConfiguration()
300: .getColorModel(transparency);
301: } catch (HeadlessException he) {
302: model = ColorModel.getRGBdefault();
303: }
304:
305: return model;
306: }
307:
308: static final BufferedImage toBufferedImage(Image img) {
309: // load the image
310: new javax.swing.ImageIcon(img, "");
311:
312: java.awt.image.BufferedImage rep = createBufferedImage(img
313: .getWidth(null), img.getHeight(null));
314: java.awt.Graphics g = rep.createGraphics();
315: g.drawImage(img, 0, 0, null);
316: g.dispose();
317: img.flush();
318:
319: return rep;
320: }
321:
322: private static final class IconInfo implements Comparable<IconInfo> {
323: final int hash;
324:
325: public IconInfo(URL u) throws IOException {
326: this (Toolkit.getDefaultToolkit().getImage(u));
327: }
328:
329: public IconInfo(Image img) throws IOException {
330: BufferedImage image = toBufferedImage(img);
331:
332: int hash;
333: try {
334: int w = image.getWidth();
335: int h = image.getHeight();
336: hash = w * 3 + h * 7;
337:
338: for (int i = 0; i < w; i++) {
339: for (int j = 0; j < h; j++) {
340: int rgb = image.getRGB(i, j);
341: hash += (rgb >> 2);
342: }
343: }
344: } catch (IndexOutOfBoundsException ex) {
345: fail("Error: " + ex.getMessage());
346: throw new Error();
347: }
348:
349: this .hash = hash;
350: }
351:
352: public IconInfo(String name, String path, int hash) {
353: this .hash = hash;
354: }
355:
356: @Override
357: public int hashCode() {
358: return hash;
359: }
360:
361: @Override
362: public boolean equals(Object obj) {
363: if (obj == null)
364: return false;
365: if (getClass() != obj.getClass())
366: return false;
367: final IconInfo other = (IconInfo) obj;
368:
369: if (this .hash != other.hash)
370: return false;
371: return true;
372: }
373:
374: public int compareTo(IconInfo another) {
375: if (hash != another.hash) {
376: return hash - another.hash;
377: }
378:
379: return 0;
380: }
381:
382: public String toString() {
383: String h = Integer.toHexString(hash);
384: if (h.length() < 8) {
385: h = "00000000".substring(h.length()) + h;
386: }
387:
388: return MessageFormat.format("Icon #{0}", h);
389: }
390: } // end of IconInfo
391:
392: }
|