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