1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XGoalSeek.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.container
.XIndexAccess
;
33 import com
.sun
.star
.sheet
.GoalResult
;
34 import com
.sun
.star
.sheet
.XGoalSeek
;
35 import com
.sun
.star
.sheet
.XSpreadsheet
;
36 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
37 import com
.sun
.star
.sheet
.XSpreadsheets
;
38 import com
.sun
.star
.table
.CellAddress
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import lib
.MultiMethodTest
;
41 import lib
.StatusException
;
46 public class _XGoalSeek
extends MultiMethodTest
{
47 public XGoalSeek oObj
= null;
48 XSpreadsheet xSheet
= null;
49 CellAddress aFormula
= null;
50 CellAddress aValue
= null;
52 public void before() {
56 XSpreadsheetDocument xSpreadsheetDocument
= (XSpreadsheetDocument
)
57 UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, oObj
);
58 XSpreadsheets oSheets
= xSpreadsheetDocument
.getSheets();
59 XIndexAccess oIndexSheets
= (XIndexAccess
) UnoRuntime
.queryInterface(
60 XIndexAccess
.class, oSheets
);
61 xSheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(
62 XSpreadsheet
.class, oIndexSheets
.getByIndex(1));
64 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
67 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
70 catch(java
.lang
.NullPointerException e
) {
74 throw new StatusException("Could not get a sheet.", ex
);
77 // set value and formula
79 xSheet
.getCellByPosition(3, 4).setValue(9);
80 xSheet
.getCellByPosition(3, 5).setFormula("= SQRT(D5)");
81 aValue
= new CellAddress((short)1, 3, 4);
82 aFormula
= new CellAddress((short)1, 3, 5);
85 throw new StatusException("Could not get set formulas on the sheet.", e
);
89 public void _seekGoal() {
90 boolean result
= true;
91 double divergence
= 0.01;
92 GoalResult goal
= oObj
.seekGoal(aFormula
, aValue
, "4");
93 log
.println("Goal Result: " + goal
.Result
+ " Divergence: " + goal
.Divergence
);
94 result
&= goal
.Divergence
< divergence
;
95 result
&= goal
.Result
> 16 - divergence
|| goal
.Result
< 16 + divergence
;
97 goal
= oObj
.seekGoal(aFormula
, aValue
, "-4");
98 log
.println("Goal Result: " + goal
.Result
+ " Divergence: " + goal
.Divergence
);
99 result
&= goal
.Divergence
> 1/divergence
;
100 result
&= goal
.Result
< divergence
|| goal
.Result
> -divergence
;
102 // just curious: let goal seek find a limiting value
104 xSheet
.getCellByPosition(3, 4).setValue(0.8);
105 xSheet
.getCellByPosition(3, 5).setFormula("= (D5 ^ 2 - 1) / (D5 - 1)");
107 catch(Exception e
) {}
108 goal
= oObj
.seekGoal(aFormula
, aValue
, "2");
109 log
.println("Goal Result: " + goal
.Result
+ " Divergence: " + goal
.Divergence
);
110 result
&= goal
.Divergence
< divergence
;
111 result
&= goal
.Result
> 16 - divergence
|| goal
.Result
< 16 + divergence
;
113 tRes
.tested("seekGoal()", result
);