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