Source Code Cross Referenced for TestMText.java in  » Internationalization-Localization » icu4j » com » ibm » richtext » test » unit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.richtext.test.unit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 com.ibm.richtext.textlayout.attributes.AttributeMap;
018:        import com.ibm.richtext.textlayout.attributes.TextAttribute;
019:
020:        import com.ibm.richtext.styledtext.MConstText;
021:        import com.ibm.richtext.styledtext.MText;
022:        import com.ibm.richtext.styledtext.StyledText;
023:        import com.ibm.richtext.styledtext.StyleModifier;
024:
025:        import java.text.CharacterIterator;
026:        import java.util.Random;
027:
028:        import java.io.*;
029:
030:        public class TestMText extends TestFmwk {
031:
032:            static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
033:
034:            public static void main(String[] args) throws Exception {
035:
036:                new TestMText().run(args);
037:            }
038:
039:            private static final int TEST_ITERATIONS = 5000;
040:            private static final int STYLE_TEST_ITERATIONS = 5000;
041:            private static final long RAND_SEED = 598436;
042:
043:            private static final int NOT_IN_MONKEY_TEST = -5000;
044:            private int testIteration = NOT_IN_MONKEY_TEST;
045:            private int theCase = NOT_IN_MONKEY_TEST;
046:
047:            private static StyleModifier createMinusModifier(final Object attr) {
048:                return new StyleModifier() {
049:                    public AttributeMap modifyStyle(AttributeMap style) {
050:                        return style.removeAttribute(attr);
051:                    }
052:                };
053:            }
054:
055:            public void test() {
056:
057:                simpleTest();
058:                styleTest();
059:                monkeyTest(true);
060:            }
061:
062:            public void simpleTest() {
063:
064:                AttributeMap boldStyle = new AttributeMap(TextAttribute.WEIGHT,
065:                        TextAttribute.WEIGHT_BOLD);
066:                AttributeMap italicStyle = new AttributeMap(
067:                        TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
068:
069:                MConstText allBold = new StyledText("bbbbb", boldStyle);
070:                MConstText allItalic = new StyledText("iii", italicStyle);
071:                MConstText plain = new StyledText("pppppp",
072:                        AttributeMap.EMPTY_ATTRIBUTE_MAP);
073:
074:                {
075:                    MText buf = new StyledText();
076:                    int ts = buf.getTimeStamp();
077:                    buf.append(allBold);
078:                    buf.append(allItalic);
079:
080:                    if (ts == buf.getTimeStamp()) {
081:                        errln("Time stamp not incremented");
082:                    }
083:
084:                    // should be bbbbbiii now
085:
086:                    if (buf.length() != allBold.length() + allItalic.length()) {
087:                        errln("Length is wrong.");
088:                    }
089:
090:                    for (int i = 0; i < buf.length(); i++) {
091:
092:                        char rightChar;
093:                        AttributeMap rightStyle;
094:
095:                        if (i < allBold.length()) {
096:                            rightChar = allBold.at(0);
097:                            rightStyle = boldStyle;
098:                        } else {
099:                            rightChar = allItalic.at(0);
100:                            rightStyle = italicStyle;
101:                        }
102:
103:                        if (buf.at(i) != rightChar) {
104:                            errln("Character is wrong.");
105:                        }
106:                        if (!buf.characterStyleAt(i).equals(rightStyle)) {
107:                            errln("Style is wrong.");
108:                        }
109:                    }
110:
111:                    int pos = 0;
112:
113:                    if (!buf.characterStyleAt(pos).equals(boldStyle)) {
114:                        errln("First style is wrong.");
115:                    }
116:                    if (buf.characterStyleLimit(pos) != allBold.length()) {
117:                        errln("Run length is wrong.");
118:                    }
119:
120:                    pos = allBold.length();
121:
122:                    if (!buf.characterStyleAt(pos).equals(italicStyle)) {
123:                        errln("Second style is wrong.");
124:                    }
125:                    if (buf.characterStyleLimit(pos) != buf.length()) {
126:                        errln("Run length is wrong.");
127:                    }
128:
129:                    {
130:                        buf.resetDamagedRange();
131:                        int oldLength = buf.length();
132:                        buf.replace(buf.length(), buf.length(), allBold, 0,
133:                                allBold.length());
134:                        // bbbbbiiibbbbb
135:
136:                        if (buf.damagedRangeStart() != oldLength) {
137:                            errln("Damaged range start is incorrect");
138:                        }
139:                        if (buf.damagedRangeLimit() != buf.length()) {
140:                            errln("Damaged range limit is incorrect");
141:                        }
142:                    }
143:
144:                    int start = allBold.length();
145:                    int limit = start + allItalic.length();
146:                    buf.remove(start, limit);
147:                    // bbbbbbbbbb
148:
149:                    if (buf.length() != 2 * allBold.length()) {
150:                        errln("Text should be twice the length of bold text.");
151:                    }
152:
153:                    pos = buf.length() / 2;
154:                    if (buf.characterStyleStart(pos) != 0
155:                            || buf.characterStyleLimit(pos) != buf.length()) {
156:                        errln("Run range is wrong.");
157:                    }
158:                    if (!buf.characterStyleAt(pos).equals(boldStyle)) {
159:                        errln("Run style is wrong.");
160:                    }
161:
162:                    ts = buf.getTimeStamp();
163:                    CharacterIterator cIter = buf.createCharacterIterator();
164:                    for (char ch = cIter.first(); ch != CharacterIterator.DONE; ch = cIter
165:                            .next()) {
166:                        if (ch != allBold.at(0)) {
167:                            errln("Character is wrong.");
168:                        }
169:                    }
170:
171:                    if (ts != buf.getTimeStamp()) {
172:                        errln("Time stamp should not have changed");
173:                    }
174:
175:                    buf.replace(0, 1, plain, 0, plain.length());
176:
177:                    if (ts == buf.getTimeStamp()) {
178:                        errln("Time stamp not incremented");
179:                    }
180:
181:                    // ppppppbbbbbbbbb
182:                    buf.replace(plain.length(), buf.length(), allItalic, 0,
183:                            allItalic.length());
184:                    // ppppppiii
185:
186:                    if (buf.length() != allItalic.length() + plain.length()) {
187:                        errln("Length is wrong.");
188:                    }
189:
190:                    pos = 0;
191:                    if (buf.characterStyleLimit(pos) != plain.length()) {
192:                        errln("Run limit is wrong.");
193:                    }
194:
195:                    pos = plain.length();
196:                    if (buf.characterStyleLimit(pos) != buf.length()) {
197:                        errln("Run limit is wrong.");
198:                    }
199:
200:                    buf.replace(plain.length(), plain.length(), allBold, 0,
201:                            allBold.length());
202:                    // ppppppbbbbbiii
203:
204:                    AttributeMap st = buf.characterStyleAt(1);
205:                    if (!st.equals(AttributeMap.EMPTY_ATTRIBUTE_MAP)) {
206:                        errln("Style is wrong.");
207:                    }
208:                    if (buf.characterStyleStart(1) != 0
209:                            || buf.characterStyleLimit(1) != plain.length()) {
210:                        errln("Style start is wrong.");
211:                    }
212:
213:                    st = buf.characterStyleAt(buf.length() - 1);
214:                    if (!st.equals(italicStyle)) {
215:                        errln("Style is wrong.");
216:                    }
217:                    if (buf.characterStyleStart(buf.length() - 1) != plain
218:                            .length()
219:                            + allBold.length()) {
220:                        errln("Style start is wrong.");
221:                    }
222:
223:                    if (buf.characterStyleLimit(buf.length() - 1) != buf
224:                            .length()) {
225:                        errln("Style limit is wrong.");
226:                    }
227:                }
228:            }
229:
230:            private static int randInt(Random rand, int limit) {
231:
232:                return randInt(rand, 0, limit);
233:            }
234:
235:            private static int randInt(Random rand, int start, int limit) {
236:
237:                if (start > limit) {
238:                    throw new IllegalArgumentException(
239:                            "Range length is negative.");
240:                } else if (start == limit) {
241:                    return start;
242:                }
243:
244:                return start + (Math.abs(rand.nextInt()) % (limit - start));
245:            }
246:
247:            public void styleTest() {
248:
249:                MText text = new StyledText("0123456789",
250:                        AttributeMap.EMPTY_ATTRIBUTE_MAP);
251:
252:                AttributeMap[] styles = new AttributeMap[text.length()];
253:                for (int i = 0; i < styles.length; i++) {
254:                    styles[i] = AttributeMap.EMPTY_ATTRIBUTE_MAP;
255:                }
256:                AttributeMap[] oldStyles = new AttributeMap[styles.length];
257:                System.arraycopy(styles, 0, oldStyles, 0, styles.length);
258:
259:                AttributeMap bigStyle = new AttributeMap(
260:                        TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON)
261:                        .addAttribute(TextAttribute.SIZE, new Float(23.0f));
262:
263:                StyleModifier[] modifiers = {
264:                        StyleModifier
265:                                .createReplaceModifier(new AttributeMap(
266:                                        TextAttribute.WEIGHT,
267:                                        TextAttribute.WEIGHT_BOLD)),
268:                        StyleModifier.createAddModifier(new AttributeMap(
269:                                TextAttribute.WEIGHT, new Float(1.0f))),
270:                        createMinusModifier(TextAttribute.WEIGHT),
271:
272:                        StyleModifier.createAddModifier(new AttributeMap(
273:                                TextAttribute.POSTURE, new Float(0.0f))),
274:                        StyleModifier.createReplaceModifier(new AttributeMap(
275:                                TextAttribute.POSTURE,
276:                                TextAttribute.POSTURE_OBLIQUE)),
277:                        createMinusModifier(TextAttribute.POSTURE),
278:
279:                        StyleModifier.createAddModifier(bigStyle),
280:                        StyleModifier.createReplaceModifier(bigStyle),
281:                        createMinusModifier(bigStyle.getKeySet()) };
282:
283:                Random rand = new Random(RAND_SEED);
284:                final int stopAt = 4;
285:
286:                for (int testIteration = 0; testIteration < STYLE_TEST_ITERATIONS + 1; testIteration++) {
287:
288:                    System.arraycopy(styles, 0, oldStyles, 0, styles.length);
289:
290:                    int startingAt = Integer.MAX_VALUE;
291:                    int endingAt = Integer.MIN_VALUE;
292:                    int oldTs = text.getTimeStamp();
293:
294:                    // hack way to do an invariant check before starting...
295:                    if (testIteration != 0) {
296:                        // modify styles
297:                        text.resetDamagedRange();
298:                        startingAt = randInt(rand, styles.length + 1);
299:                        endingAt = randInt(rand, startingAt, styles.length + 1);
300:                        StyleModifier modifier = modifiers[randInt(rand,
301:                                modifiers.length)];
302:
303:                        if (testIteration == stopAt) {
304:                            testIteration = stopAt;
305:                        }
306:                        text.modifyCharacterStyles(startingAt, endingAt,
307:                                modifier);
308:
309:                        for (int j = startingAt; j < endingAt; j++) {
310:                            styles[j] = modifier.modifyStyle(styles[j]);
311:                        }
312:                    }
313:
314:                    // check invariants
315:                    AttributeMap oldStyle = null;
316:                    int textLength = text.length();
317:                    for (int runStart = 0; runStart < textLength;) {
318:
319:                        AttributeMap currentStyle = text
320:                                .characterStyleAt(runStart);
321:                        int runLimit = text.characterStyleLimit(runStart);
322:                        if (runStart >= runLimit) {
323:                            errln("Run length is not positive");
324:                        }
325:                        if (currentStyle.equals(oldStyle)) {
326:                            errln("Styles didn't merge");
327:                        }
328:
329:                        for (int pos = runStart; pos < runLimit; pos++) {
330:                            AttributeMap charStyleAtPos = text
331:                                    .characterStyleAt(pos);
332:                            if (currentStyle != charStyleAtPos) {
333:                                errln("Iterator style is not equal to text style at "
334:                                        + pos + ".");
335:                            }
336:                            AttributeMap expected = styles[pos];
337:                            if (!currentStyle.equals(expected)) {
338:                                errln("Iterator style doesn't match expected style at "
339:                                        + pos + ".");
340:                            }
341:                            if (!(text.characterStyleStart(pos) == runStart)
342:                                    || !(text.characterStyleLimit(pos) == runLimit)) {
343:                                errln("style run start / limit is not consistent");
344:                            }
345:                        }
346:                        runStart = runLimit;
347:                    }
348:                    if (textLength > 0) {
349:                        if (text.characterStyleAt(textLength) != text
350:                                .characterStyleAt(textLength - 1)) {
351:                            errln("Character styles at end aren't the same");
352:                        }
353:                    }
354:
355:                    // check damaged range:
356:                    int damageStart = Integer.MAX_VALUE;
357:                    int damageLimit = Integer.MIN_VALUE;
358:                    for (int i = 0; i < textLength; i++) {
359:                        if (!styles[i].equals(oldStyles[i])) {
360:                            damageStart = Math.min(i, damageStart);
361:                            damageLimit = Math.max(i + 1, damageLimit);
362:                        }
363:                    }
364:                    if (damageStart != text.damagedRangeStart()
365:                            || damageLimit != text.damagedRangeLimit()) {
366:                        logln("Test iteration: " + testIteration);
367:                        logln("startingAt: " + startingAt + ";  endingAt: "
368:                                + endingAt);
369:                        logln("damageStart: " + damageStart
370:                                + ";  damageLimit: " + damageLimit);
371:                        logln("text.rangeStart: " + text.damagedRangeStart()
372:                                + "text.rangeLimit: "
373:                                + text.damagedRangeLimit());
374:                        errln("Damage range start or limit is not expected value");
375:                    }
376:
377:                    if ((damageLimit == Integer.MIN_VALUE) != (oldTs == text
378:                            .getTimeStamp())) {
379:
380:                        errln("timeStamp is incorrect");
381:                    }
382:                }
383:            }
384:
385:            public void msg(String message, int level, boolean incCount,
386:                    boolean newln) {
387:                if (level == ERR && testIteration != NOT_IN_MONKEY_TEST) {
388:                    message = "testIteration=" + testIteration + "; testCase="
389:                            + theCase + message;
390:                }
391:                super .msg(message, level, incCount, newln);
392:            }
393:
394:            /**
395:             * Perform a random series of operations on an MText and
396:             * check the result of each operation against a set of invariants.
397:             */
398:            public void monkeyTest(boolean streaming) {
399:
400:                /*
401:                    You can add any operation to the switch statement provided it
402:                    preserves the following invariants:
403:                    - The String plainText contains the same text as the StyledStringBuffer.
404:                      Obviously, for the test to be meaningful plainText must be computed
405:                      independently of the buffer (ie don't write:  plainText = buf.getStyledString().toString()).
406:                    - Every 'b' is bold, every 'i' is italic, every 'p' is plain, and
407:                      no other characters appear in the text.
408:                 */
409:
410:                AttributeMap boldAttrs = new AttributeMap(TextAttribute.WEIGHT,
411:                        TextAttribute.WEIGHT_BOLD);
412:                AttributeMap italicAttrs = new AttributeMap(
413:                        TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
414:                AttributeMap emptyAttrs = AttributeMap.EMPTY_ATTRIBUTE_MAP;
415:
416:                final String bold1Str_getString = "b";
417:                MConstText bold1Str = new StyledText(bold1Str_getString,
418:                        boldAttrs);
419:
420:                final String italic1Str_getString = "i";
421:                MConstText italic1Str = new StyledText(italic1Str_getString,
422:                        italicAttrs);
423:
424:                final String plain1Str_getString = "p";
425:                MConstText plain1Str = new StyledText(plain1Str_getString,
426:                        emptyAttrs);
427:
428:                StyledText temp = new StyledText();
429:                temp.append(bold1Str);
430:                temp.append(italic1Str);
431:                final String boldItalicStr_getString = bold1Str_getString
432:                        .concat(italic1Str_getString);
433:                MConstText boldItalicStr = temp;
434:
435:                temp = new StyledText();
436:                temp.append(bold1Str);
437:                temp.append(bold1Str);
438:                temp.append(bold1Str);
439:                final String bold3Str_getString = "bbb";
440:                MConstText bold3Str = temp;
441:
442:                MText buf = new StyledText();
443:                String plainText = new String();
444:                //int testIteration=0; - now instance variables so errln can report it
445:                //int theCase=0;
446:
447:                final int NUM_CASES = 14;
448:                boolean[] casesExecuted = new boolean[NUM_CASES];
449:                final int stopAt = -1;
450:                Random rand = new Random(RAND_SEED);
451:
452:                final String ALWAYS_DIFFERENT = "\uFEFF";
453:
454:                for (testIteration = 0; testIteration < TEST_ITERATIONS; testIteration++) {
455:
456:                    theCase = randInt(rand, NUM_CASES);
457:
458:                    casesExecuted[theCase] = true;
459:
460:                    if (testIteration == stopAt) {
461:                        testIteration = stopAt; // Convenient place to put breakpoint
462:                    }
463:
464:                    int timeStamp = buf.getTimeStamp();
465:                    String oldPlainText = plainText;
466:                    if (oldPlainText == null) {
467:                        errln("oldPlainText is null!");
468:                    }
469:
470:                    switch (theCase) {
471:
472:                    case 0:
473:                        // create new string; replace chars at start with different style
474:                        buf = new StyledText();
475:                        buf.append(bold3Str);
476:                        buf.replace(0, 1, italic1Str, 0, italic1Str.length());
477:                        buf.replace(0, 0, italic1Str, 0, italic1Str.length());
478:
479:                        plainText = bold3Str_getString.substring(1, bold3Str
480:                                .length());
481:                        plainText = italic1Str_getString.concat(plainText);
482:                        plainText = italic1Str_getString.concat(plainText);
483:                        oldPlainText = null;
484:                        break;
485:
486:                    case 1:
487:                        // delete the last character from the string
488:                        if (buf.length() == 0) {
489:                            buf.replace(0, 0, italic1Str, 0, italic1Str
490:                                    .length());
491:                            plainText = italic1Str_getString;
492:                            oldPlainText = ALWAYS_DIFFERENT;
493:                        }
494:                        buf.remove(buf.length() - 1, buf.length());
495:                        plainText = plainText.substring(0,
496:                                plainText.length() - 1);
497:                        break;
498:
499:                    case 2:
500:                        // replace some of the buffer with boldItalicStr
501:                        int rStart = randInt(rand, buf.length() + 1);
502:                        int rStop = randInt(rand, rStart, buf.length() + 1);
503:                        buf.replace(rStart, rStop, boldItalicStr);
504:                        {
505:                            String newString = (rStart > 0) ? plainText
506:                                    .substring(0, rStart) : new String();
507:                            newString = newString
508:                                    .concat(boldItalicStr_getString);
509:                            if (rStop < plainText.length())
510:                                newString = newString.concat(plainText
511:                                        .substring(rStop, plainText.length()));
512:                            oldPlainText = ALWAYS_DIFFERENT;
513:                            plainText = newString;
514:                        }
515:                        break;
516:
517:                    case 3:
518:                        // repeatedly insert strings into the center of the buffer
519:                    {
520:                        int insPos = buf.length() / 2;
521:                        String prefix = plainText.substring(0, insPos);
522:                        String suffix = plainText.substring(insPos, plainText
523:                                .length());
524:                        String middle = new String();
525:                        for (int ii = 0; ii < 4; ii++) {
526:                            MConstText which = (ii % 2 == 0) ? boldItalicStr
527:                                    : bold3Str;
528:                            String whichString = (ii % 2 == 0) ? boldItalicStr_getString
529:                                    : bold3Str_getString;
530:                            int tempPos = insPos + middle.length();
531:                            buf.insert(tempPos, which);
532:                            middle = middle.concat(whichString);
533:                        }
534:                        plainText = prefix.concat(middle).concat(suffix);
535:                        oldPlainText = ALWAYS_DIFFERENT;
536:                    }
537:                        break;
538:
539:                    case 4:
540:                        // insert bold1Str at end
541:                        buf.append(bold1Str);
542:                        plainText = plainText.concat(bold1Str_getString);
543:                        break;
544:
545:                    case 5:
546:                        // delete a character from the string
547:                        if (buf.length() > 0) {
548:                            int delPos = randInt(rand, buf.length() - 1);
549:                            buf.remove(delPos, delPos + 1);
550:                            plainText = plainText.substring(0, delPos).concat(
551:                                    plainText.substring(delPos + 1));
552:                        } else {
553:                            buf.replace(0, 0, plain1Str, 0, plain1Str.length());
554:                            plainText = plain1Str_getString;
555:                        }
556:                        break;
557:
558:                    case 6:
559:                        // replace the contents of the buffer (except the first character) with itself
560:                    {
561:                        int start = buf.length() > 1 ? 1 : 0;
562:                        buf.replace(start, buf.length(), buf);
563:                        plainText = plainText.substring(0, start).concat(
564:                                plainText);
565:                        if (buf.length() > 0) {
566:                            oldPlainText = ALWAYS_DIFFERENT;
567:                        }
568:                    }
569:                        break;
570:
571:                    case 7:
572:                        // append the contents of the buffer to itself
573:                    {
574:                        MConstText content = buf;
575:                        buf.insert(buf.length(), content);
576:                        plainText = plainText.concat(plainText);
577:                    }
578:                        break;
579:
580:                    case 8:
581:                        // replace the buffer with boldItalicStr+bold3Str
582:                    {
583:                        MText replacement = new StyledText();
584:                        replacement.append(boldItalicStr);
585:                        replacement.append(bold3Str);
586:                        buf.replace(0, buf.length(), replacement, 0,
587:                                replacement.length());
588:                        plainText = boldItalicStr_getString
589:                                .concat(bold3Str_getString);
590:                        oldPlainText = ALWAYS_DIFFERENT;
591:                    }
592:                        break;
593:
594:                    case 9:
595:                        // insert bold1Str at end - same as 4 but uses different API
596:                        buf.replace(buf.length(), buf.length(),
597:                                bold1Str_getString.toCharArray(), 0,
598:                                bold1Str_getString.length(), boldAttrs);
599:                        plainText = plainText.concat(bold1Str_getString);
600:                        break;
601:
602:                    case 10:
603:                        // remove all
604:                        buf.remove();
605:                        plainText = "";
606:                        oldPlainText = ALWAYS_DIFFERENT;
607:                        break;
608:
609:                    case 11:
610:                        // remove all - different way
611:                        buf.remove(0, buf.length());
612:                        plainText = "";
613:                        break;
614:
615:                    case 12:
616:                        // insert 'i' at 3rd character (or last, if fewer than 3 chars)
617:                    {
618:                        int insPos = Math.min(buf.length(), 3);
619:                        buf.replace(insPos, insPos, 'i', italicAttrs);
620:                        plainText = (plainText.substring(0, insPos)).concat(
621:                                italic1Str_getString).concat(
622:                                plainText.substring(insPos));
623:                    }
624:                        break;
625:
626:                    case 13:
627:                        if (streaming) {
628:                            Throwable error = null;
629:                            try {
630:                                ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
631:                                ObjectOutputStream objOut = new ObjectOutputStream(
632:                                        bytesOut);
633:                                objOut.writeObject(buf);
634:
635:                                ByteArrayInputStream bytesIn = new ByteArrayInputStream(
636:                                        bytesOut.toByteArray());
637:                                ObjectInputStream objIn = new ObjectInputStream(
638:                                        bytesIn);
639:                                buf = (MText) objIn.readObject();
640:                                oldPlainText = null;
641:                            } catch (IOException e) {
642:                                error = e;
643:                            } catch (ClassNotFoundException e) {
644:                                error = e;
645:                            }
646:                            if (error != null) {
647:                                error.printStackTrace();
648:                                errln("Streaming problem: " + error);
649:                            }
650:                        }
651:                        break;
652:
653:                    default:
654:                        errln("Invalid case.");
655:                    }
656:
657:                    // Check time stamp if oldPlainText != null.
658:                    // Time stamp should be different iff
659:                    // oldPlainText == plainText
660:                    if (oldPlainText != null) {
661:                        if ((timeStamp == buf.getTimeStamp()) != oldPlainText
662:                                .equals(plainText)) {
663:                            logln("plainText hashCode: " + plainText.hashCode());
664:                            logln("oldPlainText hashCode: "
665:                                    + oldPlainText.hashCode());
666:                            errln("Time stamp is incorrect");
667:                        }
668:                    }
669:
670:                    // now check invariants:
671:                    if (plainText.length() != buf.length()) {
672:                        errln("Lengths don't match");
673:                    }
674:
675:                    for (int j = 0; j < buf.length(); j++) {
676:                        if (buf.at(j) != plainText.charAt(j)) {
677:                            errln("Characters don't match.");
678:                        }
679:                    }
680:
681:                    int start;
682:                    for (start = 0; start < buf.length();) {
683:
684:                        if (start != buf.characterStyleStart(start)) {
685:                            errln("style start is wrong");
686:                        }
687:                        int limit = buf.characterStyleLimit(start);
688:                        if (start >= limit) {
689:                            errln("start >= limit");
690:                        }
691:                        char current = plainText.charAt(start);
692:
693:                        AttributeMap comp = null;
694:                        if (current == 'p') {
695:                            comp = emptyAttrs;
696:                        } else if (current == 'b') {
697:                            comp = boldAttrs;
698:                        } else if (current == 'i') {
699:                            comp = italicAttrs;
700:                        } else {
701:                            errln("An invalid character snuck in!");
702:                        }
703:
704:                        AttributeMap startStyle = buf.characterStyleAt(start);
705:                        if (!comp.equals(startStyle)) {
706:                            errln("Style is not expected style.");
707:                        }
708:
709:                        for (int j = start; j < limit; j++) {
710:                            if (plainText.charAt(j) != current) {
711:                                errln("Character doesn't match style.");
712:                            }
713:                            if (buf.characterStyleAt(j) != startStyle) {
714:                                errln("Incorrect style in run");
715:                            }
716:                        }
717:
718:                        if (limit < buf.length()) {
719:                            if (plainText.charAt(limit) == current) {
720:                                errln("Style run ends too soon.");
721:                            }
722:                        }
723:                        start = limit;
724:                    }
725:                    if (start != buf.length()) {
726:                        errln("Last limit is not buffer length.");
727:                    }
728:
729:                    // won't try to compute and check damaged range;  however,
730:                    // if nonempty it should always be within text
731:                    int damageStart = buf.damagedRangeStart();
732:                    int damageLimit = buf.damagedRangeLimit();
733:                    if (damageStart == Integer.MAX_VALUE) {
734:                        if (damageLimit != Integer.MIN_VALUE) {
735:                            errln("Invalid empty interval");
736:                        }
737:                    } else {
738:                        if (damageStart > damageLimit) {
739:                            errln("Damage range inverted");
740:                        }
741:                        if (damageStart < 0 || damageLimit > buf.length()) {
742:                            errln("Damage range endpoint out of bounds");
743:                        }
744:                    }
745:                }
746:
747:                testIteration = NOT_IN_MONKEY_TEST;
748:
749:                boolean allCasesExecuted = true;
750:                for (int index = 0; index < NUM_CASES; index++) {
751:                    allCasesExecuted &= casesExecuted[index];
752:                    if (casesExecuted[index] == false) {
753:                        logln("Case " + index + " not executed.");
754:                    }
755:                }
756:                //if (allCasesExecuted) {
757:                //    logln("All cases executed.");
758:                //}
759:            }
760:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.