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._sw;
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.beans.Property;
35 import com.sun.star.beans.PropertyAttribute;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.container.XIndexAccess;
38 import com.sun.star.container.XNameAccess;
39 import com.sun.star.container.XNameContainer;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.style.XStyle;
42 import com.sun.star.style.XStyleFamiliesSupplier;
43 import com.sun.star.text.XText;
44 import com.sun.star.text.XTextCursor;
45 import com.sun.star.text.XTextDocument;
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 is represented by service
53  * <code>com.sun.star.style.Style</code>. <p>
54  * Object implements the following interfaces :
55  * <ul>
56  *  <li> <code>com::sun::star::container::XNamed</code></li>
57  *  <li> <code>com::sun::star::style::Style</code></li>
58  *  <li> <code>com::sun::star::style::XStyle</code></li>
59  * </ul> <p>
60  * This object test <b> is NOT </b> designed to be run in several
61  * threads concurently.
62  * @see com.sun.star.container.XNamed
63  * @see com.sun.star.style.Style
64  * @see com.sun.star.style.XStyle
65  * @see ifc.container._XNamed
66  * @see ifc.style._Style
67  * @see ifc.style._XStyle
68  */
69 public class SwXStyle extends TestCase {
70     XTextDocument xTextDoc;
71     SOfficeFactory SOF = null;
72 
73     /**
74     * Creates text document.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78         try {
79             log.println( "creating a textdocument" );
80             xTextDoc = SOF.createTextDoc( null );
81         } catch ( com.sun.star.uno.Exception e ) {
82             e.printStackTrace( log );
83             throw new StatusException( "Couldn't create document", e );
84         }
85     }
86 
87     /**
88     * Disposes text document.
89     */
cleanup( TestParameters tParam, PrintWriter log )90     protected void cleanup( TestParameters tParam, PrintWriter log ) {
91         log.println( "    disposing xTextDoc " );
92         util.DesktopTools.closeDoc(xTextDoc);
93     }
94 
95     /**
96     * Creating a Testenvironment for the interfaces to be tested.
97     * At first style families are gotten from a text document using
98     * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
99     * gotten from this style family using <code>XIndexAccess</code> interface.
100     * Next, method creates an instance of the service
101     * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
102     * previously obtained style family using <code>XNameContainer</code>
103     * interface. Finally, method creates a cursor of a major text of text
104     * document and sets it's property 'CharStyleName' value to the name of
105     * previously created style's name.
106     *     Object relations created :
107     * <ul>
108     *  <li> <code>'PoolStyle'</code> for
109     *      {@link ifc.style._XStyle} : slyle indexed '10' obtained from
110     *  StyleFamily indexed '0' from text document using
111     *  <code>XIndexAccess</code> interface.</li>
112     * </ul>
113     */
createTestEnvironment(TestParameters Param, PrintWriter log)114     protected synchronized TestEnvironment createTestEnvironment
115             (TestParameters Param, PrintWriter log) {
116 
117         TestEnvironment tEnv = null;
118         XNameAccess oSFNA = null;
119         XStyle oStyle = null;
120         XStyle oMyStyle = null;
121 
122         log.println("creating a test environment");
123 
124         try {
125             log.println("getting style");
126             XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
127                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
128                 xTextDoc);
129             XNameAccess oSF = oSFS.getStyleFamilies();
130             XIndexAccess oSFsIA = (XIndexAccess)
131                 UnoRuntime.queryInterface(XIndexAccess.class, oSF);
132             oSFNA = (XNameAccess) AnyConverter.toObject(
133                         new Type(XNameAccess.class),oSFsIA.getByIndex(0));
134             XIndexAccess oSFIA = (XIndexAccess)
135                 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
136             oStyle = (XStyle) AnyConverter.toObject(
137                     new Type(XStyle.class),oSFIA.getByIndex(10));
138         } catch ( com.sun.star.lang.WrappedTargetException e ) {
139             log.println("Error: exception occurred.");
140             e.printStackTrace(log);
141             throw new StatusException( "Couldn't create environment ", e );
142         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
143             log.println("Error: exception occurred.");
144             e.printStackTrace(log);
145             throw new StatusException( "Couldn't create environment ", e );
146         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
147             log.println("Error: exception occurred.");
148             e.printStackTrace(log);
149             throw new StatusException( "Couldn't create environment ", e );
150         }
151 
152         try {
153             log.print("Creating a user-defined style... ");
154             XMultiServiceFactory oMSF = (XMultiServiceFactory)
155                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
156             XInterface oInt = (XInterface)
157                 oMSF.createInstance("com.sun.star.style.CharacterStyle");
158             oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
159         } catch ( com.sun.star.uno.Exception e ) {
160             log.println("Error: exception occurred.");
161             e.printStackTrace(log);
162             throw new StatusException( "Couldn't create environment ", e );
163         }
164 
165 
166         if (oMyStyle == null)
167             log.println("FAILED");
168         else
169             log.println("OK");
170             XNameContainer oSFNC = (XNameContainer)
171             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
172 
173         try {
174             if ( oSFNC.hasByName("My Style") )
175                 oSFNC.removeByName("My Style");
176             oSFNC.insertByName("My Style", oMyStyle);
177         } catch ( com.sun.star.lang.WrappedTargetException e ) {
178             e.printStackTrace(log);
179             throw new StatusException( "Couldn't create environment ", e );
180         } catch     ( com.sun.star.lang.IllegalArgumentException e ) {
181             e.printStackTrace(log);
182             throw new StatusException( "Couldn't create environment ", e );
183         } catch ( com.sun.star.container.NoSuchElementException e ) {
184             e.printStackTrace(log);
185             throw new StatusException( "Couldn't create environment ", e );
186         } catch ( com.sun.star.container.ElementExistException e ) {
187             e.printStackTrace(log);
188             throw new StatusException( "Couldn't create environment ", e );
189         }
190 
191         XText oText = xTextDoc.getText();
192         XTextCursor oCursor = oText.createTextCursor();
193         XPropertySet xProp = (XPropertySet)
194             UnoRuntime.queryInterface(XPropertySet.class, oCursor);
195 
196         try {
197             xProp.setPropertyValue("CharStyleName", oMyStyle.getName());
198         } catch ( com.sun.star.lang.WrappedTargetException e ) {
199             e.printStackTrace( log );
200             throw new StatusException( "Couldn't create environment ", e );
201         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
202             e.printStackTrace( log );
203             throw new StatusException( "Couldn't create environment ", e );
204         } catch ( com.sun.star.beans.PropertyVetoException e ) {
205             e.printStackTrace( log );
206             throw new StatusException( "Couldn't create environment ", e );
207         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
208             e.printStackTrace( log );
209             throw new StatusException( "Couldn't create environment ", e );
210         }
211 
212         log.println("creating a new environment for object");
213         tEnv = new TestEnvironment(oMyStyle);
214         tEnv.addObjRelation("PoolStyle", oStyle);
215 
216         XPropertySet xStyleProp = (XPropertySet)
217             UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
218         tEnv.addObjRelation("PropertyNames",getPropertyNames(xStyleProp));
219 
220         return tEnv;
221     }
222 
getPropertyNames(XPropertySet props)223     public String[] getPropertyNames(XPropertySet props) {
224         Property[] the_props = props.getPropertySetInfo().getProperties();
225         String[] names = new String[the_props.length];
226         String placebo = "";
227         for (int i=0;i<the_props.length;i++) {
228             boolean isWritable =
229                 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
230             if (isWritable) placebo=the_props[i].Name;
231         }
232         for (int i=0;i<the_props.length;i++) {
233             boolean isWritable =
234                 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
235             if (isWritable) {
236                 names[i]=the_props[i].Name;
237             } else {
238                 names[i] = placebo;
239             }
240         }
241         return names;
242     }
243 
244 }    // finish class SwXStyle
245