1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.text;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir import lib.Status;
28cdf0e10cSrcweir import util.XInstCreator;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
31cdf0e10cSrcweir import com.sun.star.text.XText;
32cdf0e10cSrcweir import com.sun.star.text.XTextContent;
33cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
34cdf0e10cSrcweir import com.sun.star.uno.XInterface;
35cdf0e10cSrcweir import lib.StatusException;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /**
38cdf0e10cSrcweir  * Testing <code>com.sun.star.text.XText</code>
39cdf0e10cSrcweir  * interface methods :
40cdf0e10cSrcweir  * <ul>
41cdf0e10cSrcweir  *  <li><code> insertTextContent()</code></li>
42cdf0e10cSrcweir  *  <li><code> removeTextContent()</code></li>
43cdf0e10cSrcweir  * </ul> <p>
44cdf0e10cSrcweir  * This test needs the following object relations :
45cdf0e10cSrcweir  * <ul>
46cdf0e10cSrcweir  *  <li> <code>'XTEXTINFO'</code> (of type <code>lib.XInstCreator</code>):
47cdf0e10cSrcweir  *   creator which can create instances of <code>XTextContent</code>
48cdf0e10cSrcweir  *   implementations. </li>
49cdf0e10cSrcweir  * <ul> <p>
50cdf0e10cSrcweir  * Test is <b> NOT </b> multithread compilant. <p>
51cdf0e10cSrcweir  * @see com.sun.star.text.XText
52cdf0e10cSrcweir  */
53cdf0e10cSrcweir public class _XText extends MultiMethodTest {
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     public static XText oObj = null;        // oObj filled by MultiMethodTest
56cdf0e10cSrcweir     XTextCursor oCursor = null;             // textcursor
57cdf0e10cSrcweir     XInstCreator info = null;               // instance creator
58cdf0e10cSrcweir     XInterface oInt = null;                 // instance to insert and remove
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /**
61cdf0e10cSrcweir      * First an instance of text content is created using relation
62cdf0e10cSrcweir      * and inserted into text. Then the number of contents is checked
63cdf0e10cSrcweir      * using the relation. Second a <code>null</code> content is tried
64cdf0e10cSrcweir      * to insert. <p>
65cdf0e10cSrcweir      *
66cdf0e10cSrcweir      * Has <b> OK </b> status if in the first case after inserting number
67cdf0e10cSrcweir      * of content objects is greater than zero and in the second
68cdf0e10cSrcweir      * case <code>IllegalArgumentException</code> is thrown. <p>
69cdf0e10cSrcweir      */
70cdf0e10cSrcweir     public void _insertTextContent() {
71cdf0e10cSrcweir         boolean result = false;
72cdf0e10cSrcweir         info = (XInstCreator)tEnv.getObjRelation( "XTEXTINFO" );
73cdf0e10cSrcweir         oInt = info.createInstance();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         // write to log what we try next
76cdf0e10cSrcweir         log.println( "test for createTextCursor()" );
77cdf0e10cSrcweir         oCursor = oObj.createTextCursor();
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         // write to log what we try next
80cdf0e10cSrcweir         log.println( "test for insertTextContent()" );
81cdf0e10cSrcweir         try {
82cdf0e10cSrcweir             oObj.insertTextContent(oCursor, (XTextContent)oInt, false);
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir         catch( com.sun.star.lang.IllegalArgumentException iaE ){
85cdf0e10cSrcweir             throw new StatusException("Couldn't insert textcontent",iaE);
86cdf0e10cSrcweir             //Status.failed(iaE.toString());
87cdf0e10cSrcweir             //return;
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         // get indexaccess to the tablecollection
91cdf0e10cSrcweir         XIndexAccess xIA = info.getCollection();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         // this comparision works just because it has to be at least one
94cdf0e10cSrcweir         // table at this point regardless which thread inserted it
95cdf0e10cSrcweir         // there is although the possibility that the first threads call
96cdf0e10cSrcweir         // failed, the second not and comparision happens after second threads
97cdf0e10cSrcweir         // otherwise if something fails it should have thrown an exception
98cdf0e10cSrcweir         //tRes.tested("insertTextContent()",    xIA.getCount() > 0 );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         if (xIA != null ) {
101cdf0e10cSrcweir             result = (xIA.getCount()>0);
102cdf0e10cSrcweir         } else {
103cdf0e10cSrcweir             result = true;
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         if (!result) log.println("The TextContent wasn't inserted");
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         // try to insert an invalid TextContent
110cdf0e10cSrcweir         log.println( "test for insertTextContent" );
111cdf0e10cSrcweir         try {
112cdf0e10cSrcweir             oObj.insertTextContent(oCursor, null, false);
113cdf0e10cSrcweir             log.println("The expected Exception doesn't occured");
114cdf0e10cSrcweir             result &= false;
115cdf0e10cSrcweir         }
116cdf0e10cSrcweir         catch( com.sun.star.lang.IllegalArgumentException iaE ){
117cdf0e10cSrcweir             // Some exception.FAILED
118cdf0e10cSrcweir             log.println("Expected Exception occured");
119cdf0e10cSrcweir             String msg = iaE.getMessage();
120cdf0e10cSrcweir             if (msg.equals("")) {
121cdf0e10cSrcweir                 log.println("But there is not detailed message");
122cdf0e10cSrcweir             } else {
123cdf0e10cSrcweir                 log.println("Detailed message: "+msg);
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             result &= true;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         tRes.tested("insertTextContent()",    result );
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     /**
134cdf0e10cSrcweir      * Removes the text contet added before. <p>
135cdf0e10cSrcweir      * Has <b> OK </b> status if the method successfully returns
136cdf0e10cSrcweir      * and no exceptions were thrown. <p>
137cdf0e10cSrcweir      * The following method tests are to be completed successfully before :
138cdf0e10cSrcweir      * <ul>
139cdf0e10cSrcweir      *  <li> <code> insertTextContent() </code> : inserts the content
140cdf0e10cSrcweir      *  to be removed in this test. </li>
141cdf0e10cSrcweir      * </ul>
142cdf0e10cSrcweir      */
143cdf0e10cSrcweir     public void _removeTextContent() {
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         // leads to a method which should be called previously
146cdf0e10cSrcweir         requiredMethod( "insertTextContent()" );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         // write to log what we try next
149cdf0e10cSrcweir         log.println( "test for  removeTextContent" );
150cdf0e10cSrcweir         try {
151cdf0e10cSrcweir             oObj.removeTextContent( (XTextContent)oInt );
152cdf0e10cSrcweir             //oObj.removeTextContent( (XTextContent)oInt );
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir         catch( com.sun.star.container.NoSuchElementException nseE ){
155cdf0e10cSrcweir             // Some exception.FAILED
156cdf0e10cSrcweir             Status.failed( nseE.toString() );
157cdf0e10cSrcweir             return;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         // no exception occured so it works
161cdf0e10cSrcweir         tRes.tested( "removeTextContent()", true );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir }  // finish class _XText
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 
167