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 
47 /**
48  *
49  * initial description
50  * @see com.sun.star.text.TextPortion
51  *
52  */
53 public class SwXTextPortion extends TestCase {
54     XTextDocument xTextDoc;
55 
initialize( TestParameters tParam, PrintWriter log )56     protected void initialize( TestParameters tParam, PrintWriter log ) {
57         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
58 
59         try {
60             log.println( "creating a textdocument" );
61             xTextDoc = SOF.createTextDoc( null );
62         } catch ( com.sun.star.uno.Exception e ) {
63             // Some exception occures.FAILED
64             e.printStackTrace( log );
65             throw new StatusException( "Couldn't create document", e );
66         }
67     }
68 
cleanup( TestParameters tParam, PrintWriter log )69     protected void cleanup( TestParameters tParam, PrintWriter log ) {
70         log.println( "    disposing xTextDoc " );
71         util.DesktopTools.closeDoc(xTextDoc);
72     }
73 
74     /**
75      *  *    creating a Testenvironment for the interfaces to be tested
76      *
77      *  @param tParam    class which contains additional test parameters
78      *  @param log        class to log the test state and result
79      *
80      *  @return    Status class
81      *
82      *  @see TestParameters
83      *  *    @see PrintWriter
84      */
createTestEnvironment(TestParameters tParam, PrintWriter log)85     protected synchronized TestEnvironment createTestEnvironment
86             (TestParameters tParam, PrintWriter log) {
87 
88         XInterface oObj = null;
89         XInterface param = null;
90         XPropertySet paraP = null;
91         XPropertySet portP = null;
92 
93         // creation of testobject here
94         // first we write what we are intend to do to log file
95         log.println( "creating a test environment" );
96 
97         // create testobject here
98 
99         XText oText = xTextDoc.getText();
100         XTextCursor oCursor = oText.createTextCursor();
101 
102         log.println( "inserting Strings" );
103         log.println( "inserting ControlCharacter" );
104 
105         try{
106             for (int i =0; i < 5; i++){
107                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
108                 oText.insertControlCharacter(
109                     oCursor, ControlCharacter.LINE_BREAK, false );
110                 oText.insertString( oCursor,
111                     "The quick brown fox jumps over the lazy Dog: SwXParagraph\n",
112                     false);
113                 oText.insertControlCharacter(
114                     oCursor, ControlCharacter.LINE_BREAK, false );
115                 oText.insertString( oCursor,
116                     "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
117                     false);
118                 oText.insertControlCharacter(
119                     oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
120             }
121         }catch(Exception e){
122             log.println("Couldn't insert Text");
123             e.printStackTrace(log);
124             throw new StatusException( "Couldn't insert Text", e );
125         }
126 
127         // Enumeration
128         XEnumerationAccess oEnumA = (XEnumerationAccess)
129             UnoRuntime.queryInterface( XEnumerationAccess.class, oText );
130         XEnumeration oEnum = oEnumA.createEnumeration();
131 
132         int n = 0;
133         while ( (oEnum.hasMoreElements()) ) {
134             try {
135                 param = (XInterface) AnyConverter.toObject(
136                         new Type(XInterface.class),oEnum.nextElement());
137                 log.println("Element Nr.: " + n );
138             } catch ( Exception e) {
139                 log.println("Couldn't get Paragraph");
140                 e.printStackTrace(log);
141                 throw new StatusException( "Couldn't get Paragraph", e );
142             }
143             n++;
144         }
145 
146         XEnumerationAccess oEnumP = (XEnumerationAccess)
147             UnoRuntime.queryInterface( XEnumerationAccess.class, param );
148         XEnumeration oEnum2 = oEnumP.createEnumeration();
149         try {
150             oObj = (XInterface)AnyConverter.toObject(
151                         new Type(XInterface.class),oEnum2.nextElement());
152         } catch ( Exception e) {
153             log.println("Couldn't get TextPortion");
154             e.printStackTrace(log);
155             throw new StatusException( "Couldn't get TextPortion", e );
156         }
157 
158         portP = (XPropertySet)
159                 UnoRuntime.queryInterface(XPropertySet.class, oObj);
160         paraP = (XPropertySet)
161                 UnoRuntime.queryInterface(XPropertySet.class, param);
162 
163 
164         log.println( "creating a new environment for Paragraph object" );
165         TestEnvironment tEnv = new TestEnvironment( oObj );
166 
167         log.println("adding ObjRelation TRO for TextContent");
168         tEnv.addObjRelation("TRO", new Boolean(true));
169 
170         log.println("adding ObjectRelation 'PARA' for CharacterProperties");
171         tEnv.addObjRelation("PARA", paraP);
172 
173         log.println("adding ObjectRelation 'PORTION' for CharacterProperties");
174         tEnv.addObjRelation("PORTION", portP);
175 
176         tEnv.addObjRelation("XTEXT", oText);
177 
178         return tEnv;
179     } // finish method getTestEnvironment
180 
181 
182 }    // finish class SwXTextPortion
183 
184