* array of patterns, with known results. The results are encoded after
* the input strings in each row.
*/
public void TestBadInput135a() {
SimpleDateFormat dateParse = new SimpleDateFormat("", Locale.US);
final String ss;
Date date;
String[] parseFormats ={"MMMM d, yyyy", "MMMM d yyyy", "M/d/yy",
"d MMMM, yyyy", "d MMMM yyyy", "d MMMM",
"MMMM d", "yyyy", "h:mm a MMMM d, yyyy" };
String[] inputStrings = {
"bogus string", null, null, null, null, null, null, null, null, null,
"April 1, 1997", "April 1, 1997", null, null, null, null, null, "April 1", null, null,
"Jan 1, 1970", "January 1, 1970", null, null, null, null, null, "January 1", null, null,
"Jan 1 2037", null, "January 1 2037", null, null, null, null, "January 1", null, null,
"1/1/70", null, null, "1/1/70", null, null, null, null, "0001", null,
"5 May 1997", null, null, null, null, "5 May 1997", "5 May", null, "0005", null,
"16 May", null, null, null, null, null, "16 May", null, "0016", null,
"April 30", null, null, null, null, null, null, "April 30", null, null,
"1998", null, null, null, null, null, null, null, "1998", null,
"1", null, null, null, null, null, null, null, "0001", null,
"3:00 pm Jan 1, 1997", null, null, null, null, null, null, null, "0003", "3:00 PM January 1, 1997",
};
final int PF_LENGTH = parseFormats.length;
final int INPUT_LENGTH = inputStrings.length;
dateParse.applyPattern("d MMMM, yyyy");
dateParse.setTimeZone(TimeZone.getDefault());
ss = "not parseable";
// String thePat;
logln("Trying to parse \"" + ss + "\" with " + dateParse.toPattern());
try {
date = dateParse.parse(ss);
} catch (Exception ex) {
logln("FAIL:" + ex);
}
for (int i = 0; i < INPUT_LENGTH; i += (PF_LENGTH + 1)) {
ParsePosition parsePosition = new ParsePosition(0);
String s = inputStrings[i];
for (int index = 0; index < PF_LENGTH; ++index) {
final String expected = inputStrings[i + 1 + index];
dateParse.applyPattern(parseFormats[index]);
dateParse.setTimeZone(TimeZone.getDefault());
try {
parsePosition.setIndex(0);
date = dateParse.parse(s, parsePosition);
if (parsePosition.getIndex() != 0) {
String s1, s2;
s1 = s.substring(0, parsePosition.getIndex());
s2 = s.substring(parsePosition.getIndex(), s.length());
if (date == null) {
errln("ERROR: null result fmt=\"" + parseFormats[index]
+ "\" pos=" + parsePosition.getIndex()
+ " " + s1 + "|" + s2);
} else {
String result = ((DateFormat) dateParse).format(date);
logln("Parsed \"" + s + "\" using \"" + dateParse.toPattern() + "\" to: " + result);
if (expected == null)
errln("FAIL: Expected parse failure");
else
if (!result.equals(expected))
errln("FAIL: Expected " + expected);
}
} else
if (expected != null) {
errln("FAIL: Expected " + expected + " from \"" + s
+ "\" with \"" + dateParse.toPattern()+ "\"");
}
} catch (Exception ex) {
logln("FAIL:" + ex);
}
}