1*76b6b121SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*76b6b121SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*76b6b121SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*76b6b121SAndrew Rist * distributed with this work for additional information 6*76b6b121SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*76b6b121SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*76b6b121SAndrew Rist * "License"); you may not use this file except in compliance 9*76b6b121SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*76b6b121SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*76b6b121SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*76b6b121SAndrew Rist * software distributed under the License is distributed on an 15*76b6b121SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*76b6b121SAndrew Rist * KIND, either express or implied. See the License for the 17*76b6b121SAndrew Rist * specific language governing permissions and limitations 18*76b6b121SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*76b6b121SAndrew Rist *************************************************************/ 21*76b6b121SAndrew Rist 22*76b6b121SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package complex.loadAllDocuments; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 27cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag; 28cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 29cdf0e10cSrcweir import com.sun.star.frame.XFrame; 30cdf0e10cSrcweir import com.sun.star.frame.XStorable; 31cdf0e10cSrcweir import com.sun.star.io.XInputStream; 32cdf0e10cSrcweir import com.sun.star.lang.XComponent; 33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 35cdf0e10cSrcweir import com.sun.star.util.XCloseable; 36cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 37cdf0e10cSrcweir 38cdf0e10cSrcweir 39cdf0e10cSrcweir import helper.URLHelper; 40cdf0e10cSrcweir 41cdf0e10cSrcweir import java.io.File; 42cdf0e10cSrcweir import java.io.InputStreamReader; 43cdf0e10cSrcweir 44cdf0e10cSrcweir import java.util.Enumeration; 45cdf0e10cSrcweir import java.util.Vector; 46cdf0e10cSrcweir 47cdf0e10cSrcweir // ---------- junit imports ----------------- 48cdf0e10cSrcweir import org.junit.After; 49cdf0e10cSrcweir import org.junit.AfterClass; 50cdf0e10cSrcweir import org.junit.Before; 51cdf0e10cSrcweir import org.junit.BeforeClass; 52cdf0e10cSrcweir import org.junit.Test; 53cdf0e10cSrcweir import org.openoffice.test.OfficeConnection; 54cdf0e10cSrcweir import org.openoffice.test.OfficeFileUrl; 55cdf0e10cSrcweir import static org.junit.Assert.*; 56cdf0e10cSrcweir // ------------------------------------------ 57cdf0e10cSrcweir 58cdf0e10cSrcweir //----------------------------------------------- 59cdf0e10cSrcweir /** @short Check the interface method XComponentLoader.loadComponentFromURL() 60cdf0e10cSrcweir 61cdf0e10cSrcweir @descr A prerequisite for this test is a server which allows access to files 62cdf0e10cSrcweir that will be loaded via three different access methods: 63cdf0e10cSrcweir <ul> 64cdf0e10cSrcweir <li>1. nfs (mounted directory / mapped network drive)</li> 65cdf0e10cSrcweir <li>2. ftp</li> 66cdf0e10cSrcweir <li>3. http</li> 67cdf0e10cSrcweir </ul> 68cdf0e10cSrcweir <p> 69cdf0e10cSrcweir The test will look for a list of files from the <i>TestDocumentPath</i> 70cdf0e10cSrcweir on and load these files from the mounted directory, via ftp and http. 71cdf0e10cSrcweir The parameters for this have to be "ftp_access" and "http_access". 72cdf0e10cSrcweir If they are not given, tests for ftp and http will fail. 73cdf0e10cSrcweir 74cdf0e10cSrcweir @todo We need a further test for accessing UNC pathes on windows! 75cdf0e10cSrcweir */ 76cdf0e10cSrcweir public class CheckXComponentLoader 77cdf0e10cSrcweir { 78cdf0e10cSrcweir //------------------------------------------- 79cdf0e10cSrcweir // some const 80cdf0e10cSrcweir 81cdf0e10cSrcweir /** used to classify the result of a loadComponentFromURL() request. */ 82cdf0e10cSrcweir private static final int RESULT_UNKNOWN = 0; 83cdf0e10cSrcweir private static final int RESULT_VALID_DOC = 1; 84cdf0e10cSrcweir private static final int RESULT_EMPTY_DOC = 2; 85cdf0e10cSrcweir private static final int RESULT_ILLEGALARGUMENTEXCEPTION = 3; 86cdf0e10cSrcweir private static final int RESULT_IOEXCEPTION = 4; 87cdf0e10cSrcweir private static final int RESULT_RUNTIMEEXCEPTION = 5; 88cdf0e10cSrcweir private static final int RESULT_EXCEPTION = 6; 89cdf0e10cSrcweir 90cdf0e10cSrcweir /** File/URL separators. */ 91cdf0e10cSrcweir private static final String fs_url = "/"; 92cdf0e10cSrcweir // private static final String fs_sys = System.getProperty("file.separator"); 93cdf0e10cSrcweir 94cdf0e10cSrcweir /** used for testing password protected files. */ 95cdf0e10cSrcweir private static final String SUFFIX_PASSWORD_TEMPFILE = "password_"; 96cdf0e10cSrcweir private static final String PREFIX_PASSWORD_TEMPFILE = ".sxw"; 97cdf0e10cSrcweir private static final String DEFAULT_PASSWORD = "DefaultPasswordForComponentLoaderTest"; 98cdf0e10cSrcweir 99cdf0e10cSrcweir //------------------------------------------- 100cdf0e10cSrcweir // member 101cdf0e10cSrcweir 102cdf0e10cSrcweir /** points to the global uno service manager. */ 103cdf0e10cSrcweir private XMultiServiceFactory m_xMSF = null; 104cdf0e10cSrcweir 105cdf0e10cSrcweir /** provides XComponentLoader interface. */ 106cdf0e10cSrcweir private XFrame m_xDesktop = null; 107cdf0e10cSrcweir 108cdf0e10cSrcweir /** provides XComponentLoader interface too. */ 109cdf0e10cSrcweir private XFrame m_xFrame = null; 110cdf0e10cSrcweir 111cdf0e10cSrcweir /** will be set to xDesktop OR xFrame. */ 112cdf0e10cSrcweir private XComponentLoader m_xLoader = null; 113cdf0e10cSrcweir 114cdf0e10cSrcweir /** can be used to open local files as stream. */ 115cdf0e10cSrcweir private XSimpleFileAccess m_xStreamProvider = null; 116cdf0e10cSrcweir 117cdf0e10cSrcweir /** directory for creating temp. files. */ 118cdf0e10cSrcweir private String m_sTempPath = null; 119cdf0e10cSrcweir 120cdf0e10cSrcweir /** directory for searching files to load */ 121cdf0e10cSrcweir private String m_sTestDocPath = null; 122cdf0e10cSrcweir 123cdf0e10cSrcweir /** files of m_sTestDocPath to test. */ 124cdf0e10cSrcweir private static Vector m_lTestFiles = null; 125cdf0e10cSrcweir 126cdf0e10cSrcweir //------------------------------------------- 127cdf0e10cSrcweir // test environment 128cdf0e10cSrcweir 129cdf0e10cSrcweir //------------------------------------------- 130cdf0e10cSrcweir /** @short A function to tell the framework, 131cdf0e10cSrcweir which test functions are available. 132cdf0e10cSrcweir 133cdf0e10cSrcweir @return All test methods. 134cdf0e10cSrcweir @todo Think about selection of tests from outside ... 135cdf0e10cSrcweir */ 136cdf0e10cSrcweir // public String[] getTestMethodNames() 137cdf0e10cSrcweir // { 138cdf0e10cSrcweir // // TODO think about trigger of sub-tests from outside 139cdf0e10cSrcweir // return new String[] 140cdf0e10cSrcweir // { 141cdf0e10cSrcweir // "checkURLEncoding" , 142cdf0e10cSrcweir // "checkURLHandling" , 143cdf0e10cSrcweir // "checkUsingOfMediaDescriptor", 144cdf0e10cSrcweir // "checkStreamLoading" , 145cdf0e10cSrcweir // "checkLoadingWithPassword" 146cdf0e10cSrcweir // }; 147cdf0e10cSrcweir // } 148cdf0e10cSrcweir 149cdf0e10cSrcweir //------------------------------------------- 150cdf0e10cSrcweir /** @short Create the environment for following tests. 151cdf0e10cSrcweir 152cdf0e10cSrcweir @descr Use either a component loader from desktop or 153cdf0e10cSrcweir from frame 154cdf0e10cSrcweir */ before()155cdf0e10cSrcweir @Before public void before() 156cdf0e10cSrcweir { 157cdf0e10cSrcweir // get uno service manager from global test environment 158cdf0e10cSrcweir m_xMSF = getMSF(); 159cdf0e10cSrcweir 160cdf0e10cSrcweir // create stream provider 161cdf0e10cSrcweir try 162cdf0e10cSrcweir { 163cdf0e10cSrcweir m_xStreamProvider = UnoRuntime.queryInterface(XSimpleFileAccess.class, m_xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess")); 164cdf0e10cSrcweir } 165cdf0e10cSrcweir catch(java.lang.Throwable ex) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir fail("Could not create a stream provider instance."); 168cdf0e10cSrcweir } 169cdf0e10cSrcweir 170cdf0e10cSrcweir // create desktop instance 171cdf0e10cSrcweir try 172cdf0e10cSrcweir { 173cdf0e10cSrcweir m_xDesktop = UnoRuntime.queryInterface(XFrame.class, m_xMSF.createInstance("com.sun.star.frame.Desktop")); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir catch(java.lang.Throwable ex) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir fail("Could not create the desktop instance."); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir // create frame instance 181cdf0e10cSrcweir m_xFrame = m_xDesktop.findFrame("testFrame_componentLoader", 182cdf0e10cSrcweir FrameSearchFlag.TASKS | FrameSearchFlag.CREATE); 183cdf0e10cSrcweir assertNotNull("Couldn't create test frame.", m_xFrame); 184cdf0e10cSrcweir 185cdf0e10cSrcweir // define default loader for testing 186cdf0e10cSrcweir // TODO think about using of bot loader instances! 187cdf0e10cSrcweir m_xLoader = UnoRuntime.queryInterface(XComponentLoader.class, m_xDesktop); 188cdf0e10cSrcweir assertNotNull("Desktop service doesnt support needed component loader interface.", m_xLoader); 189cdf0e10cSrcweir 190cdf0e10cSrcweir // get temp path for this environment 191cdf0e10cSrcweir final String tempDirURL = util.utils.getOfficeTemp/*Dir*/(getMSF()); 192cdf0e10cSrcweir m_sTempPath = graphical.FileHelper.getSystemPathFromFileURL(tempDirURL); 193cdf0e10cSrcweir // m_sTempPath = "."+fs_sys; 194cdf0e10cSrcweir 195cdf0e10cSrcweir // get all files from the given directory 196cdf0e10cSrcweir // TODO URLHelper should ignore directories! 197cdf0e10cSrcweir m_lTestFiles = new Vector(); 198cdf0e10cSrcweir final String sTestDocURL = OfficeFileUrl.getAbsolute(new File("testdocuments")); 199cdf0e10cSrcweir m_sTestDocPath = graphical.FileHelper.getSystemPathFromFileURL(sTestDocURL); 200cdf0e10cSrcweir try 201cdf0e10cSrcweir { 202cdf0e10cSrcweir File aBaseDir = new File(m_sTestDocPath); 203cdf0e10cSrcweir Vector lDirContent = URLHelper.getSystemFilesFromDir(aBaseDir.getPath()); 204cdf0e10cSrcweir Enumeration lList = lDirContent.elements(); 205cdf0e10cSrcweir int nBasePathLength = m_sTestDocPath.length(); 206cdf0e10cSrcweir while(lList.hasMoreElements()) 207cdf0e10cSrcweir { 208cdf0e10cSrcweir File aFile = (File)lList.nextElement(); 209cdf0e10cSrcweir 210cdf0e10cSrcweir // ignore broken links and directories at all 211cdf0e10cSrcweir if ( 212cdf0e10cSrcweir (!aFile.exists()) || 213cdf0e10cSrcweir (!aFile.isFile()) 214cdf0e10cSrcweir ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir continue; 217cdf0e10cSrcweir } 218cdf0e10cSrcweir 219cdf0e10cSrcweir String sCompletePath = aFile.getAbsolutePath(); 220cdf0e10cSrcweir String sSubPath = sCompletePath.substring(nBasePathLength); 221cdf0e10cSrcweir 222cdf0e10cSrcweir // Some test files are checked into CVS. ignore CVS helper files! 223cdf0e10cSrcweir // if (sSubPath.indexOf("CVS") > -1) 224cdf0e10cSrcweir // { 225cdf0e10cSrcweir // continue; 226cdf0e10cSrcweir // } 227cdf0e10cSrcweir 228cdf0e10cSrcweir m_lTestFiles.add(sSubPath); 229cdf0e10cSrcweir } 230cdf0e10cSrcweir } 231cdf0e10cSrcweir catch(java.lang.Throwable ex) 232cdf0e10cSrcweir { 233cdf0e10cSrcweir fail("Couldn't find test documents."); 234cdf0e10cSrcweir } 235cdf0e10cSrcweir } 236cdf0e10cSrcweir 237cdf0e10cSrcweir //------------------------------------------- 238cdf0e10cSrcweir /** @short close the environment. 239cdf0e10cSrcweir */ after()240cdf0e10cSrcweir @After public void after() 241cdf0e10cSrcweir { 242cdf0e10cSrcweir XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xFrame); 243cdf0e10cSrcweir try 244cdf0e10cSrcweir { 245cdf0e10cSrcweir xClose.close(false); 246cdf0e10cSrcweir } 247cdf0e10cSrcweir catch(com.sun.star.util.CloseVetoException exVeto) 248cdf0e10cSrcweir { fail("Test frame couldn't be closed successfully."); } 249cdf0e10cSrcweir 250cdf0e10cSrcweir m_xFrame = null; 251cdf0e10cSrcweir m_xLoader = null; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir 254cdf0e10cSrcweir //------------------------------------------- 255cdf0e10cSrcweir /** @short Look for files in the given directory for loading. 256cdf0e10cSrcweir */ checkUsingOfMediaDescriptor()257cdf0e10cSrcweir @Test public void checkUsingOfMediaDescriptor() 258cdf0e10cSrcweir { 259cdf0e10cSrcweir InteractionHandler xHandler = new InteractionHandler(); 260cdf0e10cSrcweir StatusIndicator xIndicator = new StatusIndicator(StatusIndicator.SHOWSTATUS_LOG); 261cdf0e10cSrcweir 262cdf0e10cSrcweir PropertyValue[] lProps = new PropertyValue[3]; 263cdf0e10cSrcweir 264cdf0e10cSrcweir lProps[0] = new PropertyValue(); 265cdf0e10cSrcweir lProps[0].Name = "Hidden"; 266cdf0e10cSrcweir lProps[0].Value = Boolean.TRUE; 267cdf0e10cSrcweir 268cdf0e10cSrcweir lProps[1] = new PropertyValue(); 269cdf0e10cSrcweir lProps[1].Name = "InteractionHandler"; 270cdf0e10cSrcweir lProps[1].Value = xHandler; 271cdf0e10cSrcweir 272cdf0e10cSrcweir lProps[2] = new PropertyValue(); 273cdf0e10cSrcweir lProps[2].Name = "StatusIndicator"; 274cdf0e10cSrcweir lProps[2].Value = xIndicator; 275cdf0e10cSrcweir 276cdf0e10cSrcweir Enumeration aSnapshot = m_lTestFiles.elements(); 277cdf0e10cSrcweir while (aSnapshot.hasMoreElements()) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir File aSysFile = new File(m_sTestDocPath, (String)aSnapshot.nextElement()); 280cdf0e10cSrcweir String sURL = URLHelper.getFileURLFromSystemPath(aSysFile); 281cdf0e10cSrcweir 282cdf0e10cSrcweir if (/*! (sURL.endsWith(".jpg") || 283cdf0e10cSrcweir sURL.endsWith(".gif"))*/ 284cdf0e10cSrcweir true 285cdf0e10cSrcweir ) 286cdf0e10cSrcweir { 287cdf0e10cSrcweir loadURL(m_xLoader, RESULT_VALID_DOC, sURL, "_blank", 0, lProps); 288cdf0e10cSrcweir // Its not needed to reset this using states! 289cdf0e10cSrcweir // Its done internaly ... 290cdf0e10cSrcweir if (!xIndicator.wasUsed()) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir System.out.println("External progress was not used for loading."); 293cdf0e10cSrcweir } 294cdf0e10cSrcweir if (xHandler.wasUsed()) 295cdf0e10cSrcweir { 296cdf0e10cSrcweir System.out.println("External interaction handler was not used for loading."); 297cdf0e10cSrcweir } 298cdf0e10cSrcweir } 299cdf0e10cSrcweir } 300cdf0e10cSrcweir } 301cdf0e10cSrcweir 302cdf0e10cSrcweir //------------------------------------------- 303cdf0e10cSrcweir /** TODO document me and move this method to a more global helper! */ impl_getTempFileName(String sTempPath, String sSuffix , String sPrefix )304cdf0e10cSrcweir private String impl_getTempFileName(String sTempPath, 305cdf0e10cSrcweir String sSuffix , 306cdf0e10cSrcweir String sPrefix ) 307cdf0e10cSrcweir { 308cdf0e10cSrcweir File aDir = new File(sTempPath); 309cdf0e10cSrcweir aDir.mkdirs(); 310cdf0e10cSrcweir // if (!aDir.exists()) 311cdf0e10cSrcweir // { 312cdf0e10cSrcweir // fail("Could not access temp directory \"" + sTempPath + "\"."); 313cdf0e10cSrcweir // } 314cdf0e10cSrcweir 315cdf0e10cSrcweir // TODO: create a temp file which not exist! 316cdf0e10cSrcweir for (int i=0; i<999999; ++i) 317cdf0e10cSrcweir { 318cdf0e10cSrcweir File aTempFile = new File(aDir, sSuffix+i+sPrefix); 319cdf0e10cSrcweir if (!aTempFile.exists()) 320cdf0e10cSrcweir { 321cdf0e10cSrcweir return aTempFile.getAbsolutePath(); 322cdf0e10cSrcweir } 323cdf0e10cSrcweir } 324cdf0e10cSrcweir 325cdf0e10cSrcweir fail("Seems that all temp file names are currently in use!"); 326cdf0e10cSrcweir return null; 327cdf0e10cSrcweir } 328cdf0e10cSrcweir 329cdf0e10cSrcweir //------------------------------------------- 330cdf0e10cSrcweir /** TODO document me and move this method to a more global helper! */ impl_createTempOfficeDocument(XComponentLoader xLoader , String sSourceURL, String sTargetURL, String sFilter , String sPassword )331cdf0e10cSrcweir private void impl_createTempOfficeDocument(XComponentLoader xLoader , 332cdf0e10cSrcweir String sSourceURL, 333cdf0e10cSrcweir String sTargetURL, 334cdf0e10cSrcweir String sFilter , 335cdf0e10cSrcweir String sPassword ) 336cdf0e10cSrcweir { 337cdf0e10cSrcweir PropertyValue[] lLoadProps = new PropertyValue[1]; 338cdf0e10cSrcweir 339cdf0e10cSrcweir lLoadProps[0] = new PropertyValue(); 340cdf0e10cSrcweir lLoadProps[0].Name = "Hidden"; 341cdf0e10cSrcweir lLoadProps[0].Value = Boolean.TRUE; 342cdf0e10cSrcweir 343cdf0e10cSrcweir PropertyValue[] lSaveProps = new PropertyValue[3]; 344cdf0e10cSrcweir 345cdf0e10cSrcweir lSaveProps[0] = new PropertyValue(); 346cdf0e10cSrcweir lSaveProps[0].Name = "FilterName"; 347cdf0e10cSrcweir lSaveProps[0].Value = sFilter; 348cdf0e10cSrcweir 349cdf0e10cSrcweir lSaveProps[1] = new PropertyValue(); 350cdf0e10cSrcweir lSaveProps[1].Name = "PassWord"; 351cdf0e10cSrcweir lSaveProps[1].Value = sPassword; 352cdf0e10cSrcweir 353cdf0e10cSrcweir lSaveProps[2] = new PropertyValue(); 354cdf0e10cSrcweir lSaveProps[2].Name = "Overwrite"; 355cdf0e10cSrcweir lSaveProps[2].Value = Boolean.TRUE; 356cdf0e10cSrcweir 357cdf0e10cSrcweir XComponent xDoc = null; 358cdf0e10cSrcweir try 359cdf0e10cSrcweir { 360cdf0e10cSrcweir // load it 361cdf0e10cSrcweir xDoc = xLoader.loadComponentFromURL(sSourceURL, "_blank", 0, lLoadProps); 362cdf0e10cSrcweir assertNotNull("Could create office document, which should be saved as temp one.", xDoc); 363cdf0e10cSrcweir 364cdf0e10cSrcweir // save it as temp file 365cdf0e10cSrcweir XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); 366cdf0e10cSrcweir xStore.storeAsURL(sTargetURL, lSaveProps); 367cdf0e10cSrcweir 368cdf0e10cSrcweir // Dont forget to close this file. Otherwise the temp file is locked! 369cdf0e10cSrcweir XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xDoc); 370cdf0e10cSrcweir xClose.close(false); 371cdf0e10cSrcweir } 372cdf0e10cSrcweir catch(java.lang.Throwable ex) 373cdf0e10cSrcweir { 374cdf0e10cSrcweir fail("Could not create temp office document."); 375cdf0e10cSrcweir } 376cdf0e10cSrcweir } 377cdf0e10cSrcweir 378cdf0e10cSrcweir //------------------------------------------- 379cdf0e10cSrcweir /** @short Check the password handling. 380cdf0e10cSrcweir 381cdf0e10cSrcweir @descr The used password is the one given 382cdf0e10cSrcweir as password for the ftp connection, 383cdf0e10cSrcweir or - if none given a default one. 384cdf0e10cSrcweir */ checkLoadingWithPassword()385cdf0e10cSrcweir @Test public void checkLoadingWithPassword() 386cdf0e10cSrcweir { 387cdf0e10cSrcweir String sTempFile = impl_getTempFileName(m_sTempPath, SUFFIX_PASSWORD_TEMPFILE, PREFIX_PASSWORD_TEMPFILE); 388cdf0e10cSrcweir File aTestFile = new File(sTempFile); 389cdf0e10cSrcweir String sTestURL = URLHelper.getFileURLFromSystemPath(aTestFile); 390cdf0e10cSrcweir 391cdf0e10cSrcweir impl_createTempOfficeDocument(m_xLoader, "private:factory/swriter", sTestURL, "StarOffice XML (Writer)", DEFAULT_PASSWORD); 392cdf0e10cSrcweir 393cdf0e10cSrcweir PropertyValue[] lArgs1 = new PropertyValue[2]; 394cdf0e10cSrcweir 395cdf0e10cSrcweir lArgs1[0] = new PropertyValue(); 396cdf0e10cSrcweir lArgs1[0].Name = "Hidden"; 397cdf0e10cSrcweir lArgs1[0].Value = Boolean.TRUE; 398cdf0e10cSrcweir 399cdf0e10cSrcweir lArgs1[1] = new PropertyValue(); 400cdf0e10cSrcweir lArgs1[1].Name = "Password"; 401cdf0e10cSrcweir lArgs1[1].Value = DEFAULT_PASSWORD; 402cdf0e10cSrcweir 403cdf0e10cSrcweir PropertyValue[] lArgs2 = new PropertyValue[1]; 404cdf0e10cSrcweir 405cdf0e10cSrcweir lArgs2[0] = new PropertyValue(); 406cdf0e10cSrcweir lArgs2[0].Name = "Hidden"; 407cdf0e10cSrcweir lArgs2[0].Value = Boolean.TRUE; 408cdf0e10cSrcweir 409cdf0e10cSrcweir loadURL(m_xLoader, RESULT_VALID_DOC, sTestURL, "_blank", 0, lArgs1); 410cdf0e10cSrcweir // TODO: wrong? loadURL(m_xLoader, RESULT_EMPTY_DOC, sTestURL, "_blank", 0, lArgs2); 411cdf0e10cSrcweir } 412cdf0e10cSrcweir 413cdf0e10cSrcweir /** 414cdf0e10cSrcweir * Check URL encoding. The first filename that matches "*.sxw" 415cdf0e10cSrcweir * is used as source for several encodings. 416cdf0e10cSrcweir */ checkURLEncoding()417cdf0e10cSrcweir @Test public void checkURLEncoding() { 418cdf0e10cSrcweir PropertyValue[] lProps = new PropertyValue[1]; 419cdf0e10cSrcweir 420cdf0e10cSrcweir lProps[0] = new PropertyValue(); 421cdf0e10cSrcweir lProps[0].Name = "Hidden"; 422cdf0e10cSrcweir lProps[0].Value = Boolean.TRUE; 423cdf0e10cSrcweir 424cdf0e10cSrcweir // first get encoding of this system 425cdf0e10cSrcweir InputStreamReader in = new InputStreamReader(System.in); 426cdf0e10cSrcweir String sSystemEncoding = in.getEncoding(); 427cdf0e10cSrcweir 428cdf0e10cSrcweir System.out.println("This system's encoding: " + sSystemEncoding); 429cdf0e10cSrcweir 430cdf0e10cSrcweir assertNotNull("Found an empty directory. There are no files for testing.", m_lTestFiles); 431cdf0e10cSrcweir 432cdf0e10cSrcweir 433cdf0e10cSrcweir // get a file name as byte array 434cdf0e10cSrcweir Enumeration aSnapshot = m_lTestFiles.elements(); 435cdf0e10cSrcweir byte[] baURL = null; 436cdf0e10cSrcweir 437cdf0e10cSrcweir while (aSnapshot.hasMoreElements()) { 438cdf0e10cSrcweir File aFile = new File(m_sTestDocPath, (String)aSnapshot.nextElement()); 439cdf0e10cSrcweir String sFile = URLHelper.getFileURLFromSystemPath(aFile); 440cdf0e10cSrcweir 441cdf0e10cSrcweir // take the first sxw file as stream 442cdf0e10cSrcweir if (sFile.endsWith(".sxw")) { 443cdf0e10cSrcweir baURL = sFile.getBytes(); 444cdf0e10cSrcweir 445cdf0e10cSrcweir break; 446cdf0e10cSrcweir } 447cdf0e10cSrcweir } 448cdf0e10cSrcweir 449cdf0e10cSrcweir assertNotNull("Found no file to load. Cannot test.", baURL); 450cdf0e10cSrcweir 451cdf0e10cSrcweir //construct several different encoded strings 452cdf0e10cSrcweir String[] sEncoding = new String[] { 453cdf0e10cSrcweir "US-ASCII", "TRUE", // us ascii encoding 454cdf0e10cSrcweir "ISO-8859-1", "TRUE", // iso encoding 455cdf0e10cSrcweir "UTF-8", "TRUE", // 8 bit utf encoding 456cdf0e10cSrcweir "UTF-16BE", "FALSE", // 16 bit big endian utf 457cdf0e10cSrcweir "UTF-16LE", "FALSE", // 16 bit little endian utf 458cdf0e10cSrcweir "UTF-16", "FALSE" // 16 bit, order specified by byte order mark 459cdf0e10cSrcweir 460cdf0e10cSrcweir }; 461cdf0e10cSrcweir 462cdf0e10cSrcweir for (int i = 0; i < sEncoding.length; i = i + 2) { 463cdf0e10cSrcweir try { 464cdf0e10cSrcweir String encURL = new String(baURL, sEncoding[i]); 465cdf0e10cSrcweir System.out.println("ENC[" + sEncoding[i] + "]"); 466cdf0e10cSrcweir 467cdf0e10cSrcweir if (sEncoding[i + 1].equals("TRUE")) { 468cdf0e10cSrcweir loadURL(m_xLoader, RESULT_VALID_DOC, encURL, "_blank", 0, 469cdf0e10cSrcweir lProps); 470cdf0e10cSrcweir } else { 471cdf0e10cSrcweir //with cws_loadenv01 changed to IllegalArgumentException 472cdf0e10cSrcweir loadURL(m_xLoader, RESULT_ILLEGALARGUMENTEXCEPTION, encURL, "_blank", 0, 473cdf0e10cSrcweir lProps); 474cdf0e10cSrcweir } 475cdf0e10cSrcweir } catch (java.io.UnsupportedEncodingException e) { 476cdf0e10cSrcweir fail("Unsopported Encoding: " + sEncoding[i] + 477cdf0e10cSrcweir "\n Not able to test encoding on this platform."); 478cdf0e10cSrcweir } 479cdf0e10cSrcweir } 480cdf0e10cSrcweir } 481cdf0e10cSrcweir 482cdf0e10cSrcweir /** 483cdf0e10cSrcweir * Check url handling with a load of URLs. 484cdf0e10cSrcweir * 1. unsupported URLs. 485cdf0e10cSrcweir * 2. "stupid" URLs 486cdf0e10cSrcweir * 3. case sensitive URLs 487cdf0e10cSrcweir * 4. FTP URLs 488cdf0e10cSrcweir * 5. HTTP URLs 489cdf0e10cSrcweir */ 490cdf0e10cSrcweir // public void checkURLHandling() { 491cdf0e10cSrcweir // PropertyValue[] lProps = new PropertyValue[1]; 492cdf0e10cSrcweir // 493cdf0e10cSrcweir // lProps[0] = new PropertyValue(); 494cdf0e10cSrcweir // lProps[0].Name = "Hidden"; 495cdf0e10cSrcweir // lProps[0].Value = Boolean.TRUE; 496cdf0e10cSrcweir // 497cdf0e10cSrcweir // System.out.println("check possible but unsupported URLs"); 498cdf0e10cSrcweir // 499cdf0e10cSrcweir // String[] sIllegalArgs = new String[] { 500cdf0e10cSrcweir // "slot:5000", "slot:10909", ".uno:SaveAs", ".uno:Open", 501cdf0e10cSrcweir // }; 502cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_ILLEGALARGUMENTEXCEPTION, sIllegalArgs, 503cdf0e10cSrcweir // "_blank", 0, lProps); 504cdf0e10cSrcweir // 505cdf0e10cSrcweir // System.out.println("check stupid URLs"); 506cdf0e10cSrcweir // 507cdf0e10cSrcweir // sIllegalArgs = new String[] { 508cdf0e10cSrcweir // "slot:xxx", "slot:111111111", ".uno:save_as", ".uno:open_this", 509cdf0e10cSrcweir // ".UnO:*", 510cdf0e10cSrcweir // }; 511cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_ILLEGALARGUMENTEXCEPTION, sIllegalArgs, 512cdf0e10cSrcweir // "_blank", 0, lProps); 513cdf0e10cSrcweir // 514cdf0e10cSrcweir // String[] sEmptyDocs = new String[] { 515cdf0e10cSrcweir // "mailo:hansi.meier@germany.sun.com", "file:/c:\\test/file.cxx", 516cdf0e10cSrcweir // "file:///c|:\\test/file.cxx", "http_server://staroffice-doc\\", 517cdf0e10cSrcweir // "c:\\\\test///\\test.sxw", "news_:staroffice-doc", 518cdf0e10cSrcweir // "newsletter@blubber", "private_factory/swriter", 519cdf0e10cSrcweir // "private:factory//swriter", "private:factory/swriter/___", 520cdf0e10cSrcweir // "c:\\test\\test.sxw", "macro:///ImportWizard.Main.Main", 521cdf0e10cSrcweir // "macro:///Euro.AutoPilotRun.StartAutoPilot", 522cdf0e10cSrcweir // "service:com.sun.star.frame.Frame", 523cdf0e10cSrcweir // "mailto:steffen.grund@germany.sun.com", "news:staroffice-doc", 524cdf0e10cSrcweir // "macro:/ExportWizard", "macro://Euro.AutoPilotRun.StartAutoPilot", 525cdf0e10cSrcweir // "service:com.sun.star.frame." 526cdf0e10cSrcweir // }; 527cdf0e10cSrcweir // 528cdf0e10cSrcweir // //with cws_loadenv01 changed to IllegalArgumentException 529cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_ILLEGALARGUMENTEXCEPTION, sEmptyDocs, "_blank", 0, 530cdf0e10cSrcweir // lProps); 531cdf0e10cSrcweir // 532cdf0e10cSrcweir // System.out.println("check case senstive URLs"); 533cdf0e10cSrcweir // 534cdf0e10cSrcweir // sIllegalArgs = new String[] { 535cdf0e10cSrcweir // "sLot:5000", "sloT:10909", ".unO:SaveAs", ".uno:OPEN", 536cdf0e10cSrcweir // }; 537cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_ILLEGALARGUMENTEXCEPTION, sIllegalArgs, 538cdf0e10cSrcweir // "_blank", 0, lProps); 539cdf0e10cSrcweir // 540cdf0e10cSrcweir // sEmptyDocs = new String[] { 541cdf0e10cSrcweir // "private:factory/SWRITER", "private:factory/SWRITER/WEB", 542cdf0e10cSrcweir // "macro:///importwizard.main.main", 543cdf0e10cSrcweir // "Macro:///euro.autopilotrun.startautopilot", 544cdf0e10cSrcweir // "Service:Com.Sun.Star.Frame.Frame", 545cdf0e10cSrcweir // "Mailto:andreas.schluens@germany.sun.com", "neWs:staroffice-doc", 546cdf0e10cSrcweir // "News:Staroffice-doc" 547cdf0e10cSrcweir // }; 548cdf0e10cSrcweir // 549cdf0e10cSrcweir // //with cws_loadenv01 changed to IllegalArgumentException 550cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_ILLEGALARGUMENTEXCEPTION, sEmptyDocs, "_blank", 0, 551cdf0e10cSrcweir // lProps); 552cdf0e10cSrcweir // 553cdf0e10cSrcweir // System.out.println("check FTP URLs"); 554cdf0e10cSrcweir // 555cdf0e10cSrcweir // String sFTPURL = (String) param.get("FtpAccess"); 556cdf0e10cSrcweir // Enumeration aSnapshot = m_lTestFiles.elements(); 557cdf0e10cSrcweir // 558cdf0e10cSrcweir // while (aSnapshot.hasMoreElements()) { 559cdf0e10cSrcweir // String doc = (String) aSnapshot.nextElement(); 560cdf0e10cSrcweir // 561cdf0e10cSrcweir // 562cdf0e10cSrcweir // // if os is windows 563cdf0e10cSrcweir // doc = doc.replace('\\', '/'); 564cdf0e10cSrcweir // if (doc.indexOf("CVS")<0) { 565cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_VALID_DOC, sFTPURL + "/" + doc, 566cdf0e10cSrcweir // "_blank", 0, lProps); 567cdf0e10cSrcweir // } 568cdf0e10cSrcweir // } 569cdf0e10cSrcweir // 570cdf0e10cSrcweir // System.out.println("check HTTP URLs"); 571cdf0e10cSrcweir // 572cdf0e10cSrcweir // String sHTTPURL = (String) param.get("HttpAccess"); 573cdf0e10cSrcweir // aSnapshot = m_lTestFiles.elements(); 574cdf0e10cSrcweir // 575cdf0e10cSrcweir // while (aSnapshot.hasMoreElements()) { 576cdf0e10cSrcweir // String doc = (String) aSnapshot.nextElement(); 577cdf0e10cSrcweir // 578cdf0e10cSrcweir // 579cdf0e10cSrcweir // // if os is windows 580cdf0e10cSrcweir // doc = doc.replace('\\', '/'); 581cdf0e10cSrcweir // if (doc.indexOf("CVS")<0) { 582cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_VALID_DOC, sHTTPURL + "/" + doc, 583cdf0e10cSrcweir // "_blank", 0, lProps); 584cdf0e10cSrcweir // } 585cdf0e10cSrcweir // } 586cdf0e10cSrcweir // } 587cdf0e10cSrcweir 588cdf0e10cSrcweir /** TODo document me 589cdf0e10cSrcweir */ checkStreamLoading()590cdf0e10cSrcweir @Test public void checkStreamLoading() 591cdf0e10cSrcweir { 592cdf0e10cSrcweir PropertyValue[] lProps = new PropertyValue[2]; 593cdf0e10cSrcweir 594cdf0e10cSrcweir lProps[0] = new PropertyValue(); 595cdf0e10cSrcweir lProps[0].Name = "Hidden"; 596cdf0e10cSrcweir lProps[0].Value = Boolean.TRUE; 597cdf0e10cSrcweir 598cdf0e10cSrcweir lProps[1] = new PropertyValue(); 599cdf0e10cSrcweir lProps[1].Name = "InputStream"; 600cdf0e10cSrcweir 601cdf0e10cSrcweir Enumeration aSnapshot = m_lTestFiles.elements(); 602cdf0e10cSrcweir while (aSnapshot.hasMoreElements()) 603cdf0e10cSrcweir { 604cdf0e10cSrcweir File aFile = new File(m_sTestDocPath, (String) aSnapshot.nextElement()); 605cdf0e10cSrcweir String sURL = URLHelper.getFileURLFromSystemPath(aFile); 606cdf0e10cSrcweir 607cdf0e10cSrcweir // if (sURL.indexOf("CVS") > -1) 608cdf0e10cSrcweir // { 609cdf0e10cSrcweir // continue; 610cdf0e10cSrcweir // } 611cdf0e10cSrcweir 612cdf0e10cSrcweir try 613cdf0e10cSrcweir { 614cdf0e10cSrcweir XInputStream xStream = m_xStreamProvider.openFileRead(sURL); 615cdf0e10cSrcweir lProps[1].Value = xStream; 616cdf0e10cSrcweir } 617cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 618cdf0e10cSrcweir { 619cdf0e10cSrcweir fail("Could not open test file \""+sURL+"\" for stream test."); 620cdf0e10cSrcweir } 621cdf0e10cSrcweir 622cdf0e10cSrcweir // check different version of "private:stream" URL! 623cdf0e10cSrcweir loadURL(m_xLoader, RESULT_VALID_DOC, "private:stream" , "_blank", 0, lProps); 624cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_VALID_DOC, "private:stream" , "_blank", 0, lProps); 625cdf0e10cSrcweir // loadURL(m_xLoader, RESULT_VALID_DOC, "private:stream/", "_blank", 0, lProps); 626cdf0e10cSrcweir } 627cdf0e10cSrcweir } 628cdf0e10cSrcweir 629cdf0e10cSrcweir /** 630cdf0e10cSrcweir * Loads one URL with the given parameters using the method 631cdf0e10cSrcweir * loadComponentFromURL(). Further it's possible to specify, whch result is 632cdf0e10cSrcweir * required and we check internally if it was reached. Logging of errors 633cdf0e10cSrcweir * and success stories is done inside this method too. Of course we catch 634cdf0e10cSrcweir * all possible exceptions and try to leave the office without any forgotten 635cdf0e10cSrcweir * but opened documents. 636cdf0e10cSrcweir */ loadURL(XComponentLoader m_xLoader, int nRequiredResult, String sURL, String sTarget, int nFlags, PropertyValue[] lProps)637cdf0e10cSrcweir private void loadURL(XComponentLoader m_xLoader, int nRequiredResult, 638cdf0e10cSrcweir String sURL, String sTarget, int nFlags, 639cdf0e10cSrcweir PropertyValue[] lProps) { 640cdf0e10cSrcweir int nResult = RESULT_EMPTY_DOC; 641cdf0e10cSrcweir XComponent xDoc = null; 642cdf0e10cSrcweir 643cdf0e10cSrcweir try { 644cdf0e10cSrcweir xDoc = m_xLoader.loadComponentFromURL(sURL, sTarget, nFlags, 645cdf0e10cSrcweir lProps); 646cdf0e10cSrcweir 647cdf0e10cSrcweir if (xDoc != null) { 648cdf0e10cSrcweir nResult = RESULT_VALID_DOC; 649cdf0e10cSrcweir } else { 650cdf0e10cSrcweir nResult = RESULT_EMPTY_DOC; 651cdf0e10cSrcweir } 652cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException exArgument) { 653cdf0e10cSrcweir nResult = RESULT_ILLEGALARGUMENTEXCEPTION; 654cdf0e10cSrcweir } catch (com.sun.star.io.IOException exIO) { 655cdf0e10cSrcweir nResult = RESULT_IOEXCEPTION; 656cdf0e10cSrcweir } catch (com.sun.star.uno.RuntimeException exRuntime) { 657cdf0e10cSrcweir nResult = RESULT_RUNTIMEEXCEPTION; 658cdf0e10cSrcweir } catch (Exception e) { 659cdf0e10cSrcweir nResult = RESULT_EXCEPTION; 660cdf0e10cSrcweir } 661cdf0e10cSrcweir 662cdf0e10cSrcweir try { 663cdf0e10cSrcweir if (xDoc != null) { 664cdf0e10cSrcweir xDoc.dispose(); 665cdf0e10cSrcweir xDoc = null; 666cdf0e10cSrcweir } 667cdf0e10cSrcweir } catch (com.sun.star.uno.RuntimeException exClosing) { 668cdf0e10cSrcweir System.out.println("exception during disposing of a document found!" + 669cdf0e10cSrcweir " Doesn't influence test - but should be checked."); 670cdf0e10cSrcweir } 671cdf0e10cSrcweir 672cdf0e10cSrcweir String sMessage = "URL[\"" + sURL + "\"]"; 673cdf0e10cSrcweir 674cdf0e10cSrcweir if (nResult == nRequiredResult) { 675cdf0e10cSrcweir System.out.println(sMessage + " expected result [" + 676cdf0e10cSrcweir convertResult2String(nResult) + "] "); 677cdf0e10cSrcweir } else { 678cdf0e10cSrcweir fail(sMessage + " unexpected result [" + 679cdf0e10cSrcweir convertResult2String(nResult) + "] " + 680cdf0e10cSrcweir "\nrequired was [" + 681cdf0e10cSrcweir convertResult2String(nRequiredResult) + "]" + 682cdf0e10cSrcweir "\nwe got [" + convertResult2String(nResult) + "]" 683cdf0e10cSrcweir ); 684cdf0e10cSrcweir } 685cdf0e10cSrcweir } 686cdf0e10cSrcweir loadURL(XComponentLoader m_xLoader, int nRequiredResult, String[] sURL, String sTarget, int nFlags, PropertyValue[] lProps)687cdf0e10cSrcweir private void loadURL(XComponentLoader m_xLoader, int nRequiredResult, 688cdf0e10cSrcweir String[] sURL, String sTarget, int nFlags, 689cdf0e10cSrcweir PropertyValue[] lProps) { 690cdf0e10cSrcweir for (int i = 0; i < sURL.length; i++) 691cdf0e10cSrcweir { 692cdf0e10cSrcweir loadURL(m_xLoader, nRequiredResult, sURL[i], sTarget, nFlags, lProps); 693cdf0e10cSrcweir } 694cdf0e10cSrcweir } 695cdf0e10cSrcweir 696cdf0e10cSrcweir /** 697cdf0e10cSrcweir * it match the int result value to a string, which can be used for logging 698cdf0e10cSrcweir */ convertResult2String(int nResult)699cdf0e10cSrcweir private static String convertResult2String(int nResult) { 700cdf0e10cSrcweir switch (nResult) { 701cdf0e10cSrcweir case RESULT_VALID_DOC: 702cdf0e10cSrcweir return "VALID_DOC"; 703cdf0e10cSrcweir 704cdf0e10cSrcweir case RESULT_EMPTY_DOC: 705cdf0e10cSrcweir return "EMPTY_DOC"; 706cdf0e10cSrcweir 707cdf0e10cSrcweir case RESULT_ILLEGALARGUMENTEXCEPTION: 708cdf0e10cSrcweir return "ILLEGALARGUMENTEXCEPTION"; 709cdf0e10cSrcweir 710cdf0e10cSrcweir case RESULT_IOEXCEPTION: 711cdf0e10cSrcweir return "IOEXCEPTION"; 712cdf0e10cSrcweir 713cdf0e10cSrcweir case RESULT_RUNTIMEEXCEPTION: 714cdf0e10cSrcweir return "RUNTIMEEXCEPTION"; 715cdf0e10cSrcweir 716cdf0e10cSrcweir case RESULT_EXCEPTION: 717cdf0e10cSrcweir return "ALLOTHEREXCEPTION"; 718cdf0e10cSrcweir } 719cdf0e10cSrcweir 720cdf0e10cSrcweir return "unknown!"; 721cdf0e10cSrcweir } 722cdf0e10cSrcweir getMSF()723cdf0e10cSrcweir private XMultiServiceFactory getMSF() 724cdf0e10cSrcweir { 725cdf0e10cSrcweir final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 726cdf0e10cSrcweir return xMSF1; 727cdf0e10cSrcweir } 728cdf0e10cSrcweir 729cdf0e10cSrcweir // setup and close connections 730cdf0e10cSrcweir @BeforeClass setUpConnection()731cdf0e10cSrcweir public static void setUpConnection() throws Exception 732cdf0e10cSrcweir { 733cdf0e10cSrcweir System.out.println("setUpConnection()"); 734cdf0e10cSrcweir connection.setUp(); 735cdf0e10cSrcweir } 736cdf0e10cSrcweir 737cdf0e10cSrcweir @AfterClass tearDownConnection()738cdf0e10cSrcweir public static void tearDownConnection() 739cdf0e10cSrcweir throws InterruptedException, com.sun.star.uno.Exception 740cdf0e10cSrcweir { 741cdf0e10cSrcweir System.out.println("tearDownConnection()"); 742cdf0e10cSrcweir connection.tearDown(); 743cdf0e10cSrcweir } 744cdf0e10cSrcweir private static final OfficeConnection connection = new OfficeConnection(); 745cdf0e10cSrcweir 746cdf0e10cSrcweir } 747