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.AnyConverter;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51  *
52  * initial description
53  * @see com.sun.star.text.TextPortion
54  *
55  */
56 public class SwXTextPortionEnumeration extends TestCase {
57 
58     XTextDocument xTextDoc;
59 
60     protected void initialize( TestParameters tParam, PrintWriter log ) {
61         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
62 
63         try {
64                 log.println( "creating a textdocument" );
65                 xTextDoc = SOF.createTextDoc( null );
66         } catch ( com.sun.star.uno.Exception e ) {
67                 // Some exception occures.FAILED
68                 e.printStackTrace( log );
69                 throw new StatusException( "Couldn't create document", e );
70         }
71     }
72 
73     protected void cleanup( TestParameters tParam, PrintWriter log ) {
74         log.println( "    disposing xTextDoc " );
75         util.DesktopTools.closeDoc(xTextDoc);
76     }
77 
78     /**
79      *    creating a Testenvironment for the interfaces to be tested
80      *
81      *  @param tParam    class which contains additional test parameters
82      *  @param log        class to log the test state and result
83      *
84      *  @return    Status class
85      *
86      *  @see TestParameters
87      *    @see PrintWriter
88      */
89     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
90 
91         XInterface param = 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 
106         try{
107             for (int i =0; i < 5; i++){
108                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
109                 oText.insertControlCharacter(
110                     oCursor, ControlCharacter.LINE_BREAK, false );
111                 oText.insertString( oCursor,
112                     "The quick brown fox jumps over the lazy Dog: SwXParagraph\n",
113                     false);
114                 oText.insertControlCharacter(
115                     oCursor, ControlCharacter.LINE_BREAK, false );
116                 oText.insertString( oCursor,
117                     "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
118                     false);
119                 oText.insertControlCharacter(
120                     oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
121             }
122         }catch(Exception e){
123             log.println("Couldn't insert Text");
124             e.printStackTrace();
125             throw new StatusException( "Couldn't insert Text", e );
126         }
127 
128         // Enumeration
129         XEnumerationAccess oEnumA = (XEnumerationAccess)
130             UnoRuntime.queryInterface( XEnumerationAccess.class, oText );
131         XEnumeration oEnum = oEnumA.createEnumeration();
132 
133         int n = 0;
134         while ( (oEnum.hasMoreElements()) ) {
135             try {
136                     param = (XInterface) AnyConverter.toObject(
137                         new Type(XInterface.class),oEnum.nextElement());
138             } catch ( Exception e) {
139                 log.println("Couldn't get Paragraph");
140                 e.printStackTrace();
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 
150         log.println( "creating a new environment for TextPortionEnumeration object" );
151         TestEnvironment tEnv = new TestEnvironment( oEnum2 );
152 
153         log.println("adding ObjRelation ENUM for XEnumeration");
154         tEnv.addObjRelation("ENUM", oEnumP);
155 
156         return tEnv;
157     } // finish method getTestEnvironment
158 
159 
160 }    // finish class SwXTextPortionEnumeration
161 
162