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