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 complexlib.ComplexTestCase;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.AnyConverter;
29 import com.sun.star.uno.XComponentContext;
30 import com.sun.star.lang.WrappedTargetException;
31 import com.sun.star.lang.WrappedTargetRuntimeException;
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XServiceInfo;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.Pair;
39 import com.sun.star.util.XCloseable;
40 import com.sun.star.frame.XStorable;
41 import com.sun.star.document.DocumentEvent;
42 import com.sun.star.document.XDocumentEventBroadcaster;
43 import com.sun.star.document.XDocumentEventListener;
44 import com.sun.star.text.XTextDocument;
45 
46 import java.util.List;
47 import java.util.ArrayList;
48 import java.io.File;
49 
50 /**
51  * a small program to load documents from one directory (recursively)
52  * and store them in another, implemented as a complex test.
53  */
54 public class LoadSaveTest extends ComplexTestCase
55 {
56     private XMultiServiceFactory m_xMSF = null;
57     private XComponentContext m_xContext = null;
58     private XDocumentEventBroadcaster m_xGEB = null;
59     private String m_TmpDir = null;
60 
61     private String m_fileURL = "file://";
62     // these should be parameters or something?
63     private String m_SourceDir = "FIXME";
64     private String m_TargetDir = "/tmp/out";
65 
getTestMethodNames()66     public String[] getTestMethodNames() {
67         return new String[] { "testLoadStore" };
68     }
69 
before()70     public void before() throws Exception
71     {
72         m_xMSF = (XMultiServiceFactory) param.getMSF();
73         XPropertySet xPropertySet = (XPropertySet)
74             UnoRuntime.queryInterface(XPropertySet.class, m_xMSF);
75         Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext");
76         m_xContext = (XComponentContext)
77             UnoRuntime.queryInterface(XComponentContext.class, defaultCtx);
78         assure("could not get component context.", m_xContext != null);
79         Object oGEB = m_xMSF.createInstance(
80                 "com.sun.star.frame.GlobalEventBroadcaster");
81         m_xGEB = (XDocumentEventBroadcaster)
82             UnoRuntime.queryInterface(XDocumentEventBroadcaster.class, oGEB);
83         assure("could not get global event broadcaster.", m_xGEB != null);
84         m_TmpDir = util.utils.getOfficeTemp/*Dir*/(m_xMSF);
85         log.println("tempdir: " + m_TmpDir);
86         log.println("sourcedir: " + m_SourceDir);
87         log.println("targetdir: " + m_TargetDir);
88     }
89 
90     /*
91     public void after()
92     {
93     }
94     */
95 
testLoadStore()96     public void testLoadStore() throws Exception
97     {
98         Pair<List<String>, List<String>> dirsFiles =
99             getDirAndFileNames(m_SourceDir);
100         makeDirs(m_TargetDir, dirsFiles.First);
101         for (String fileName : dirsFiles.Second)
102         {
103             try {
104                 testDoc(fileName);
105             } catch (Exception e) {
106                 report(e);
107             }
108         }
109     }
110 
testDoc(String fileName)111     public void testDoc(String fileName) throws Exception
112     {
113         XComponent xDoc = null;
114         EventListener xListener = new EventListener();
115         try {
116             m_xGEB.addDocumentEventListener(xListener);
117 
118             log.println("Loading document: " + fileName + " ...");
119 
120             PropertyValue[] loadProps = new PropertyValue[1];
121             loadProps[0] = new PropertyValue();
122             loadProps[0].Name = "ReadOnly";
123             loadProps[0].Value = new Boolean(true);
124 
125             String sourceFile = m_fileURL + m_SourceDir + fileName;
126 
127             xDoc = util.DesktopTools.loadDoc(m_xMSF, sourceFile, loadProps);
128 
129             log.println("... done");
130 
131             {
132                 // apparently OnLayoutFinished is not sent for every doc???
133                 // 10 seconds is evidently not enough for large documents
134                 int time = 0;
135                 while (!xListener.IsLayoutFinished() && (time < 30000)) {
136                     Thread.sleep(100);
137                     time += 100;
138                 }
139                 if (time >= 30000) {
140                     log.println("timeout: no OnLayoutFinished received!");
141                 }
142             }
143 
144             log.println("Storing document: " + fileName + " ...");
145 
146             XStorable xStor = (XStorable) UnoRuntime.queryInterface(
147                         XStorable.class, xDoc);
148 
149             String targetFile = m_fileURL + m_TargetDir + fileName;
150 
151             xStor.storeToURL(targetFile, new PropertyValue[0]);
152 
153             log.println("... done");
154 
155         } finally {
156             if (xDoc != null) {
157                 util.DesktopTools.closeDoc(xDoc);
158             }
159             if (xListener != null) {
160                 m_xGEB.removeDocumentEventListener(xListener);
161             }
162         }
163     }
164 
165     class EventListener implements XDocumentEventListener
166     {
167         boolean m_isLayoutFinished = false;
IsLayoutFinished()168         boolean IsLayoutFinished() { return m_isLayoutFinished; }
documentEventOccured(DocumentEvent Event)169         public void documentEventOccured(DocumentEvent Event)
170         {
171 //            log.println("event: " + Event.EventName);
172             if ("OnLayoutFinished".equals(Event.EventName))
173             {
174                 // we only have one doc at any time, so no need to check
175                 m_isLayoutFinished = true;
176 //                log.println("received OnLayoutFinished");
177             }
178         }
disposing(EventObject Event)179         public void disposing(EventObject Event) { }
180     }
181 
report2(Exception e)182     void report2(Exception e)
183     {
184         if (e instanceof WrappedTargetException)
185         {
186             log.println("Cause:");
187             Exception cause = (Exception)
188                 (((WrappedTargetException)e).TargetException);
189             log.println(cause.toString());
190             report2(cause);
191         } else if (e instanceof WrappedTargetRuntimeException) {
192             log.println("Cause:");
193             Exception cause = (Exception)
194                 (((WrappedTargetRuntimeException)e).TargetException);
195             log.println(cause.toString());
196             report2(cause);
197         }
198     }
199 
report(Exception e)200     void report(Exception e) {
201         log.println("Exception occurred:");
202         log.println(e.toString());
203         e.printStackTrace((java.io.PrintWriter) log);
204         report2(e);
205 //        failed();
206     }
207 
getDirAndFileNames(String dir)208     Pair<List<String>, List<String>> getDirAndFileNames(String dir)
209     {
210         List<String> dirs = new ArrayList<String>();
211         List<String> files = new ArrayList<String>();
212         File root = new File(dir);
213         getDirAndFileNames(root, "", dirs, files);
214         return new Pair<List<String>, List<String>>(dirs, files);
215     }
216 
getDirAndFileNames(File file, String relPath, List<String> dirs, List<String> files)217     void getDirAndFileNames(File file, String relPath,
218             List<String> dirs, List<String> files)
219     {
220         assure("does not exist: " + relPath, file.exists());
221         if (file.isDirectory()) {
222             dirs.add(relPath);
223             File[] subfiles = file.listFiles();
224             for (File subfile : subfiles)
225             {
226                 String subfileName =
227                     relPath + File.separator + subfile.getName();
228                 getDirAndFileNames(subfile, subfileName, dirs, files);
229             }
230         }
231         else if (file.isFile()) {
232             if (file.getName().endsWith(".odt")) {
233                 files.add(relPath);
234             }
235         }
236     }
237 
makeDirs(String target, List<String> dirs)238     void makeDirs(String target, List<String> dirs) throws Exception
239     {
240         for (String dir : dirs) {
241             File f = new File(target + dir);
242             if (!f.exists()) {
243                 if (!f.mkdir()) {
244                     throw new Exception("cannot mkdir: " + target + dir);
245                 }
246             }
247         }
248     }
249 }
250 
251