xref: /trunk/test/testuno/source/fvt/uno/sc/formula/TestFormulaDocs.java (revision bcc22a4c08e1268a4f06e54fb146f88ef49f2cdc)
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 package fvt.uno.sc.formula;
23 
24 import static org.junit.Assert.*;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 
31 import org.openoffice.test.common.Testspace;
32 import org.openoffice.test.common.Logger;
33 
34 import org.openoffice.test.uno.UnoApp;
35 
36 import testlib.uno.SCUtil;
37 import static testlib.uno.TestUtil.*;
38 
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.document.MacroExecMode;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.sheet.XSpreadsheet;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.sheet.XSpreadsheets;
45 import com.sun.star.table.XCell;
46 import com.sun.star.uno.Any;
47 import com.sun.star.text.XText;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.util.XModifiable;
50 
51 import java.util.logging.Level;
52 
53 
54 public class TestFormulaDocs {
55 
56     @Rule
57     public Logger log = Logger.getLogger(this);
58 
59     UnoApp unoApp = new UnoApp();
60     XComponent scComponent = null;
61 
62     @Before
63     public void setUp() throws Exception {
64         unoApp.start();
65     }
66 
67     @After
68     public void tearDown() throws Exception {
69         unoApp.close();
70     }
71 
72     /**
73      * Test evaluation of formulas in a sample document
74      *
75      * @throws Exception
76      */
77 
78     @Test
79     public void testFormulaDocs() throws Exception {
80         testOneDoc( "uno/sc/fvt/FormulaTest1.ods");
81         testOneDoc( "uno/sc/fvt/StarBasicYearMonthDateHourMinuteSecondTests.ods");
82         testOneDoc( "uno/sc/fvt/StarBasicCLng.ods");
83         testOneDoc( "uno/sc/fvt/DGET on formulas.ods");
84         testOneDoc( "uno/sc/fvt/Basic Line as variable and Line Input.ods");
85     }
86 
87     public void testOneDoc( String filename) throws Exception {
88         // open the spreadsheet document
89         String sample = Testspace.prepareData( filename);
90         // enable macros
91         PropertyValue prop = new PropertyValue();
92         prop.Name = "MacroExecutionMode";
93         prop.Value = MacroExecMode.ALWAYS_EXECUTE_NO_WARN;
94         XSpreadsheetDocument scDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
95             XSpreadsheetDocument.class, unoApp.loadDocument(sample, prop));
96         XSpreadsheet xSheet = SCUtil.getCurrentSheet( scDoc);
97 
98         // find the "TestID" and "TestOK" markers
99         int nTestIdCol = -1;
100         int nTestOkCol = -1;
101         int nTestRowStart = -1;
102         for( int y = 0; y < 8; ++y) {
103             for( int x = 0; x < 26; ++x) {
104                 XCell xCell = xSheet.getCellByPosition( x, y);
105                 XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
106                 String name = xText.getString();
107                 if( name.equals( "TestID")) {
108                     assertTrue( "Multiple rows with TestID marker!", nTestIdCol == -1);
109                     assertTrue( nTestRowStart == -1);
110                     nTestIdCol = x;
111                     nTestRowStart = y + 1;
112                 } else if( name.equals( "TestOK")) {
113                     assertTrue( "Multiple rows with TestOK marker!", nTestOkCol == -1);
114                     assertTrue( "TestID and TestOK marker not in same row!", nTestRowStart == y + 1);
115                     nTestOkCol = x;
116                 }
117             }
118         }
119         assertTrue( "Column \"TestID\" not found!", nTestIdCol >= 0);
120         assertTrue( "Column \"TestOK\" not found!", nTestOkCol >= 0);
121 
122         int nTestRowEnd = nTestRowStart + 100; // TODO: get the last row from the sheet
123         int nTestCount = 0;
124         int nFailCount = 0;
125         for( int y = nTestRowStart; y < nTestRowEnd; ++y) {
126             // get the test id
127             XCell xCell = xSheet.getCellByPosition( nTestIdCol, y);
128             XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
129             String testId = xText.getString();
130             // ignore rows without test ids
131             if( testId.length() == 0)
132                 continue;
133             ++nTestCount;
134 
135             // get and check the test result
136             xCell = xSheet.getCellByPosition( nTestOkCol, y);
137             String testOk = ((XText)UnoRuntime.queryInterface( XText.class, xCell)).getString();
138             assertTrue( "Test result must be TRUE or FALSE", testOk.equals("TRUE") || testOk.equals("FALSE"));
139             boolean bOK = testOk.equals("TRUE");
140             // mark evaluated test results
141             SCUtil.setProperties( xCell, "CellBackColor", (Integer)(bOK ? 0x00FF00 : 0xFF0000));
142             // handle failed test cases
143             if( !bOK) {
144                 ++nFailCount;
145                 log.log( Level.SEVERE, "\ttest \""+testId+" failed");
146             }
147         }
148 
149         assertTrue( (""+nFailCount+" of "+nTestCount+" tests failed"), nFailCount==0);
150 
151         XModifiable modified = (XModifiable)UnoRuntime.queryInterface( XModifiable.class, scDoc);
152         modified.setModified( false);
153         SCUtil.closeFile( scDoc);
154     }
155 
156 }
157 
158