You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2010/04/20 14:57:27 UTC

svn commit: r935896 - in /poi/trunk/src: documentation/content/xdocs/ ooxml/java/org/apache/poi/openxml4j/opc/internal/ ooxml/testcases/org/apache/poi/ ooxml/testcases/org/apache/poi/openxml4j/opc/ scratchpad/testcases/org/apache/poi/hsmf/extractor/ te...

Author: yegor
Date: Tue Apr 20 12:57:27 2010
New Revision: 935896

URL: http://svn.apache.org/viewvc?rev=935896&view=rev
Log:
 Fixed locale-sensitive formatters in PackagePropertiesPart, see Bugzilla 49138

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

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=935896&r1=935895&r2=935896&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Apr 20 12:57:27 2010
@@ -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>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java?rev=935896&r1=935895&r2=935896&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java Tue Apr 20 12:57:27 2010
@@ -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
 		}
 		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
 		}
 		SimpleDateFormat df = new SimpleDateFormat(
 				"yyyy-MM-dd'T'HH:mm:ss'Z'");
+        df.setTimeZone(TimeZone.getTimeZone("UTC"));
 		return df.format(d.getValue());
 	}
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java?rev=935896&r1=935895&r2=935896&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java Tue Apr 20 12:57:27 2010
@@ -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.XSSFTestDataS
 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 
 		_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 
 		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 
 		_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);
+        }
+    }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java?rev=935896&r1=935895&r2=935896&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java Tue Apr 20 12:57:27 2010
@@ -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 TestPackageCoreProper
 		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 TestPackageCoreProper
 
 	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));
 

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java?rev=935896&r1=935895&r2=935896&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java Tue Apr 20 12:57:27 2010
@@ -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 TestOutlookTextExtrac
       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");
    }
    

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java?rev=935896&r1=935895&r2=935896&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java Tue Apr 20 12:57:27 2010
@@ -17,9 +17,14 @@
 
 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
 		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));
 
-			// should contain "Jul" in the String
-			assertTrue( formatter.formatCellValue(cell).indexOf("Jul") > -1);
+            String fmt = cell.getCellStyle().getDataFormatString();
+            //assert the correct month form, as in the original Excel format
+            String monthPtrn = fmt.indexOf("mmmm") != -1 ? "MMMM" : "MMM";
+
+            // 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
 		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
 		// 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));
+		
 	}
 
 	/**



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org