1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XSheetAnnotation;
38 import com.sun.star.sheet.XSheetAnnotationAnchor;
39 import com.sun.star.sheet.XSheetAnnotationsSupplier;
40 import com.sun.star.sheet.XSpreadsheet;
41 import com.sun.star.sheet.XSpreadsheetDocument;
42 import com.sun.star.sheet.XSpreadsheets;
43 import com.sun.star.table.XCell;
44 import com.sun.star.table.XCellRange;
45 import com.sun.star.text.XSimpleText;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 
51 /**
52 * Test for object which represents a collection of annotations
53 * for a spreadsheet document (implements
54 * <code>com.sun.star.sheet.CellAnnotations</code>). <p>
55 * Object implements the following interfaces :
56 * <ul>
57 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
58 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
59 *  <li> <code>com::sun::star::sheet::XSheetAnnotations</code></li>
60 * </ul> <p>
61 * This object test <b> is NOT </b> designed to be run in several
62 * threads concurently.
63 * @see com.sun.star.sheet.CellAnnotations
64 * @see com.sun.star.container.XIndexAccess
65 * @see com.sun.star.container.XElementAccess
66 * @see com.sun.star.sheet.XSheetAnnotations
67 * @see ifc.container._XIndexAccess
68 * @see ifc.container._XElementAccess
69 * @see ifc.sheet._XSheetAnnotations
70 */
71 public class ScAnnotationsObj extends TestCase {
72     static XSpreadsheetDocument xSheetDoc = null;
73 
74     /**
75     * Creates Spreadsheet document.
76     */
initialize( TestParameters tParam, PrintWriter log )77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79 
80         try {
81             log.println( "creating a Spreadsheet document" );
82             xSheetDoc = SOF.createCalcDoc(null);
83         } catch ( com.sun.star.uno.Exception e ) {
84             // Some exception occured.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88 
89     }
90 
91     /**
92     * Disposes Spreadsheet document.
93     */
cleanup( TestParameters tParam, PrintWriter log )94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing xSheetDoc " );
96         XComponent oComp = (XComponent)
97             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
98         util.DesktopTools.closeDoc(oComp);
99     }
100 
101     /**
102     * Creating a Testenvironment for the interfaces to be tested.
103     * From a document collection of spreadsheets a single one is
104     * retrieved and one annotation is added to it. Then a collection
105     * of annotations is retrieved using spreadsheet's
106     * <code>com.sun.star.sheet.XSheetAnnotationsSupplier</code> interface.
107     */
createTestEnvironment(TestParameters Param, PrintWriter log)108     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
109 
110         XInterface oObj = null;
111 
112         // creation of testobject here
113         // first we write what we are intend to do to log file
114         log.println( "Creating a test environment" );
115 
116         log.println("Getting test object ") ;
117 
118         XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
119             UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
120         XSpreadsheets sheets = (XSpreadsheets) xSpreadsheetDoc.getSheets();
121 
122         XNameAccess oNames = (XNameAccess)
123             UnoRuntime.queryInterface( XNameAccess.class, sheets );
124         XCell oCell = null;
125         XSpreadsheet oSheet  = null;
126         try {
127             oSheet = (XSpreadsheet) AnyConverter.toObject(
128                     new Type(XSpreadsheet.class),
129                         oNames.getByName(oNames.getElementNames()[0]));
130             // adding an annotation...
131             XCellRange oCRange = (XCellRange)
132                 UnoRuntime.queryInterface(XCellRange.class, oSheet);
133             oCell = oCRange.getCellByPosition(10,10);
134         } catch (com.sun.star.lang.WrappedTargetException e) {
135             e.printStackTrace(log);
136             throw new StatusException(
137                 "Error getting test object from spreadsheet document",e);
138         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
139             e.printStackTrace(log) ;
140             throw new StatusException(
141                 "Error getting test object from spreadsheet document",e) ;
142         } catch (com.sun.star.container.NoSuchElementException e) {
143             e.printStackTrace(log) ;
144             throw new StatusException(
145                 "Error getting test object from spreadsheet document",e) ;
146         } catch (com.sun.star.lang.IllegalArgumentException e) {
147             e.printStackTrace(log) ;
148             throw new StatusException(
149                 "Error getting test object from spreadsheet document",e) ;
150         }
151 
152         XSheetAnnotationAnchor oAnnoA = (XSheetAnnotationAnchor)
153             UnoRuntime.queryInterface(XSheetAnnotationAnchor.class, oCell);
154         XSheetAnnotation oAnno = oAnnoA.getAnnotation();
155         XSimpleText sText = ((XSimpleText)
156             UnoRuntime.queryInterface(XSimpleText.class, oAnno));
157         sText.setString("ScAnnotationsObj");
158 
159         XSheetAnnotationsSupplier supp = (XSheetAnnotationsSupplier)
160             UnoRuntime.queryInterface(
161                 XSheetAnnotationsSupplier.class, oSheet);
162         oObj = supp.getAnnotations();
163 
164         TestEnvironment tEnv = new TestEnvironment( oObj );
165 
166         return tEnv;
167     }
168 
169 }    // finish class ScAnnotationsObj
170 
171 
172