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