1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package complex
.writer
;
33 import com
.sun
.star
.beans
.PropertyValue
;
34 import com
.sun
.star
.container
.XNamed
;
35 import com
.sun
.star
.container
.XNameAccess
;
36 import com
.sun
.star
.frame
.XStorable
;
37 import com
.sun
.star
.lang
.XComponent
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.text
.XBookmarksSupplier
;
40 import com
.sun
.star
.text
.XSimpleText
;
41 import com
.sun
.star
.text
.XText
;
42 import com
.sun
.star
.text
.XTextContent
;
43 import com
.sun
.star
.text
.XTextCursor
;
44 import com
.sun
.star
.text
.XTextDocument
;
45 import com
.sun
.star
.text
.XTextRange
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
47 import complexlib
.ComplexTestCase
;
48 import java
.math
.BigInteger
;
50 class BookmarkHashes
{
51 public String m_sName
;
52 public BigInteger m_nSetupHash
;
53 public BigInteger m_nInsertRandomHash
;
54 public BigInteger m_nDeleteRandomHash
;
55 public BigInteger m_nLinebreakHash
;
56 public BigInteger m_nOdfReloadHash
;
57 public BigInteger m_nMsWordReloadHash
;
59 public BookmarkHashes(String sName
) {
63 public String
checkExpectationString(BookmarkHashes aExpectation
) {
64 StringBuffer buffer
= new StringBuffer();
65 buffer
.append("Comparing " + m_sName
+ " to expectations from " + aExpectation
.m_sName
+ "\n");
66 buffer
.append(compareHashString("after setup", m_nSetupHash
, aExpectation
.m_nSetupHash
));
67 buffer
.append(compareHashString("after insert random", m_nInsertRandomHash
, aExpectation
.m_nInsertRandomHash
));
68 buffer
.append(compareHashString("after delete random", m_nDeleteRandomHash
, aExpectation
.m_nDeleteRandomHash
));
69 buffer
.append(compareHashString("after line breaks", m_nLinebreakHash
, aExpectation
.m_nLinebreakHash
));
70 buffer
.append(compareHashString("after ODF roundtrip", m_nOdfReloadHash
, aExpectation
.m_nOdfReloadHash
));
71 buffer
.append(compareHashString("after MsWord roundtrip", m_nMsWordReloadHash
, aExpectation
.m_nMsWordReloadHash
));
72 return buffer
.toString();
75 public boolean meetsExpectation(BookmarkHashes aExpectation
) {
76 return m_nSetupHash
.equals(aExpectation
.m_nSetupHash
)
77 && m_nInsertRandomHash
.equals(aExpectation
.m_nInsertRandomHash
)
78 && m_nDeleteRandomHash
.equals(aExpectation
.m_nDeleteRandomHash
)
79 && m_nLinebreakHash
.equals(aExpectation
.m_nLinebreakHash
)
80 && m_nOdfReloadHash
.equals(aExpectation
.m_nOdfReloadHash
)
81 && m_nMsWordReloadHash
.equals(aExpectation
.m_nMsWordReloadHash
);
84 private String
compareHashString(String sCheckName
, BigInteger nActual
, BigInteger nExpectation
) {
85 StringBuffer buffer
= new StringBuffer(sCheckName
);
87 if(nActual
.equals(nExpectation
))
88 buffer
.append("good (" + nActual
.toString(16) + ")");
90 buffer
.append("bad (actual:" + nActual
.toString(16) + ", expected: " + nExpectation
.toString(16) + ")");
92 return buffer
.toString();
95 static public java
.math
.BigInteger
getBookmarksHash(XTextDocument xDoc
)
96 throws com
.sun
.star
.uno
.Exception
, java
.security
.NoSuchAlgorithmException
98 StringBuffer buffer
= new StringBuffer("");
99 XBookmarksSupplier xBookmarksSupplier
= (XBookmarksSupplier
)UnoRuntime
.queryInterface(
100 XBookmarksSupplier
.class,
102 XNameAccess xBookmarks
= xBookmarksSupplier
.getBookmarks();
103 for(String sBookmarkname
: xBookmarks
.getElementNames()) {
104 Object xBookmark
= xBookmarks
.getByName(sBookmarkname
);
105 XTextContent xBookmarkAsContent
= (XTextContent
)UnoRuntime
.queryInterface(
108 buffer
.append(sBookmarkname
);
110 buffer
.append(xBookmarkAsContent
.getAnchor().getString());
113 java
.security
.MessageDigest sha1
= java
.security
.MessageDigest
.getInstance("SHA-1");
115 sha1
.update(buffer
.toString().getBytes());
116 return new java
.math
.BigInteger(sha1
.digest());
120 public class CheckBookmarks
extends ComplexTestCase
{
121 private XMultiServiceFactory m_xMsf
= null;
122 private XTextDocument m_xDoc
= null;
123 private XTextDocument m_xOdfReloadedDoc
= null;
124 private XTextDocument m_xMsWordReloadedDoc
= null;
126 public String
[] getTestMethodNames() {
127 return new String
[]{"checkBookmarks"};
130 private BookmarkHashes
getDEV300m41Expectations() {
131 BookmarkHashes result
= new BookmarkHashes("DEV300m41");
132 result
.m_nSetupHash
= new BigInteger("-4b0706744e8452fe1ae9d5e1c28cf70fb6194795",16);
133 result
.m_nInsertRandomHash
= new BigInteger("25aa0fad3f4881832dcdfe658ec2efa8a1a02bc5",16);
134 result
.m_nDeleteRandomHash
= new BigInteger("-3ec87e810b46d734677c351ad893bbbf9ea10f55",16);
135 result
.m_nLinebreakHash
= new BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
136 result
.m_nOdfReloadHash
= new BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
137 result
.m_nMsWordReloadHash
= new BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
141 public void checkBookmarks()
142 throws com
.sun
.star
.uno
.Exception
,
143 com
.sun
.star
.io
.IOException
,
144 java
.security
.NoSuchAlgorithmException
147 m_xMsf
= (XMultiServiceFactory
)param
.getMSF();
148 m_xDoc
= util
.WriterTools
.createTextDoc(m_xMsf
);
149 BookmarkHashes actualHashes
= new BookmarkHashes("actual");
150 BookmarkHashes expectedHashes
= getDEV300m41Expectations();
152 actualHashes
.m_nSetupHash
= BookmarkHashes
.getBookmarksHash(m_xDoc
);
153 insertRandomParts(200177);
154 actualHashes
.m_nInsertRandomHash
= BookmarkHashes
.getBookmarksHash(m_xDoc
);
155 deleteRandomParts(4711);
156 actualHashes
.m_nDeleteRandomHash
= BookmarkHashes
.getBookmarksHash(m_xDoc
);
157 insertLinebreaks(007);
158 actualHashes
.m_nLinebreakHash
= BookmarkHashes
.getBookmarksHash(m_xDoc
);
159 m_xOdfReloadedDoc
= reloadFrom("StarOffice XML (Writer)", "odf");
160 actualHashes
.m_nOdfReloadHash
= BookmarkHashes
.getBookmarksHash(m_xOdfReloadedDoc
);
161 m_xMsWordReloadedDoc
= reloadFrom("MS Word 97", "doc");
162 actualHashes
.m_nMsWordReloadHash
= BookmarkHashes
.getBookmarksHash(m_xMsWordReloadedDoc
);
163 log
.println(actualHashes
.checkExpectationString(expectedHashes
));
164 if(!actualHashes
.meetsExpectation(expectedHashes
))
165 failed("CheckBookmark did not meet expectations (" + expectedHashes
.m_sName
+ ").");
167 // closing test document
169 util
.DesktopTools
.closeDoc(m_xDoc
);
170 if(m_xOdfReloadedDoc
!= null)
171 util
.DesktopTools
.closeDoc(m_xOdfReloadedDoc
);
172 if(m_xMsWordReloadedDoc
!= null)
173 util
.DesktopTools
.closeDoc(m_xMsWordReloadedDoc
);
177 private void setupBookmarks()
178 throws com
.sun
.star
.uno
.Exception
180 XText xText
= m_xDoc
.getText();
181 XSimpleText xSimpleText
= (XSimpleText
)UnoRuntime
.queryInterface(
184 for(int nPara
=0; nPara
<10; ++nPara
) {
185 for(int nBookmark
=0; nBookmark
<100; ++nBookmark
){
187 xText
.createTextCursor(),
188 "P" + nPara
+ "word" + nBookmark
,
189 "P" + nPara
+ "word" + nBookmark
);
190 XTextCursor xWordCrsr
= xText
.createTextCursor();
191 xWordCrsr
.setString(" ");
193 XTextCursor xParaCrsr
= xText
.createTextCursor();
194 XTextRange xParaCrsrAsRange
= (XTextRange
)UnoRuntime
.queryInterface(
197 xText
.insertControlCharacter(xParaCrsrAsRange
, com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
201 private void insertRandomParts(long seed
)
202 throws com
.sun
.star
.uno
.Exception
204 java
.util
.Random rnd
= new java
.util
.Random(seed
);
205 XTextCursor xCrsr
= m_xDoc
.getText().createTextCursor();
206 for(int i
=0; i
<600; i
++) {
207 xCrsr
.goRight((short)rnd
.nextInt(100), false);
208 xCrsr
.setString(Long
.toString(rnd
.nextLong()));
212 private void deleteRandomParts(long seed
)
213 throws com
.sun
.star
.uno
.Exception
215 java
.util
.Random rnd
= new java
.util
.Random(seed
);
216 XTextCursor xCrsr
= m_xDoc
.getText().createTextCursor();
217 for(int i
=0; i
<600; i
++) {
218 xCrsr
.goRight((short)rnd
.nextInt(100), false);
219 xCrsr
.goRight((short)rnd
.nextInt(20), true);
224 private void insertLinebreaks(long seed
)
225 throws com
.sun
.star
.uno
.Exception
227 XText xText
= m_xDoc
.getText();
228 java
.util
.Random rnd
= new java
.util
.Random(seed
);
229 XTextCursor xCrsr
= m_xDoc
.getText().createTextCursor();
230 for(int i
=0; i
<30; i
++) {
231 xCrsr
.goRight((short)rnd
.nextInt(300), false);
232 XTextRange xCrsrAsRange
= (XTextRange
)UnoRuntime
.queryInterface(
235 xText
.insertControlCharacter(xCrsrAsRange
, com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
239 private void insertBookmark(XTextCursor crsr
, String name
, String content
)
240 throws com
.sun
.star
.uno
.Exception
242 XMultiServiceFactory xDocFactory
= (XMultiServiceFactory
)UnoRuntime
.queryInterface(
243 XMultiServiceFactory
.class,
246 Object xBookmark
= xDocFactory
.createInstance("com.sun.star.text.Bookmark");
247 XTextContent xBookmarkAsTextContent
= (XTextContent
)UnoRuntime
.queryInterface(
250 crsr
.setString(content
);
251 XNamed xBookmarkAsNamed
= (XNamed
)UnoRuntime
.queryInterface(
254 xBookmarkAsNamed
.setName(name
);
255 m_xDoc
.getText().insertTextContent(crsr
, xBookmarkAsTextContent
, true);
258 private XTextDocument
reloadFrom(String sFilter
, String sExtension
)
259 throws com
.sun
.star
.io
.IOException
261 String sFileUrl
= util
.utils
.getOfficeTemp(m_xMsf
) + "/Bookmarktest." + sExtension
;
263 PropertyValue
[] aStoreProperties
= new PropertyValue
[2];
264 aStoreProperties
[0] = new PropertyValue();
265 aStoreProperties
[1] = new PropertyValue();
266 aStoreProperties
[0].Name
= "Override";
267 aStoreProperties
[0].Value
= true;
268 aStoreProperties
[1].Name
= "FilterName";
269 aStoreProperties
[1].Value
= sFilter
;
270 XStorable xStorable
= (XStorable
)UnoRuntime
.queryInterface(
273 xStorable
.storeToURL(sFileUrl
, aStoreProperties
);
274 return util
.WriterTools
.loadTextDoc(m_xMsf
, sFileUrl
);
276 if(util
.utils
.fileExists(m_xMsf
, sFileUrl
))
277 util
.utils
.deleteFile(m_xMsf
, sFileUrl
);