KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > FontConverterTest


1 package com.thoughtworks.xstream.converters.extended;
2
3 import com.thoughtworks.xstream.XStream;
4 import junit.framework.Test;
5 import junit.framework.TestCase;
6 import junit.framework.TestSuite;
7
8 import java.awt.*;
9 import java.awt.font.TextAttribute JavaDoc;
10 import java.util.Map JavaDoc;
11
12 public class FontConverterTest extends TestCase {
13     private XStream xstream;
14     private Font in;
15
16     public static Test suite() {
17         // Only try to run this test case if graphics environment is available
18
try {
19             new Font("Arial", Font.BOLD, 20);
20             return new TestSuite(FontConverterTest.class);
21         } catch (Throwable JavaDoc t) {
22             return new TestSuite();
23         }
24     }
25
26     protected void setUp() throws Exception JavaDoc {
27         super.setUp();
28         xstream = new XStream();
29         in = new Font("Arial", Font.BOLD, 20);
30     }
31
32     public void testConvertsToFontThatEqualsOriginal() {
33         // execute
34
Font out = (Font) xstream.fromXML(xstream.toXML(in));
35
36         // assert
37
assertEquals(in, out);
38     }
39
40     public void testProducesFontThatHasTheSameAttributes() {
41         // execute
42
Font out = (Font) xstream.fromXML(xstream.toXML(in));
43
44         // assert
45
Map JavaDoc inAttributes = in.getAttributes();
46         Map JavaDoc outAttributes = out.getAttributes();
47
48         // these attributes don't have a valid .equals() method (bad Sun!), so we can't use them in the test.
49
inAttributes.remove(TextAttribute.TRANSFORM);
50         outAttributes.remove(TextAttribute.TRANSFORM);
51
52         assertEquals(inAttributes, outAttributes);
53     }
54
55     public void testCorrectlyInitializesFontToPreventJvmCrash() {
56         // If a font has not been constructed in the correct way, the JVM crashes horribly through some internal
57
// native code, whenever the font is rendered to screen.
58

59         // execute
60
Font out = (Font) xstream.fromXML(xstream.toXML(in));
61
62         Toolkit.getDefaultToolkit().getFontMetrics(out);
63         // if the JVM hasn't crashed yet, we're good.
64

65     }
66 }
67
Popular Tags