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