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 complex.writer;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
31*cdf0e10cSrcweir import com.sun.star.container.XNamed;
32*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
33*cdf0e10cSrcweir import com.sun.star.frame.XStorable;
34*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
36*cdf0e10cSrcweir import com.sun.star.text.XBookmarksSupplier;
37*cdf0e10cSrcweir import com.sun.star.text.XSimpleText;
38*cdf0e10cSrcweir import com.sun.star.text.XText;
39*cdf0e10cSrcweir import com.sun.star.text.XTextContent;
40*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
41*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
42*cdf0e10cSrcweir import com.sun.star.text.XTextRange;
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import complexlib.ComplexTestCase;
45*cdf0e10cSrcweir import java.math.BigInteger;
46*cdf0e10cSrcweir import org.junit.After;
47*cdf0e10cSrcweir import org.junit.AfterClass;
48*cdf0e10cSrcweir import org.junit.Before;
49*cdf0e10cSrcweir import org.junit.BeforeClass;
50*cdf0e10cSrcweir import org.junit.Test;
51*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
52*cdf0e10cSrcweir import static org.junit.Assert.*;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir class BookmarkHashes {
55*cdf0e10cSrcweir     public BigInteger m_nSetupHash;
56*cdf0e10cSrcweir     public BigInteger m_nInsertRandomHash;
57*cdf0e10cSrcweir     public BigInteger m_nDeleteRandomHash;
58*cdf0e10cSrcweir     public BigInteger m_nLinebreakHash;
59*cdf0e10cSrcweir     public BigInteger m_nOdfReloadHash;
60*cdf0e10cSrcweir     public BigInteger m_nMsWordReloadHash;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     public void assertExpectation(BookmarkHashes aExpectation) {
63*cdf0e10cSrcweir         assertEquals(aExpectation.m_nSetupHash, m_nSetupHash);
64*cdf0e10cSrcweir         assertEquals(aExpectation.m_nInsertRandomHash, m_nInsertRandomHash);
65*cdf0e10cSrcweir         assertEquals(aExpectation.m_nDeleteRandomHash, m_nDeleteRandomHash);
66*cdf0e10cSrcweir         assertEquals(aExpectation.m_nLinebreakHash, m_nLinebreakHash);
67*cdf0e10cSrcweir         assertEquals(aExpectation.m_nOdfReloadHash, m_nOdfReloadHash);
68*cdf0e10cSrcweir         assertEquals(aExpectation.m_nMsWordReloadHash, m_nMsWordReloadHash);
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     static public java.math.BigInteger getBookmarksHash(XTextDocument xDoc)
72*cdf0e10cSrcweir         throws com.sun.star.uno.Exception, java.security.NoSuchAlgorithmException
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         StringBuffer buffer = new StringBuffer("");
75*cdf0e10cSrcweir         XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)UnoRuntime.queryInterface(
76*cdf0e10cSrcweir             XBookmarksSupplier.class,
77*cdf0e10cSrcweir             xDoc);
78*cdf0e10cSrcweir         XNameAccess xBookmarks = xBookmarksSupplier.getBookmarks();
79*cdf0e10cSrcweir         for(String sBookmarkname : xBookmarks.getElementNames()) {
80*cdf0e10cSrcweir             Object xBookmark = xBookmarks.getByName(sBookmarkname);
81*cdf0e10cSrcweir             XTextContent xBookmarkAsContent = (XTextContent)UnoRuntime.queryInterface(
82*cdf0e10cSrcweir                 XTextContent.class,
83*cdf0e10cSrcweir                 xBookmark);
84*cdf0e10cSrcweir             buffer.append(sBookmarkname);
85*cdf0e10cSrcweir             buffer.append(":");
86*cdf0e10cSrcweir             buffer.append(xBookmarkAsContent.getAnchor().getString());
87*cdf0e10cSrcweir             buffer.append(";");
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir         java.security.MessageDigest sha1 = java.security.MessageDigest.getInstance("SHA-1");
90*cdf0e10cSrcweir         sha1.reset();
91*cdf0e10cSrcweir         sha1.update(buffer.toString().getBytes());
92*cdf0e10cSrcweir         return new java.math.BigInteger(sha1.digest());
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir public class CheckBookmarks {
97*cdf0e10cSrcweir     private XMultiServiceFactory m_xMsf = null;
98*cdf0e10cSrcweir     private XTextDocument m_xDoc = null;
99*cdf0e10cSrcweir     private XTextDocument m_xOdfReloadedDoc = null;
100*cdf0e10cSrcweir     private XTextDocument m_xMsWordReloadedDoc = null;
101*cdf0e10cSrcweir     private final BookmarkHashes actualHashes = new BookmarkHashes();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     private BookmarkHashes getDEV300m41Expectations() {
104*cdf0e10cSrcweir         BookmarkHashes result = new BookmarkHashes();
105*cdf0e10cSrcweir         result.m_nSetupHash = new BigInteger("-4b0706744e8452fe1ae9d5e1c28cf70fb6194795",16);
106*cdf0e10cSrcweir         result.m_nInsertRandomHash = new BigInteger("25aa0fad3f4881832dcdfe658ec2efa8a1a02bc5",16);
107*cdf0e10cSrcweir         result.m_nDeleteRandomHash = new BigInteger("-3ec87e810b46d734677c351ad893bbbf9ea10f55",16);
108*cdf0e10cSrcweir         result.m_nLinebreakHash = new BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
109*cdf0e10cSrcweir         result.m_nOdfReloadHash = new BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
110*cdf0e10cSrcweir         result.m_nMsWordReloadHash = new BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
111*cdf0e10cSrcweir         return result;
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     @Test public void checkBookmarks()
115*cdf0e10cSrcweir         throws com.sun.star.uno.Exception,
116*cdf0e10cSrcweir             com.sun.star.io.IOException,
117*cdf0e10cSrcweir             java.security.NoSuchAlgorithmException
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         actualHashes.assertExpectation(getDEV300m41Expectations());
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     @Before public void setUpDocuments() throws Exception {
123*cdf0e10cSrcweir         m_xMsf = UnoRuntime.queryInterface(
124*cdf0e10cSrcweir             XMultiServiceFactory.class,
125*cdf0e10cSrcweir             connection.getComponentContext().getServiceManager());
126*cdf0e10cSrcweir         m_xDoc = util.WriterTools.createTextDoc(m_xMsf);
127*cdf0e10cSrcweir         setupBookmarks();
128*cdf0e10cSrcweir         actualHashes.m_nSetupHash = BookmarkHashes.getBookmarksHash(m_xDoc);
129*cdf0e10cSrcweir         insertRandomParts(200177);
130*cdf0e10cSrcweir         actualHashes.m_nInsertRandomHash = BookmarkHashes.getBookmarksHash(m_xDoc);
131*cdf0e10cSrcweir         deleteRandomParts(4711);
132*cdf0e10cSrcweir         actualHashes.m_nDeleteRandomHash = BookmarkHashes.getBookmarksHash(m_xDoc);
133*cdf0e10cSrcweir         insertLinebreaks(007);
134*cdf0e10cSrcweir         actualHashes.m_nLinebreakHash = BookmarkHashes.getBookmarksHash(m_xDoc);
135*cdf0e10cSrcweir         m_xOdfReloadedDoc = reloadFrom("StarOffice XML (Writer)", "odf");
136*cdf0e10cSrcweir         actualHashes.m_nOdfReloadHash = BookmarkHashes.getBookmarksHash(m_xOdfReloadedDoc);
137*cdf0e10cSrcweir         m_xMsWordReloadedDoc = reloadFrom("MS Word 97", "doc");
138*cdf0e10cSrcweir         actualHashes.m_nMsWordReloadHash = BookmarkHashes.getBookmarksHash(m_xMsWordReloadedDoc);
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     @After public void tearDownDocuments() {
142*cdf0e10cSrcweir         util.DesktopTools.closeDoc(m_xDoc);
143*cdf0e10cSrcweir         util.DesktopTools.closeDoc(m_xOdfReloadedDoc);
144*cdf0e10cSrcweir         util.DesktopTools.closeDoc(m_xMsWordReloadedDoc);
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
148*cdf0e10cSrcweir         connection.setUp();
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
152*cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         connection.tearDown();
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     private void setupBookmarks()
160*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         XText xText = m_xDoc.getText();
163*cdf0e10cSrcweir         XSimpleText xSimpleText = (XSimpleText)UnoRuntime.queryInterface(
164*cdf0e10cSrcweir             XSimpleText.class,
165*cdf0e10cSrcweir             xText);
166*cdf0e10cSrcweir         for(int nPara=0; nPara<10; ++nPara) {
167*cdf0e10cSrcweir             for(int nBookmark=0; nBookmark<100; ++nBookmark){
168*cdf0e10cSrcweir                 insertBookmark(
169*cdf0e10cSrcweir                     xText.createTextCursor(),
170*cdf0e10cSrcweir                     "P" + nPara + "word" + nBookmark,
171*cdf0e10cSrcweir                     "P" + nPara + "word" + nBookmark);
172*cdf0e10cSrcweir                 XTextCursor xWordCrsr = xText.createTextCursor();
173*cdf0e10cSrcweir                 xWordCrsr.setString(" ");
174*cdf0e10cSrcweir             }
175*cdf0e10cSrcweir             XTextCursor xParaCrsr = xText.createTextCursor();
176*cdf0e10cSrcweir             XTextRange xParaCrsrAsRange = (XTextRange)UnoRuntime.queryInterface(
177*cdf0e10cSrcweir                 XTextRange.class,
178*cdf0e10cSrcweir                 xParaCrsr);
179*cdf0e10cSrcweir             xText.insertControlCharacter(xParaCrsrAsRange, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     private void insertRandomParts(long seed)
184*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
185*cdf0e10cSrcweir     {
186*cdf0e10cSrcweir         java.util.Random rnd = new java.util.Random(seed);
187*cdf0e10cSrcweir         XTextCursor xCrsr = m_xDoc.getText().createTextCursor();
188*cdf0e10cSrcweir         for(int i=0; i<600; i++) {
189*cdf0e10cSrcweir             xCrsr.goRight((short)rnd.nextInt(100), false);
190*cdf0e10cSrcweir             xCrsr.setString(Long.toString(rnd.nextLong()));
191*cdf0e10cSrcweir         }
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     private void deleteRandomParts(long seed)
195*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         java.util.Random rnd = new java.util.Random(seed);
198*cdf0e10cSrcweir         XTextCursor xCrsr = m_xDoc.getText().createTextCursor();
199*cdf0e10cSrcweir         for(int i=0; i<600; i++) {
200*cdf0e10cSrcweir             xCrsr.goRight((short)rnd.nextInt(100), false);
201*cdf0e10cSrcweir             xCrsr.goRight((short)rnd.nextInt(20), true);
202*cdf0e10cSrcweir             xCrsr.setString("");
203*cdf0e10cSrcweir         }
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     private void insertLinebreaks(long seed)
207*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         XText xText = m_xDoc.getText();
210*cdf0e10cSrcweir         java.util.Random rnd = new java.util.Random(seed);
211*cdf0e10cSrcweir         XTextCursor xCrsr = m_xDoc.getText().createTextCursor();
212*cdf0e10cSrcweir         for(int i=0; i<30; i++) {
213*cdf0e10cSrcweir             xCrsr.goRight((short)rnd.nextInt(300), false);
214*cdf0e10cSrcweir             XTextRange xCrsrAsRange = (XTextRange)UnoRuntime.queryInterface(
215*cdf0e10cSrcweir                 XTextRange.class,
216*cdf0e10cSrcweir                 xCrsr);
217*cdf0e10cSrcweir             xText.insertControlCharacter(xCrsrAsRange, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
218*cdf0e10cSrcweir         }
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     private void insertBookmark(XTextCursor crsr, String name, String content)
222*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir         XMultiServiceFactory xDocFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
225*cdf0e10cSrcweir             XMultiServiceFactory.class,
226*cdf0e10cSrcweir             m_xDoc);
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir         Object xBookmark = xDocFactory.createInstance("com.sun.star.text.Bookmark");
229*cdf0e10cSrcweir         XTextContent xBookmarkAsTextContent = (XTextContent)UnoRuntime.queryInterface(
230*cdf0e10cSrcweir             XTextContent.class,
231*cdf0e10cSrcweir             xBookmark);
232*cdf0e10cSrcweir         crsr.setString(content);
233*cdf0e10cSrcweir         XNamed xBookmarkAsNamed = (XNamed)UnoRuntime.queryInterface(
234*cdf0e10cSrcweir             XNamed.class,
235*cdf0e10cSrcweir             xBookmark);
236*cdf0e10cSrcweir         xBookmarkAsNamed.setName(name);
237*cdf0e10cSrcweir         m_xDoc.getText().insertTextContent(crsr, xBookmarkAsTextContent, true);
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     private XTextDocument reloadFrom(String sFilter, String sExtension)
241*cdf0e10cSrcweir         throws com.sun.star.io.IOException
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         String sFileUrl = util.utils.getOfficeTemp(m_xMsf) + "/Bookmarktest." + sExtension;
244*cdf0e10cSrcweir         try {
245*cdf0e10cSrcweir             PropertyValue[] aStoreProperties = new PropertyValue[2];
246*cdf0e10cSrcweir             aStoreProperties[0] = new PropertyValue();
247*cdf0e10cSrcweir             aStoreProperties[1] = new PropertyValue();
248*cdf0e10cSrcweir             aStoreProperties[0].Name = "Override";
249*cdf0e10cSrcweir             aStoreProperties[0].Value = true;
250*cdf0e10cSrcweir             aStoreProperties[1].Name = "FilterName";
251*cdf0e10cSrcweir             aStoreProperties[1].Value = sFilter;
252*cdf0e10cSrcweir             XStorable xStorable = (XStorable)UnoRuntime.queryInterface(
253*cdf0e10cSrcweir                 XStorable.class,
254*cdf0e10cSrcweir                 m_xDoc);
255*cdf0e10cSrcweir             xStorable.storeToURL(sFileUrl, aStoreProperties);
256*cdf0e10cSrcweir             return util.WriterTools.loadTextDoc(m_xMsf, sFileUrl);
257*cdf0e10cSrcweir         } finally {
258*cdf0e10cSrcweir             if(util.utils.fileExists(m_xMsf, sFileUrl))
259*cdf0e10cSrcweir                 util.utils.deleteFile(m_xMsf, sFileUrl);
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir     }
262*cdf0e10cSrcweir }
263