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.XNameAccess;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XText;
41 import com.sun.star.text.XTextContent;
42 import com.sun.star.text.XTextCursor;
43 import com.sun.star.text.XTextDocument;
44 import com.sun.star.text.XTextSectionsSupplier;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49  *
50  * initial description
51  * @see com.sun.star.text.XText
52  *
53  */
54 public class SwXTextSections extends TestCase {
55     XTextDocument xTextDoc;
56 
57     protected void initialize( TestParameters tParam, PrintWriter log ) {
58         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
59 
60         try {
61             log.println( "creating a textdocument" );
62             xTextDoc = SOF.createTextDoc( null );
63         } catch ( com.sun.star.uno.Exception e ) {
64             // Some exception occures.FAILED
65             e.printStackTrace( log );
66             throw new StatusException( "Couldn't create document", e );
67         }
68     }
69 
70     protected void cleanup( TestParameters tParam, PrintWriter log ) {
71         log.println( "    disposing xTextDoc " );
72         util.DesktopTools.closeDoc(xTextDoc);
73     }
74 
75     /**
76      *    creating a Testenvironment for the interfaces to be tested
77      */
78     public synchronized TestEnvironment createTestEnvironment
79         (TestParameters Param, PrintWriter log ) throws StatusException {
80 
81         XInterface oObj = null;
82         XInterface oTS = null;
83         XNameAccess oTSSuppName = null;
84 
85         XText oText = null;
86 
87         // creation of testobject here
88         // first we write what we are intend to do to log file
89         log.println( "creating a test environment" );
90 
91 
92         oText = xTextDoc.getText();
93         XTextCursor oCursor = oText.createTextCursor();
94 
95 
96         log.println( "inserting TextSections" );
97 
98         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
99             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
100 
101         // First TextSection
102         try {
103             oTS = (XInterface) oDocMSF.createInstance
104                 ("com.sun.star.text.TextSection");
105             XTextContent oTSC = (XTextContent)
106                 UnoRuntime.queryInterface(XTextContent.class, oTS);
107             oText.insertTextContent(oCursor, oTSC, false);
108         }
109         catch(Exception e){
110             e.printStackTrace( log );
111             throw new StatusException( "Couldn't create document", e );
112         }
113 
114         // Second TextSection
115         try {
116             oTS = (XInterface) oDocMSF.createInstance
117                 ("com.sun.star.text.TextSection");
118             XTextContent oTSC = (XTextContent)
119                 UnoRuntime.queryInterface(XTextContent.class, oTS);
120             oText.insertTextContent(oCursor, oTSC, false);
121         }
122         catch(Exception e){
123             e.printStackTrace( log );
124             throw new StatusException( "Couldn't create document", e );
125         }
126 
127 
128         log.println( "try to get a TextSection with the XTextSectionSupplier()" );
129 
130         try{
131             XTextSectionsSupplier oTSSupp = (XTextSectionsSupplier)
132                 UnoRuntime.queryInterface( XTextSectionsSupplier.class,
133                 xTextDoc );
134             oTSSuppName = oTSSupp.getTextSections();
135         }
136         catch(Exception e){
137             System.out.println("Couldn't get Textsection " + e);
138         }
139 
140 
141         oObj = oTSSuppName;
142 
143         log.println( "creating a new environment for TextSections object" );
144         TestEnvironment tEnv = new TestEnvironment( oObj );
145 
146         log.println( "adding TextDocument as mod relation to environment" );
147         tEnv.addObjRelation("TEXTDOC", xTextDoc);
148 
149         return tEnv;
150     } // finish method getTestEnvironment
151 
152 }    // finish class SwXTextSection
153