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