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