]> source.dussan.org Git - poi.git/commitdiff
Fixed locale-sensitive formatters in PackagePropertiesPart, see Bugzilla 49138
authorYegor Kozlov <yegor@apache.org>
Tue, 20 Apr 2010 12:57:27 +0000 (12:57 +0000)
committerYegor Kozlov <yegor@apache.org>
Tue, 20 Apr 2010 12:57:27 +0000 (12:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@935896 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java
src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java

index 0a8cdaeadbd2dbecbc3f9b8fafad69295e44a007..23c88fd9a49c0e45e20993654c0afe6ad958846b 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-SNAPSHOT" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">49138 - Fixed locale-sensitive formatters in PackagePropertiesPart</action>
            <action dev="POI-DEVELOPERS" type="fix">49153 - Ensure that CTVectorVariant is included in poi-ooxml-schemas.jar</action>
            <action dev="POI-DEVELOPERS" type="add">49146 - Added accessors to CoreProperties.Keywords  </action>
            <action dev="POI-DEVELOPERS" type="fix">48916 - Propagate parent to parent-aware records decoded from Escher</action>
index f27d05cbac6c7d9669cc5e3853f89417ac3d93df..800895df6cf534dbc0523cf5594f54efc0a54130 100644 (file)
@@ -22,6 +22,8 @@ import java.io.OutputStream;
 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
 
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
@@ -561,6 +563,7 @@ public final class PackagePropertiesPart extends PackagePart implements
                }
                SimpleDateFormat df = new SimpleDateFormat(
                                "yyyy-MM-dd'T'HH:mm:ss'Z'");
+               df.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date d = df.parse(s, new ParsePosition(0));
                if (d == null) {
                        throw new InvalidFormatException("Date not well formated");
@@ -582,6 +585,7 @@ public final class PackagePropertiesPart extends PackagePart implements
                }
                SimpleDateFormat df = new SimpleDateFormat(
                                "yyyy-MM-dd'T'HH:mm:ss'Z'");
+        df.setTimeZone(TimeZone.getTimeZone("UTC"));
                return df.format(d.getValue());
        }
 
index 1aac08740bfaa7990eeac87f118eb4a6f383eec8..56f1db4613c01ad2a907b1e08e6bf0c3151461e2 100644 (file)
@@ -17,8 +17,7 @@
 
 package org.apache.poi;
 
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.util.*;
 
 import junit.framework.TestCase;
 
@@ -27,7 +26,7 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
-import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties;
+import org.apache.poi.openxml4j.util.Nullable;
 
 /**
  * Test setting extended and custom OOXML properties
@@ -143,8 +142,8 @@ public final class TestPOIXMLProperties extends TestCase {
                _coreProperties.setContentStatus(contentStatus);
                assertEquals("Draft", contentStatus);
                Date created = _coreProperties.getCreated();
-               SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM d, ''yy");
-               assertEquals("Mon, Jul 20, '09", formatter.format(created));
+               // the original file contains a following value: 2009-07-20T13:12:00Z
+               assertTrue(dateTimeEqualToUTCString(created, "2009-07-20T13:12:00Z"));
                String creator = _coreProperties.getCreator();
                assertEquals("Paolo Mottadelli", creator);
                String subject = _coreProperties.getSubject();
@@ -153,6 +152,21 @@ public final class TestPOIXMLProperties extends TestCase {
                assertEquals("Hello World", title);
        }
 
+    public void testTransitiveSetters() {
+               XWPFDocument doc = new XWPFDocument();
+        CoreProperties cp = doc.getProperties().getCoreProperties();
+
+        Date dateCreated = new GregorianCalendar(2010, 6, 15, 10, 0, 0).getTime();
+        cp.setCreated(new Nullable<Date>(dateCreated));
+        assertEquals(dateCreated.toString(), cp.getCreated().toString());
+
+        doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
+        cp = doc.getProperties().getCoreProperties();
+        Date dt3 = cp.getCreated();
+        assertEquals(dateCreated.toString(), dt3.toString());
+
+    }
+
        public void testGetSetRevision() {
                String revision = _coreProperties.getRevision();
                assertTrue("Revision number is 1", Integer.parseInt(revision) > 1);
@@ -161,4 +175,26 @@ public final class TestPOIXMLProperties extends TestCase {
                _coreProperties.setRevision("20xx");
                assertEquals("20", _coreProperties.getRevision());
        }
+       
+       public static boolean dateTimeEqualToUTCString(Date dateTime, String utcString) {
+               Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.UK);
+        utcCalendar.setTimeInMillis(dateTime.getTime());
+        String dateTimeUtcString = utcCalendar.get(Calendar.YEAR) + "-" + 
+               zeroPad((utcCalendar.get(Calendar.MONTH)+1)) + "-" + 
+               zeroPad(utcCalendar.get(Calendar.DAY_OF_MONTH)) + "T" + 
+               zeroPad(utcCalendar.get(Calendar.HOUR_OF_DAY)) + ":" +
+               zeroPad(utcCalendar.get(Calendar.MINUTE)) + ":" + 
+               zeroPad(utcCalendar.get(Calendar.SECOND)) + "Z";
+               
+               
+        return utcString.equals(dateTimeUtcString);
+    }
+
+       private static String zeroPad(long i) {
+        if (i >= 0 && i <=9) {
+            return "0" + i;
+        } else {
+            return String.valueOf(i);
+        }
+    }
 }
index d76c24b0c2bc34b627ea5b8e903794bf171ddf46..b934aa308ef1c500778e8775cf2f88c7594f513c 100644 (file)
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.TimeZone;
 
 import junit.framework.TestCase;
 
@@ -64,6 +65,7 @@ public final class TestPackageCoreProperties extends TestCase {
                OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE);
 
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+        df.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
                                0));
 
@@ -96,6 +98,7 @@ public final class TestPackageCoreProperties extends TestCase {
 
        private void compareProperties(OPCPackage p) throws InvalidFormatException {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+               df.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date expectedDate = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
                                0));
 
index e8c9dfdc6323a455ee0e9095b7ddfc6055367307..2a00b37fe4e9b1015a8cc28050e910cac5b4b52b 100644 (file)
@@ -19,6 +19,9 @@ package org.apache.poi.hsmf.extractor;
 
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 
 import junit.framework.TestCase;
 
@@ -57,7 +60,10 @@ public final class TestOutlookTextExtractor extends TestCase {
       assertEquals(-1, text.indexOf("CC:"));
       assertEquals(-1, text.indexOf("BCC:"));
       assertContains(text, "Subject: Test the content transformer\n");
-      assertContains(text, "Date: Thu, 14 Jun 2007 09:42:55\n");
+      Calendar cal = new GregorianCalendar(2007, 5, 14, 9, 42, 55);
+      SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss");
+      String dateText = f.format(cal.getTime());
+      assertContains(text, "Date: " + dateText + "\n");
       assertContains(text, "The quick brown fox jumps over the lazy dog");
    }
    
index 48583e894688124a18cfe747036e54eb02de6fed..5006440b0a07d0d4bafa3bf2b7f85309df6354d9 100644 (file)
 
 package org.apache.poi.hssf.usermodel;
 
+import java.math.BigDecimal;
 import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
 import java.text.Format;
+import java.text.SimpleDateFormat;
+import java.util.GregorianCalendar;
 import java.util.Iterator;
+import java.util.Locale;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.ss.usermodel.Cell;
@@ -175,13 +180,19 @@ public final class TestHSSFDataFormatter extends TestCase {
                log("==== VALID DATE FORMATS ====");
                while (it.hasNext()) {
                        Cell cell = it.next();
-                       log(formatter.formatCellValue(cell));
+            String fmtval = formatter.formatCellValue(cell);
+            log(fmtval);
 
                        // should not be equal to "555.555"
-                       assertTrue( ! "555.555".equals(formatter.formatCellValue(cell)));
+                       assertTrue( ! "555.555".equals(fmtval));
+
+            String fmt = cell.getCellStyle().getDataFormatString();
+            //assert the correct month form, as in the original Excel format
+            String monthPtrn = fmt.indexOf("mmmm") != -1 ? "MMMM" : "MMM";
 
-                       // should contain "Jul" in the String
-                       assertTrue( formatter.formatCellValue(cell).indexOf("Jul") > -1);
+            // this line is intended to compute how "July" would look like in the current locale
+            String jul = new SimpleDateFormat(monthPtrn).format(new GregorianCalendar(2010,6,15).getTime());
+                       assertTrue( fmtval.indexOf(jul) > -1);
                }
 
                // test number formats
@@ -203,8 +214,10 @@ public final class TestHSSFDataFormatter extends TestCase {
                while (it.hasNext()) {
                        HSSFCell cell = (HSSFCell) it.next();
                        log(formatter.formatCellValue(cell));
-                       // should be equal to "1234567890.12345"
-                       assertEquals("1234567890.12345", formatter.formatCellValue(cell));
+                       // should be equal to "1234567890.12345" 
+                       // in some locales the the decimal delimiter is a comma, not a dot
+                       char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
+                       assertEquals("1234567890" + decimalSeparator + "12345", formatter.formatCellValue(cell));
                }
 
                // test Zip+4 format
@@ -248,7 +261,9 @@ public final class TestHSSFDataFormatter extends TestCase {
                // now with a formula evaluator
                HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
                log(formatter.formatCellValue(cell, evaluator) + "\t\t\t (with evaluator)");
-               assertEquals("24.50%", formatter.formatCellValue(cell,evaluator));
+               char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
+               assertEquals("24" + decimalSeparator + "50%", formatter.formatCellValue(cell,evaluator));
+               
        }
 
        /**