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