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.XEnumeration;
36 import com.sun.star.container.XEnumerationAccess;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.text.ControlCharacter;
39 import com.sun.star.text.XText;
40 import com.sun.star.text.XTextCursor;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 import com.sun.star.util.XCloseable;
47 
48 /**
49  * Test for object which is represented by service
50  * <code>com.sun.star.text.Paragraph</code>. <p>
51  * Object implements the following interfaces :
52  * <ul>
53  *  <li> <code>com::sun::star::text::XTextContent</code></li>
54  *  <li> <code>com::sun::star::text::TextContent</code></li>
55  *  <li> <code>com::sun::star::style::CharacterPropertiesComplex</code></li>
56  *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
57  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
58  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
59  *  <li> <code>com::sun::star::beans::XPropertyState</code></li>
60  *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
61  *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
62  *  <li> <code>com::sun::star::lang::XComponent</code></li>
63  *  <li> <code>com::sun::star::style::CharacterPropertiesAsian</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.text.XTextContent
68  * @see com.sun.star.text.TextContent
69  * @see com.sun.star.style.CharacterPropertiesComplex
70  * @see com.sun.star.beans.XPropertySet
71  * @see com.sun.star.container.XElementAccess
72  * @see com.sun.star.container.XEnumerationAccess
73  * @see com.sun.star.beans.XPropertyState
74  * @see com.sun.star.style.CharacterProperties
75  * @see com.sun.star.style.ParagraphProperties
76  * @see com.sun.star.lang.XComponent
77  * @see com.sun.star.style.CharacterPropertiesAsian
78  * @see com.sun.star.text.Paragraph
79  * @see ifc.text._XTextContent
80  * @see ifc.text._TextContent
81  * @see ifc.style._CharacterPropertiesComplex
82  * @see ifc.beans._XPropertySet
83  * @see ifc.container._XElementAccess
84  * @see ifc.container._XEnumerationAccess
85  * @see ifc.beans._XPropertyState
86  * @see ifc.style._CharacterProperties
87  * @see ifc.style._ParagraphProperties
88  * @see ifc.lang._XComponent
89  * @see ifc.style._CharacterPropertiesAsian
90  */
91 public class SwXParagraph extends TestCase {
92     XTextDocument xTextDoc;
93 
94     /**
95      * Creates text document.
96      */
initialize( TestParameters tParam, PrintWriter log )97     protected void initialize( TestParameters tParam, PrintWriter log ) {
98 
99     }
100 
cleanup(TestParameters tParam, PrintWriter log)101     protected void cleanup(TestParameters tParam, PrintWriter log) {
102         log.println("    disposing xTextDoc ");
103 
104         try {
105             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
106             XCloseable.class, xTextDoc);
107             closer.close(true);
108         } catch (com.sun.star.util.CloseVetoException e) {
109             log.println("couldn't close document");
110         } catch (com.sun.star.lang.DisposedException e) {
111             log.println("couldn't close document");
112         }
113     }
114 
115 
116     /**
117      * Creating a Testenvironment for the interfaces to be tested. To obtain
118      * test component, at first several paragraphs are inserted to a major text
119      * of text document. Then enumeration of text paragraphs is created, and
120      * some paragraph is gotten using <code>XEnumeration</code> interface.<p>
121      *     Object relations created :
122      * <ul>
123      *  <li> <code>'PARA'</code> for
124      *      {@link ifc.style._CharacterProperties} : paragraph </li>
125      *  <li> <code>'PORTION'</code> for
126      *      {@link ifc.style._CharacterProperties} : some text portion of
127      *  paragraph. To obtain text portion, enumeration of paragraph text portions is
128      *  created, and some text portion is gotten using <code>XEnumeration</code>
129      *  interface. </li>
130      *  <li> <code>'NRULES'</code> for
131      *      {@link ifc.style._ParagraphProperties} : value of property
132      *  'NumberingRules' of paragraph. Method changes property
133      *  'NumberingStyleName' of previously obtained paragraph, and gets value of
134      *  a property 'NumberingRules'. </li>
135      * </ul>
136      */
createTestEnvironment(TestParameters tParam, PrintWriter log)137     protected synchronized TestEnvironment createTestEnvironment
138     (TestParameters tParam, PrintWriter log) {
139 
140         if (xTextDoc != null) {
141             log.println("    disposing xTextDoc ");
142 
143             try {
144                 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
145                 XCloseable.class, xTextDoc);
146                 closer.close(true);
147             } catch (com.sun.star.util.CloseVetoException e) {
148                 log.println("couldn't close document");
149             } catch (com.sun.star.lang.DisposedException e) {
150                 log.println("couldn't close document");
151             }
152         }
153         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) tParam.getMSF() );
154         try {
155             log.println( "creating a textdocument" );
156             xTextDoc = SOF.createTextDoc( null );
157         } catch ( com.sun.star.uno.Exception e ) {
158             e.printStackTrace( log );
159             throw new StatusException( "Couldn't create document", e );
160         }
161 
162         XInterface oObj = null;
163         XPropertySet paraP = null;
164         XPropertySet portP = null;
165         Object nRules = null;
166         XInterface port = null;
167         XInterface para = null;
168 
169         log.println( "creating a test environment" );
170 
171 
172         XText oText = xTextDoc.getText();
173         XTextCursor oCursor = oText.createTextCursor();
174 
175         log.println( "inserting some lines" );
176         try {
177             for (int i=0; i<5; i++){
178                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
179                 oText.insertString( oCursor,
180                 " The quick brown fox jumps over the lazy Dog: SwXParagraph",
181                 false);
182                 oText.insertControlCharacter(
183                 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
184                 oText.insertString( oCursor,
185                 "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
186                 false);
187                 oText.insertControlCharacter(oCursor,
188                 ControlCharacter.PARAGRAPH_BREAK, false );
189                 oText.insertControlCharacter(
190                 oCursor, ControlCharacter.LINE_BREAK, false );
191             }
192         } catch ( com.sun.star.lang.IllegalArgumentException e ){
193             e.printStackTrace(log);
194             throw new StatusException( "Couldn't insert lines", e );
195         }
196 
197         // Enumeration
198         XEnumerationAccess oEnumA = (XEnumerationAccess)
199         UnoRuntime.queryInterface(XEnumerationAccess.class, oText );
200         XEnumeration oEnum = oEnumA.createEnumeration();
201 
202         try {
203             para = (XInterface) AnyConverter.toObject(
204             new Type(XInterface.class),oEnum.nextElement());
205             XEnumerationAccess oEnumB = (XEnumerationAccess)
206             UnoRuntime.queryInterface( XEnumerationAccess.class, para );
207             XEnumeration oEnum2 = oEnumB.createEnumeration();
208             port = (XInterface) AnyConverter.toObject(
209             new Type(XInterface.class),oEnum2.nextElement());
210         } catch ( com.sun.star.lang.WrappedTargetException e ) {
211             e.printStackTrace(log);
212             log.println("Error: exception occurred...");
213         } catch ( com.sun.star.container.NoSuchElementException e ) {
214             e.printStackTrace(log);
215             log.println("Error: exception occurred...");
216         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
217             e.printStackTrace(log);
218             log.println("Error: exception occurred...");
219         }
220 
221         try {
222             portP = (XPropertySet)
223             UnoRuntime.queryInterface(XPropertySet.class, port);
224             paraP = (XPropertySet)
225             UnoRuntime.queryInterface(XPropertySet.class, para);
226             paraP.setPropertyValue("NumberingStyleName","Numbering 4");
227             nRules = paraP.getPropertyValue("NumberingRules");
228         } catch ( com.sun.star.lang.WrappedTargetException e ) {
229             log.println("Error, exception occurred...");
230             e.printStackTrace(log);
231             throw new StatusException( "Couldn't get Paragraph", e );
232         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
233             log.println("Error, exception occurred...");
234             e.printStackTrace(log);
235             throw new StatusException( "Couldn't get Paragraph", e );
236         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
237             log.println("Error, exception occurred...");
238             e.printStackTrace(log);
239             throw new StatusException( "Couldn't get Paragraph", e );
240         } catch ( com.sun.star.beans.PropertyVetoException e ) {
241             log.println("Error, exception occurred...");
242             e.printStackTrace(log);
243             throw new StatusException( "Couldn't get Paragraph", e );
244         }
245 
246 
247         try {
248             oObj = (XInterface) AnyConverter.toObject(
249             new Type(XInterface.class),oEnum.nextElement());
250         } catch ( Exception e) {
251             log.println("Error, exception occurred...");
252             e.printStackTrace(log);
253             throw new StatusException( "Couldn't get Paragraph", e );
254         }
255 
256 
257         log.println( "creating a new environment for Paragraph object" );
258         TestEnvironment tEnv = new TestEnvironment( oObj );
259 
260         log.println("adding ObjectRelation 'PARA' for CharacterProperties");
261         tEnv.addObjRelation("PARA", paraP);
262 
263         log.println("adding ObjectRelation 'PORTION' for CharacterProperties");
264         tEnv.addObjRelation("PORTION", portP);
265 
266         log.println("adding ObjectRelation 'NRULES' for ParagraphProperties");
267         tEnv.addObjRelation("NRULES", nRules);
268 
269         tEnv.addObjRelation("NoAttach","SwXParagraph");
270 
271         return tEnv;
272     } // finish method getTestEnvironment
273 
274 }    // finish class SwXParagraph
275 
276