01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 08. July 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.extended;
13:
14: import com.thoughtworks.xstream.XStream;
15:
16: import junit.framework.Test;
17: import junit.framework.TestCase;
18: import junit.framework.TestSuite;
19:
20: import java.awt.Font;
21: import java.awt.Toolkit;
22: import java.awt.font.TextAttribute;
23: import java.util.Map;
24:
25: public class FontConverterTest extends TestCase {
26: private XStream xstream;
27: private Font in;
28:
29: public static Test suite() {
30: // Only try to run this test case if graphics environment is available
31: try {
32: new Font("Arial", Font.BOLD, 20);
33: return new TestSuite(FontConverterTest.class);
34: } catch (Throwable t) {
35: return new TestSuite();
36: }
37: }
38:
39: protected void setUp() throws Exception {
40: super .setUp();
41: xstream = new XStream();
42: in = new Font("Arial", Font.BOLD, 20);
43: }
44:
45: public void testConvertsToFontThatEqualsOriginal() {
46: // execute
47: Font out = (Font) xstream.fromXML(xstream.toXML(in));
48:
49: // assert
50: assertEquals(in, out);
51: }
52:
53: public void testProducesFontThatHasTheSameAttributes() {
54: // execute
55: Font out = (Font) xstream.fromXML(xstream.toXML(in));
56:
57: // assert
58: Map inAttributes = in.getAttributes();
59: Map outAttributes = out.getAttributes();
60:
61: // these attributes don't have a valid .equals() method (bad Sun!), so we can't use them in the test.
62: inAttributes.remove(TextAttribute.TRANSFORM);
63: outAttributes.remove(TextAttribute.TRANSFORM);
64:
65: assertEquals(inAttributes, outAttributes);
66: }
67:
68: public void testCorrectlyInitializesFontToPreventJvmCrash() {
69: // If a font has not been constructed in the correct way, the JVM crashes horribly through some internal
70: // native code, whenever the font is rendered to screen.
71:
72: // execute
73: Font out = (Font) xstream.fromXML(xstream.toXML(in));
74:
75: Toolkit.getDefaultToolkit().getFontMetrics(out);
76: // if the JVM hasn't crashed yet, we're good.
77:
78: }
79: }
|