1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.container.XIndexAccess;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.sheet.XSpreadsheet;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.sheet.XSpreadsheets;
45 import com.sun.star.uno.AnyConverter;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.sheet.TableValidation</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 *  <li> <code>com::sun::star::sheet::TableValidation</code></li>
56 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
57 *  <li> <code>com::sun::star::sheet::XSheetCondition</code></li>
58 * </ul>
59 * @see com.sun.star.sheet.TableValidation
60 * @see com.sun.star.beans.XPropertySet
61 * @see com.sun.star.sheet.XSheetCondition
62 * @see ifc.sheet._TableValidation
63 * @see ifc.beans._XPropertySet
64 * @see ifc.sheet._XSheetCondition
65 */
66 public class ScTableValidationObj extends TestCase {
67     static XSpreadsheetDocument xSpreadsheetDoc = null;
68 
69     /**
70     * Creates Spreadsheet document.
71     */
72     protected void initialize( TestParameters tParam, PrintWriter log ) {
73         // get a soffice factory object
74         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
75 
76         try {
77             log.println( "creating a sheetdocument" );
78             xSpreadsheetDoc = SOF.createCalcDoc(null);;
79         } catch (com.sun.star.uno.Exception e ) {
80             // Some exception occures.FAILED
81             e.printStackTrace( log );
82             throw new StatusException( "Couldn't create document", e );
83         }
84     }
85 
86     /**
87     * Disposes Spreadsheet document.
88     */
89     protected void cleanup( TestParameters tParam, PrintWriter log ) {
90         log.println( "    disposing xSheetDoc " );
91         XComponent oComp = (XComponent)
92             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
93         util.DesktopTools.closeDoc(oComp);
94     }
95 
96     /**
97     * Creating a Testenvironment for the interfaces to be tested.
98     * Retrieves a collection of spreadsheets from the document and takes one of
99     * them. Fills some cell in the spreadsheet. Obtains the property
100     * <code>'Validation'</code> of the spreadsheet. The value of the property
101     * is the instance of the service <code>com.sun.star.sheet.TableValidation</code>.
102     * @see com.sun.star.sheet.TableValidation
103     */
104     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
105 
106         XInterface oObj = null;
107 
108         log.println("getting sheets");
109         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
110 
111         log.println("getting a sheet");
112         XSpreadsheet oSheet = null;
113         XIndexAccess oIndexAccess = (XIndexAccess)
114             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
115         try {
116             oSheet = (XSpreadsheet) AnyConverter.toObject(
117                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
118         } catch (com.sun.star.lang.WrappedTargetException e) {
119             e.printStackTrace(log);
120             throw new StatusException( "Couldn't get a spreadsheet", e);
121         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
122             e.printStackTrace(log);
123             throw new StatusException( "Couldn't get a spreadsheet", e);
124         } catch (com.sun.star.lang.IllegalArgumentException e) {
125             e.printStackTrace(log);
126             throw new StatusException( "Couldn't get a spreadsheet", e);
127         }
128 
129         log.println("filling some cells");
130         try {
131             oSheet.getCellByPosition(5, 5).setValue(15);
132             oSheet.getCellByPosition(1, 4).setValue(10);
133             oSheet.getCellByPosition(2, 0).setValue(-5.15);
134         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
135             e.printStackTrace(log);
136             throw new StatusException(
137                 "Exception occurred while filling cells", e);
138         }
139 
140         XPropertySet Props = null;
141 
142         try {
143             Props = (XPropertySet)
144                 UnoRuntime.queryInterface(XPropertySet.class, oSheet);
145             oObj = (XInterface) AnyConverter.toObject(
146                 new Type(XInterface.class),Props.getPropertyValue("Validation"));
147         } catch (com.sun.star.lang.WrappedTargetException e){
148             e.printStackTrace(log);
149             throw new StatusException("Couldn't get property 'Validation'", e);
150         } catch (com.sun.star.beans.UnknownPropertyException e){
151             e.printStackTrace(log);
152             throw new StatusException("Couldn't get property 'Validation'", e);
153         } catch (com.sun.star.lang.IllegalArgumentException e){
154             e.printStackTrace(log);
155             throw new StatusException("Couldn't get property 'Validation'", e);
156         }
157 
158         log.println("creating a new environment for object");
159         TestEnvironment tEnv = new TestEnvironment(oObj);
160 
161         return tEnv;
162     } // finish method getTestEnvironment
163 }    // finish class ScTableValidationObj
164 
165