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 package mod._sc;
28 
29 import java.io.PrintWriter;
30 
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.SOfficeFactory;
36 
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.uno.AnyConverter;
41 import com.sun.star.uno.Type;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 
45 /**
46 * Test for object which is represented by service
47 * <code>com.sun.star.TableAutoFormats</code>. <p>
48 * In StarCalc application there is a collection of autoformats
49 * for tables (you can select a predefined format for a
50 * table or create your own). The object represents
51 * this collection. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
55 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
56 *  <li> <code>com::sun::star::container::XNameReplace</code></li>
57 *  <li> <code>com::sun::star::container::XNameContainer</code></li>
58 * </ul>
59 * This object test <b> is NOT </b> designed to be run in several
60 * threads concurently.
61 * @see com.sun.star.container.XNameAccess
62 * @see com.sun.star.container.XElementAccess
63 * @see com.sun.star.container.XNameReplace
64 * @see com.sun.star.container.XNameContainer
65 * @see ifc.container._XNameAccess
66 * @see ifc.container._XElementAccess
67 * @see ifc.container._XNameReplace
68 * @see ifc.container._XNameContainer
69 */
70 public class ScAutoFormatsObj extends TestCase{
71     static XSpreadsheetDocument xSheetDoc = null;
72     static SOfficeFactory SOF = null;
73 
74     /**
75     * Creates Spreadsheet document.
76     */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         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 occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91     * Disposes Spreadsheet document.
92     */
93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xSheetDoc " );
95         XComponent oComp = (XComponent) UnoRuntime.queryInterface
96             (XComponent.class, xSheetDoc) ;
97         util.DesktopTools.closeDoc(oComp);
98     }
99 
100 
101     /**
102     * Creating a Testenvironment for the interfaces to be tested.
103     * Creates an instance of the service
104     * <code>com.sun.star.sheet.TableAutoFormats</code>.
105     *     Object relations created :
106     * <ul>
107     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
108     *      {@link ifc.container._XNameContainer},
109     *      {@link ifc.container._XNameReplace}  N relations
110     *   which represents objects to be inserted - one
111     *   object for each interface thread. </li>
112     * </ul>
113     */
114     public synchronized TestEnvironment createTestEnvironment
115             (TestParameters Param, PrintWriter log )
116             throws StatusException {
117 
118         XInterface oObj = null;
119 
120         try {
121             // creation of testobject here
122             // get AutoFormats
123             XComponent xComp = (XComponent)UnoRuntime.queryInterface
124                 (XComponent.class, xSheetDoc);
125             oObj = (XInterface) AnyConverter.toObject(
126                 new Type(XInterface.class),((XMultiServiceFactory)Param.getMSF()).createInstance
127                                     ("com.sun.star.sheet.TableAutoFormats"));
128             Object secondInstance = SOF.createInstance
129                 (xComp, "com.sun.star.sheet.TableAutoFormat");
130 
131             TestEnvironment tEnv = new TestEnvironment(oObj) ;
132 
133             //adding ObjRelation for XNameContainer
134             tEnv.addObjRelation("SecondInstance",secondInstance);
135 
136             // INSTANCEn : _XNameContainer; _XNameReplace
137             log.println( "adding INSTANCEn as mod relation to environment" );
138             int THRCNT = 1;
139             if ((String)Param.get("THRCNT") != null) {
140                 Integer.parseInt((String)Param.get("THRCNT"));
141             }
142             for (int n = 1; n < (THRCNT+1) ;n++ ) {
143                 log.println( "adding INSTANCE" + n
144                     +" as mod relation to environment" );
145                 tEnv.addObjRelation("INSTANCE" + n, SOF.createInstance(xComp,
146                     "com.sun.star.sheet.TableAutoFormat"));
147             }
148 
149             return tEnv;
150         } catch (com.sun.star.uno.Exception e) {
151             log.println ("Exception occured while creating test Object.");
152             e.printStackTrace(log);
153             throw new StatusException("Couldn't create test object", e);
154         }
155     }
156 
157 }    // finish class ScAutoFormatsObj
158 
159