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 ifc.text;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import util.XInstCreator;
33 
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.text.XText;
36 import com.sun.star.text.XTextContent;
37 import com.sun.star.text.XTextCursor;
38 import com.sun.star.uno.XInterface;
39 import lib.StatusException;
40 
41 /**
42  * Testing <code>com.sun.star.text.XText</code>
43  * interface methods :
44  * <ul>
45  *  <li><code> insertTextContent()</code></li>
46  *  <li><code> removeTextContent()</code></li>
47  * </ul> <p>
48  * This test needs the following object relations :
49  * <ul>
50  *  <li> <code>'XTEXTINFO'</code> (of type <code>lib.XInstCreator</code>):
51  *   creator which can create instances of <code>XTextContent</code>
52  *   implementations. </li>
53  * <ul> <p>
54  * Test is <b> NOT </b> multithread compilant. <p>
55  * @see com.sun.star.text.XText
56  */
57 public class _XText extends MultiMethodTest {
58 
59     public static XText oObj = null;        // oObj filled by MultiMethodTest
60     XTextCursor oCursor = null;             // textcursor
61     XInstCreator info = null;               // instance creator
62     XInterface oInt = null;                 // instance to insert and remove
63 
64     /**
65      * First an instance of text content is created using relation
66      * and inserted into text. Then the number of contents is checked
67      * using the relation. Second a <code>null</code> content is tried
68      * to insert. <p>
69      *
70      * Has <b> OK </b> status if in the first case after inserting number
71      * of content objects is greater than zero and in the second
72      * case <code>IllegalArgumentException</code> is thrown. <p>
73      */
74     public void _insertTextContent() {
75         boolean result = false;
76         info = (XInstCreator)tEnv.getObjRelation( "XTEXTINFO" );
77         oInt = info.createInstance();
78 
79         // write to log what we try next
80         log.println( "test for createTextCursor()" );
81         oCursor = oObj.createTextCursor();
82 
83         // write to log what we try next
84         log.println( "test for insertTextContent()" );
85         try {
86             oObj.insertTextContent(oCursor, (XTextContent)oInt, false);
87         }
88         catch( com.sun.star.lang.IllegalArgumentException iaE ){
89             throw new StatusException("Couldn't insert textcontent",iaE);
90             //Status.failed(iaE.toString());
91             //return;
92         }
93 
94         // get indexaccess to the tablecollection
95         XIndexAccess xIA = info.getCollection();
96 
97         // this comparision works just because it has to be at least one
98         // table at this point regardless which thread inserted it
99         // there is although the possibility that the first threads call
100         // failed, the second not and comparision happens after second threads
101         // otherwise if something fails it should have thrown an exception
102         //tRes.tested("insertTextContent()",    xIA.getCount() > 0 );
103 
104         if (xIA != null ) {
105             result = (xIA.getCount()>0);
106         } else {
107             result = true;
108         }
109 
110         if (!result) log.println("The TextContent wasn't inserted");
111 
112 
113         // try to insert an invalid TextContent
114         log.println( "test for insertTextContent" );
115         try {
116             oObj.insertTextContent(oCursor, null, false);
117             log.println("The expected Exception doesn't occured");
118             result &= false;
119         }
120         catch( com.sun.star.lang.IllegalArgumentException iaE ){
121             // Some exception.FAILED
122             log.println("Expected Exception occured");
123             String msg = iaE.getMessage();
124             if (msg.equals("")) {
125                 log.println("But there is not detailed message");
126             } else {
127                 log.println("Detailed message: "+msg);
128             }
129 
130             result &= true;
131         }
132 
133         tRes.tested("insertTextContent()",    result );
134     }
135 
136 
137     /**
138      * Removes the text contet added before. <p>
139      * Has <b> OK </b> status if the method successfully returns
140      * and no exceptions were thrown. <p>
141      * The following method tests are to be completed successfully before :
142      * <ul>
143      *  <li> <code> insertTextContent() </code> : inserts the content
144      *  to be removed in this test. </li>
145      * </ul>
146      */
147     public void _removeTextContent() {
148 
149         // leads to a method which should be called previously
150         requiredMethod( "insertTextContent()" );
151 
152         // write to log what we try next
153         log.println( "test for  removeTextContent" );
154         try {
155             oObj.removeTextContent( (XTextContent)oInt );
156             //oObj.removeTextContent( (XTextContent)oInt );
157         }
158         catch( com.sun.star.container.NoSuchElementException nseE ){
159             // Some exception.FAILED
160             Status.failed( nseE.toString() );
161             return;
162         }
163 
164         // no exception occured so it works
165         tRes.tested( "removeTextContent()", true );
166 
167     }
168 }  // finish class _XText
169 
170 
171