independently of the buffer (ie don't write: plainText = buf.getStyledString().toString()).
- Every 'b' is bold, every 'i' is italic, every 'p' is plain, and
no other characters appear in the text.
*/
AttributeMap boldAttrs = new AttributeMap(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
AttributeMap italicAttrs = new AttributeMap(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
AttributeMap emptyAttrs = AttributeMap.EMPTY_ATTRIBUTE_MAP;
final String bold1Str_getString = "b";
MConstText bold1Str = new StyledText(bold1Str_getString, boldAttrs);
final String italic1Str_getString = "i";
MConstText italic1Str = new StyledText(italic1Str_getString, italicAttrs);
final String plain1Str_getString = "p";
MConstText plain1Str = new StyledText(plain1Str_getString, emptyAttrs);
StyledText temp = new StyledText();
temp.append(bold1Str);
temp.append(italic1Str);
final String boldItalicStr_getString = bold1Str_getString.concat(italic1Str_getString);
MConstText boldItalicStr = temp;
temp = new StyledText();
temp.append(bold1Str);
temp.append(bold1Str);
temp.append(bold1Str);
final String bold3Str_getString = "bbb";
MConstText bold3Str = temp;
MText buf = new StyledText();
String plainText = new String();
//int testIteration=0; - now instance variables so errln can report it
//int theCase=0;
final int NUM_CASES = 14;
boolean[] casesExecuted = new boolean[NUM_CASES];
final int stopAt = -1;
Random rand = new Random(RAND_SEED);
final String ALWAYS_DIFFERENT = "\uFEFF";
for (testIteration=0; testIteration < TEST_ITERATIONS; testIteration++) {
theCase = randInt(rand, NUM_CASES);
casesExecuted[theCase] = true;
if (testIteration == stopAt) {
testIteration = stopAt; // Convenient place to put breakpoint
}
int timeStamp = buf.getTimeStamp();
String oldPlainText = plainText;
if (oldPlainText == null) {
errln("oldPlainText is null!");
}
switch (theCase) {
case 0:
// create new string; replace chars at start with different style
buf = new StyledText();
buf.append(bold3Str);
buf.replace(0, 1, italic1Str, 0, italic1Str.length());
buf.replace(0, 0, italic1Str, 0, italic1Str.length());
plainText = bold3Str_getString.substring(1, bold3Str.length());
plainText = italic1Str_getString.concat(plainText);
plainText = italic1Str_getString.concat(plainText);
oldPlainText = null;
break;
case 1:
// delete the last character from the string
if (buf.length() == 0) {
buf.replace(0, 0, italic1Str, 0, italic1Str.length());
plainText = italic1Str_getString;
oldPlainText = ALWAYS_DIFFERENT;
}
buf.remove(buf.length()-1, buf.length());
plainText = plainText.substring(0, plainText.length()-1);
break;
case 2:
// replace some of the buffer with boldItalicStr
int rStart = randInt(rand, buf.length()+1);
int rStop = randInt(rand, rStart, buf.length()+1);
buf.replace(rStart, rStop, boldItalicStr);
{
String newString = (rStart>0)? plainText.substring(0, rStart) : new String();
newString = newString.concat(boldItalicStr_getString);
if (rStop < plainText.length())
newString = newString.concat(plainText.substring(rStop, plainText.length()));
oldPlainText = ALWAYS_DIFFERENT;
plainText = newString;
}
break;
case 3:
// repeatedly insert strings into the center of the buffer
{
int insPos = buf.length() / 2;
String prefix = plainText.substring(0, insPos);
String suffix = plainText.substring(insPos, plainText.length());
String middle = new String();
for (int ii=0; ii<4; ii++) {
MConstText which = (ii%2==0)? boldItalicStr : bold3Str;
String whichString = (ii%2==0)? boldItalicStr_getString : bold3Str_getString;
int tempPos = insPos+middle.length();
buf.insert(tempPos, which);
middle = middle.concat(whichString);
}
plainText = prefix.concat(middle).concat(suffix);
oldPlainText = ALWAYS_DIFFERENT;
}
break;
case 4:
// insert bold1Str at end
buf.append(bold1Str);
plainText = plainText.concat(bold1Str_getString);
break;
case 5:
// delete a character from the string
if (buf.length() > 0) {
int delPos = randInt(rand, buf.length()-1);
buf.remove(delPos, delPos+1);
plainText = plainText.substring(0, delPos).concat(plainText.substring(delPos+1));
}
else {
buf.replace(0, 0, plain1Str, 0, plain1Str.length());
plainText = plain1Str_getString;
}
break;
case 6:
// replace the contents of the buffer (except the first character) with itself
{
int start = buf.length() > 1? 1 : 0;
buf.replace(start, buf.length(), buf);
plainText = plainText.substring(0, start).concat(plainText);
if (buf.length() > 0) {
oldPlainText = ALWAYS_DIFFERENT;
}
}
break;
case 7:
// append the contents of the buffer to itself
{
MConstText content = buf;
buf.insert(buf.length(), content);
plainText = plainText.concat(plainText);
}
break;
case 8:
// replace the buffer with boldItalicStr+bold3Str
{
MText replacement = new StyledText();
replacement.append(boldItalicStr);
replacement.append(bold3Str);
buf.replace(0, buf.length(), replacement, 0, replacement.length());
plainText = boldItalicStr_getString.concat(bold3Str_getString);
oldPlainText = ALWAYS_DIFFERENT;
}
break;
case 9:
// insert bold1Str at end - same as 4 but uses different API
buf.replace(buf.length(),
buf.length(),
bold1Str_getString.toCharArray(),
0,
bold1Str_getString.length(),
boldAttrs);
plainText = plainText.concat(bold1Str_getString);
break;
case 10:
// remove all
buf.remove();
plainText = "";
oldPlainText = ALWAYS_DIFFERENT;
break;
case 11:
// remove all - different way
buf.remove(0, buf.length());
plainText = "";
break;
case 12:
// insert 'i' at 3rd character (or last, if fewer than 3 chars)
{
int insPos = Math.min(buf.length(), 3);
buf.replace(insPos, insPos, 'i', italicAttrs);
plainText = (plainText.substring(0, insPos)).
concat(italic1Str_getString).
concat(plainText.substring(insPos));
}
break;
case 13:
if (streaming) {
Throwable error = null;
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
objOut.writeObject(buf);
ByteArrayInputStream bytesIn =
new ByteArrayInputStream(bytesOut.toByteArray());
ObjectInputStream objIn = new ObjectInputStream(bytesIn);
buf = (MText) objIn.readObject();
oldPlainText = null;
}
catch(IOException e) {
error = e;
}
catch(ClassNotFoundException e) {
error = e;
}
if (error != null) {
error.printStackTrace();
errln("Streaming problem: " + error);
}
}
break;
default:
errln("Invalid case.");
}
// Check time stamp if oldPlainText != null.
// Time stamp should be different iff
// oldPlainText == plainText
if (oldPlainText != null) {
if ((timeStamp==buf.getTimeStamp()) !=
oldPlainText.equals(plainText)) {
logln("plainText hashCode: " + plainText.hashCode());
logln("oldPlainText hashCode: " + oldPlainText.hashCode());
errln("Time stamp is incorrect");
}
}
// now check invariants:
if (plainText.length() != buf.length()) {
errln("Lengths don't match");
}
for (int j=0; j < buf.length(); j++) {
if (buf.at(j) != plainText.charAt(j)) {
errln("Characters don't match.");
}
}
int start;
for (start = 0; start < buf.length();) {
if (start != buf.characterStyleStart(start)) {
errln("style start is wrong");
}
int limit = buf.characterStyleLimit(start);
if (start >= limit) {
errln("start >= limit");
}
char current = plainText.charAt(start);
AttributeMap comp = null;
if (current == 'p') {
comp = emptyAttrs;
}
else if (current == 'b') {
comp = boldAttrs;
}
else if (current == 'i') {
comp = italicAttrs;
}
else {
errln("An invalid character snuck in!");
}
AttributeMap startStyle = buf.characterStyleAt(start);
if (!comp.equals(startStyle)) {
errln("Style is not expected style.");
}
for (int j = start; j < limit; j++) {