xref: /trunk/test/testuno/source/fvt/uno/sc/formula/TestFormulaRoundTrip.java (revision 371410417827e07a8f8596550830c16c641bed7c)
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 
22 
23 package fvt.uno.sc.formula;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 import java.util.Arrays;
28 import java.util.Collection;
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.Parameterized;
35 import org.junit.runners.Parameterized.Parameters;
36 
37 import org.openoffice.test.common.Testspace;
38 import org.openoffice.test.uno.UnoApp;
39 
40 import testlib.uno.SCUtil;
41 import static testlib.uno.TestUtil.*;
42 
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.frame.XStorable;
46 import com.sun.star.lang.XComponent;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.sheet.XSpreadsheets;
50 import com.sun.star.table.XCell;
51 
52 public class TestFormulaRoundTrip {
53     private UnoApp unoApp = new UnoApp();
54 
55     private XSpreadsheetDocument scDocument = null;
56     private XComponent scComponent = null;
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         unoApp.start();
61         scComponent = unoApp.newDocument("scalc");
62         scDocument = SCUtil.getSCDocument(scComponent);
63     }
64 
65     @After
tearDown()66     public void tearDown() throws Exception {
67         unoApp.closeDocument(scComponent);
68         unoApp.close();
69     }
70 
generateTestDocument(XSpreadsheetDocument scDocument)71     private XSpreadsheet generateTestDocument(XSpreadsheetDocument scDocument) throws Exception {
72         XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
73         SCUtil.setTextToCell(sheet, 0, 0, "Hello world");
74         SCUtil.setFormulaToCell(sheet, 0, 1, "=A1");
75         return sheet;
76     }
77 
78     @Test
testMSExcel2003XMLFormulaRoundTrip()79     public void testMSExcel2003XMLFormulaRoundTrip() throws Exception {
80         XSpreadsheet sheet = generateTestDocument(scDocument);
81         String formulaValue = SCUtil.getTextFromCell(sheet, 0, 1);
82         assertEquals("Hello world", formulaValue);
83         String path = "output/sc/temp.xml";
84         saveFormatTo("MS Excel 2003 XML", Testspace.getUrl(path));
85         unoApp.closeDocument(scComponent);
86 
87         scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(
88             XSpreadsheetDocument.class, unoApp.loadDocument(Testspace.getPath(path)));
89         sheet = SCUtil.getCurrentSheet(scDocument);
90         String formulaValue2 = SCUtil.getTextFromCell(sheet, 0, 1);
91         assertEquals("Hello world", formulaValue2);
92         String formula2 = SCUtil.getFormulaFromCell(sheet, 0, 1);
93         assertEquals("=A1", formula2);
94     }
95 
96     @Test
testStarOfficeXMLFormulaRoundTrip()97     public void testStarOfficeXMLFormulaRoundTrip() throws Exception {
98         XSpreadsheet sheet = generateTestDocument(scDocument);
99         String formulaValue = SCUtil.getTextFromCell(sheet, 0, 1);
100         assertEquals("Hello world", formulaValue);
101         String path = "output/sc/temp.sxc";
102         saveFormatTo("StarOffice XML (Calc)", Testspace.getUrl(path));
103         unoApp.closeDocument(scComponent);
104 
105         scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(
106             XSpreadsheetDocument.class, unoApp.loadDocument(Testspace.getPath(path)));
107         sheet = SCUtil.getCurrentSheet(scDocument);
108         String formulaValue2 = SCUtil.getTextFromCell(sheet, 0, 1);
109         assertEquals("Hello world", formulaValue2);
110         String formula2 = SCUtil.getFormulaFromCell(sheet, 0, 1);
111         assertEquals("=A1", formula2);
112     }
113 
saveFormatTo(String filterName, String storeUrl)114     private void saveFormatTo(String filterName, String storeUrl) throws Exception {
115         PropertyValue[] storeProps = new PropertyValue[2];
116         storeProps[0] = new PropertyValue();
117         storeProps[0].Name = "FilterName";
118         storeProps[0].Value = filterName;
119         storeProps[1] = new PropertyValue();
120         storeProps[1].Name = "Overwrite";
121         storeProps[1].Value = new Boolean(true);
122         XStorable scStorable =
123             (XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent);
124         scStorable.storeAsURL(storeUrl, storeProps);
125     }
126 }
127