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 
23 package fvt.uno.sc.formula;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 import java.util.Arrays;
28 import java.util.Collection;
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.Parameterized;
35 import org.junit.runners.Parameterized.Parameters;
36 
37 import org.openoffice.test.uno.UnoApp;
38 
39 import testlib.uno.SCUtil;
40 import static testlib.uno.TestUtil.*;
41 
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.XCell;
47 
48 /**
49  * Check the addition operator works in formula
50  * @author test
51  *
52  */
53 @RunWith(value=Parameterized.class)
54 public class AddtionOperatorInFormula {
55 
56 	private double[] inputData;
57 	private double expected;
58 
59 	@Parameters
data()60 	public static Collection<Object[]> data(){
61 		double[] input1 = new double[] {1.34, 2004.1234};
62 		double[] input2 = new double[] {-0.4, -0.73};
63 		double[] input3 = new double[] {5.25, -0.35, 11.23, 45, -123.111};
64 		double[] input4 = new double[] {-0, 0, 0, -0.0000};
65 		return Arrays.asList(new Object[][]{
66 				{addtionExpectedData(input1), input1},
67 				{addtionExpectedData(input2), input2},
68 				{addtionExpectedData(input3), input3},
69 				{addtionExpectedData(input4), input4}
70 		});
71 	}
72 
AddtionOperatorInFormula(double expected, double[] inputData)73 	public AddtionOperatorInFormula(double expected, double[] inputData) {
74 		this.inputData = inputData;
75 		this.expected = expected;
76 	}
77 
78 	UnoApp unoApp = new UnoApp();
79 
80 	XSpreadsheetDocument scDocument = null;
81 	XComponent scComponent = null;
82 
83 	@Before
setUp()84 	public void setUp() throws Exception {
85 		unoApp.start();
86 	}
87 
88 	@After
tearDown()89 	public void tearDown() throws Exception {
90 		unoApp.closeDocument(scComponent);
91 		unoApp.close();
92 	}
93 
94 	@Test
testAddtion()95 	public void testAddtion() throws Exception {
96 		String sheetname = "AddTest";
97 		String inputformula = null;
98 		double cellvalue = 0;
99 
100 		//Create Spreadsheet file.
101 		scComponent = unoApp.newDocument("scalc");
102 		scDocument = SCUtil.getSCDocument(scComponent);
103 
104 		//Create a sheet at the first place.
105 		XSpreadsheets spreadsheets = scDocument.getSheets();
106 		spreadsheets.insertNewByName(sheetname, (short) 0);
107 		XSpreadsheet sheet = SCUtil.getSCSheetByName(scDocument, sheetname);
108 
109 		//Active the new sheet.
110 		SCUtil.setCurrentSheet(scDocument, sheet);
111 
112 		//Input formula string in cell A1.
113 		XCell cell = sheet.getCellByPosition(0, 0);
114 		inputformula = toFormula(connectByOperator(inputData, "+"));
115 		cell.setFormula(inputformula);
116 
117 		//Get the formula calculation result.
118 		cellvalue = cell.getValue();
119 
120 		//Verify whether the actual result equal to the expected.
121 		assertEquals("Unexpected calculate result.", expected, cellvalue, 0);
122 
123 	}
124 
125 	//Calculate the expected result
addtionExpectedData(double[] inputData)126 	private static double addtionExpectedData(double[] inputData){
127 		double data = 0;
128 		for (double input : inputData) {
129 			data += input;
130 		}
131 		return data;
132 	}
133 
134 }
135