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.container.XNameAccess;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.sheet.XSheetAnnotation;
42 import com.sun.star.sheet.XSheetAnnotationAnchor;
43 import com.sun.star.sheet.XSheetAnnotationsSupplier;
44 import com.sun.star.sheet.XSpreadsheet;
45 import com.sun.star.sheet.XSpreadsheetDocument;
46 import com.sun.star.sheet.XSpreadsheets;
47 import com.sun.star.table.XCell;
48 import com.sun.star.table.XCellRange;
49 import com.sun.star.text.XSimpleText;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 
55 /**
56 * Test for object which represents a collection of annotations
57 * for a spreadsheet document (implements
58 * <code>com.sun.star.sheet.CellAnnotations</code>). <p>
59 * Object implements the following interfaces :
60 * <ul>
61 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
62 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
63 *  <li> <code>com::sun::star::sheet::XSheetAnnotations</code></li>
64 * </ul> <p>
65 * This object test <b> is NOT </b> designed to be run in several
66 * threads concurently.
67 * @see com.sun.star.sheet.CellAnnotations
68 * @see com.sun.star.container.XIndexAccess
69 * @see com.sun.star.container.XElementAccess
70 * @see com.sun.star.sheet.XSheetAnnotations
71 * @see ifc.container._XIndexAccess
72 * @see ifc.container._XElementAccess
73 * @see ifc.sheet._XSheetAnnotations
74 */
75 public class ScAnnotationsObj extends TestCase {
76     static XSpreadsheetDocument xSheetDoc = null;
77 
78     /**
79     * Creates Spreadsheet document.
80     */
81     protected void initialize( TestParameters tParam, PrintWriter log ) {
82         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
83 
84         try {
85             log.println( "creating a Spreadsheet document" );
86             xSheetDoc = SOF.createCalcDoc(null);
87         } catch ( com.sun.star.uno.Exception e ) {
88             // Some exception occures.FAILED
89             e.printStackTrace( log );
90             throw new StatusException( "Couldn't create document", e );
91         }
92 
93     }
94 
95     /**
96     * Disposes Spreadsheet document.
97     */
98     protected void cleanup( TestParameters tParam, PrintWriter log ) {
99         log.println( "    disposing xSheetDoc " );
100         XComponent oComp = (XComponent)
101             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
102         util.DesktopTools.closeDoc(oComp);
103     }
104 
105     /**
106     * Creating a Testenvironment for the interfaces to be tested.
107     * From a document collection of spreadsheets a single one is
108     * retrieved and one annotation is added to it. Then a collection
109     * of annotations is retrieved using spreadsheet's
110     * <code>com.sun.star.sheet.XSheetAnnotationsSupplier</code> interface.
111     */
112     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
113 
114         XInterface oObj = null;
115 
116         // creation of testobject here
117         // first we write what we are intend to do to log file
118         log.println( "Creating a test environment" );
119 
120         log.println("Getting test object ") ;
121 
122         XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
123             UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
124         XSpreadsheets sheets = (XSpreadsheets) xSpreadsheetDoc.getSheets();
125 
126         XNameAccess oNames = (XNameAccess)
127             UnoRuntime.queryInterface( XNameAccess.class, sheets );
128         XCell oCell = null;
129         XSpreadsheet oSheet  = null;
130         try {
131             oSheet = (XSpreadsheet) AnyConverter.toObject(
132                     new Type(XSpreadsheet.class),
133                         oNames.getByName(oNames.getElementNames()[0]));
134             // adding an annotation...
135             XCellRange oCRange = (XCellRange)
136                 UnoRuntime.queryInterface(XCellRange.class, oSheet);
137             oCell = oCRange.getCellByPosition(10,10);
138         } catch (com.sun.star.lang.WrappedTargetException e) {
139             e.printStackTrace(log);
140             throw new StatusException(
141                 "Error getting test object from spreadsheet document",e);
142         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143             e.printStackTrace(log) ;
144             throw new StatusException(
145                 "Error getting test object from spreadsheet document",e) ;
146         } catch (com.sun.star.container.NoSuchElementException e) {
147             e.printStackTrace(log) ;
148             throw new StatusException(
149                 "Error getting test object from spreadsheet document",e) ;
150         } catch (com.sun.star.lang.IllegalArgumentException e) {
151             e.printStackTrace(log) ;
152             throw new StatusException(
153                 "Error getting test object from spreadsheet document",e) ;
154         }
155 
156         XSheetAnnotationAnchor oAnnoA = (XSheetAnnotationAnchor)
157             UnoRuntime.queryInterface(XSheetAnnotationAnchor.class, oCell);
158         XSheetAnnotation oAnno = oAnnoA.getAnnotation();
159         XSimpleText sText = ((XSimpleText)
160             UnoRuntime.queryInterface(XSimpleText.class, oAnno));
161         sText.setString("ScAnnotationsObj");
162 
163         XSheetAnnotationsSupplier supp = (XSheetAnnotationsSupplier)
164             UnoRuntime.queryInterface(
165                 XSheetAnnotationsSupplier.class, oSheet);
166         oObj = supp.getAnnotations();
167 
168         TestEnvironment tEnv = new TestEnvironment( oObj );
169 
170         return tEnv;
171     }
172 
173 }    // finish class ScAnnotationsObj
174 
175 
176