001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.test.unit;
014:
015: import com.ibm.icu.dev.test.TestFmwk;
016:
017: import java.io.*;
018: import java.awt.Color;
019:
020: import com.ibm.richtext.styledtext.MText;
021: import com.ibm.richtext.styledtext.StandardTabRuler;
022: import com.ibm.richtext.styledtext.StyledText;
023: import com.ibm.richtext.styledtext.StyleModifier;
024:
025: import com.ibm.richtext.textlayout.attributes.AttributeMap;
026: import com.ibm.richtext.textlayout.attributes.TextAttribute;
027:
028: public class TestMTextStreaming extends TestFmwk {
029:
030: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
031:
032: public static void main(String[] args) throws Exception {
033:
034: new TestMTextStreaming().run(args);
035: }
036:
037: public TestMTextStreaming() {
038: }
039:
040: public void test() {
041:
042: simpleTest();
043: allAttributesTest();
044: }
045:
046: private void simpleTest() {
047:
048: AttributeMap style = AttributeMap.EMPTY_ATTRIBUTE_MAP;
049: MText text = new StyledText("Hello world!", style);
050:
051: streamAndCompare(text);
052: }
053:
054: private static class TestModifier extends StyleModifier {
055:
056: private Object fKey;
057: private Object fValue;
058:
059: public AttributeMap modifyStyle(AttributeMap style) {
060:
061: return style.addAttribute(fKey, fValue);
062: }
063:
064: TestModifier(Object key, Object value) {
065:
066: fKey = key;
067: fValue = value;
068: }
069: }
070:
071: private void allAttributesTest() {
072:
073: AttributeMap style = AttributeMap.EMPTY_ATTRIBUTE_MAP;
074: MText text = new StyledText("Hello world!", style);
075:
076: int length = text.length();
077:
078: final boolean CHARACTER = true;
079: final boolean PARAGRAPH = false;
080:
081: addStyle(text, 0, length / 2, TextAttribute.FAMILY, "Times",
082: CHARACTER);
083: addStyle(text, length / 2, length, TextAttribute.WEIGHT,
084: TextAttribute.WEIGHT_BOLD, CHARACTER);
085: addStyle(text, 0, length / 2, TextAttribute.POSTURE,
086: TextAttribute.POSTURE_OBLIQUE, CHARACTER);
087: addStyle(text, 0, length / 2, TextAttribute.SIZE, new Float(
088: 13.7f), CHARACTER);
089: addStyle(text, length / 2, length, TextAttribute.SUPERSCRIPT,
090: TextAttribute.SUPERSCRIPT_SUB, CHARACTER);
091: addStyle(text, 0, length / 2, TextAttribute.FOREGROUND,
092: Color.blue, CHARACTER);
093: addStyle(text, 0, length / 2, TextAttribute.BACKGROUND,
094: Color.red, CHARACTER);
095: addStyle(text, 0, length - 1, TextAttribute.STRIKETHROUGH,
096: Boolean.TRUE, CHARACTER);
097:
098: addStyle(text, 0, length, TextAttribute.EXTRA_LINE_SPACING,
099: new Float(4), PARAGRAPH);
100: addStyle(text, 0, length, TextAttribute.FIRST_LINE_INDENT,
101: new Float(6), PARAGRAPH);
102: addStyle(text, 0, length, TextAttribute.MIN_LINE_SPACING,
103: new Float(7), PARAGRAPH);
104: addStyle(text, 0, length, TextAttribute.LINE_FLUSH,
105: TextAttribute.FLUSH_TRAILING, PARAGRAPH);
106: addStyle(text, 0, length, TextAttribute.LEADING_MARGIN,
107: new Float(9), PARAGRAPH);
108: addStyle(text, 0, length, TextAttribute.TRAILING_MARGIN,
109: new Float(9), PARAGRAPH);
110: addStyle(text, 0, length, TextAttribute.TAB_RULER,
111: new StandardTabRuler(), PARAGRAPH);
112:
113: streamAndCompare(text);
114: }
115:
116: private static void addStyle(MText text, int start, int limit,
117: Object key, Object value, boolean character) {
118:
119: StyleModifier modifier = new TestModifier(key, value);
120:
121: if (character) {
122: text.modifyCharacterStyles(start, limit, modifier);
123: } else {
124: text.modifyParagraphStyles(start, limit, modifier);
125: }
126: }
127:
128: public void streamAndCompare(MText text) {
129:
130: Throwable error = null;
131:
132: try {
133: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
134: ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
135: objOut.writeObject(text);
136:
137: ByteArrayInputStream bytesIn = new ByteArrayInputStream(
138: bytesOut.toByteArray());
139: ObjectInputStream objIn = new ObjectInputStream(bytesIn);
140: MText streamedText = (MText) objIn.readObject();
141: if (!isEqual(text, streamedText)) {
142: isEqual(text, streamedText);
143: errln("Streamed text is not equal");
144: }
145: }
146: /* catch(OptionalDataException e) {
147: error = e;
148: }
149: catch(StreamCorruptedException e) {
150: error = e;
151: }*/
152: catch (IOException e) {
153: error = e;
154: } catch (ClassNotFoundException e) {
155: error = e;
156: }
157:
158: if (error != null) {
159: error.printStackTrace();
160: errln("Serialization failed.");
161: }
162: }
163:
164: public static boolean isEqual(MText lhs, MText rhs) {
165:
166: return lhs.equals(rhs);
167: }
168: }
|