100d096e9SLiu Zhe /************************************************************** 200d096e9SLiu Zhe * 300d096e9SLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 400d096e9SLiu Zhe * or more contributor license agreements. See the NOTICE file 500d096e9SLiu Zhe * distributed with this work for additional information 600d096e9SLiu Zhe * regarding copyright ownership. The ASF licenses this file 700d096e9SLiu Zhe * to you under the Apache License, Version 2.0 (the 800d096e9SLiu Zhe * "License"); you may not use this file except in compliance 900d096e9SLiu Zhe * with the License. You may obtain a copy of the License at 1000d096e9SLiu Zhe * 1100d096e9SLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 1200d096e9SLiu Zhe * 1300d096e9SLiu Zhe * Unless required by applicable law or agreed to in writing, 1400d096e9SLiu Zhe * software distributed under the License is distributed on an 1500d096e9SLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1600d096e9SLiu Zhe * KIND, either express or implied. See the License for the 1700d096e9SLiu Zhe * specific language governing permissions and limitations 1800d096e9SLiu Zhe * under the License. 1900d096e9SLiu Zhe * 2000d096e9SLiu Zhe *************************************************************/ 21*eba4d44aSLiu Zhe package fvt.uno.sw.field; 2200d096e9SLiu Zhe 2300d096e9SLiu Zhe import static org.junit.Assert.assertEquals; 2400d096e9SLiu Zhe import static org.junit.Assert.assertTrue; 2500d096e9SLiu Zhe 2600d096e9SLiu Zhe import java.util.Calendar; 2700d096e9SLiu Zhe 2800d096e9SLiu Zhe import org.junit.After; 2900d096e9SLiu Zhe import org.junit.AfterClass; 3000d096e9SLiu Zhe import org.junit.Before; 3100d096e9SLiu Zhe import org.junit.BeforeClass; 3200d096e9SLiu Zhe import org.junit.Test; 3319bdf417SLiu Zhe import org.openoffice.test.common.FileUtil; 3400d096e9SLiu Zhe import org.openoffice.test.common.Testspace; 3500d096e9SLiu Zhe import org.openoffice.test.uno.UnoApp; 3600d096e9SLiu Zhe 3700d096e9SLiu Zhe import com.sun.star.beans.PropertyValue; 3800d096e9SLiu Zhe import com.sun.star.beans.XPropertySet; 3900d096e9SLiu Zhe import com.sun.star.container.XEnumeration; 4000d096e9SLiu Zhe import com.sun.star.container.XEnumerationAccess; 4100d096e9SLiu Zhe import com.sun.star.frame.XStorable; 4200d096e9SLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 4300d096e9SLiu Zhe import com.sun.star.text.XTextDocument; 4400d096e9SLiu Zhe import com.sun.star.text.XTextField; 4500d096e9SLiu Zhe import com.sun.star.text.XTextFieldsSupplier; 4600d096e9SLiu Zhe import com.sun.star.uno.UnoRuntime; 4700d096e9SLiu Zhe import com.sun.star.util.DateTime; 4800d096e9SLiu Zhe /** 4900d096e9SLiu Zhe * Test Date and Time Field can be created and Load 5000d096e9SLiu Zhe * @author test 5100d096e9SLiu Zhe * 5200d096e9SLiu Zhe */ 5300d096e9SLiu Zhe public class CheckDateTimeField { 5400d096e9SLiu Zhe 5500d096e9SLiu Zhe 5600d096e9SLiu Zhe 57ba41a487SLiu Zhe private String tempPath = "uno/sw/temp/" ; 5800d096e9SLiu Zhe private String tempFileName = String.valueOf(System.currentTimeMillis()); 59970a06e3SLiu Zhe private static final UnoApp app = new UnoApp(); 6000d096e9SLiu Zhe 61970a06e3SLiu Zhe private XTextDocument document = null; 6200d096e9SLiu Zhe @Before setUpDocument()6300d096e9SLiu Zhe public void setUpDocument() throws Exception { 6400d096e9SLiu Zhe document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter")); 6500d096e9SLiu Zhe } 6600d096e9SLiu Zhe 6700d096e9SLiu Zhe @After tearDownDocument()6800d096e9SLiu Zhe public void tearDownDocument() { 6900d096e9SLiu Zhe app.closeDocument(document); 7019bdf417SLiu Zhe FileUtil.deleteFile(Testspace.getFile(tempPath)); 7100d096e9SLiu Zhe } 7200d096e9SLiu Zhe 7300d096e9SLiu Zhe 7400d096e9SLiu Zhe 7500d096e9SLiu Zhe @BeforeClass setUpConnection()7600d096e9SLiu Zhe public static void setUpConnection() throws Exception { 7700d096e9SLiu Zhe app.start(); 7800d096e9SLiu Zhe } 7900d096e9SLiu Zhe 8000d096e9SLiu Zhe @AfterClass tearDownConnection()8100d096e9SLiu Zhe public static void tearDownConnection() throws InterruptedException, Exception { 8200d096e9SLiu Zhe app.close(); 8300d096e9SLiu Zhe } 8400d096e9SLiu Zhe 8500d096e9SLiu Zhe 8600d096e9SLiu Zhe 8700d096e9SLiu Zhe /** 8800d096e9SLiu Zhe * Test Time field can be created correctly. 8900d096e9SLiu Zhe * 1.Create a Time in new document 9000d096e9SLiu Zhe * 2.Verify the Time is created by check the date hour in the new document 9100d096e9SLiu Zhe * 3.Save and close the new document to doc format 9200d096e9SLiu Zhe * 4.Reload the new save doc file, check the time field 9319bdf417SLiu Zhe * @throws Exception 9400d096e9SLiu Zhe */ 9500d096e9SLiu Zhe 9600d096e9SLiu Zhe @Test testCreateTimeFieldSaveDoc()9719bdf417SLiu Zhe public void testCreateTimeFieldSaveDoc() throws Exception { 9800d096e9SLiu Zhe 9900d096e9SLiu Zhe String url = Testspace.getUrl(tempPath + tempFileName + ".doc"); 10000d096e9SLiu Zhe PropertyValue[] propsValue = new PropertyValue[1]; 10100d096e9SLiu Zhe propsValue[0] = new PropertyValue(); 10200d096e9SLiu Zhe propsValue[0].Name = "FilterName"; 10300d096e9SLiu Zhe propsValue[0].Value = "MS Word 97"; 10400d096e9SLiu Zhe 10500d096e9SLiu Zhe createTimeFiled(document, url, propsValue); 10600d096e9SLiu Zhe } 10700d096e9SLiu Zhe /** 10800d096e9SLiu Zhe * Test Time Field can be created correctly. 10900d096e9SLiu Zhe * 1.Create a Time Field in new document 11000d096e9SLiu Zhe * 2.Verify the Time Field is created by check the date hour in the new document 11100d096e9SLiu Zhe * 3.Save and close the new document to doc format 11200d096e9SLiu Zhe * 4.Reload the new save odt file, check the Time Field 11319bdf417SLiu Zhe * @throws Exception 11400d096e9SLiu Zhe */ 11500d096e9SLiu Zhe @Test testCreateTimeFieldSaveODT()11619bdf417SLiu Zhe public void testCreateTimeFieldSaveODT() throws Exception { 11700d096e9SLiu Zhe 11800d096e9SLiu Zhe String url = Testspace.getUrl(tempPath + tempFileName + ".odt"); 11900d096e9SLiu Zhe PropertyValue[] propsValue = new PropertyValue[0]; 12000d096e9SLiu Zhe createTimeFiled(document, url, propsValue); 12100d096e9SLiu Zhe 12200d096e9SLiu Zhe } 12300d096e9SLiu Zhe createTimeFiled(XTextDocument document, String url, PropertyValue[] propsValue)12419bdf417SLiu Zhe private void createTimeFiled(XTextDocument document, String url, PropertyValue[] propsValue) throws Exception { 12500d096e9SLiu Zhe XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 12619bdf417SLiu Zhe XTextField dateFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.DateTime")); 12719bdf417SLiu Zhe 12819bdf417SLiu Zhe 12919bdf417SLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, dateFiled); 13019bdf417SLiu Zhe props.setPropertyValue("IsDate", false); 13119bdf417SLiu Zhe 13219bdf417SLiu Zhe document.getText().insertTextContent(document.getText().getEnd(), dateFiled, false); 13319bdf417SLiu Zhe DateTime dateField = (DateTime) props.getPropertyValue("DateTimeValue"); 13419bdf417SLiu Zhe 13519bdf417SLiu Zhe String dateString = document.getText().getString(); 13619bdf417SLiu Zhe assertTrue("Verify time field is creatd, by verify it's hour", dateString.indexOf(String.valueOf(dateField.Hours).trim()) != -1); 13719bdf417SLiu Zhe assertTrue("Verify time field is creatd, by verify it's minutes", dateString.indexOf(String.valueOf(dateField.Minutes).trim()) != -1); 13819bdf417SLiu Zhe int expectHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 13919bdf417SLiu Zhe assertEquals("Verify time field is creatd, value is right, by compare Hour", expectHour, dateField.Hours); 140cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 14119bdf417SLiu Zhe store.storeAsURL(url, propsValue); 14219bdf417SLiu Zhe app.closeDocument(document); 14319bdf417SLiu Zhe document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url)); 144cebb507aSLiu Zhe XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 14519bdf417SLiu Zhe XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 14619bdf417SLiu Zhe XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 14719bdf417SLiu Zhe while (enumeration.hasMoreElements()) { 14819bdf417SLiu Zhe Object field = enumeration.nextElement(); 14919bdf417SLiu Zhe XPropertySet props2 = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 15019bdf417SLiu Zhe DateTime dateField2 = (DateTime) props2.getPropertyValue("DateTimeValue"); 15119bdf417SLiu Zhe assertEquals("Verify time field is creatd correct by save and reload.", expectHour, dateField2.Hours); 15200d096e9SLiu Zhe } 15300d096e9SLiu Zhe 15419bdf417SLiu Zhe 15500d096e9SLiu Zhe } 15600d096e9SLiu Zhe /** 15700d096e9SLiu Zhe * Test Date Field can be created correctly. 15800d096e9SLiu Zhe * 1.Create a Date in new document 15900d096e9SLiu Zhe * 2.Verify the Date is created by check the date hour in the new document 16000d096e9SLiu Zhe * 3.Save and close the new document to doc format 16100d096e9SLiu Zhe * 4.Reload the new save doc file, check the Date field 16219bdf417SLiu Zhe * @throws Exception 16300d096e9SLiu Zhe */ 16400d096e9SLiu Zhe 16500d096e9SLiu Zhe @Test testCreateDateFieldSaveDoc()16619bdf417SLiu Zhe public void testCreateDateFieldSaveDoc() throws Exception { 16700d096e9SLiu Zhe 16800d096e9SLiu Zhe String url = Testspace.getUrl(tempPath + tempFileName + ".doc"); 16900d096e9SLiu Zhe PropertyValue[] propsValue = new PropertyValue[1]; 17000d096e9SLiu Zhe propsValue[0] = new PropertyValue(); 17100d096e9SLiu Zhe propsValue[0].Name = "FilterName"; 17200d096e9SLiu Zhe propsValue[0].Value = "MS Word 97"; 17300d096e9SLiu Zhe 17400d096e9SLiu Zhe createDateFiled(document, url, propsValue); 17500d096e9SLiu Zhe } 17600d096e9SLiu Zhe /** 17700d096e9SLiu Zhe * Test Date Field can be created correctly. 17800d096e9SLiu Zhe * 1.Create a Date field in new document 17900d096e9SLiu Zhe * 2.Verify the dateField is created by check the date hour in the new document 18000d096e9SLiu Zhe * 3.Save and close the new document to doc format 18100d096e9SLiu Zhe * 4.Reload the new save odt file, check the date field 18219bdf417SLiu Zhe * @throws Exception 18300d096e9SLiu Zhe */ 18400d096e9SLiu Zhe @Test testCreateDateFieldSaveODT()18519bdf417SLiu Zhe public void testCreateDateFieldSaveODT() throws Exception { 18600d096e9SLiu Zhe 18700d096e9SLiu Zhe String url = Testspace.getUrl(tempPath + tempFileName + ".odt"); 18800d096e9SLiu Zhe PropertyValue[] propsValue = new PropertyValue[0]; 18900d096e9SLiu Zhe createDateFiled(document, url, propsValue); 19000d096e9SLiu Zhe 19100d096e9SLiu Zhe } createDateFiled(XTextDocument document, String url, PropertyValue[] propsValue)19219bdf417SLiu Zhe private void createDateFiled(XTextDocument document, String url, PropertyValue[] propsValue) throws Exception { 19300d096e9SLiu Zhe XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 19419bdf417SLiu Zhe XTextField dateFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.DateTime")); 19519bdf417SLiu Zhe 19619bdf417SLiu Zhe 19719bdf417SLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, dateFiled); 19819bdf417SLiu Zhe props.setPropertyValue("IsDate", true); 19919bdf417SLiu Zhe 20019bdf417SLiu Zhe document.getText().insertTextContent(document.getText().getEnd(), dateFiled, false); 20119bdf417SLiu Zhe DateTime dateField = (DateTime) props.getPropertyValue("DateTimeValue"); 20219bdf417SLiu Zhe 20319bdf417SLiu Zhe String dateString = document.getText().getString(); 20419bdf417SLiu Zhe assertTrue("Verify date field is creatd, by verify it's Month", dateString.indexOf(String.valueOf(dateField.Month).trim()) != -1); 20519bdf417SLiu Zhe assertTrue("Verify date field is creatd, by verify it's Day", dateString.indexOf(String.valueOf(dateField.Day).trim()) != -1); 20619bdf417SLiu Zhe int expectDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 20719bdf417SLiu Zhe assertEquals("Verify date field is creatd, value is right, by compare Day", expectDay, dateField.Day); 208cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 20919bdf417SLiu Zhe store.storeAsURL(url, propsValue); 21019bdf417SLiu Zhe app.closeDocument(document); 21119bdf417SLiu Zhe document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url)); 212cebb507aSLiu Zhe XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 21319bdf417SLiu Zhe XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 21419bdf417SLiu Zhe XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 21519bdf417SLiu Zhe while (enumeration.hasMoreElements()) { 21619bdf417SLiu Zhe Object field = enumeration.nextElement(); 21719bdf417SLiu Zhe XPropertySet props2 = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 21819bdf417SLiu Zhe DateTime dateField2 = (DateTime) props2.getPropertyValue("DateTimeValue"); 21919bdf417SLiu Zhe assertEquals("Verify date field is creatd correct by save and reload.", expectDay, dateField2.Day); 22000d096e9SLiu Zhe } 22100d096e9SLiu Zhe 22200d096e9SLiu Zhe } 22300d096e9SLiu Zhe 22400d096e9SLiu Zhe 22500d096e9SLiu Zhe 22600d096e9SLiu Zhe 22700d096e9SLiu Zhe } 228