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