2 * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
\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
13 package com.ibm.richtext.test.unit;
\r
15 import com.ibm.icu.dev.test.TestFmwk;
\r
18 import java.awt.Color;
\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
25 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
26 import com.ibm.richtext.textlayout.attributes.TextAttribute;
\r
28 public class TestMTextStreaming extends TestFmwk {
\r
30 static final String COPYRIGHT =
\r
31 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
33 public static void main(String[] args) throws Exception {
\r
35 new TestMTextStreaming().run(args);
\r
38 public TestMTextStreaming() {
\r
41 public void test() {
\r
44 allAttributesTest();
\r
47 private void simpleTest() {
\r
49 AttributeMap style = AttributeMap.EMPTY_ATTRIBUTE_MAP;
\r
50 MText text = new StyledText("Hello world!", style);
\r
52 streamAndCompare(text);
\r
55 private static class TestModifier extends StyleModifier {
\r
57 private Object fKey;
\r
58 private Object fValue;
\r
60 public AttributeMap modifyStyle(AttributeMap style) {
\r
62 return style.addAttribute(fKey, fValue);
\r
65 TestModifier(Object key, Object value) {
\r
72 private void allAttributesTest() {
\r
74 AttributeMap style = AttributeMap.EMPTY_ATTRIBUTE_MAP;
\r
75 MText text = new StyledText("Hello world!", style);
\r
77 int length = text.length();
\r
79 final boolean CHARACTER = true;
\r
80 final boolean PARAGRAPH = false;
\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
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
99 streamAndCompare(text);
\r
102 private static void addStyle(MText text,
\r
107 boolean character) {
\r
109 StyleModifier modifier = new TestModifier(key, value);
\r
112 text.modifyCharacterStyles(start, limit, modifier);
\r
115 text.modifyParagraphStyles(start, limit, modifier);
\r
119 public void streamAndCompare(MText text) {
\r
121 Throwable error = null;
\r
124 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
\r
125 ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
\r
126 objOut.writeObject(text);
\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
137 /* catch(OptionalDataException e) {
\r
140 catch(StreamCorruptedException e) {
\r
143 catch(IOException e) {
\r
146 catch(ClassNotFoundException e) {
\r
150 if (error != null) {
\r
151 error.printStackTrace();
\r
152 errln("Serialization failed.");
\r
156 public static boolean isEqual(MText lhs, MText rhs) {
\r
158 return lhs.equals(rhs);
\r