Author: liuzhe
Date: Fri Aug 17 06:33:45 2012
New Revision: 1374159

URL: http://svn.apache.org/viewvc?rev=1374159&view=rev
Log:
#120603# - [testuno]Test Create and Load Date and Time Field in Writer by UNO
Patch by: Zong Dong Jun <[email protected]>
Review by: Liu Zhe <[email protected]>

Added:
    incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/
    
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/CheckDateTimeField.java
    incubator/ooo/trunk/main/test/testuno/source/testlib/uno/sw/
    incubator/ooo/trunk/main/test/testuno/source/testlib/uno/sw/SWUtil.java
Modified:
    
incubator/ooo/trunk/main/test/testuno/source/org/openoffice/test/uno/UnoApp.java

Modified: 
incubator/ooo/trunk/main/test/testuno/source/org/openoffice/test/uno/UnoApp.java
URL: 
http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/org/openoffice/test/uno/UnoApp.java?rev=1374159&r1=1374158&r2=1374159&view=diff
==============================================================================
--- 
incubator/ooo/trunk/main/test/testuno/source/org/openoffice/test/uno/UnoApp.java
 (original)
+++ 
incubator/ooo/trunk/main/test/testuno/source/org/openoffice/test/uno/UnoApp.java
 Fri Aug 17 06:33:45 2012
@@ -84,7 +84,7 @@ public class UnoApp {
                        SystemUtil.sleep(2);
                }
 
-               throw new RuntimeException("OpenOffice can be connected!");
+               throw new RuntimeException("OpenOffice can't be connected!");
        }
 
        private Timer timer = new Timer(true);
@@ -145,6 +145,12 @@ public class UnoApp {
                return 
componentLoader.loadComponentFromURL(FileUtil.getUrl(file), "_blank", 0, new 
PropertyValue[0]);
        }
        
+       
+       public XComponent loadDocumentFromURL(String url) throws Exception {
+               XComponentLoader componentLoader = (XComponentLoader) 
UnoRuntime.queryInterface(XComponentLoader.class, desktop);
+               return componentLoader.loadComponentFromURL(url, "_blank", 0, 
new PropertyValue[0]);
+       }
+       
        public XComponent newDocument(String type) throws Exception {
                XComponentLoader componentLoader = (XComponentLoader) 
UnoRuntime.queryInterface(XComponentLoader.class, desktop);
                return componentLoader.loadComponentFromURL("private:factory/" 
+ type, "_blank", 0, new PropertyValue[0]);

Added: 
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/CheckDateTimeField.java
URL: 
http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/CheckDateTimeField.java?rev=1374159&view=auto
==============================================================================
--- 
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/CheckDateTimeField.java
 (added)
+++ 
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/CheckDateTimeField.java
 Fri Aug 17 06:33:45 2012
@@ -0,0 +1,249 @@
+/**************************************************************
+ * 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * 
+ *************************************************************/
+package testcase.uno.sw.field;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Calendar;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openoffice.test.common.Testspace;
+import org.openoffice.test.uno.UnoApp;
+
+import com.sun.star.beans.PropertyValue;
+import com.sun.star.beans.XPropertySet;
+import com.sun.star.container.XEnumeration;
+import com.sun.star.container.XEnumerationAccess;
+import com.sun.star.frame.XStorable;
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.text.XTextDocument;
+import com.sun.star.text.XTextField;
+import com.sun.star.text.XTextFieldsSupplier;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.util.DateTime;
+/**
+ * Test Date and Time Field can be created and Load
+ * @author test
+ *
+ */
+public class CheckDateTimeField {
+       
+
+       private static final UnoApp app = new UnoApp();
+       
+       private XTextDocument document = null;
+       private String tempPath = "testcase/uno/sw/temp/" ;
+       private String tempFileName =  
String.valueOf(System.currentTimeMillis());
+       
+       @Before
+       public void setUpDocument() throws Exception {
+               document = (XTextDocument) 
UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
+       }
+
+       @After
+       public void tearDownDocument() {
+            app.closeDocument(document);
+               // FileUtil.deleteFile(Testspace.getFile(tempPath));
+       }
+
+
+
+       @BeforeClass
+       public static void setUpConnection() throws Exception {
+               app.start();
+       }
+
+       @AfterClass
+       public static void tearDownConnection() throws InterruptedException, 
Exception {
+               app.close();
+       }
+       
+       
+
+       /**
+        * Test Time field can be created correctly.
+        * 1.Create a Time in new document
+        * 2.Verify the Time is created by check the date hour in the new 
document
+        * 3.Save and close the new document to doc format
+        * 4.Reload the new save doc file, check the  time field
+        */
+
+       @Test
+       public void testCreateTimeFieldSaveDoc() {
+               
+               String url = Testspace.getUrl(tempPath + tempFileName + ".doc");
+               PropertyValue[] propsValue = new PropertyValue[1];
+               propsValue[0] = new PropertyValue();
+               propsValue[0].Name = "FilterName";
+               propsValue[0].Value = "MS Word 97";
+               
+               createTimeFiled(document, url, propsValue);
+       }
+       /**
+        * Test Time Field can be created correctly.
+        * 1.Create a Time Field in new document
+        * 2.Verify the Time Field is created by check the date hour in the new 
document
+        * 3.Save and close the new document to doc format
+        * 4.Reload the new save odt file, check the Time Field
+        */
+       @Test
+       public void testCreateTimeFieldSaveODT() {
+               
+               String url = Testspace.getUrl(tempPath + tempFileName + ".odt");
+               PropertyValue[] propsValue = new PropertyValue[0];
+               createTimeFiled(document, url, propsValue);
+               
+       }
+       
+       private void createTimeFiled(XTextDocument document, String url, 
PropertyValue[] propsValue) {
+               XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) 
UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
+               try {
+                       XTextField  dateFiled = 
(XTextField)UnoRuntime.queryInterface(XTextField.class, 
sevriceFactory.createInstance("com.sun.star.text.textfield.DateTime"));
+                       
+               
+                       XPropertySet props = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, dateFiled);
+                       props.setPropertyValue("IsDate", false);
+                       
+                       
document.getText().insertTextContent(document.getText().getEnd(), dateFiled, 
false);
+                       DateTime dateField = (DateTime) 
props.getPropertyValue("DateTimeValue");
+               
+                       String dateString = document.getText().getString();
+                       assertTrue("Verify time field is creatd, by verify it's 
hour",  dateString.indexOf(String.valueOf(dateField.Hours).trim()) != -1);
+                       assertTrue("Verify time field is creatd, by verify it's 
minutes",  dateString.indexOf(String.valueOf(dateField.Minutes).trim()) != -1);
+                       int expectHour = 
Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
+                       assertEquals("Verify time field is creatd, value is 
right, by compare Hour", expectHour, dateField.Hours);
+                       XStorable store = 
UnoRuntime.queryInterface(XStorable.class, document);
+                       store.storeAsURL(url, propsValue);
+                       app.closeDocument(document);
+                       
+                       try {
+                               document = (XTextDocument) 
UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
+                               XTextFieldsSupplier fieldsSupplier = 
UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
+                               XEnumerationAccess xEnumeratedFields = 
fieldsSupplier.getTextFields();
+                               XEnumeration enumeration = 
xEnumeratedFields.createEnumeration();
+                               while (enumeration.hasMoreElements()) {
+                                         Object field =  
enumeration.nextElement();
+                                               XPropertySet props2 = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
+                                               DateTime dateField2 = 
(DateTime) props2.getPropertyValue("DateTimeValue");
+                                               assertEquals("Verify time field 
is creatd correct by save and reload.", expectHour, dateField2.Hours);
+                               }
+                               
+                               
+                       } catch (Exception e) {
+                               e.printStackTrace();
+                       }
+                       
+                       
+                       
+                       
+               } catch (com.sun.star.uno.Exception e) {
+                       e.printStackTrace();
+               }
+               
+       }
+       /**
+        * Test Date Field can be created correctly.
+        * 1.Create a Date in new document
+        * 2.Verify the Date is created by check the date hour in the new 
document
+        * 3.Save and close the new document to doc format
+        * 4.Reload the new save doc file, check the  Date field
+        */
+
+       @Test
+       public void testCreateDateFieldSaveDoc() {
+               
+               String url = Testspace.getUrl(tempPath + tempFileName + ".doc");
+               PropertyValue[] propsValue = new PropertyValue[1];
+               propsValue[0] = new PropertyValue();
+               propsValue[0].Name = "FilterName";
+               propsValue[0].Value = "MS Word 97";
+               
+               createDateFiled(document, url, propsValue);
+       }
+       /**
+        * Test Date Field can be created correctly.
+        * 1.Create a Date field in new document
+        * 2.Verify the dateField is created by check the date hour in the new 
document
+        * 3.Save and close the new document to doc format
+        * 4.Reload the new save odt file, check the date  field
+        */
+       @Test
+       public void testCreateDateFieldSaveODT() {
+               
+               String url = Testspace.getUrl(tempPath + tempFileName + ".odt");
+               PropertyValue[] propsValue = new PropertyValue[0];
+               createDateFiled(document, url, propsValue);
+               
+       }
+       private void createDateFiled(XTextDocument document, String url, 
PropertyValue[] propsValue) {
+               XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) 
UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
+               try {
+                       XTextField  dateFiled = 
(XTextField)UnoRuntime.queryInterface(XTextField.class, 
sevriceFactory.createInstance("com.sun.star.text.textfield.DateTime"));
+                       
+               
+                       XPropertySet props = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, dateFiled);
+                       props.setPropertyValue("IsDate", true);
+                       
+                       
document.getText().insertTextContent(document.getText().getEnd(), dateFiled, 
false);
+                       DateTime dateField = (DateTime) 
props.getPropertyValue("DateTimeValue");
+               
+                       String dateString = document.getText().getString();
+                       assertTrue("Verify date field is creatd, by verify it's 
Month",  dateString.indexOf(String.valueOf(dateField.Month).trim()) != -1);
+                       assertTrue("Verify date field is creatd, by verify it's 
Day",  dateString.indexOf(String.valueOf(dateField.Day).trim()) != -1);
+                       int expectDay = 
Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
+                       assertEquals("Verify date field is creatd, value is 
right, by compare Day", expectDay, dateField.Day);
+                       XStorable store = 
UnoRuntime.queryInterface(XStorable.class, document);
+                       store.storeAsURL(url, propsValue);
+                       app.closeDocument(document);
+                       
+                       try {
+                               document = (XTextDocument) 
UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
+                               XTextFieldsSupplier fieldsSupplier = 
UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
+                               XEnumerationAccess xEnumeratedFields = 
fieldsSupplier.getTextFields();
+                               XEnumeration enumeration = 
xEnumeratedFields.createEnumeration();
+                               while (enumeration.hasMoreElements()) {
+                                         Object field =  
enumeration.nextElement();
+                                               XPropertySet props2 = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
+                                               DateTime dateField2 = 
(DateTime) props2.getPropertyValue("DateTimeValue");
+                                               assertEquals("Verify date field 
is creatd correct by save and reload.", expectDay, dateField2.Day);
+                               }
+                               
+                               
+                       } catch (Exception e) {
+                               e.printStackTrace();
+                       }
+                       
+                       
+               } catch (com.sun.star.uno.Exception e) {
+                       e.printStackTrace();
+               }
+               
+       }
+       
+       
+       
+
+}

Added: incubator/ooo/trunk/main/test/testuno/source/testlib/uno/sw/SWUtil.java
URL: 
http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testlib/uno/sw/SWUtil.java?rev=1374159&view=auto
==============================================================================
--- incubator/ooo/trunk/main/test/testuno/source/testlib/uno/sw/SWUtil.java 
(added)
+++ incubator/ooo/trunk/main/test/testuno/source/testlib/uno/sw/SWUtil.java Fri 
Aug 17 06:33:45 2012
@@ -0,0 +1,45 @@
+package testlib.uno.sw;
+
+import org.openoffice.test.uno.UnoApp;
+
+import com.sun.star.beans.PropertyValue;
+import com.sun.star.frame.XStorable;
+import com.sun.star.io.IOException;
+import com.sun.star.text.XTextDocument;
+import com.sun.star.uno.UnoRuntime;
+
+public class SWUtil {
+       
+       private static final UnoApp app = new UnoApp();
+       public static void saveAsDoc(XTextDocument document, String url) {
+               saveAs(document, "MS Word 97", url);
+               
+       }
+
+       public static void saveAsODT(XTextDocument document, String url) {
+               saveAs(document, "writer8", url);
+       }
+       
+       public static void saveAs(XTextDocument document, String filterValue, 
String url) {
+               XStorable store = UnoRuntime.queryInterface(XStorable.class, 
document);
+               
+               PropertyValue[] propsValue = new PropertyValue[1];
+               propsValue[0] = new PropertyValue();
+               propsValue[0].Name = "FilterName";
+               propsValue[0].Value = filterValue;
+               try {
+                       store.storeAsURL(url, propsValue);
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+       
+       public static XTextDocument newDocument() {
+               try {
+                       return (XTextDocument) 
UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return null;
+       }
+}


Reply via email to