DocumentTest.java (07d7dbdc) DocumentTest.java (1ff9903b)
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

--- 18 unchanged lines hidden (view full) ---

27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.Assert;
32import org.openoffice.test.common.FileUtil;
33import org.openoffice.test.uno.UnoApp;
34
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

--- 18 unchanged lines hidden (view full) ---

27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.Assert;
32import org.openoffice.test.common.FileUtil;
33import org.openoffice.test.uno.UnoApp;
34
35import testlib.uno.SWUtil;
36
35import com.sun.star.text.XTextDocument;
36import com.sun.star.text.XTextCursor;
37import com.sun.star.text.XText;
38import com.sun.star.beans.XPropertySet;
39import com.sun.star.beans.PropertyValue;
40import com.sun.star.frame.*;
41import com.sun.star.uno.UnoRuntime;
37import com.sun.star.text.XTextDocument;
38import com.sun.star.text.XTextCursor;
39import com.sun.star.text.XText;
40import com.sun.star.beans.XPropertySet;
41import com.sun.star.beans.PropertyValue;
42import com.sun.star.frame.*;
43import com.sun.star.uno.UnoRuntime;
42import com.sun.star.util.XCloseable;
43import com.sun.star.lang.XComponent;
44import com.sun.star.lang.XComponent;
44import com.sun.star.container.XEnumerationAccess;
45import com.sun.star.container.XEnumeration;
46
47
48public class DocumentTest {
49 UnoApp unoApp = new UnoApp();
50 XTextDocument textDocument = null;
51 File temp = null;
52 String workingFilePath = "";
53 String workingTemplatePath = "";
45
46
47public class DocumentTest {
48 UnoApp unoApp = new UnoApp();
49 XTextDocument textDocument = null;
50 File temp = null;
51 String workingFilePath = "";
52 String workingTemplatePath = "";
54
55 /**
56 * @throws java.lang.Exception
57 */
53
58 @Before
59 public void setUp() throws Exception {
60 unoApp.start();
61
62 FileUtil.deleteFile(getPath("temp"));
63 temp = new File(getPath("temp"));
64 temp.mkdirs();
65
66 //copy sample file to temp folder
67 workingFilePath = prepareData("uno/sw/DocumentTest.odt");
68 workingTemplatePath = prepareData("uno/sw/DocumentTest.ott");
69 }
70
71 @After
72 public void tearDown() throws Exception {
73 unoApp.close();
74 }
54 @Before
55 public void setUp() throws Exception {
56 unoApp.start();
57
58 FileUtil.deleteFile(getPath("temp"));
59 temp = new File(getPath("temp"));
60 temp.mkdirs();
61
62 //copy sample file to temp folder
63 workingFilePath = prepareData("uno/sw/DocumentTest.odt");
64 workingTemplatePath = prepareData("uno/sw/DocumentTest.ott");
65 }
66
67 @After
68 public void tearDown() throws Exception {
69 unoApp.close();
70 }
75
76 private XComponent newDocumentFromTemplate(String templatePath) throws Exception
77 {
78 XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
79 PropertyValue[] pros = new PropertyValue[1];
80 pros[0] = new PropertyValue();
81 pros[0].Name = "AsTemplate";
82 pros[0].Value = new Boolean(true);
83
84 XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
85 return component;
86 }
87
88
89 /**
71 /**
90 * test close document
72 * test new document and close document
91 * @throws Exception
92 */
93 @Test
73 * @throws Exception
74 */
75 @Test
94 public void testCloseDocument() throws Exception
76 public void testNewAndCloseDocument() throws Exception
95 {
96 XComponent component = unoApp.newDocument("swriter");
77 {
78 XComponent component = unoApp.newDocument("swriter");
97 unoApp.closeDocument(component);
98 XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();
99 Assert.assertTrue("Document has been closed.",xModel==null);
100 }
101
102 /**
103 * test new document
104 * @throws Exception
105 */
106 @Test
107 public void testNewDocument() throws Exception
108 {
109 XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
110 XComponent component = componentLoader.loadComponentFromURL("private:factory/" + "swriter", "_blank", 0, new PropertyValue[0]);
111 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
112 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
113 String title = xTitle.getTitle();
114 Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
79 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
80 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
81 String title = xTitle.getTitle();
82 Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
115 unoApp.closeDocument(textDocument);
116 }
117
83 unoApp.closeDocument(component);
84 XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();
85 Assert.assertTrue("Document has been closed.",xModel==null);
86 }
118 /**
119 * test new document from template
120 * @throws Exception
121 */
122 @Test
123 public void testNewDocumentFromTemplate() throws Exception
87 /**
88 * test new document from template
89 * @throws Exception
90 */
91 @Test
92 public void testNewDocumentFromTemplate() throws Exception
124 {
125 XComponent component = this.newDocumentFromTemplate(workingTemplatePath);
93 {
94 XComponent component = SWUtil.newDocumentFromTemplate(workingTemplatePath,unoApp);
126 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
95 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
127 XText xText = textDocument.getText();
128 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
96 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
129 xText = textDocument.getText();
97 XText xText = textDocument.getText();
130 XTextCursor xTextCursor = xText.createTextCursor();
131 xTextCursor.gotoEnd(true);
132 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
98 XTextCursor xTextCursor = xText.createTextCursor();
99 xTextCursor.gotoEnd(true);
100 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
133 String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
134
135 Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
136
101 String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
102 Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
137 Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
103 Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
138
139 unoApp.closeDocument(textDocument);
140 }
141
142 /**
143 * test save document as odt
144 * @throws Exception
145 */
146 @Test
147 public void testSaveDocument() throws Exception
148 {
149 XComponent component = unoApp.loadDocument(workingFilePath);
150 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
151 XText xText = textDocument.getText();
152 XTextCursor xTextCursor = xText.createTextCursor();
153 xTextCursor.gotoEnd(true);
104 }
105
106 /**
107 * test save document as odt
108 * @throws Exception
109 */
110 @Test
111 public void testSaveDocument() throws Exception
112 {
113 XComponent component = unoApp.loadDocument(workingFilePath);
114 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
115 XText xText = textDocument.getText();
116 XTextCursor xTextCursor = xText.createTextCursor();
117 xTextCursor.gotoEnd(true);
154 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
155
156 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
157
158 XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
159 xStorable.store();
160 unoApp.closeDocument(textDocument);
161
118 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
119 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
120 SWUtil.save(textDocument);
121 unoApp.closeDocument(textDocument);
162 component = unoApp.loadDocument(workingFilePath);
163 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
164 xText = textDocument.getText();
165 xTextCursor = xText.createTextCursor();
166 xTextCursor.gotoEnd(true);
122 component = unoApp.loadDocument(workingFilePath);
123 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
124 xText = textDocument.getText();
125 xTextCursor = xText.createTextCursor();
126 xTextCursor.gotoEnd(true);
167 xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
168
127 xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
169 Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
128 Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
170 unoApp.closeDocument(textDocument);
171 }
172
173 /**
174 * test save document as doc
175 * @throws Exception
176 */
177 @Test
178 public void testSaveAsDocument() throws Exception
179 {
180 File saveAsFile = new File(workingFilePath + ".doc");
181 XComponent component = unoApp.loadDocument(workingFilePath);
182 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
183 XText xText = textDocument.getText();
184 XTextCursor xTextCursor = xText.createTextCursor();
185 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
186
187 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
188 xText.insertString(xTextCursor, "test Save odt as doc.", false);
129 }
130
131 /**
132 * test save document as doc
133 * @throws Exception
134 */
135 @Test
136 public void testSaveAsDocument() throws Exception
137 {
138 File saveAsFile = new File(workingFilePath + ".doc");
139 XComponent component = unoApp.loadDocument(workingFilePath);
140 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
141 XText xText = textDocument.getText();
142 XTextCursor xTextCursor = xText.createTextCursor();
143 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
144
145 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
146 xText.insertString(xTextCursor, "test Save odt as doc.", false);
189
190 XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
191 PropertyValue[] storeProps = new PropertyValue[2];
192
193 storeProps[0] = new PropertyValue();
194 storeProps[0].Name = "Overwrite";
195 storeProps[0].Value = new Boolean(true);
196
197 storeProps[1] = new PropertyValue();
198 storeProps[1].Name = "FilterName";
199 storeProps[1].Value = "MS Word 97";
200
201 xStorable.storeAsURL(FileUtil.getUrl(saveAsFile), storeProps);
147 SWUtil.saveAsDoc(textDocument, FileUtil.getUrl(saveAsFile));
202 Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
148 Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
203 unoApp.closeDocument(textDocument);
204 }
205
206 /**
207 * test export document as pdf
208 * @throws Exception
209 */
210 @Test
211 public void testExportAsPDF() throws Exception

--- 15 unchanged lines hidden (view full) ---

227
228 storeProps[2] = new PropertyValue();
229 storeProps[2].Name = "CompressionMode";
230 storeProps[2].Value = "1";
231
232 xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
233
234 Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
149 }
150
151 /**
152 * test export document as pdf
153 * @throws Exception
154 */
155 @Test
156 public void testExportAsPDF() throws Exception

--- 15 unchanged lines hidden (view full) ---

172
173 storeProps[2] = new PropertyValue();
174 storeProps[2].Name = "CompressionMode";
175 storeProps[2].Value = "1";
176
177 xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
178
179 Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
235
236 unoApp.closeDocument(textDocument);
237 }
238
239 /**
240 * test save document as template
241 * @throws Exception
242 */
243 @Test
244 public void testSaveAsTemplate() throws Exception

--- 20 unchanged lines hidden (view full) ---

265 storeProps[1].Name="TemplateRegionName";
266 storeProps[1].Value="My Templates";
267
268 storeProps[2] = new PropertyValue();
269 storeProps[2].Name="AsTemplate";
270 storeProps[2].Value=new Boolean(true);
271
272 xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
180 }
181
182 /**
183 * test save document as template
184 * @throws Exception
185 */
186 @Test
187 public void testSaveAsTemplate() throws Exception

--- 20 unchanged lines hidden (view full) ---

208 storeProps[1].Name="TemplateRegionName";
209 storeProps[1].Value="My Templates";
210
211 storeProps[2] = new PropertyValue();
212 storeProps[2].Name="AsTemplate";
213 storeProps[2].Value=new Boolean(true);
214
215 xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
273 unoApp.closeDocument(textDocument);
274
275 component = this.newDocumentFromTemplate(saveAsFile.getAbsolutePath());
276 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
277 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
278 xText = textDocument.getText();
279 xTextCursor = xText.createTextCursor();
280 xTextCursor.gotoEnd(true);
281 xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
282 String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
283 Assert.assertEquals("Save document as template, heading style is remained. ", "Heading 1", paraStyle);
284 Assert.assertEquals("Save document as template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
285 unoApp.closeDocument(textDocument);
216 unoApp.closeDocument(textDocument);
217 Assert.assertTrue("Export document as ott.", saveAsFile.exists());
286 }
287
288}
218 }
219
220}