]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/test/unit/TestMTextStreaming.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / test / unit / TestMTextStreaming.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.test.unit;\r
14 \r
15 import com.ibm.icu.dev.test.TestFmwk;\r
16 \r
17 import java.io.*;\r
18 import java.awt.Color;\r
19 \r
20 import com.ibm.richtext.styledtext.MText;\r
21 import com.ibm.richtext.styledtext.StandardTabRuler;\r
22 import com.ibm.richtext.styledtext.StyledText;\r
23 import com.ibm.richtext.styledtext.StyleModifier;\r
24 \r
25 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
26 import com.ibm.richtext.textlayout.attributes.TextAttribute;\r
27 \r
28 public class TestMTextStreaming extends TestFmwk {\r
29 \r
30     static final String COPYRIGHT =\r
31                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
32                 \r
33     public static void main(String[] args) throws Exception {\r
34 \r
35         new TestMTextStreaming().run(args);\r
36     }\r
37 \r
38     public TestMTextStreaming() {\r
39     }\r
40 \r
41     public void test() {\r
42 \r
43         simpleTest();\r
44         allAttributesTest();\r
45     }\r
46 \r
47     private void simpleTest() {\r
48 \r
49         AttributeMap style = AttributeMap.EMPTY_ATTRIBUTE_MAP;\r
50         MText text = new StyledText("Hello world!", style);\r
51 \r
52         streamAndCompare(text);\r
53     }\r
54 \r
55     private static class TestModifier extends StyleModifier {\r
56 \r
57         private Object fKey;\r
58         private Object fValue;\r
59 \r
60         public AttributeMap modifyStyle(AttributeMap style) {\r
61 \r
62             return style.addAttribute(fKey, fValue);\r
63         }\r
64 \r
65         TestModifier(Object key, Object value) {\r
66 \r
67             fKey = key;\r
68             fValue = value;\r
69         }\r
70     }\r
71 \r
72     private void allAttributesTest() {\r
73 \r
74         AttributeMap style = AttributeMap.EMPTY_ATTRIBUTE_MAP;\r
75         MText text = new StyledText("Hello world!", style);\r
76 \r
77         int length = text.length();\r
78 \r
79         final boolean CHARACTER = true;\r
80         final boolean PARAGRAPH = false;\r
81 \r
82         addStyle(text, 0, length/2, TextAttribute.FAMILY, "Times", CHARACTER);\r
83         addStyle(text, length/2, length, TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, CHARACTER);\r
84         addStyle(text, 0, length/2, TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, CHARACTER);\r
85         addStyle(text, 0, length/2, TextAttribute.SIZE, new Float(13.7f), CHARACTER);\r
86         addStyle(text, length/2, length, TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, CHARACTER);\r
87         addStyle(text, 0, length/2, TextAttribute.FOREGROUND, Color.blue, CHARACTER);\r
88         addStyle(text, 0, length/2, TextAttribute.BACKGROUND, Color.red, CHARACTER);\r
89         addStyle(text, 0, length-1, TextAttribute.STRIKETHROUGH, Boolean.TRUE, CHARACTER);\r
90 \r
91         addStyle(text, 0, length, TextAttribute.EXTRA_LINE_SPACING, new Float(4), PARAGRAPH);\r
92         addStyle(text, 0, length, TextAttribute.FIRST_LINE_INDENT, new Float(6), PARAGRAPH);\r
93         addStyle(text, 0, length, TextAttribute.MIN_LINE_SPACING, new Float(7), PARAGRAPH);\r
94         addStyle(text, 0, length, TextAttribute.LINE_FLUSH, TextAttribute.FLUSH_TRAILING, PARAGRAPH);\r
95         addStyle(text, 0, length, TextAttribute.LEADING_MARGIN, new Float(9), PARAGRAPH);\r
96         addStyle(text, 0, length, TextAttribute.TRAILING_MARGIN, new Float(9), PARAGRAPH);\r
97         addStyle(text, 0, length, TextAttribute.TAB_RULER, new StandardTabRuler(), PARAGRAPH);\r
98 \r
99         streamAndCompare(text);\r
100     }\r
101 \r
102     private static void addStyle(MText text,\r
103                                  int start,\r
104                                  int limit,\r
105                                  Object key,\r
106                                  Object value,\r
107                                  boolean character) {\r
108 \r
109         StyleModifier modifier = new TestModifier(key, value);\r
110 \r
111         if (character) {\r
112             text.modifyCharacterStyles(start, limit, modifier);\r
113         }\r
114         else {\r
115             text.modifyParagraphStyles(start, limit, modifier);\r
116         }\r
117     }\r
118 \r
119     public void streamAndCompare(MText text) {\r
120 \r
121         Throwable error = null;\r
122 \r
123         try {\r
124             ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();\r
125             ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);\r
126             objOut.writeObject(text);\r
127 \r
128             ByteArrayInputStream bytesIn =\r
129                             new ByteArrayInputStream(bytesOut.toByteArray());\r
130             ObjectInputStream objIn = new ObjectInputStream(bytesIn);\r
131             MText streamedText = (MText) objIn.readObject();\r
132             if (!isEqual(text, streamedText)) {\r
133                 isEqual(text, streamedText);\r
134                 errln("Streamed text is not equal");\r
135             }\r
136         }\r
137 /*        catch(OptionalDataException e) {\r
138             error = e;\r
139         }\r
140         catch(StreamCorruptedException e) {\r
141             error = e;\r
142         }*/\r
143         catch(IOException e) {\r
144             error = e;\r
145         }\r
146         catch(ClassNotFoundException e) {\r
147             error = e;\r
148         }\r
149 \r
150         if (error != null) {\r
151             error.printStackTrace();\r
152             errln("Serialization failed.");\r
153         }\r
154     }\r
155 \r
156     public static boolean isEqual(MText lhs, MText rhs) {\r
157 \r
158         return lhs.equals(rhs);\r
159     }\r
160 }