*/
public void TestAdopt()
{
String formatStr = "{0,date},{1},{2,number}";
String formatStrChange = "{0,number},{1,number},{2,date}";
MessageFormat msg = new MessageFormat(formatStr);
MessageFormat msgCmp = new MessageFormat(formatStr);
Format[] formats = msg.getFormats();
Format[] formatsCmp = msgCmp.getFormats();
Format[] formatsChg = null;
Format[] formatsAct = null;
Format a = null;
Format b = null;
Format[] formatsToAdopt = null;
if (formats==null || formatsCmp==null || (formats.length <= 0) || (formats.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
int i;
for (i = 0; i < formats.length; i++) {
a = formats[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("a != b");
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
msg.applyPattern( formatStrChange ); //set msg formats to something different
formatsChg = msg.getFormats(); // tested function
if (formatsChg==null || (formatsChg.length != formats.length)) {
errln("Error getting Formats");
return;
}
boolean diff;
diff = true;
for (i = 0; i < formats.length; i++) {
a = formatsChg[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (a.equals(b)) {
logln("formatsChg == formatsCmp at index " + i);
diff = false;
}
}
}
if (!diff) {
errln("*** MSG getFormats diff err.");
return;
}
logln("MSG getFormats tested.");
msg.setFormats( formatsCmp ); //tested function
formatsAct = msg.getFormats();
if (formatsAct==null || (formatsAct.length <=0) || (formatsAct.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern());
assertEquals("msg.toPattern()", formatStr, msg.toPattern());
for (i = 0; i < formatsAct.length; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("formatsAct != formatsCmp at index " + i);
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
logln("MSG setFormats tested.");
//----
msg.applyPattern( formatStrChange ); //set msg formats to something different
formatsToAdopt = new Format[formatsCmp.length];
if (formatsToAdopt==null) {
errln("memory allocation error");
return;
}
for (i = 0; i < formatsCmp.length; i++) {
if (formatsCmp[i] == null) {
formatsToAdopt[i] = null;
} else {
formatsToAdopt[i] = (Format) formatsCmp[i].clone();
if (formatsToAdopt[i]==null) {
errln("Can't clone format at index " + i);
return;
}
}
}
msg.setFormats( formatsToAdopt ); // function to test
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern());
assertEquals("msg.toPattern()", formatStr, msg.toPattern());
formatsAct = msg.getFormats();
if (formatsAct==null || (formatsAct.length <=0) || (formatsAct.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
for (i = 0; i < formatsAct.length; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("a != b");
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
logln("MSG adoptFormats tested.");
//---- adoptFormat
msg.applyPattern( formatStrChange ); //set msg formats to something different
formatsToAdopt = new Format[formatsCmp.length];
if (formatsToAdopt==null) {
errln("memory allocation error");
return;
}
for (i = 0; i < formatsCmp.length; i++) {
if (formatsCmp[i] == null) {
formatsToAdopt[i] = null;
} else {
formatsToAdopt[i] = (Format) formatsCmp[i].clone();
if (formatsToAdopt[i]==null) {
errln("Can't clone format at index " + i);
return;
}
}
}
for ( i = 0; i < formatsCmp.length; i++ ) {
msg.setFormat( i, formatsToAdopt[i] ); // function to test
}
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern());
assertEquals("msg.toPattern()", formatStr, msg.toPattern());
formatsAct = msg.getFormats();
if (formatsAct==null || (formatsAct.length <=0) || (formatsAct.length != formatsCmp.length)) {
errln("Error getting Formats");