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