169efe8daSLiu Zhe /************************************************************** 269efe8daSLiu Zhe * 369efe8daSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 469efe8daSLiu Zhe * or more contributor license agreements. See the NOTICE file 569efe8daSLiu Zhe * distributed with this work for additional information 669efe8daSLiu Zhe * regarding copyright ownership. The ASF licenses this file 769efe8daSLiu Zhe * to you under the Apache License, Version 2.0 (the 869efe8daSLiu Zhe * "License"); you may not use this file except in compliance 969efe8daSLiu Zhe * with the License. You may obtain a copy of the License at 1069efe8daSLiu Zhe * 1169efe8daSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 1269efe8daSLiu Zhe * 1369efe8daSLiu Zhe * Unless required by applicable law or agreed to in writing, 1469efe8daSLiu Zhe * software distributed under the License is distributed on an 1569efe8daSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1669efe8daSLiu Zhe * KIND, either express or implied. See the License for the 1769efe8daSLiu Zhe * specific language governing permissions and limitations 1869efe8daSLiu Zhe * under the License. 1969efe8daSLiu Zhe * 2069efe8daSLiu Zhe *************************************************************/ 21*eba4d44aSLiu Zhe package fvt.uno.sc.data; 2269efe8daSLiu Zhe 2369efe8daSLiu Zhe import static org.junit.Assert.*; 2469efe8daSLiu Zhe 2569efe8daSLiu Zhe import java.util.Arrays; 2669efe8daSLiu Zhe import java.util.Collection; 2769efe8daSLiu Zhe 2869efe8daSLiu Zhe import org.junit.After; 2969efe8daSLiu Zhe import org.junit.AfterClass; 3069efe8daSLiu Zhe import org.junit.Before; 3169efe8daSLiu Zhe import org.junit.BeforeClass; 3269efe8daSLiu Zhe import org.junit.Test; 3369efe8daSLiu Zhe import org.junit.runner.RunWith; 3469efe8daSLiu Zhe import org.junit.runners.Parameterized; 3569efe8daSLiu Zhe import org.junit.runners.Parameterized.Parameters; 3669efe8daSLiu Zhe import org.openoffice.test.uno.UnoApp; 3769efe8daSLiu Zhe import testlib.uno.SCUtil; 3869efe8daSLiu Zhe import com.sun.star.lang.XComponent; 3969efe8daSLiu Zhe import com.sun.star.sheet.GeneralFunction; 4069efe8daSLiu Zhe import com.sun.star.sheet.SubTotalColumn; 4169efe8daSLiu Zhe import com.sun.star.sheet.XCellRangeData; 4269efe8daSLiu Zhe import com.sun.star.sheet.XSpreadsheet; 4369efe8daSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument; 4469efe8daSLiu Zhe import com.sun.star.sheet.XSubTotalCalculatable; 4569efe8daSLiu Zhe import com.sun.star.sheet.XSubTotalDescriptor; 4669efe8daSLiu Zhe import com.sun.star.table.XCellRange; 4769efe8daSLiu Zhe import com.sun.star.uno.Enum; 4869efe8daSLiu Zhe import com.sun.star.uno.UnoRuntime; 4969efe8daSLiu Zhe 5069efe8daSLiu Zhe @RunWith(value = Parameterized.class) 5169efe8daSLiu Zhe public class SubTotalsFunction { 5269efe8daSLiu Zhe private static final UnoApp app = new UnoApp(); 5369efe8daSLiu Zhe 5469efe8daSLiu Zhe UnoApp unoApp = new UnoApp(); 5569efe8daSLiu Zhe XSpreadsheetDocument scDocument = null; 5669efe8daSLiu Zhe XComponent scComponent = null; 5769efe8daSLiu Zhe 5869efe8daSLiu Zhe private GeneralFunction operator; 5969efe8daSLiu Zhe 6069efe8daSLiu Zhe private String operatorString; 6169efe8daSLiu Zhe 6269efe8daSLiu Zhe private int operatorvalue; 6369efe8daSLiu Zhe 6469efe8daSLiu Zhe private double bssubtotalresult; 6569efe8daSLiu Zhe 6669efe8daSLiu Zhe private double cssubtotalresult; 6769efe8daSLiu Zhe 6869efe8daSLiu Zhe private double mssubtotalresult; 6969efe8daSLiu Zhe 7069efe8daSLiu Zhe private double grandtotal; 7169efe8daSLiu Zhe 7269efe8daSLiu Zhe @Parameters data()7369efe8daSLiu Zhe public static Collection<Object[]> data() throws Exception { 7469efe8daSLiu Zhe // Remove GeneralFunction.Auto,GeneralFunction.NONE 7569efe8daSLiu Zhe return Arrays.asList(new Object[][] { 7669efe8daSLiu Zhe { GeneralFunction.SUM, "Sum", 9, 12, 12, 4, 28 }, 7769efe8daSLiu Zhe { GeneralFunction.AVERAGE, "Average", 1, 4, 6, 2, 4 }, 7869efe8daSLiu Zhe { GeneralFunction.COUNT, "Count", 3, 3, 2, 2, 7 }, 7969efe8daSLiu Zhe { GeneralFunction.COUNTNUMS, "Count", 2, 3, 2, 2, 7 }, 8069efe8daSLiu Zhe { GeneralFunction.MAX, "Max", 4, 6, 7, 3, 7 }, 8169efe8daSLiu Zhe { GeneralFunction.MIN, "Min", 5, 2, 5, 1, 1 }, 8269efe8daSLiu Zhe { GeneralFunction.VAR, "Var", 10, 4, 2, 2, 4.666666667 }, 8369efe8daSLiu Zhe { GeneralFunction.PRODUCT, "Product", 6, 48, 35, 3, 5040 }, 8469efe8daSLiu Zhe { GeneralFunction.STDEVP, "StDev", 8, 1.6329931619, 1, 1, 2 }, 8569efe8daSLiu Zhe { GeneralFunction.STDEV, "StDev", 7, 2, 1.4142135624, 8669efe8daSLiu Zhe 1.4142135624, 2.1602468995 }, 8769efe8daSLiu Zhe { GeneralFunction.VARP, "Var", 11, 2.6666666667, 1, 1, 4 }, }); 8869efe8daSLiu Zhe } 8969efe8daSLiu Zhe 9069efe8daSLiu Zhe @Before setUpDocument()9169efe8daSLiu Zhe public void setUpDocument() throws Exception { 9269efe8daSLiu Zhe unoApp.start(); 9369efe8daSLiu Zhe } 9469efe8daSLiu Zhe 9569efe8daSLiu Zhe @After tearDownDocument()9669efe8daSLiu Zhe public void tearDownDocument() { 9769efe8daSLiu Zhe unoApp.close(); 9869efe8daSLiu Zhe unoApp.closeDocument(scComponent); 9969efe8daSLiu Zhe 10069efe8daSLiu Zhe } 10169efe8daSLiu Zhe 10269efe8daSLiu Zhe @BeforeClass setUpConnection()10369efe8daSLiu Zhe public static void setUpConnection() throws Exception { 10469efe8daSLiu Zhe 10569efe8daSLiu Zhe } 10669efe8daSLiu Zhe 10769efe8daSLiu Zhe @AfterClass tearDownConnection()10869efe8daSLiu Zhe public static void tearDownConnection() throws InterruptedException, 10969efe8daSLiu Zhe Exception { 11069efe8daSLiu Zhe 11169efe8daSLiu Zhe } 11269efe8daSLiu Zhe SubTotalsFunction(Enum operator, String operatorString, int operatorvalue, double bssubtotalresult, double cssubtotalresult, double mssubtotalresult, double grandtotal)11369efe8daSLiu Zhe public SubTotalsFunction(Enum operator, String operatorString, 11469efe8daSLiu Zhe int operatorvalue, double bssubtotalresult, 11569efe8daSLiu Zhe double cssubtotalresult, double mssubtotalresult, double grandtotal) { 11669efe8daSLiu Zhe this.operator = (GeneralFunction) operator; 11769efe8daSLiu Zhe this.operatorString = operatorString; 11869efe8daSLiu Zhe this.operatorvalue = operatorvalue; 11969efe8daSLiu Zhe this.bssubtotalresult = bssubtotalresult; 12069efe8daSLiu Zhe this.cssubtotalresult = cssubtotalresult; 12169efe8daSLiu Zhe this.mssubtotalresult = mssubtotalresult; 12269efe8daSLiu Zhe this.grandtotal = grandtotal; 12369efe8daSLiu Zhe } 12469efe8daSLiu Zhe 12569efe8daSLiu Zhe @Test test()12669efe8daSLiu Zhe public void test() throws Exception { 12769efe8daSLiu Zhe // New document and input data in document 12869efe8daSLiu Zhe scComponent = unoApp.newDocument("scalc"); 12969efe8daSLiu Zhe scDocument = SCUtil.getSCDocument(scComponent); 13069efe8daSLiu Zhe XSpreadsheet currentsheet = SCUtil.getCurrentSheet(scDocument); 13169efe8daSLiu Zhe XCellRange xdataRange = (XCellRange) UnoRuntime.queryInterface( 13269efe8daSLiu Zhe XCellRange.class, currentsheet); 13369efe8daSLiu Zhe XCellRange sourceRange = currentsheet.getCellRangeByName("A1:E8"); 13469efe8daSLiu Zhe XCellRangeData sourceData = (XCellRangeData) UnoRuntime.queryInterface( 13569efe8daSLiu Zhe XCellRangeData.class, sourceRange); 13669efe8daSLiu Zhe Object[][] Source = { { "Level", "Code", "No.", "Team", "Name" }, 13769efe8daSLiu Zhe { "BS", 20, 4, "B", "Elle" }, { "BS", 20, 6, "C", "Sweet" }, 13869efe8daSLiu Zhe { "BS", 20, 2, "A", "Chcomic" }, { "CS", 30, 5, "A", "Ally" }, 13969efe8daSLiu Zhe { "MS", 10, 1, "A", "Joker" }, { "MS", 10, 3, "B", "Kevin" }, 14069efe8daSLiu Zhe { "CS", 30, 7, "C", "Tom" } }; 14169efe8daSLiu Zhe sourceData.setDataArray(Source); 14269efe8daSLiu Zhe 14369efe8daSLiu Zhe // Create SubTotals 14469efe8daSLiu Zhe XSubTotalCalculatable xSub = (XSubTotalCalculatable) UnoRuntime 14569efe8daSLiu Zhe .queryInterface(XSubTotalCalculatable.class, sourceRange); 14669efe8daSLiu Zhe XSubTotalDescriptor xSubDesc = xSub.createSubTotalDescriptor(true); 14769efe8daSLiu Zhe SubTotalColumn[] aColumns = new SubTotalColumn[1]; 14869efe8daSLiu Zhe // calculate sum of third column 14969efe8daSLiu Zhe aColumns[0] = new SubTotalColumn(); 15069efe8daSLiu Zhe aColumns[0].Column = 2; 15169efe8daSLiu Zhe aColumns[0].Function = operator; 15269efe8daSLiu Zhe // group by first column 15369efe8daSLiu Zhe xSubDesc.addNew(aColumns, 0); 15469efe8daSLiu Zhe xSub.applySubTotals(xSubDesc, true); 15569efe8daSLiu Zhe 15669efe8daSLiu Zhe // Verify BS SubTotals result 15769efe8daSLiu Zhe String BSsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$2:$C$4)"; 15869efe8daSLiu Zhe String BSsubtotalsString = "BS " + operatorString; 15969efe8daSLiu Zhe 16069efe8daSLiu Zhe assertEquals(bssubtotalresult, 16169efe8daSLiu Zhe SCUtil.getValueFromCell(currentsheet, 2, 4), 0.000000001); 16269efe8daSLiu Zhe assertEquals(BSsubtotals, SCUtil.getFormulaFromCell(currentsheet, 2, 4)); 16369efe8daSLiu Zhe assertEquals(BSsubtotalsString, 16469efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 4)); 16569efe8daSLiu Zhe 16669efe8daSLiu Zhe // Verify CS SubTotals result 16769efe8daSLiu Zhe String CSsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$6:$C$7)"; 16869efe8daSLiu Zhe String CSsubtotalsString = "CS " + operatorString; 16969efe8daSLiu Zhe 17069efe8daSLiu Zhe assertEquals(cssubtotalresult, 17169efe8daSLiu Zhe SCUtil.getValueFromCell(currentsheet, 2, 7), 0.000000001); 17269efe8daSLiu Zhe assertEquals(CSsubtotals, SCUtil.getFormulaFromCell(currentsheet, 2, 7)); 17369efe8daSLiu Zhe assertEquals(CSsubtotalsString, 17469efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 7)); 17569efe8daSLiu Zhe 17669efe8daSLiu Zhe // Verify MS SubTotals result 17769efe8daSLiu Zhe String MSsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$9:$C$10)"; 17869efe8daSLiu Zhe String MSsubtotalsString = "MS " + operatorString; 17969efe8daSLiu Zhe 18069efe8daSLiu Zhe assertEquals(mssubtotalresult, 18169efe8daSLiu Zhe SCUtil.getValueFromCell(currentsheet, 2, 10), 0.000000001); 18269efe8daSLiu Zhe assertEquals(MSsubtotals, 18369efe8daSLiu Zhe SCUtil.getFormulaFromCell(currentsheet, 2, 10)); 18469efe8daSLiu Zhe assertEquals(MSsubtotalsString, 18569efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 10)); 18669efe8daSLiu Zhe 18769efe8daSLiu Zhe // Verify GrandTotal result 18869efe8daSLiu Zhe String GTsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$2:$C$11)"; 18969efe8daSLiu Zhe String GTsubtotalsString = "Grand Total"; 19069efe8daSLiu Zhe 19169efe8daSLiu Zhe assertEquals(grandtotal, SCUtil.getValueFromCell(currentsheet, 2, 11), 19269efe8daSLiu Zhe 0.000000001); 19369efe8daSLiu Zhe assertEquals(GTsubtotals, 19469efe8daSLiu Zhe SCUtil.getFormulaFromCell(currentsheet, 2, 11)); 19569efe8daSLiu Zhe assertEquals(GTsubtotalsString, 19669efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 11)); 19769efe8daSLiu Zhe 19869efe8daSLiu Zhe // Save the file and reload it 19969efe8daSLiu Zhe SCUtil.saveFileAs(scComponent, "Subtotals", "ods"); 20069efe8daSLiu Zhe XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp, 20169efe8daSLiu Zhe scDocument, "Subtotals.ods"); 20269efe8daSLiu Zhe scDocument = scDocumentTemp; 20369efe8daSLiu Zhe currentsheet = SCUtil.getCurrentSheet(scDocument); 20469efe8daSLiu Zhe 20569efe8daSLiu Zhe // verify it again 20669efe8daSLiu Zhe // Verify BS SubTotals result 20769efe8daSLiu Zhe BSsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$2:$C$4)"; 20869efe8daSLiu Zhe BSsubtotalsString = "BS " + operatorString; 20969efe8daSLiu Zhe 21069efe8daSLiu Zhe assertEquals(bssubtotalresult, 21169efe8daSLiu Zhe SCUtil.getValueFromCell(currentsheet, 2, 4), 0.000000001); 21269efe8daSLiu Zhe assertEquals(BSsubtotals, SCUtil.getFormulaFromCell(currentsheet, 2, 4)); 21369efe8daSLiu Zhe assertEquals(BSsubtotalsString, 21469efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 4)); 21569efe8daSLiu Zhe 21669efe8daSLiu Zhe // Verify CS SubTotals result 21769efe8daSLiu Zhe CSsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$6:$C$7)"; 21869efe8daSLiu Zhe CSsubtotalsString = "CS " + operatorString; 21969efe8daSLiu Zhe 22069efe8daSLiu Zhe assertEquals(cssubtotalresult, 22169efe8daSLiu Zhe SCUtil.getValueFromCell(currentsheet, 2, 7), 0.000000001); 22269efe8daSLiu Zhe assertEquals(CSsubtotals, SCUtil.getFormulaFromCell(currentsheet, 2, 7)); 22369efe8daSLiu Zhe assertEquals(CSsubtotalsString, 22469efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 7)); 22569efe8daSLiu Zhe 22669efe8daSLiu Zhe // Verify MS SubTotals result 22769efe8daSLiu Zhe MSsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$9:$C$10)"; 22869efe8daSLiu Zhe MSsubtotalsString = "MS " + operatorString; 22969efe8daSLiu Zhe 23069efe8daSLiu Zhe assertEquals(mssubtotalresult, 23169efe8daSLiu Zhe SCUtil.getValueFromCell(currentsheet, 2, 10), 0.000000001); 23269efe8daSLiu Zhe assertEquals(MSsubtotals, 23369efe8daSLiu Zhe SCUtil.getFormulaFromCell(currentsheet, 2, 10)); 23469efe8daSLiu Zhe assertEquals(MSsubtotalsString, 23569efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 10)); 23669efe8daSLiu Zhe 23769efe8daSLiu Zhe // Verify GrandTotal result 23869efe8daSLiu Zhe GTsubtotals = "=SUBTOTAL(" + operatorvalue + ";$C$2:$C$11)"; 23969efe8daSLiu Zhe GTsubtotalsString = "Grand Total"; 24069efe8daSLiu Zhe 24169efe8daSLiu Zhe assertEquals(grandtotal, SCUtil.getValueFromCell(currentsheet, 2, 11), 24269efe8daSLiu Zhe 0.000000001); 24369efe8daSLiu Zhe assertEquals(GTsubtotals, 24469efe8daSLiu Zhe SCUtil.getFormulaFromCell(currentsheet, 2, 11)); 24569efe8daSLiu Zhe assertEquals(GTsubtotalsString, 24669efe8daSLiu Zhe SCUtil.getTextFromCell(currentsheet, 0, 11)); 24769efe8daSLiu Zhe } 24869efe8daSLiu Zhe } 249