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