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.XPropertySet;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XNameAccess;
37 import com.sun.star.container.XNameContainer;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.style.XStyleFamiliesSupplier;
40 import com.sun.star.text.ControlCharacter;
41 import com.sun.star.text.XText;
42 import com.sun.star.text.XTextCursor;
43 import com.sun.star.text.XTextDocument;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 /**
50  * Test for object which is represented by service
51  * <code>com.sun.star.text.NumberingRules</code>.<p>
52  * Object implements the following interfaces :
53  * <ul>
54  *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
55  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
56  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
57  *  <li> <code>com::sun::star::text::NumberingRules</code></li>
58  * </ul> <p>
59  * This object test <b> is NOT </b> designed to be run in several
60  * threads concurently.
61  * @see com.sun.star.beans.XPropertySet
62  * @see com.sun.star.container.XIndexAccess
63  * @see com.sun.star.container.XElementAccess
64  * @see com.sun.star.text.NumberingRules
65  * @see ifc.beans._XPropertySet
66  * @see ifc.container._XIndexAccess
67  * @see ifc.container._XElementAccess
68  * @see ifc.text._NumberingRules
69  */
70 public class SwXNumberingRules extends TestCase {
71     XTextDocument xTextDoc;
72 
73     /**
74     * Creates text document.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory 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 some strings are inserted to a text document, then style families
98     * are gotten from text document using <code>XStyleFamiliesSupplier</code>.
99     * Then style family 'NuberingRules' is retrieved, then style indexed '0'
100     * is obtained. At the end property value 'NumberingRules' is obtained from
101     * given style.
102     */
createTestEnvironment(TestParameters tParam, PrintWriter log)103     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
104         XInterface oObj = null;
105         XIndexAccess NumStyleI = null;
106 
107         XText oText = xTextDoc.getText();
108         XTextCursor oCursor = oText.createTextCursor();
109 
110         try {
111             for (int i=0; i<5; i++) {
112                 oText.insertString(oCursor, "The quick brown fox jumps "+
113                     "over the lazy Dog", false);
114                 oText.insertControlCharacter(oCursor,
115                     ControlCharacter.PARAGRAPH_BREAK, false);
116             }
117         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
118             log.println("Error, exception occured...");
119             e.printStackTrace(log);
120         }
121 
122         XStyleFamiliesSupplier oStyleFamiliesSupplier = (XStyleFamiliesSupplier)
123             UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDoc);
124 
125         try {
126             XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
127             XNameContainer NumStyles = (XNameContainer) AnyConverter.toObject(
128                 new Type(XNameContainer.class),
129                     oStyleFamilies.getByName("NumberingStyles"));
130             NumStyleI = (XIndexAccess)
131                 UnoRuntime.queryInterface(XIndexAccess.class,NumStyles);
132         } catch ( com.sun.star.lang.WrappedTargetException e ) {
133             log.println("Error, exception occured...");
134             e.printStackTrace(log);
135         } catch ( com.sun.star.container.NoSuchElementException e ) {
136             log.println("Error, no such style family...");
137             e.printStackTrace(log);
138         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
139             log.println("Error, exception occured...");
140             e.printStackTrace(log);
141         }
142 
143         Object instance1 = null;
144 
145         try {
146             oObj = (XInterface) AnyConverter.toObject(
147                     new Type(XInterface.class),NumStyleI.getByIndex(0));
148             XPropertySet props = (XPropertySet)
149                 UnoRuntime.queryInterface(XPropertySet.class, oObj);
150             oObj = (XInterface) AnyConverter.toObject(
151                 new Type(XInterface.class),props.getPropertyValue("NumberingRules"));
152             XIndexAccess nRules = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, props.getPropertyValue("NumberingRules"));
153             instance1 = nRules.getByIndex(0);
154         } catch ( com.sun.star.lang.WrappedTargetException e ) {
155             log.println("Error, exception occured...");
156             e.printStackTrace(log);
157         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
158             log.println("Error, exception occured...");
159             e.printStackTrace(log);
160         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
161             log.println("Error, exception occured...");
162             e.printStackTrace(log);
163         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
164             log.println("Error, exception occured...");
165             e.printStackTrace(log);
166         }
167 
168         TestEnvironment tEnv = new TestEnvironment( oObj );
169 
170         tEnv.addObjRelation("INSTANCE1", instance1);
171         return tEnv;
172     }
173 
174 }    // finish class SwXNumberingRules
175