111e9ca94SHerbert Dürr /************************************************************** 211e9ca94SHerbert Dürr * 311e9ca94SHerbert Dürr * Licensed to the Apache Software Foundation (ASF) under one 411e9ca94SHerbert Dürr * or more contributor license agreements. See the NOTICE file 511e9ca94SHerbert Dürr * distributed with this work for additional information 611e9ca94SHerbert Dürr * regarding copyright ownership. The ASF licenses this file 711e9ca94SHerbert Dürr * to you under the Apache License, Version 2.0 (the 811e9ca94SHerbert Dürr * "License"); you may not use this file except in compliance 911e9ca94SHerbert Dürr * with the License. You may obtain a copy of the License at 1011e9ca94SHerbert Dürr * 1111e9ca94SHerbert Dürr * http://www.apache.org/licenses/LICENSE-2.0 1211e9ca94SHerbert Dürr * 1311e9ca94SHerbert Dürr * Unless required by applicable law or agreed to in writing, 1411e9ca94SHerbert Dürr * software distributed under the License is distributed on an 1511e9ca94SHerbert Dürr * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1611e9ca94SHerbert Dürr * KIND, either express or implied. See the License for the 1711e9ca94SHerbert Dürr * specific language governing permissions and limitations 1811e9ca94SHerbert Dürr * under the License. 1911e9ca94SHerbert Dürr * 2011e9ca94SHerbert Dürr *************************************************************/ 2111e9ca94SHerbert Dürr 2211e9ca94SHerbert Dürr package fvt.uno.sc.formula; 2311e9ca94SHerbert Dürr 2411e9ca94SHerbert Dürr import static org.junit.Assert.*; 2511e9ca94SHerbert Dürr 2611e9ca94SHerbert Dürr import org.junit.After; 2711e9ca94SHerbert Dürr import org.junit.Before; 2811e9ca94SHerbert Dürr import org.junit.Rule; 2911e9ca94SHerbert Dürr import org.junit.Test; 3011e9ca94SHerbert Dürr 3111e9ca94SHerbert Dürr import org.openoffice.test.common.Testspace; 3211e9ca94SHerbert Dürr import org.openoffice.test.common.Logger; 3311e9ca94SHerbert Dürr 3411e9ca94SHerbert Dürr import org.openoffice.test.uno.UnoApp; 3511e9ca94SHerbert Dürr 3611e9ca94SHerbert Dürr import testlib.uno.SCUtil; 3711e9ca94SHerbert Dürr import static testlib.uno.TestUtil.*; 3811e9ca94SHerbert Dürr 392a312b7aScbmarcum import com.sun.star.beans.PropertyValue; 402a312b7aScbmarcum import com.sun.star.document.MacroExecMode; 4111e9ca94SHerbert Dürr import com.sun.star.lang.XComponent; 4211e9ca94SHerbert Dürr import com.sun.star.sheet.XSpreadsheet; 4311e9ca94SHerbert Dürr import com.sun.star.sheet.XSpreadsheetDocument; 4411e9ca94SHerbert Dürr import com.sun.star.sheet.XSpreadsheets; 4511e9ca94SHerbert Dürr import com.sun.star.table.XCell; 4611e9ca94SHerbert Dürr import com.sun.star.uno.Any; 4711e9ca94SHerbert Dürr import com.sun.star.text.XText; 4811e9ca94SHerbert Dürr import com.sun.star.uno.UnoRuntime; 4911e9ca94SHerbert Dürr import com.sun.star.util.XModifiable; 5011e9ca94SHerbert Dürr 5111e9ca94SHerbert Dürr import java.util.logging.Level; 5211e9ca94SHerbert Dürr 5311e9ca94SHerbert Dürr 5411e9ca94SHerbert Dürr public class TestFormulaDocs { 5511e9ca94SHerbert Dürr 5611e9ca94SHerbert Dürr @Rule 5711e9ca94SHerbert Dürr public Logger log = Logger.getLogger(this); 5811e9ca94SHerbert Dürr 5911e9ca94SHerbert Dürr UnoApp unoApp = new UnoApp(); 6011e9ca94SHerbert Dürr XComponent scComponent = null; 6111e9ca94SHerbert Dürr 6211e9ca94SHerbert Dürr @Before setUp()6311e9ca94SHerbert Dürr public void setUp() throws Exception { 6411e9ca94SHerbert Dürr unoApp.start(); 6511e9ca94SHerbert Dürr } 6611e9ca94SHerbert Dürr 6711e9ca94SHerbert Dürr @After tearDown()6811e9ca94SHerbert Dürr public void tearDown() throws Exception { 6911e9ca94SHerbert Dürr unoApp.close(); 7011e9ca94SHerbert Dürr } 7111e9ca94SHerbert Dürr 7211e9ca94SHerbert Dürr /** 7311e9ca94SHerbert Dürr * Test evaluation of formulas in a sample document 7411e9ca94SHerbert Dürr * 7511e9ca94SHerbert Dürr * @throws Exception 7611e9ca94SHerbert Dürr */ 7711e9ca94SHerbert Dürr 7811e9ca94SHerbert Dürr @Test testFormulaDocs()7911e9ca94SHerbert Dürr public void testFormulaDocs() throws Exception { 8011e9ca94SHerbert Dürr testOneDoc("uno/sc/fvt/FormulaTest1.ods"); 8104143ddcScbmarcum testOneDoc("uno/sc/fvt/StarBasicCLng.ods"); 8204143ddcScbmarcum testOneDoc("uno/sc/fvt/Basic Line as variable and Line Input.ods"); 83*e3244c50Scbmarcum testOneDoc("uno/sc/fvt/comment-in-single-line-if-then-else.ods"); 8411e9ca94SHerbert Dürr } 8511e9ca94SHerbert Dürr testOneDoc( String filename)8611e9ca94SHerbert Dürr public void testOneDoc( String filename) throws Exception { 8711e9ca94SHerbert Dürr // open the spreadsheet document 8811e9ca94SHerbert Dürr String sample = Testspace.prepareData( filename); 892a312b7aScbmarcum // enable macros 902a312b7aScbmarcum PropertyValue prop = new PropertyValue(); 912a312b7aScbmarcum prop.Name = "MacroExecutionMode"; 922a312b7aScbmarcum prop.Value = MacroExecMode.ALWAYS_EXECUTE_NO_WARN; 932a312b7aScbmarcum XSpreadsheetDocument scDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface( 942a312b7aScbmarcum XSpreadsheetDocument.class, unoApp.loadDocument(sample, prop)); 9511e9ca94SHerbert Dürr XSpreadsheet xSheet = SCUtil.getCurrentSheet( scDoc); 9611e9ca94SHerbert Dürr 9711e9ca94SHerbert Dürr // find the "TestID" and "TestOK" markers 9811e9ca94SHerbert Dürr int nTestIdCol = -1; 9911e9ca94SHerbert Dürr int nTestOkCol = -1; 10011e9ca94SHerbert Dürr int nTestRowStart = -1; 10111e9ca94SHerbert Dürr for( int y = 0; y < 8; ++y) { 10211e9ca94SHerbert Dürr for( int x = 0; x < 8; ++x) { 10311e9ca94SHerbert Dürr XCell xCell = xSheet.getCellByPosition( x, y); 10411e9ca94SHerbert Dürr XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell); 10511e9ca94SHerbert Dürr String name = xText.getString(); 10611e9ca94SHerbert Dürr if( name.equals( "TestID")) { 10711e9ca94SHerbert Dürr assertTrue( "Multiple rows with TestID marker!", nTestIdCol == -1); 10811e9ca94SHerbert Dürr assertTrue( nTestRowStart == -1); 10911e9ca94SHerbert Dürr nTestIdCol = x; 11011e9ca94SHerbert Dürr nTestRowStart = y + 1; 11111e9ca94SHerbert Dürr } else if( name.equals( "TestOK")) { 11211e9ca94SHerbert Dürr assertTrue( "Multiple rows with TestOK marker!", nTestOkCol == -1); 11311e9ca94SHerbert Dürr assertTrue( "TestID and TestOK marker not in same row!", nTestRowStart == y + 1); 11411e9ca94SHerbert Dürr nTestOkCol = x; 11511e9ca94SHerbert Dürr } 11611e9ca94SHerbert Dürr } 11711e9ca94SHerbert Dürr } 11811e9ca94SHerbert Dürr assertTrue( "Column \"TestID\" not found!", nTestIdCol >= 0); 11911e9ca94SHerbert Dürr assertTrue( "Column \"TestOK\" not found!", nTestOkCol >= 0); 12011e9ca94SHerbert Dürr 1219930860eSHerbert Dürr int nTestRowEnd = nTestRowStart + 100; // TODO: get the last row from the sheet 12211e9ca94SHerbert Dürr int nTestCount = 0; 12311e9ca94SHerbert Dürr int nFailCount = 0; 12411e9ca94SHerbert Dürr for( int y = nTestRowStart; y < nTestRowEnd; ++y) { 12511e9ca94SHerbert Dürr // get the test id 12611e9ca94SHerbert Dürr XCell xCell = xSheet.getCellByPosition( nTestIdCol, y); 12711e9ca94SHerbert Dürr XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell); 12811e9ca94SHerbert Dürr String testId = xText.getString(); 12911e9ca94SHerbert Dürr // ignore rows without test ids 13011e9ca94SHerbert Dürr if( testId.length() == 0) 13111e9ca94SHerbert Dürr continue; 13211e9ca94SHerbert Dürr ++nTestCount; 13311e9ca94SHerbert Dürr 13411e9ca94SHerbert Dürr // get and check the test result 13511e9ca94SHerbert Dürr xCell = xSheet.getCellByPosition( nTestOkCol, y); 1369930860eSHerbert Dürr String testOk = ((XText)UnoRuntime.queryInterface( XText.class, xCell)).getString(); 1379930860eSHerbert Dürr assertTrue( "Test result must be TRUE or FALSE", testOk.equals("TRUE") || testOk.equals("FALSE")); 1389930860eSHerbert Dürr boolean bOK = testOk.equals("TRUE"); 13911e9ca94SHerbert Dürr // mark evaluated test results 14011e9ca94SHerbert Dürr SCUtil.setProperties( xCell, "CellBackColor", (Integer)(bOK ? 0x00FF00 : 0xFF0000)); 14111e9ca94SHerbert Dürr // handle failed test cases 14211e9ca94SHerbert Dürr if( !bOK) { 14311e9ca94SHerbert Dürr ++nFailCount; 14411e9ca94SHerbert Dürr log.log( Level.SEVERE, "\ttest \""+testId+" failed"); 14511e9ca94SHerbert Dürr } 14611e9ca94SHerbert Dürr } 14711e9ca94SHerbert Dürr 14811e9ca94SHerbert Dürr assertTrue( (""+nFailCount+" of "+nTestCount+" tests failed"), nFailCount==0); 14911e9ca94SHerbert Dürr 15011e9ca94SHerbert Dürr XModifiable modified = (XModifiable)UnoRuntime.queryInterface( XModifiable.class, scDoc); 15111e9ca94SHerbert Dürr modified.setModified( false); 15211e9ca94SHerbert Dürr SCUtil.closeFile( scDoc); 15311e9ca94SHerbert Dürr } 15411e9ca94SHerbert Dürr 15511e9ca94SHerbert Dürr } 15611e9ca94SHerbert Dürr 157