18191ab5fSLiu Zhe /**************************************************************
28191ab5fSLiu Zhe  *
38191ab5fSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
48191ab5fSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
58191ab5fSLiu Zhe  * distributed with this work for additional information
68191ab5fSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
78191ab5fSLiu Zhe  * to you under the Apache License, Version 2.0 (the
88191ab5fSLiu Zhe  * "License"); you may not use this file except in compliance
98191ab5fSLiu Zhe  * with the License.  You may obtain a copy of the License at
108191ab5fSLiu Zhe  *
118191ab5fSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
128191ab5fSLiu Zhe  *
138191ab5fSLiu Zhe  * Unless required by applicable law or agreed to in writing,
148191ab5fSLiu Zhe  * software distributed under the License is distributed on an
158191ab5fSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
168191ab5fSLiu Zhe  * KIND, either express or implied.  See the License for the
178191ab5fSLiu Zhe  * specific language governing permissions and limitations
188191ab5fSLiu Zhe  * under the License.
198191ab5fSLiu Zhe  *
208191ab5fSLiu Zhe  *************************************************************/
218191ab5fSLiu Zhe 
228191ab5fSLiu Zhe 
23*eba4d44aSLiu Zhe package fvt.uno.sc.formula;
248191ab5fSLiu Zhe 
258191ab5fSLiu Zhe import static org.junit.Assert.assertEquals;
268191ab5fSLiu Zhe 
278191ab5fSLiu Zhe import java.util.Arrays;
288191ab5fSLiu Zhe import java.util.Collection;
298191ab5fSLiu Zhe 
308191ab5fSLiu Zhe import org.junit.After;
318191ab5fSLiu Zhe import org.junit.Before;
328191ab5fSLiu Zhe import org.junit.Test;
338191ab5fSLiu Zhe import org.junit.runner.RunWith;
348191ab5fSLiu Zhe import org.junit.runners.Parameterized;
358191ab5fSLiu Zhe import org.junit.runners.Parameterized.Parameters;
368191ab5fSLiu Zhe 
378191ab5fSLiu Zhe import org.openoffice.test.uno.UnoApp;
388191ab5fSLiu Zhe 
398191ab5fSLiu Zhe import testlib.uno.SCUtil;
408191ab5fSLiu Zhe import static testlib.uno.TestUtil.*;
418191ab5fSLiu Zhe 
428191ab5fSLiu Zhe import com.sun.star.lang.XComponent;
438191ab5fSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
448191ab5fSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
458191ab5fSLiu Zhe import com.sun.star.sheet.XSpreadsheets;
468191ab5fSLiu Zhe import com.sun.star.table.XCell;
478191ab5fSLiu Zhe 
488191ab5fSLiu Zhe /**
498191ab5fSLiu Zhe  * Check the addition operator works in formula
508191ab5fSLiu Zhe  * @author test
518191ab5fSLiu Zhe  *
528191ab5fSLiu Zhe  */
538191ab5fSLiu Zhe @RunWith(value=Parameterized.class)
548191ab5fSLiu Zhe public class AddtionOperatorInFormula {
558191ab5fSLiu Zhe 
568191ab5fSLiu Zhe 	private double[] inputData;
578191ab5fSLiu Zhe 	private double expected;
588191ab5fSLiu Zhe 
598191ab5fSLiu Zhe 	@Parameters
data()608191ab5fSLiu Zhe 	public static Collection<Object[]> data(){
618191ab5fSLiu Zhe 		double[] input1 = new double[] {1.34, 2004.1234};
628191ab5fSLiu Zhe 		double[] input2 = new double[] {-0.4, -0.73};
638191ab5fSLiu Zhe 		double[] input3 = new double[] {5.25, -0.35, 11.23, 45, -123.111};
648191ab5fSLiu Zhe 		double[] input4 = new double[] {-0, 0, 0, -0.0000};
658191ab5fSLiu Zhe 		return Arrays.asList(new Object[][]{
668191ab5fSLiu Zhe 				{addtionExpectedData(input1), input1},
678191ab5fSLiu Zhe 				{addtionExpectedData(input2), input2},
688191ab5fSLiu Zhe 				{addtionExpectedData(input3), input3},
698191ab5fSLiu Zhe 				{addtionExpectedData(input4), input4}
708191ab5fSLiu Zhe 		});
718191ab5fSLiu Zhe 	}
728191ab5fSLiu Zhe 
AddtionOperatorInFormula(double expected, double[] inputData)738191ab5fSLiu Zhe 	public AddtionOperatorInFormula(double expected, double[] inputData) {
748191ab5fSLiu Zhe 		this.inputData = inputData;
758191ab5fSLiu Zhe 		this.expected = expected;
768191ab5fSLiu Zhe 	}
778191ab5fSLiu Zhe 
788191ab5fSLiu Zhe 	UnoApp unoApp = new UnoApp();
798191ab5fSLiu Zhe 
808191ab5fSLiu Zhe 	XSpreadsheetDocument scDocument = null;
818191ab5fSLiu Zhe 	XComponent scComponent = null;
828191ab5fSLiu Zhe 
838191ab5fSLiu Zhe 	@Before
setUp()848191ab5fSLiu Zhe 	public void setUp() throws Exception {
858191ab5fSLiu Zhe 		unoApp.start();
868191ab5fSLiu Zhe 	}
878191ab5fSLiu Zhe 
888191ab5fSLiu Zhe 	@After
tearDown()898191ab5fSLiu Zhe 	public void tearDown() throws Exception {
908191ab5fSLiu Zhe 		unoApp.closeDocument(scComponent);
918191ab5fSLiu Zhe 		unoApp.close();
928191ab5fSLiu Zhe 	}
938191ab5fSLiu Zhe 
948191ab5fSLiu Zhe 	@Test
testAddtion()958191ab5fSLiu Zhe 	public void testAddtion() throws Exception {
968191ab5fSLiu Zhe 		String sheetname = "AddTest";
978191ab5fSLiu Zhe 		String inputformula = null;
988191ab5fSLiu Zhe 		double cellvalue = 0;
998191ab5fSLiu Zhe 
1008191ab5fSLiu Zhe 		//Create Spreadsheet file.
1018191ab5fSLiu Zhe 		scComponent = unoApp.newDocument("scalc");
1028191ab5fSLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
1038191ab5fSLiu Zhe 
1048191ab5fSLiu Zhe 		//Create a sheet at the first place.
1058191ab5fSLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
1068191ab5fSLiu Zhe 		spreadsheets.insertNewByName(sheetname, (short) 0);
1078191ab5fSLiu Zhe 		XSpreadsheet sheet = SCUtil.getSCSheetByName(scDocument, sheetname);
1088191ab5fSLiu Zhe 
1098191ab5fSLiu Zhe 		//Active the new sheet.
1108191ab5fSLiu Zhe 		SCUtil.setCurrentSheet(scDocument, sheet);
1118191ab5fSLiu Zhe 
1128191ab5fSLiu Zhe 		//Input formula string in cell A1.
1138191ab5fSLiu Zhe 		XCell cell = sheet.getCellByPosition(0, 0);
1148191ab5fSLiu Zhe 		inputformula = toFormula(connectByOperator(inputData, "+"));
1158191ab5fSLiu Zhe 		cell.setFormula(inputformula);
1168191ab5fSLiu Zhe 
1178191ab5fSLiu Zhe 		//Get the formula calculation result.
1188191ab5fSLiu Zhe 		cellvalue = cell.getValue();
1198191ab5fSLiu Zhe 
1208191ab5fSLiu Zhe 		//Verify whether the actual result equal to the expected.
1218191ab5fSLiu Zhe 		assertEquals("Unexpected calculate result.", expected, cellvalue, 0);
1228191ab5fSLiu Zhe 
1238191ab5fSLiu Zhe 	}
1248191ab5fSLiu Zhe 
1258191ab5fSLiu Zhe 	//Calculate the expected result
addtionExpectedData(double[] inputData)1268191ab5fSLiu Zhe 	private static double addtionExpectedData(double[] inputData){
1278191ab5fSLiu Zhe 		double data = 0;
1288191ab5fSLiu Zhe 		for (double input : inputData) {
1298191ab5fSLiu Zhe 			data += input;
1308191ab5fSLiu Zhe 		}
1318191ab5fSLiu Zhe 		return data;
1328191ab5fSLiu Zhe 	}
1338191ab5fSLiu Zhe 
1348191ab5fSLiu Zhe }
135