2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package complex
.junitskeleton
;
20 import com
.sun
.star
.lang
.XComponent
;
21 import com
.sun
.star
.lang
.XMultiServiceFactory
;
22 import com
.sun
.star
.uno
.UnoRuntime
;
23 import com
.sun
.star
.util
.XCloseable
;
25 import java
.io
.RandomAccessFile
;
27 import lib
.TestParameters
;
29 import util
.SOfficeFactory
;
31 // ---------- junit imports -----------------
32 import org
.junit
.After
;
33 import org
.junit
.AfterClass
;
34 import org
.junit
.Before
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.Test
;
37 import org
.openoffice
.test
.OfficeConnection
;
38 import static org
.junit
.Assert
.*;
39 // ------------------------------------------
46 private static TestParameters param
= null;
48 @Test public void check() {
49 assertTrue("Couldn't open document", open());
50 System
.out
.println("check");
51 assertTrue("Couldn't close document", close());
52 String tempDirURL
= util
.utils
.getOfficeTemp
/*Dir*/(getMSF());
53 System
.out
.println("temp dir URL is: " + tempDirURL
);
54 String tempDir
= graphical
.FileHelper
.getSystemPathFromFileURL(tempDirURL
);
55 assertTrue("Temp directory doesn't exist.", new File(tempDir
).exists());
58 private boolean open()
60 System
.out
.println("open()");
61 // get multiservicefactory -----------------------------------------
62 final XMultiServiceFactory xMsf
= getMSF();
64 SOfficeFactory
.getFactory(xMsf
);
66 // some Tests need the qadevOOo TestParameters, it is like a Hashmap for Properties.
67 param
= new TestParameters();
68 param
.put("ServiceFactory", xMsf
); // some qadevOOo functions need the ServiceFactory
73 private boolean close()
75 System
.out
.println("close()");
80 @Test public void checkDocument()
82 System
.out
.println("checkDocument()");
83 final String sREADME
= TestDocument
.getUrl("README.txt");
84 System
.out
.println("README is in:" + sREADME
);
85 File aFile
= new File(sREADME
);
88 // It is a little bit stupid that office urls not compatible to java file urls
89 System
.out
.println("java.io.File can't access Office file urls.");
90 String sREADMESystemPath
= graphical
.FileHelper
.getSystemPathFromFileURL(sREADME
);
91 aFile
= new File(sREADMESystemPath
);
92 assertTrue("File '" + sREADMESystemPath
+ "' doesn't exists.", aFile
.exists());
97 RandomAccessFile aAccess
= new RandomAccessFile(aFile
, "r");
98 long nLength
= aAccess
.length();
99 System
.out
.println("File length: " + nLength
);
100 assertTrue("File length wrong", nLength
> 0);
101 String sLine
= aAccess
.readLine();
102 assertTrue("Line must not be empty", sLine
.length() > 0);
103 System
.out
.println(" Line: '" + sLine
+ "'");
104 System
.out
.println(" length: " + sLine
.length());
105 assertTrue("File length not near equal to string length", sLine
.length() + 2 >= nLength
);
108 catch (java
.io
.FileNotFoundException e
)
110 fail("Can't find file: " + sREADME
+ " - " + e
.getMessage());
112 catch (java
.io
.IOException e
)
114 fail("IO Exception: " + e
.getMessage());
119 @Test public void checkOpenDocumentWithOffice()
121 // SOfficeFactory aFactory = new SOfficeFactory(getMSF());
122 SOfficeFactory SOF
= SOfficeFactory
.getFactory(getMSF());
123 final String sREADME
= TestDocument
.getUrl("README.txt");
126 XComponent aDocument
= SOF
.loadDocument(sREADME
);
127 complex
.junitskeleton
.justatest
.shortWait();
128 XCloseable xClose
= UnoRuntime
.queryInterface(XCloseable
.class, aDocument
);
131 catch (com
.sun
.star
.lang
.IllegalArgumentException ex
)
133 fail("Illegal argument exception caught: " + ex
.getMessage());
135 catch (com
.sun
.star
.io
.IOException ex
)
137 fail("IOException caught: " + ex
.getMessage());
139 catch (com
.sun
.star
.uno
.Exception ex
)
141 fail("Exception caught: " + ex
.getMessage());
145 // marked as prepare for test, will call before every test
146 @Before public void before()
148 System
.out
.println("before()");
149 System
.setProperty("THIS IS A TEST", "Hallo");
153 // marked as post for test, will call after every test
154 @After public void after()
156 System
.out
.println("after()");
157 String sValue
= System
.getProperty("THIS IS A TEST");
158 assertEquals(sValue
, "Hallo");
162 private XMultiServiceFactory
getMSF()
164 final XMultiServiceFactory xMSF1
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
168 // setup and close connections
169 @BeforeClass public static void setUpConnection() throws Exception
{
170 System
.out
.println("setUpConnection()");
174 @AfterClass public static void tearDownConnection()
175 throws InterruptedException
, com
.sun
.star
.uno
.Exception
177 System
.out
.println("tearDownConnection()");
178 connection
.tearDown();
181 private static final OfficeConnection connection
= new OfficeConnection();