2 * CheckCrossReferences.java
4 * Created on November 1, 2007, 1:49 PM
6 * To change this template, choose Tools | Template Manager
7 * and open the template in the editor.
10 package complex
.writer
;
12 import complexlib
.ComplexTestCase
;
13 import com
.sun
.star
.lang
.XMultiServiceFactory
;
14 import com
.sun
.star
.uno
.UnoRuntime
;
20 public class CheckCrossReferences
extends ComplexTestCase
{
22 private com
.sun
.star
.text
.XTextDocument xDoc
;
23 private com
.sun
.star
.container
.XEnumeration xParaEnum
;
24 private com
.sun
.star
.container
.XEnumeration xPortionEnum
;
25 private com
.sun
.star
.util
.XRefreshable xFldsRefresh
;
27 public String
[] getTestMethodNames() {
28 return new String
[]{"checkCrossReferences"};
31 public com
.sun
.star
.text
.XTextField
getNextField() {
33 com
.sun
.star
.text
.XTextField xField
= null;
34 if ( xPortionEnum
!= null ) {
36 while ( xPortionEnum
.hasMoreElements() ) {
37 com
.sun
.star
.beans
.XPropertySet xPortionProps
=
38 (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
39 com
.sun
.star
.beans
.XPropertySet
.class , xPortionEnum
.nextElement());
40 final String sPortionType
=
41 xPortionProps
.getPropertyValue( "TextPortionType" ).toString();
42 if ( sPortionType
.equals( "TextField") ) {
43 xField
= (com
.sun
.star
.text
.XTextField
)UnoRuntime
.queryInterface(
44 com
.sun
.star
.text
.XTextField
.class,
45 xPortionProps
.getPropertyValue( "TextField" ) );
46 if ( xField
== null ) {
47 System
.out
.println("Cannot retrieve next field.");
48 failed("Cannot retrieve next field.");
54 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
55 System
.out
.println("Cannot retrieve next field.");
57 failed(e
.getMessage());
59 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
60 System
.out
.println("Cannot retrieve next field.");
62 failed(e
.getMessage());
64 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
65 System
.out
.println("Cannot retrieve next field.");
67 failed(e
.getMessage());
72 while ( xParaEnum
.hasMoreElements() ) {
74 com
.sun
.star
.container
.XEnumerationAccess aPara
=
75 (com
.sun
.star
.container
.XEnumerationAccess
)UnoRuntime
.queryInterface(
76 com
.sun
.star
.container
.XEnumerationAccess
.class, xParaEnum
.nextElement());
77 xPortionEnum
= aPara
.createEnumeration();
78 while ( xPortionEnum
.hasMoreElements() ) {
79 com
.sun
.star
.beans
.XPropertySet xPortionProps
=
80 (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
81 com
.sun
.star
.beans
.XPropertySet
.class , xPortionEnum
.nextElement());
82 final String sPortionType
=
83 xPortionProps
.getPropertyValue( "TextPortionType" ).toString();
84 if ( sPortionType
.equals( "TextField") ) {
85 xField
= (com
.sun
.star
.text
.XTextField
)UnoRuntime
.queryInterface(
86 com
.sun
.star
.text
.XTextField
.class,
87 xPortionProps
.getPropertyValue( "TextField" ) );
88 if ( xField
== null ) {
89 System
.out
.println("Cannot retrieve next field.");
90 failed("Cannot retrieve next field.");
96 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
97 System
.out
.println("Cannot retrieve next field.");
99 failed(e
.getMessage());
101 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
102 System
.out
.println("Cannot retrieve next field.");
104 failed(e
.getMessage());
106 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
107 System
.out
.println("Cannot retrieve next field.");
109 failed(e
.getMessage());
114 if ( xField
== null ) {
115 System
.out
.println("Cannot retrieve next field.");
116 failed("Cannot retrieve next field.");
123 public com
.sun
.star
.beans
.XPropertySet
getFieldProps(
124 com
.sun
.star
.text
.XTextField xField
) {
125 com
.sun
.star
.beans
.XPropertySet xProps
=
126 (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
127 com
.sun
.star
.beans
.XPropertySet
.class, xField
);
129 if ( xProps
== null ) {
130 System
.out
.println("Cannot retrieve field properties.");
131 failed("Cannot retrieve field properties.");
138 public void checkField( com
.sun
.star
.text
.XTextField xField
,
139 com
.sun
.star
.beans
.XPropertySet xProps
,
141 String aExpectedFldResult
) {
142 // set requested format
144 xProps
.setPropertyValue("ReferenceFieldPart", new Short(nFormat
));
145 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
146 System
.out
.println("Cannot set ReferenceFieldPart property at field.");
148 failed(e
.getMessage());
150 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
151 System
.out
.println("Cannot set ReferenceFieldPart property at field.");
153 failed(e
.getMessage());
155 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
156 System
.out
.println("Cannot set ReferenceFieldPart property at field.");
158 failed(e
.getMessage());
160 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
161 System
.out
.println("Cannot set ReferenceFieldPart property at field.");
163 failed(e
.getMessage());
166 // refresh fields in order to get new format applied
167 xFldsRefresh
.refresh();
169 String aFldResult
= xField
.getPresentation( false );
170 assure( "set reference field format doesn't result in correct field result",
171 aFldResult
.equals(aExpectedFldResult
), true );
174 public void checkCrossReferences() throws com
.sun
.star
.uno
.Exception
{
175 // load test document
177 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)param
.getMSF();
178 xDoc
= util
.WriterTools
.loadTextDoc( xMSF
, util
.utils
.getFullTestURL("CheckCrossReferences.odt"));
179 } catch(com
.sun
.star
.uno
.RuntimeException e
) {
180 System
.out
.println("Cannot load test document.");
182 failed(e
.getMessage());
186 // setup paragraph enumeration
188 com
.sun
.star
.container
.XEnumerationAccess xParaEnumAccess
=
189 (com
.sun
.star
.container
.XEnumerationAccess
)UnoRuntime
.queryInterface(
190 com
.sun
.star
.container
.XEnumerationAccess
.class, xDoc
.getText());
191 xParaEnum
= xParaEnumAccess
.createEnumeration();
194 // get field refresher
196 com
.sun
.star
.text
.XTextFieldsSupplier xFieldSupp
=
197 (com
.sun
.star
.text
.XTextFieldsSupplier
)UnoRuntime
.queryInterface(
198 com
.sun
.star
.text
.XTextFieldsSupplier
.class, xDoc
);
199 xFldsRefresh
= (com
.sun
.star
.util
.XRefreshable
)UnoRuntime
.queryInterface(
200 com
.sun
.star
.util
.XRefreshable
.class, xFieldSupp
.getTextFields());
203 // check first reference field
205 // strings for checking
206 final String FldResult1
= "*i*";
207 final String FldResult2
= "+b+*i*";
208 final String FldResult3
= "-1-+b+*i*";
209 final String FldResult4
= "1.";
210 final String FldResult5
= " 1.";
211 final String FldResult6
= "A. 1.";
213 // variables for current field
214 com
.sun
.star
.text
.XTextField xField
= null;
215 com
.sun
.star
.beans
.XPropertySet xProps
= null;
217 log
.println( "Checking field reference formats NUMBER, NUMBER_NO_CONTEXT and NUMBER_FULL_CONTEXT for existing fields" );
218 xField
= getNextField();
219 xProps
= getFieldProps( xField
);
220 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER
, FldResult2
);
221 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_NO_CONTEXT
, FldResult1
);
222 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_FULL_CONTEXT
, FldResult3
);
224 xField
= getNextField();
225 xProps
= getFieldProps( xField
);
226 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER
, FldResult1
);
227 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_NO_CONTEXT
, FldResult1
);
228 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_FULL_CONTEXT
, FldResult3
);
230 xField
= getNextField();
231 xProps
= getFieldProps( xField
);
232 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER
, FldResult3
);
233 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_NO_CONTEXT
, FldResult1
);
234 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_FULL_CONTEXT
, FldResult3
);
236 xField
= getNextField();
237 xProps
= getFieldProps( xField
);
238 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER
, FldResult5
);
239 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_NO_CONTEXT
, FldResult4
);
240 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_FULL_CONTEXT
, FldResult6
);
242 xField
= getNextField();
243 xProps
= getFieldProps( xField
);
244 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER
, FldResult4
);
245 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_NO_CONTEXT
, FldResult4
);
246 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_FULL_CONTEXT
, FldResult6
);
248 xField
= getNextField();
249 xProps
= getFieldProps( xField
);
250 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER
, FldResult6
);
251 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_NO_CONTEXT
, FldResult4
);
252 checkField( xField
, xProps
, com
.sun
.star
.text
.ReferenceFieldPart
.NUMBER_FULL_CONTEXT
, FldResult6
);
255 // insert a certain cross-reference bookmark and a reference field to this bookmark
257 log
.println("Checking insert of cross-reference bookmark and corresponding reference field");
258 // restart paragraph enumeration
259 com
.sun
.star
.container
.XEnumerationAccess xParaEnumAccess
=
260 (com
.sun
.star
.container
.XEnumerationAccess
)UnoRuntime
.queryInterface(
261 com
.sun
.star
.container
.XEnumerationAccess
.class, xDoc
.getText());
262 xParaEnum
= xParaEnumAccess
.createEnumeration();
264 // iterate on the paragraphs to find certain paragraph to insert the bookmark
265 com
.sun
.star
.text
.XTextRange xParaTextRange
= null;
266 while ( xParaEnum
.hasMoreElements() ) {
268 xParaTextRange
= (com
.sun
.star
.text
.XTextRange
)UnoRuntime
.queryInterface(
269 com
.sun
.star
.text
.XTextRange
.class, xParaEnum
.nextElement());
270 if ( xParaTextRange
.getString().equals( "J" ) ) {
274 xParaTextRange
= null;
276 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
277 System
.out
.println("Cannot find paragraph to insert cross-reference bookmark.");
279 failed(e
.getMessage());
281 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
282 System
.out
.println("Cannot find paragraph to insert cross-reference bookmark.");
284 failed(e
.getMessage());
288 if ( xParaTextRange
== null ) {
289 System
.out
.println("Cannot find paragraph to insert cross-reference bookmark.");
290 failed("Cannot find paragraph to insert cross-reference bookmark.");
295 XMultiServiceFactory xFac
= (XMultiServiceFactory
)UnoRuntime
.queryInterface(
296 XMultiServiceFactory
.class, xDoc
);
297 final String cBookmarkName
= "__RefNumPara__47114711";
298 com
.sun
.star
.text
.XTextContent xBookmark
= null;
300 xBookmark
= (com
.sun
.star
.text
.XTextContent
)UnoRuntime
.queryInterface(
301 com
.sun
.star
.text
.XTextContent
.class,
302 xFac
.createInstance( "com.sun.star.text.Bookmark" ) );
303 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
304 System
.out
.println("Cannot create bookmark.");
306 failed(e
.getMessage());
308 } catch (com
.sun
.star
.uno
.Exception e
) {
309 System
.out
.println("Cannot create bookmark.");
311 failed(e
.getMessage());
314 if ( xBookmark
!= null ) {
315 com
.sun
.star
.container
.XNamed xName
=
316 (com
.sun
.star
.container
.XNamed
)UnoRuntime
.queryInterface(
317 com
.sun
.star
.container
.XNamed
.class, xBookmark
);
318 xName
.setName( cBookmarkName
);
319 xBookmark
.attach(xParaTextRange
.getStart());
322 // insert reference field, which references the inserted bookmark
323 com
.sun
.star
.text
.XTextContent xNewField
= null;
325 xNewField
= (com
.sun
.star
.text
.XTextContent
)UnoRuntime
.queryInterface(
326 com
.sun
.star
.text
.XTextContent
.class,
327 xFac
.createInstance( "com.sun.star.text.TextField.GetReference" ) );
328 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
329 System
.out
.println("Cannot create new field.");
331 failed(e
.getMessage());
333 } catch (com
.sun
.star
.uno
.Exception e
) {
334 System
.out
.println("Cannot create new field.");
336 failed(e
.getMessage());
339 if ( xNewField
!= null ) {
340 com
.sun
.star
.beans
.XPropertySet xFieldProps
=
341 (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
342 com
.sun
.star
.beans
.XPropertySet
.class, xNewField
);
343 xFieldProps
.setPropertyValue( "ReferenceFieldPart", new Short(com
.sun
.star
.text
.ReferenceFieldPart
.TEXT
) );
344 xFieldProps
.setPropertyValue( "ReferenceFieldSource", new Short(com
.sun
.star
.text
.ReferenceFieldSource
.BOOKMARK
) );
345 xFieldProps
.setPropertyValue( "SourceName", cBookmarkName
);
346 com
.sun
.star
.text
.XTextRange xFieldTextRange
=
347 (com
.sun
.star
.text
.XTextRange
)UnoRuntime
.queryInterface(
348 com
.sun
.star
.text
.XTextRange
.class, xParaEnum
.nextElement());
349 xNewField
.attach(xFieldTextRange
.getEnd());
350 xFldsRefresh
.refresh();
353 // check inserted reference field
354 com
.sun
.star
.text
.XTextField xField
=
355 (com
.sun
.star
.text
.XTextField
)UnoRuntime
.queryInterface(
356 com
.sun
.star
.text
.XTextField
.class, xNewField
);
357 assure( "inserted reference field doesn't has correct field result",
358 xField
.getPresentation( false ).equals("J"), true );
360 xParaTextRange
.getStart().setString( "Hallo new bookmark: " );
361 xFldsRefresh
.refresh();
362 assure( "inserted reference field doesn't has correct field result. Instead it's: "+xField
.getPresentation( false ),
363 xField
.getPresentation( false ).equals("Hallo new bookmark: J"), true );
366 // closing test document
367 util
.DesktopTools
.closeDoc( xDoc
);