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.container.XEnumeration;
39 import com.sun.star.container.XEnumerationAccess;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.text.ControlCharacter;
42 import com.sun.star.text.XText;
43 import com.sun.star.text.XTextCursor;
44 import com.sun.star.text.XTextDocument;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49  * Test for object which is represented by service
50  * <code>com.sun.star.text.ParagraphEnumeration</code>. <p>
51  * Object implements the following interfaces :
52  * <ul>
53  *  <li> <code>com::sun::star::container::XEnumeration</code></li>
54  * </ul> <p>
55  * This object test <b> is NOT </b> designed to be run in several
56  * threads concurently.
57  * @see com.sun.star.container.XEnumeration
58  * @see ifc.container._XEnumeration
59  */
60 public class SwXParagraphEnumeration extends TestCase {
61         XTextDocument xTextDoc = null;
62 
63     /**
64     * Creates text document.
65     */
66     protected void initialize( TestParameters tParam, PrintWriter log ) {
67         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
68         try {
69             log.println( "creating a textdocument" );
70             xTextDoc = SOF.createTextDoc( null );
71         } catch ( com.sun.star.uno.Exception e ) {
72             e.printStackTrace( log );
73             throw new StatusException( "Couldn't create document", e );
74         }
75     }
76 
77     /**
78     * Disposes text document.
79     */
80     protected void cleanup( TestParameters tParam, PrintWriter log ) {
81         log.println( "    disposing xTextDoc " );
82         util.DesktopTools.closeDoc(xTextDoc);
83     }
84 
85     /**
86     * Creating a Testenvironment for the interfaces to be tested. After major
87     * text is gotten from a text document, three paragraphs (each of them
88     * filled by 5 strings) are inserted to major text. Finally, paragraph
89     * enumeration is created using <code>XEnumeration</code> interface.
90     *     Object relations created :
91     * <ul>
92     *  <li> <code>'ENUM'</code> for
93     *      {@link ifc.container._XEnumeration} : major text of text document
94     *  with several paragraphs inserted, queried to
95     *  <code>XEnumerationAccess</code> interface.</li>
96     * </ul>
97     */
98     public synchronized TestEnvironment createTestEnvironment(
99             TestParameters tParam, PrintWriter log ) throws StatusException {
100         XInterface oObj = null;
101 
102         log.println( "creating a test environment" );
103         XText oText = xTextDoc.getText();
104         XTextCursor oCursor = oText.createTextCursor();
105 
106         for (int i=0; i<3; i++) {
107             try {
108                 oText.insertString( oCursor, "Paragraph Number: " + i, false);
109                 oText.insertControlCharacter( oCursor,
110                     ControlCharacter.LINE_BREAK, false );
111             } catch ( com.sun.star.lang.IllegalArgumentException e ){
112                 log.println( "EXCEPTION: " + e);
113             }
114 
115             for (int j=0; j<5; j++){
116                 try {
117                     oText.insertString( oCursor,"The quick brown fox jumps"+
118                         " over the lazy Dog: SwXParagraph", false);
119                     oText.insertControlCharacter( oCursor,
120                         ControlCharacter.LINE_BREAK, false );
121                     oText.insertString( oCursor, "THE QUICK BROWN FOX JUMPS"+
122                         " OVER THE LAZY DOG: SwXParagraph", false);
123                     oText.insertControlCharacter( oCursor,
124                         ControlCharacter.LINE_BREAK, false );
125                 } catch ( com.sun.star.lang.IllegalArgumentException e ){
126                     log.println( "EXCEPTION: " + e);
127                 }
128             }
129 
130             try {
131                 oText.insertControlCharacter( oCursor,
132                     ControlCharacter.PARAGRAPH_BREAK, false );
133             } catch ( com.sun.star.lang.IllegalArgumentException e ){
134                 log.println( "EXCEPTION: " + e);
135             }
136         }
137 
138         // Enumeration
139         XEnumerationAccess oEnumA = (XEnumerationAccess)
140             UnoRuntime.queryInterface( XEnumerationAccess.class, oText );
141         XEnumeration oEnum = oEnumA.createEnumeration();
142 
143         oObj = oEnum;
144 
145         log.println("creating a new environment for ParagraphEnumeration object");
146         TestEnvironment tEnv = new TestEnvironment( oObj );
147 
148         tEnv.addObjRelation("ENUM", oEnumA);
149 
150         return tEnv;
151     } // finish method getTestEnvironment
152 
153 }    // finish class SwXParagraphEnumeration
154 
155