1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package mod._sw;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.PrintWriter;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir import lib.TestCase;
34*cdf0e10cSrcweir import lib.TestEnvironment;
35*cdf0e10cSrcweir import lib.TestParameters;
36*cdf0e10cSrcweir import util.SOfficeFactory;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
39*cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
40*cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
41*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
42*cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
43*cdf0e10cSrcweir import com.sun.star.text.XText;
44*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
45*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
46*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
47*cdf0e10cSrcweir import com.sun.star.uno.Type;
48*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
49*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
50*cdf0e10cSrcweir import com.sun.star.util.XCloseable;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir /**
53*cdf0e10cSrcweir  * Test for object which is represented by service
54*cdf0e10cSrcweir  * <code>com.sun.star.text.Paragraph</code>. <p>
55*cdf0e10cSrcweir  * Object implements the following interfaces :
56*cdf0e10cSrcweir  * <ul>
57*cdf0e10cSrcweir  *  <li> <code>com::sun::star::text::XTextContent</code></li>
58*cdf0e10cSrcweir  *  <li> <code>com::sun::star::text::TextContent</code></li>
59*cdf0e10cSrcweir  *  <li> <code>com::sun::star::style::CharacterPropertiesComplex</code></li>
60*cdf0e10cSrcweir  *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
61*cdf0e10cSrcweir  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
62*cdf0e10cSrcweir  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
63*cdf0e10cSrcweir  *  <li> <code>com::sun::star::beans::XPropertyState</code></li>
64*cdf0e10cSrcweir  *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
65*cdf0e10cSrcweir  *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
66*cdf0e10cSrcweir  *  <li> <code>com::sun::star::lang::XComponent</code></li>
67*cdf0e10cSrcweir  *  <li> <code>com::sun::star::style::CharacterPropertiesAsian</code></li>
68*cdf0e10cSrcweir  * </ul> <p>
69*cdf0e10cSrcweir  * This object test <b> is NOT </b> designed to be run in several
70*cdf0e10cSrcweir  * threads concurently.
71*cdf0e10cSrcweir  * @see com.sun.star.text.XTextContent
72*cdf0e10cSrcweir  * @see com.sun.star.text.TextContent
73*cdf0e10cSrcweir  * @see com.sun.star.style.CharacterPropertiesComplex
74*cdf0e10cSrcweir  * @see com.sun.star.beans.XPropertySet
75*cdf0e10cSrcweir  * @see com.sun.star.container.XElementAccess
76*cdf0e10cSrcweir  * @see com.sun.star.container.XEnumerationAccess
77*cdf0e10cSrcweir  * @see com.sun.star.beans.XPropertyState
78*cdf0e10cSrcweir  * @see com.sun.star.style.CharacterProperties
79*cdf0e10cSrcweir  * @see com.sun.star.style.ParagraphProperties
80*cdf0e10cSrcweir  * @see com.sun.star.lang.XComponent
81*cdf0e10cSrcweir  * @see com.sun.star.style.CharacterPropertiesAsian
82*cdf0e10cSrcweir  * @see com.sun.star.text.Paragraph
83*cdf0e10cSrcweir  * @see ifc.text._XTextContent
84*cdf0e10cSrcweir  * @see ifc.text._TextContent
85*cdf0e10cSrcweir  * @see ifc.style._CharacterPropertiesComplex
86*cdf0e10cSrcweir  * @see ifc.beans._XPropertySet
87*cdf0e10cSrcweir  * @see ifc.container._XElementAccess
88*cdf0e10cSrcweir  * @see ifc.container._XEnumerationAccess
89*cdf0e10cSrcweir  * @see ifc.beans._XPropertyState
90*cdf0e10cSrcweir  * @see ifc.style._CharacterProperties
91*cdf0e10cSrcweir  * @see ifc.style._ParagraphProperties
92*cdf0e10cSrcweir  * @see ifc.lang._XComponent
93*cdf0e10cSrcweir  * @see ifc.style._CharacterPropertiesAsian
94*cdf0e10cSrcweir  */
95*cdf0e10cSrcweir public class SwXParagraph extends TestCase {
96*cdf0e10cSrcweir     XTextDocument xTextDoc;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     /**
99*cdf0e10cSrcweir      * Creates text document.
100*cdf0e10cSrcweir      */
101*cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     protected void cleanup(TestParameters tParam, PrintWriter log) {
106*cdf0e10cSrcweir         log.println("    disposing xTextDoc ");
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         try {
109*cdf0e10cSrcweir             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
110*cdf0e10cSrcweir             XCloseable.class, xTextDoc);
111*cdf0e10cSrcweir             closer.close(true);
112*cdf0e10cSrcweir         } catch (com.sun.star.util.CloseVetoException e) {
113*cdf0e10cSrcweir             log.println("couldn't close document");
114*cdf0e10cSrcweir         } catch (com.sun.star.lang.DisposedException e) {
115*cdf0e10cSrcweir             log.println("couldn't close document");
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     /**
121*cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested. To obtain
122*cdf0e10cSrcweir      * test component, at first several paragraphs are inserted to a major text
123*cdf0e10cSrcweir      * of text document. Then enumeration of text paragraphs is created, and
124*cdf0e10cSrcweir      * some paragraph is gotten using <code>XEnumeration</code> interface.<p>
125*cdf0e10cSrcweir      *     Object relations created :
126*cdf0e10cSrcweir      * <ul>
127*cdf0e10cSrcweir      *  <li> <code>'PARA'</code> for
128*cdf0e10cSrcweir      *      {@link ifc.style._CharacterProperties} : paragraph </li>
129*cdf0e10cSrcweir      *  <li> <code>'PORTION'</code> for
130*cdf0e10cSrcweir      *      {@link ifc.style._CharacterProperties} : some text portion of
131*cdf0e10cSrcweir      *  paragraph. To obtain text portion, enumeration of paragraph text portions is
132*cdf0e10cSrcweir      *  created, and some text portion is gotten using <code>XEnumeration</code>
133*cdf0e10cSrcweir      *  interface. </li>
134*cdf0e10cSrcweir      *  <li> <code>'NRULES'</code> for
135*cdf0e10cSrcweir      *      {@link ifc.style._ParagraphProperties} : value of property
136*cdf0e10cSrcweir      *  'NumberingRules' of paragraph. Method changes property
137*cdf0e10cSrcweir      *  'NumberingStyleName' of previously obtained paragraph, and gets value of
138*cdf0e10cSrcweir      *  a property 'NumberingRules'. </li>
139*cdf0e10cSrcweir      * </ul>
140*cdf0e10cSrcweir      */
141*cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment
142*cdf0e10cSrcweir     (TestParameters tParam, PrintWriter log) {
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         if (xTextDoc != null) {
145*cdf0e10cSrcweir             log.println("    disposing xTextDoc ");
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir             try {
148*cdf0e10cSrcweir                 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
149*cdf0e10cSrcweir                 XCloseable.class, xTextDoc);
150*cdf0e10cSrcweir                 closer.close(true);
151*cdf0e10cSrcweir             } catch (com.sun.star.util.CloseVetoException e) {
152*cdf0e10cSrcweir                 log.println("couldn't close document");
153*cdf0e10cSrcweir             } catch (com.sun.star.lang.DisposedException e) {
154*cdf0e10cSrcweir                 log.println("couldn't close document");
155*cdf0e10cSrcweir             }
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) tParam.getMSF() );
158*cdf0e10cSrcweir         try {
159*cdf0e10cSrcweir             log.println( "creating a textdocument" );
160*cdf0e10cSrcweir             xTextDoc = SOF.createTextDoc( null );
161*cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
162*cdf0e10cSrcweir             e.printStackTrace( log );
163*cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         XInterface oObj = null;
167*cdf0e10cSrcweir         XPropertySet paraP = null;
168*cdf0e10cSrcweir         XPropertySet portP = null;
169*cdf0e10cSrcweir         Object nRules = null;
170*cdf0e10cSrcweir         XInterface port = null;
171*cdf0e10cSrcweir         XInterface para = null;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         log.println( "creating a test environment" );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         XText oText = xTextDoc.getText();
177*cdf0e10cSrcweir         XTextCursor oCursor = oText.createTextCursor();
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         log.println( "inserting some lines" );
180*cdf0e10cSrcweir         try {
181*cdf0e10cSrcweir             for (int i=0; i<5; i++){
182*cdf0e10cSrcweir                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
183*cdf0e10cSrcweir                 oText.insertString( oCursor,
184*cdf0e10cSrcweir                 " The quick brown fox jumps over the lazy Dog: SwXParagraph",
185*cdf0e10cSrcweir                 false);
186*cdf0e10cSrcweir                 oText.insertControlCharacter(
187*cdf0e10cSrcweir                 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
188*cdf0e10cSrcweir                 oText.insertString( oCursor,
189*cdf0e10cSrcweir                 "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
190*cdf0e10cSrcweir                 false);
191*cdf0e10cSrcweir                 oText.insertControlCharacter(oCursor,
192*cdf0e10cSrcweir                 ControlCharacter.PARAGRAPH_BREAK, false );
193*cdf0e10cSrcweir                 oText.insertControlCharacter(
194*cdf0e10cSrcweir                 oCursor, ControlCharacter.LINE_BREAK, false );
195*cdf0e10cSrcweir             }
196*cdf0e10cSrcweir         } catch ( com.sun.star.lang.IllegalArgumentException e ){
197*cdf0e10cSrcweir             e.printStackTrace(log);
198*cdf0e10cSrcweir             throw new StatusException( "Couldn't insert lines", e );
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         // Enumeration
202*cdf0e10cSrcweir         XEnumerationAccess oEnumA = (XEnumerationAccess)
203*cdf0e10cSrcweir         UnoRuntime.queryInterface(XEnumerationAccess.class, oText );
204*cdf0e10cSrcweir         XEnumeration oEnum = oEnumA.createEnumeration();
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir         try {
207*cdf0e10cSrcweir             para = (XInterface) AnyConverter.toObject(
208*cdf0e10cSrcweir             new Type(XInterface.class),oEnum.nextElement());
209*cdf0e10cSrcweir             XEnumerationAccess oEnumB = (XEnumerationAccess)
210*cdf0e10cSrcweir             UnoRuntime.queryInterface( XEnumerationAccess.class, para );
211*cdf0e10cSrcweir             XEnumeration oEnum2 = oEnumB.createEnumeration();
212*cdf0e10cSrcweir             port = (XInterface) AnyConverter.toObject(
213*cdf0e10cSrcweir             new Type(XInterface.class),oEnum2.nextElement());
214*cdf0e10cSrcweir         } catch ( com.sun.star.lang.WrappedTargetException e ) {
215*cdf0e10cSrcweir             e.printStackTrace(log);
216*cdf0e10cSrcweir             log.println("Error: exception occured...");
217*cdf0e10cSrcweir         } catch ( com.sun.star.container.NoSuchElementException e ) {
218*cdf0e10cSrcweir             e.printStackTrace(log);
219*cdf0e10cSrcweir             log.println("Error: exception occured...");
220*cdf0e10cSrcweir         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
221*cdf0e10cSrcweir             e.printStackTrace(log);
222*cdf0e10cSrcweir             log.println("Error: exception occured...");
223*cdf0e10cSrcweir         }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         try {
226*cdf0e10cSrcweir             portP = (XPropertySet)
227*cdf0e10cSrcweir             UnoRuntime.queryInterface(XPropertySet.class, port);
228*cdf0e10cSrcweir             paraP = (XPropertySet)
229*cdf0e10cSrcweir             UnoRuntime.queryInterface(XPropertySet.class, para);
230*cdf0e10cSrcweir             paraP.setPropertyValue("NumberingStyleName","Numbering 4");
231*cdf0e10cSrcweir             nRules = paraP.getPropertyValue("NumberingRules");
232*cdf0e10cSrcweir         } catch ( com.sun.star.lang.WrappedTargetException e ) {
233*cdf0e10cSrcweir             log.println("Error, exception occured...");
234*cdf0e10cSrcweir             e.printStackTrace(log);
235*cdf0e10cSrcweir             throw new StatusException( "Couldn't get Paragraph", e );
236*cdf0e10cSrcweir         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
237*cdf0e10cSrcweir             log.println("Error, exception occured...");
238*cdf0e10cSrcweir             e.printStackTrace(log);
239*cdf0e10cSrcweir             throw new StatusException( "Couldn't get Paragraph", e );
240*cdf0e10cSrcweir         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
241*cdf0e10cSrcweir             log.println("Error, exception occured...");
242*cdf0e10cSrcweir             e.printStackTrace(log);
243*cdf0e10cSrcweir             throw new StatusException( "Couldn't get Paragraph", e );
244*cdf0e10cSrcweir         } catch ( com.sun.star.beans.PropertyVetoException e ) {
245*cdf0e10cSrcweir             log.println("Error, exception occured...");
246*cdf0e10cSrcweir             e.printStackTrace(log);
247*cdf0e10cSrcweir             throw new StatusException( "Couldn't get Paragraph", e );
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir         try {
252*cdf0e10cSrcweir             oObj = (XInterface) AnyConverter.toObject(
253*cdf0e10cSrcweir             new Type(XInterface.class),oEnum.nextElement());
254*cdf0e10cSrcweir         } catch ( Exception e) {
255*cdf0e10cSrcweir             log.println("Error, exception occured...");
256*cdf0e10cSrcweir             e.printStackTrace(log);
257*cdf0e10cSrcweir             throw new StatusException( "Couldn't get Paragraph", e );
258*cdf0e10cSrcweir         }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         log.println( "creating a new environment for Paragraph object" );
262*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         log.println("adding ObjectRelation 'PARA' for CharacterProperties");
265*cdf0e10cSrcweir         tEnv.addObjRelation("PARA", paraP);
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir         log.println("adding ObjectRelation 'PORTION' for CharacterProperties");
268*cdf0e10cSrcweir         tEnv.addObjRelation("PORTION", portP);
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         log.println("adding ObjectRelation 'NRULES' for ParagraphProperties");
271*cdf0e10cSrcweir         tEnv.addObjRelation("NRULES", nRules);
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         tEnv.addObjRelation("NoAttach","SwXParagraph");
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         return tEnv;
276*cdf0e10cSrcweir     } // finish method getTestEnvironment
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir }    // finish class SwXParagraph
279*cdf0e10cSrcweir 
280