1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package fvt.uno.sw.field;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 
26 import java.util.Calendar;
27 
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.openoffice.test.common.FileUtil;
34 import org.openoffice.test.common.Testspace;
35 import org.openoffice.test.uno.UnoApp;
36 
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.container.XEnumeration;
40 import com.sun.star.container.XEnumerationAccess;
41 import com.sun.star.frame.XStorable;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.text.XTextDocument;
44 import com.sun.star.text.XTextField;
45 import com.sun.star.text.XTextFieldsSupplier;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.util.DateTime;
48 /**
49  * Test Date and Time Field can be created and Load
50  * @author test
51  *
52  */
53 public class CheckDateTimeField {
54 
55 
56 
57 	private String tempPath = "uno/sw/temp/" ;
58 	private String tempFileName =  String.valueOf(System.currentTimeMillis());
59 	private static final UnoApp app = new UnoApp();
60 
61 	private XTextDocument document = null;
62 	@Before
setUpDocument()63 	public void setUpDocument() throws Exception {
64 		document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
65 	}
66 
67 	@After
tearDownDocument()68 	public void tearDownDocument() {
69 	     app.closeDocument(document);
70 		 FileUtil.deleteFile(Testspace.getFile(tempPath));
71 	}
72 
73 
74 
75 	@BeforeClass
setUpConnection()76 	public static void setUpConnection() throws Exception {
77 		app.start();
78 	}
79 
80 	@AfterClass
tearDownConnection()81 	public static void tearDownConnection() throws InterruptedException, Exception {
82 		app.close();
83 	}
84 
85 
86 
87 	/**
88 	 * Test Time field can be created correctly.
89 	 * 1.Create a Time in new document
90 	 * 2.Verify the Time is created by check the date hour in the new document
91 	 * 3.Save and close the new document to doc format
92 	 * 4.Reload the new save doc file, check the  time field
93 	 * @throws Exception
94 	 */
95 
96 	@Test
testCreateTimeFieldSaveDoc()97 	public void testCreateTimeFieldSaveDoc() throws Exception {
98 
99 		String url = Testspace.getUrl(tempPath + tempFileName + ".doc");
100 		PropertyValue[] propsValue = new PropertyValue[1];
101 		propsValue[0] = new PropertyValue();
102 		propsValue[0].Name = "FilterName";
103 		propsValue[0].Value = "MS Word 97";
104 
105 		createTimeFiled(document, url, propsValue);
106 	}
107 	/**
108 	 * Test Time Field can be created correctly.
109 	 * 1.Create a Time Field in new document
110 	 * 2.Verify the Time Field is created by check the date hour in the new document
111 	 * 3.Save and close the new document to doc format
112 	 * 4.Reload the new save odt file, check the Time Field
113 	 * @throws Exception
114 	 */
115 	@Test
testCreateTimeFieldSaveODT()116 	public void testCreateTimeFieldSaveODT() throws Exception {
117 
118 		String url = Testspace.getUrl(tempPath + tempFileName + ".odt");
119 		PropertyValue[] propsValue = new PropertyValue[0];
120 		createTimeFiled(document, url, propsValue);
121 
122 	}
123 
createTimeFiled(XTextDocument document, String url, PropertyValue[] propsValue)124 	private void createTimeFiled(XTextDocument document, String url, PropertyValue[] propsValue) throws Exception {
125 		XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
126 		XTextField  dateFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.DateTime"));
127 
128 
129 		XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, dateFiled);
130 		props.setPropertyValue("IsDate", false);
131 
132 		document.getText().insertTextContent(document.getText().getEnd(), dateFiled, false);
133 		DateTime dateField = (DateTime) props.getPropertyValue("DateTimeValue");
134 
135 		String dateString = document.getText().getString();
136 		assertTrue("Verify time field is creatd, by verify it's hour",  dateString.indexOf(String.valueOf(dateField.Hours).trim()) != -1);
137 		assertTrue("Verify time field is creatd, by verify it's minutes",  dateString.indexOf(String.valueOf(dateField.Minutes).trim()) != -1);
138 		int expectHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
139 		assertEquals("Verify time field is creatd, value is right, by compare Hour", expectHour, dateField.Hours);
140 		XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
141 		store.storeAsURL(url, propsValue);
142 		app.closeDocument(document);
143 		document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
144 		XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
145 		XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
146 		XEnumeration enumeration = xEnumeratedFields.createEnumeration();
147 		while (enumeration.hasMoreElements()) {
148 			  Object field =  enumeration.nextElement();
149 				XPropertySet props2 = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
150 				DateTime dateField2 = (DateTime) props2.getPropertyValue("DateTimeValue");
151 				assertEquals("Verify time field is creatd correct by save and reload.", expectHour, dateField2.Hours);
152 		}
153 
154 
155 	}
156 	/**
157 	 * Test Date Field can be created correctly.
158 	 * 1.Create a Date in new document
159 	 * 2.Verify the Date is created by check the date hour in the new document
160 	 * 3.Save and close the new document to doc format
161 	 * 4.Reload the new save doc file, check the  Date field
162 	 * @throws Exception
163 	 */
164 
165 	@Test
testCreateDateFieldSaveDoc()166 	public void testCreateDateFieldSaveDoc() throws Exception {
167 
168 		String url = Testspace.getUrl(tempPath + tempFileName + ".doc");
169 		PropertyValue[] propsValue = new PropertyValue[1];
170 		propsValue[0] = new PropertyValue();
171 		propsValue[0].Name = "FilterName";
172 		propsValue[0].Value = "MS Word 97";
173 
174 		createDateFiled(document, url, propsValue);
175 	}
176 	/**
177 	 * Test Date Field can be created correctly.
178 	 * 1.Create a Date field in new document
179 	 * 2.Verify the dateField is created by check the date hour in the new document
180 	 * 3.Save and close the new document to doc format
181 	 * 4.Reload the new save odt file, check the date  field
182 	 * @throws Exception
183 	 */
184 	@Test
testCreateDateFieldSaveODT()185 	public void testCreateDateFieldSaveODT() throws Exception {
186 
187 		String url = Testspace.getUrl(tempPath + tempFileName + ".odt");
188 		PropertyValue[] propsValue = new PropertyValue[0];
189 		createDateFiled(document, url, propsValue);
190 
191 	}
createDateFiled(XTextDocument document, String url, PropertyValue[] propsValue)192 	private void createDateFiled(XTextDocument document, String url, PropertyValue[] propsValue) throws Exception {
193 		XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
194 		XTextField  dateFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.DateTime"));
195 
196 
197 		XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, dateFiled);
198 		props.setPropertyValue("IsDate", true);
199 
200 		document.getText().insertTextContent(document.getText().getEnd(), dateFiled, false);
201 		DateTime dateField = (DateTime) props.getPropertyValue("DateTimeValue");
202 
203 		String dateString = document.getText().getString();
204 		assertTrue("Verify date field is creatd, by verify it's Month",  dateString.indexOf(String.valueOf(dateField.Month).trim()) != -1);
205 		assertTrue("Verify date field is creatd, by verify it's Day",  dateString.indexOf(String.valueOf(dateField.Day).trim()) != -1);
206 		int expectDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
207 		assertEquals("Verify date field is creatd, value is right, by compare Day", expectDay, dateField.Day);
208 		XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
209 		store.storeAsURL(url, propsValue);
210 		app.closeDocument(document);
211 		document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
212 		XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
213 		XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
214 		XEnumeration enumeration = xEnumeratedFields.createEnumeration();
215 		while (enumeration.hasMoreElements()) {
216 			  Object field =  enumeration.nextElement();
217 				XPropertySet props2 = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
218 				DateTime dateField2 = (DateTime) props2.getPropertyValue("DateTimeValue");
219 				assertEquals("Verify date field is creatd correct by save and reload.", expectDay, dateField2.Day);
220 		}
221 
222 	}
223 
224 
225 
226 
227 }
228