Line 0
Link Here
|
|
|
1 |
/* Licensed to the Apache Software Foundation (ASF) under one |
2 |
* or more contributor license agreements. See the NOTICE file |
3 |
* distributed with this work for additional information |
4 |
* regarding copyright ownership. The ASF licenses this file |
5 |
* to you under the Apache License, Version 2.0 (the |
6 |
* "License"); you may not use this file except in compliance |
7 |
* with the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, |
12 |
* software distributed under the License is distributed on an |
13 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 |
* KIND, either express or implied. See the License for the |
15 |
* specific language governing permissions and limitations |
16 |
* under the License. |
17 |
* |
18 |
*************************************************************/ |
19 |
|
20 |
|
21 |
package testcase.uno.sc; |
22 |
|
23 |
import static org.junit.Assert.assertEquals; |
24 |
|
25 |
import java.util.Arrays; |
26 |
import java.util.Collection; |
27 |
|
28 |
import org.junit.After; |
29 |
import org.junit.Before; |
30 |
import org.junit.Test; |
31 |
import org.junit.runner.RunWith; |
32 |
import org.junit.runners.Parameterized; |
33 |
import org.junit.runners.Parameterized.Parameters; |
34 |
|
35 |
import org.openoffice.test.uno.UnoApp; |
36 |
|
37 |
import com.sun.star.frame.XController; |
38 |
import com.sun.star.frame.XModel; |
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.XSpreadsheetView; |
43 |
import com.sun.star.sheet.XSpreadsheets; |
44 |
import com.sun.star.table.XCell; |
45 |
import com.sun.star.uno.UnoRuntime; |
46 |
|
47 |
/** |
48 |
* Check the addition operator works in formula |
49 |
* @author test |
50 |
* |
51 |
*/ |
52 |
@RunWith(value=Parameterized.class) |
53 |
public class AddtionOperatorInFormula { |
54 |
|
55 |
private double[] inputData; |
56 |
private double expected; |
57 |
|
58 |
@Parameters |
59 |
public static Collection<Object[]> data(){ |
60 |
double[] input1 = new double[] {1.34, 2004.1234}; |
61 |
double[] input2 = new double[] {-0.4, -0.73}; |
62 |
double[] input3 = new double[] {5.25, -0.35, 11.23, 45, -123.111}; |
63 |
double[] input4 = new double[] {-0, 0, 0, -0.0000}; |
64 |
return Arrays.asList(new Object[][]{ |
65 |
{addtionExpectedData(input1), input1}, |
66 |
{addtionExpectedData(input2), input2}, |
67 |
{addtionExpectedData(input3), input3}, |
68 |
{addtionExpectedData(input4), input4} |
69 |
}); |
70 |
} |
71 |
|
72 |
public AddtionOperatorInFormula(double expected, double[] inputData) { |
73 |
this.inputData = inputData; |
74 |
this.expected = expected; |
75 |
} |
76 |
|
77 |
UnoApp unoApp = new UnoApp(); |
78 |
|
79 |
XSpreadsheetDocument scDocument = null; |
80 |
XComponent scComponent = null; |
81 |
|
82 |
@Before |
83 |
public void setUp() throws Exception { |
84 |
unoApp.start(); |
85 |
} |
86 |
|
87 |
@After |
88 |
public void tearDown() throws Exception { |
89 |
Thread.sleep(10000); |
90 |
unoApp.closeDocument(scComponent); |
91 |
Thread.sleep(1000); |
92 |
unoApp.close(); |
93 |
} |
94 |
|
95 |
@Test |
96 |
public void testAddtion() throws Exception { |
97 |
String sheetname = "AddTest"; |
98 |
String inputformula = null; |
99 |
double cellvalue = 0; |
100 |
|
101 |
//Create Spreadsheet file. |
102 |
scComponent = unoApp.newDocument("scalc"); |
103 |
scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent); |
104 |
|
105 |
//Create a sheet at the first place. |
106 |
XSpreadsheets spreadsheets = scDocument.getSheets(); |
107 |
spreadsheets.insertNewByName(sheetname, (short) 0); |
108 |
Object sheetObj = spreadsheets.getByName(sheetname); |
109 |
XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj); |
110 |
|
111 |
//Active the new sheet. |
112 |
XModel scModel = (XModel) UnoRuntime.queryInterface(XModel.class, scDocument); |
113 |
XController scController = scModel.getCurrentController(); |
114 |
XSpreadsheetView sheetview = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, scController); |
115 |
sheetview.setActiveSheet(sheet); |
116 |
|
117 |
//Input formula string in cell A1. |
118 |
XCell cell = sheet.getCellByPosition(0, 0); |
119 |
inputformula = toFormula(createFormulaString(inputData, "+")); |
120 |
cell.setFormula(inputformula); |
121 |
|
122 |
//Get the formula calculation result. |
123 |
cellvalue = cell.getValue(); |
124 |
|
125 |
//Verify whether the actual result equal to the expected. |
126 |
assertEquals("Unexpected calculate result.", expected, cellvalue, 0); |
127 |
|
128 |
Thread.sleep(5000); |
129 |
} |
130 |
|
131 |
//Create formula string |
132 |
private static String toFormula(String expression) { |
133 |
return "=" + expression; |
134 |
} |
135 |
|
136 |
private static String createFormulaString(double[] inputData, String operator) { |
137 |
StringBuffer buffer = new StringBuffer(); |
138 |
|
139 |
for (int i = 0; i < inputData.length; i++) { |
140 |
buffer.append(inputData[i]); |
141 |
if (i < inputData.length - 1) { |
142 |
buffer.append(operator); |
143 |
} |
144 |
} |
145 |
return buffer.toString(); |
146 |
} |
147 |
|
148 |
//Calculate the expected result |
149 |
private static double addtionExpectedData(double[] inputData){ |
150 |
double data = 0; |
151 |
for (double input : inputData) { |
152 |
data += input; |
153 |
} |
154 |
return data; |
155 |
} |
156 |
|
157 |
} |