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