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