Author: liuzhe Date: Wed Aug 22 09:04:09 2012 New Revision: 1375950 URL: http://svn.apache.org/viewvc?rev=1375950&view=rev Log: #120648# - [testUNO patch] Utility methods created to genertate random data, get properties list and a sc script refactor Patch by: Zhu Shan <shanzh...@gmail.com> Review by: Liu Zhe <aliu...@gmail.com>
Added: incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sc/formula/ incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sc/formula/AddtionOperatorInFormula.java incubator/ooo/trunk/main/test/testuno/source/testlib/uno/CellInfo.java incubator/ooo/trunk/main/test/testuno/source/testlib/uno/TestUtil.java Added: incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sc/formula/AddtionOperatorInFormula.java URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sc/formula/AddtionOperatorInFormula.java?rev=1375950&view=auto ============================================================================== --- incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sc/formula/AddtionOperatorInFormula.java (added) +++ incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sc/formula/AddtionOperatorInFormula.java Wed Aug 22 09:04:09 2012 @@ -0,0 +1,134 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + *************************************************************/ + + +package testcase.uno.sc.formula; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collection; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import org.openoffice.test.uno.UnoApp; + +import testlib.uno.SCUtil; +import static testlib.uno.TestUtil.*; + +import com.sun.star.lang.XComponent; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.table.XCell; + +/** + * Check the addition operator works in formula + * @author test + * + */ +@RunWith(value=Parameterized.class) +public class AddtionOperatorInFormula { + + private double[] inputData; + private double expected; + + @Parameters + public static Collection<Object[]> data(){ + double[] input1 = new double[] {1.34, 2004.1234}; + double[] input2 = new double[] {-0.4, -0.73}; + double[] input3 = new double[] {5.25, -0.35, 11.23, 45, -123.111}; + double[] input4 = new double[] {-0, 0, 0, -0.0000}; + return Arrays.asList(new Object[][]{ + {addtionExpectedData(input1), input1}, + {addtionExpectedData(input2), input2}, + {addtionExpectedData(input3), input3}, + {addtionExpectedData(input4), input4} + }); + } + + public AddtionOperatorInFormula(double expected, double[] inputData) { + this.inputData = inputData; + this.expected = expected; + } + + UnoApp unoApp = new UnoApp(); + + XSpreadsheetDocument scDocument = null; + XComponent scComponent = null; + + @Before + public void setUp() throws Exception { + unoApp.start(); + } + + @After + public void tearDown() throws Exception { + unoApp.closeDocument(scComponent); + unoApp.close(); + } + + @Test + public void testAddtion() throws Exception { + String sheetname = "AddTest"; + String inputformula = null; + double cellvalue = 0; + + //Create Spreadsheet file. + scComponent = unoApp.newDocument("scalc"); + scDocument = SCUtil.getSCDocument(scComponent); + + //Create a sheet at the first place. + XSpreadsheets spreadsheets = scDocument.getSheets(); + spreadsheets.insertNewByName(sheetname, (short) 0); + XSpreadsheet sheet = SCUtil.getSCSheetByName(scDocument, sheetname); + + //Active the new sheet. + SCUtil.setCurrentSheet(scDocument, sheet); + + //Input formula string in cell A1. + XCell cell = sheet.getCellByPosition(0, 0); + inputformula = toFormula(connectByOperator(inputData, "+")); + cell.setFormula(inputformula); + + //Get the formula calculation result. + cellvalue = cell.getValue(); + + //Verify whether the actual result equal to the expected. + assertEquals("Unexpected calculate result.", expected, cellvalue, 0); + + } + + //Calculate the expected result + private static double addtionExpectedData(double[] inputData){ + double data = 0; + for (double input : inputData) { + data += input; + } + return data; + } + +} Added: incubator/ooo/trunk/main/test/testuno/source/testlib/uno/CellInfo.java URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testlib/uno/CellInfo.java?rev=1375950&view=auto ============================================================================== --- incubator/ooo/trunk/main/test/testuno/source/testlib/uno/CellInfo.java (added) +++ incubator/ooo/trunk/main/test/testuno/source/testlib/uno/CellInfo.java Wed Aug 22 09:04:09 2012 @@ -0,0 +1,53 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + *************************************************************/ + +package testlib.uno; + +/** + * A class to save a cell column and row info + * + */ +public class CellInfo { + private int col; + private int row; + + public CellInfo() { + this.col = 0; + this.row = 0; + } + public CellInfo(int col, int row) { + this.col = col; + this.row = row; + } + + public int getCol() { + return col; + } + public void setCol(int col) { + this.col = col; + } + public int getRow() { + return row; + } + public void setRow(int row) { + this.row = row; + } +} Added: incubator/ooo/trunk/main/test/testuno/source/testlib/uno/TestUtil.java URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testlib/uno/TestUtil.java?rev=1375950&view=auto ============================================================================== --- incubator/ooo/trunk/main/test/testuno/source/testlib/uno/TestUtil.java (added) +++ incubator/ooo/trunk/main/test/testuno/source/testlib/uno/TestUtil.java Wed Aug 22 09:04:09 2012 @@ -0,0 +1,178 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + *************************************************************/ + + +package testlib.uno; + +import java.util.Random; + +import com.sun.star.beans.Property; +import com.sun.star.beans.PropertyAttribute; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.XPropertySetInfo; +import com.sun.star.uno.UnoRuntime; + +import testlib.uno.CellInfo; + + +/** + * Utilities for UNO automation testing + * + */ + +public class TestUtil { + + private static int colLimit = 1024; + private static int rowLimit = 1048576; + private static Random random = new Random(); + + public TestUtil() { + + } + + /** + * Generate a random cell index + * @return cellIndex column: cellIndex[0] row: cellIndex[1] + * @throws Exception + */ + public static CellInfo randCell() throws Exception { + CellInfo cInfo = new CellInfo(); + + cInfo.setCol(random.nextInt(colLimit)); + cInfo.setRow(random.nextInt(rowLimit)); + + return cInfo; + } + + /** + * Generate a random cell index, in the limited range + * @param colTop The max column limit + * @param rowTop The max row limit + * @return + * @throws Exception + */ + public static CellInfo randCell(int colTop, int rowTop) throws Exception { + CellInfo cInfo = new CellInfo(); + + cInfo.setCol(random.nextInt(colTop)); + cInfo.setRow(random.nextInt(rowTop)); + + return cInfo; + } + + /** + * Generate a random decimal RGB color number + * @return + * @throws Exception + */ + public static int randColor() throws Exception { + int r = random.nextInt(256); + int g = random.nextInt(256); + int b = random.nextInt(256); + + return r * 65536 + g * 256 + b; + } + + /** + * Generate a random decimal RGB color number in limited color space + * @param rMax The R value limit, [0, rMax) + * @param gMax The G value limit, [0, gMax) + * @param bMax The B value limit, [0, bMax) + * @return + * @throws Exception + */ + public static int randColor(int rMax, int gMax, int bMax) throws Exception { + int r = random.nextInt(rMax) % 256; + int g = random.nextInt(gMax) % 256; + int b = random.nextInt(bMax) % 256; + + return r * 65536 + g * 256 + b; + } + + /** + * Add "=" before a string + * @param expression + * @return + */ + public static String toFormula(String expression) { + return "=" + expression; + } + + /** + * Use specific operator to connect a series of number + * @param number + * @param operator + * @return + */ + public static String connectByOperator(double[] number, String operator) throws Exception{ + StringBuffer buffer = new StringBuffer(); + + for (int i = 0; i < number.length; i++) { + buffer.append(number[i]); + if (i < number.length - 1) { + buffer.append(operator); + } + } + return buffer.toString(); + } + + /** + * Print the properties list of specific object to console + * @param obj The instance of the object of which the property list you want to get. e.g. instance of XCell. + * @throws Exception + */ + public static void printPropertiesList(Object obj) throws Exception { + // Get the property set of specific object + XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj); + XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo(); + + // Get all properties info + Property[] aProps = xPropertySetInfo.getProperties(); + + for (int i = 0; i < aProps.length; i++) { + // Print name and type of each property + System.out.print("[" + (i + 1) + "]: Name=\"" + aProps[i].Name + "\" " + aProps[i].Type.toString() + " ("); + + // Get flag. pay attention to the READONLY properties + short nAttribs = aProps[i].Attributes; + if ((nAttribs & PropertyAttribute.MAYBEVOID) != 0) + System.out.print("MAYBEVOID|"); + if ((nAttribs & PropertyAttribute.BOUND) != 0) + System.out.print("BOUND|"); + if ((nAttribs & PropertyAttribute.CONSTRAINED) != 0) + System.out.print("CONSTRAINED|"); + if ((nAttribs & PropertyAttribute.READONLY) != 0) + System.out.print("READONLY|"); + if ((nAttribs & PropertyAttribute.TRANSIENT) != 0) + System.out.print("TRANSIENT|"); + if ((nAttribs & PropertyAttribute.MAYBEAMBIGUOUS ) != 0) + System.out.print("MAYBEAMBIGUOUS|"); + if ((nAttribs & PropertyAttribute.MAYBEDEFAULT) != 0) + System.out.print("MAYBEDEFAULT|"); + if ((nAttribs & PropertyAttribute.REMOVEABLE) != 0) + System.out.print("REMOVEABLE|"); + + System.out.println(")"); + } + + } + +}