ASF Bugzilla – Attachment 34000 Details for
Bug 59780
SimpleDateFormat usage incorrect in PackagePropertiesPart class
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
J-Unit Test for PackagePropertiesPart.setDate Method
PackagePropertiesPartTest.java (text/x-java-source), 5.16 KB, created by
Eliut Hernandez
on 2016-07-01 16:56:33 UTC
(
hide
)
Description:
J-Unit Test for PackagePropertiesPart.setDate Method
Filename:
MIME Type:
Creator:
Eliut Hernandez
Created:
2016-07-01 16:56:33 UTC
Size:
5.16 KB
patch
obsolete
> > >import java.text.ParsePosition; >import java.text.SimpleDateFormat; >import java.util.Date; >import java.util.Locale; >import java.util.regex.Matcher; >import java.util.regex.Pattern; > >import org.apache.poi.openxml4j.exceptions.InvalidFormatException; >import org.apache.poi.util.LocaleUtil; >import org.junit.Assert; > >public class PackagePropertiesPartTest { > > @org.junit.Test > public void shouldFailCurrentImplementationWithColonInTimeZone() throws InvalidFormatException { > > String dateStr = "2016-06-28T16:26:46+03:00"; > > InvalidFormatException ex = null; > try { > currentPackaPropertiesPart(dateStr); > } catch (InvalidFormatException e) { > ex = e; > } > > Assert.assertNotNull(ex); > } > > @org.junit.Test > public void shouldFailCurrentImplementationWithNoColonInTimeZone() throws InvalidFormatException { > String dateStr = "2016-06-28T16:26:46+0300"; > > InvalidFormatException ex = null; > try { > currentPackaPropertiesPart(dateStr); > } catch (InvalidFormatException e) { > ex = e; > } > > Assert.assertNotNull(ex); > > } > > private static void currentPackaPropertiesPart (String dateStr) throws InvalidFormatException { > String DEFAULT_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; > String ALTERNATIVE_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SS'Z'"; > > String dateTzStr = dateStr.endsWith("Z") ? dateStr : (dateStr + "Z"); > SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATEFORMAT, Locale.ROOT); > df.setTimeZone(LocaleUtil.TIMEZONE_UTC); > Date d = df.parse(dateTzStr, new ParsePosition(0)); > if (d == null) { > df = new SimpleDateFormat(ALTERNATIVE_DATEFORMAT, Locale.ROOT); > df.setTimeZone(LocaleUtil.TIMEZONE_UTC); > d = df.parse(dateTzStr, new ParsePosition(0)); > } > > if (d == null) { > throw new InvalidFormatException("Date " + dateTzStr + " not well formated, " > + "expected format " + DEFAULT_DATEFORMAT + " or " + ALTERNATIVE_DATEFORMAT); > } > } > > @org.junit.Test > public void testFixWithColonInTimeZone() { > String dateStr = "2016-06-28T16:26:46+03:00"; > long dateInMillis = 1467120406000L; > > InvalidFormatException ex = null; > Date date = null; > try { > date = patchedPackagePropertiesPart(dateStr); > } catch (InvalidFormatException e) { > ex = e; > } > > Assert.assertNull(ex); > Assert.assertNotNull(date); > Assert.assertEquals(dateInMillis, date.getTime()); > } > > @org.junit.Test > public void testFixWithNoColonInTimeZone() { > String dateStr = "2016-06-28T16:26:46+03:00"; > long dateInMillis = 1467120406000L; > > InvalidFormatException ex = null; > Date date = null; > try { > date = patchedPackagePropertiesPart(dateStr); > } catch (InvalidFormatException e) { > ex = e; > } > > Assert.assertNull(ex); > Assert.assertNotNull(date); > Assert.assertEquals(dateInMillis, date.getTime()); > } > > private static Date patchedPackagePropertiesPart (String dateStr) throws InvalidFormatException { > String DEFAULT_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ssX"; > String ALTERNATIVE_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSX"; > > // Returns 1.6 from 1.6.0_37 > String jdkVersion = System.getProperty("java.version").substring(0,3); > float jdkNum = Float.valueOf(jdkVersion); > if (jdkNum < 1.7) { > DEFAULT_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; > ALTERNATIVE_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; > > Matcher zoneColon = Pattern.compile("([-+]\\d{1,2}:\\d{2})").matcher(""); > if (zoneColon.reset(dateStr).find()) { > String offset = zoneColon.group(1).replace(":", ""); > dateStr = dateStr.replace(zoneColon.group(1), offset); > } > } > > //String dateTzStr = dateStr.endsWith("Z") ? dateStr : (dateStr + "Z"); > SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATEFORMAT, Locale.ROOT); > df.setTimeZone(LocaleUtil.TIMEZONE_UTC); > //Date d = df.parse(dateTzStr, new ParsePosition(0)); > Date d = df.parse(dateStr, new ParsePosition(0)); > if (d == null) { > df = new SimpleDateFormat(ALTERNATIVE_DATEFORMAT, Locale.ROOT); > df.setTimeZone(LocaleUtil.TIMEZONE_UTC); > //d = df.parse(dateTzStr, new ParsePosition(0)); > d = df.parse(dateStr, new ParsePosition(0)); > } > > if (d == null) { > //throw new InvalidFormatException("Date " + dateTzStr + " not well formated, " > throw new InvalidFormatException("Date " + dateStr + " not well formated, " > + "expected format " + DEFAULT_DATEFORMAT + " or " + ALTERNATIVE_DATEFORMAT); > } > > return d; > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 59780
: 34000