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