1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package complex.junitskeleton;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.io.IOException;
26cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.util.XCloseable;
31cdf0e10cSrcweir import java.io.File;
32cdf0e10cSrcweir import java.io.RandomAccessFile;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import lib.TestParameters;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import util.SOfficeFactory;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // ---------- junit imports -----------------
39cdf0e10cSrcweir import org.junit.After;
40cdf0e10cSrcweir import org.junit.AfterClass;
41cdf0e10cSrcweir import org.junit.Before;
42cdf0e10cSrcweir import org.junit.BeforeClass;
43cdf0e10cSrcweir import org.junit.Test;
44cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
45cdf0e10cSrcweir import static org.junit.Assert.*;
46cdf0e10cSrcweir // ------------------------------------------
47cdf0e10cSrcweir 
48cdf0e10cSrcweir public class Skeleton
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     /**
51cdf0e10cSrcweir      * The test parameters
52cdf0e10cSrcweir      */
53cdf0e10cSrcweir     private static TestParameters param = null;
54cdf0e10cSrcweir 
check()55cdf0e10cSrcweir     @Test public void check() {
56cdf0e10cSrcweir         assertTrue("Couldn't open document", open());
57cdf0e10cSrcweir         System.out.println("check");
58cdf0e10cSrcweir         assertTrue("Couldn't close document", close());
59cdf0e10cSrcweir         String tempDirURL = util.utils.getOfficeTemp/*Dir*/(getMSF());
60cdf0e10cSrcweir         System.out.println("temp dir URL is: " + tempDirURL);
61cdf0e10cSrcweir         String tempDir = graphical.FileHelper.getSystemPathFromFileURL(tempDirURL);
62cdf0e10cSrcweir         assertTrue("Temp directory doesn't exist.", new File(tempDir).exists());
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
open()65cdf0e10cSrcweir     private boolean open()
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir             System.out.println("open()");
68cdf0e10cSrcweir             // get multiservicefactory -----------------------------------------
69cdf0e10cSrcweir             final XMultiServiceFactory xMsf = getMSF();
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             // some Tests need the qadevOOo TestParameters, it is like a Hashmap for Properties.
74cdf0e10cSrcweir             param = new TestParameters();
75cdf0e10cSrcweir             param.put("ServiceFactory", xMsf); // some qadevOOo functions need the ServiceFactory
76cdf0e10cSrcweir 
77cdf0e10cSrcweir             return true;
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir 
close()80cdf0e10cSrcweir     private boolean close()
81cdf0e10cSrcweir         {
82cdf0e10cSrcweir             System.out.println("close()");
83cdf0e10cSrcweir             return true;
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     // marked as test
checkDocument()87cdf0e10cSrcweir     @Test public void checkDocument()
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             System.out.println("checkDocument()");
90cdf0e10cSrcweir             final String sREADME = TestDocument.getUrl("README.txt");
91cdf0e10cSrcweir             System.out.println("README is in:" + sREADME);
92cdf0e10cSrcweir             File aFile = new File(sREADME);
93cdf0e10cSrcweir             if (! aFile.exists())
94cdf0e10cSrcweir             {
95cdf0e10cSrcweir                 // It is a little bit stupid that office urls not compatible to java file urls
96cdf0e10cSrcweir                 System.out.println("java.io.File can't access Office file urls.");
97cdf0e10cSrcweir                 String sREADMESystemPath = graphical.FileHelper.getSystemPathFromFileURL(sREADME);
98cdf0e10cSrcweir                 aFile = new File(sREADMESystemPath);
99cdf0e10cSrcweir                 assertTrue("File '" + sREADMESystemPath + "' doesn't exists.", aFile.exists());
100cdf0e10cSrcweir             }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             try
103cdf0e10cSrcweir             {
104cdf0e10cSrcweir                 RandomAccessFile aAccess = new RandomAccessFile(aFile, "r");
105cdf0e10cSrcweir                 long nLength = aAccess.length();
106cdf0e10cSrcweir                 System.out.println("File length: " + nLength);
107cdf0e10cSrcweir                 assertTrue("File length wrong", nLength > 0);
108cdf0e10cSrcweir                 String sLine = aAccess.readLine();
109cdf0e10cSrcweir                 assertTrue("Line must not be empty", sLine.length() > 0);
110cdf0e10cSrcweir                 System.out.println("       Line: '" + sLine + "'");
111cdf0e10cSrcweir                 System.out.println("     length: " + sLine.length());
112cdf0e10cSrcweir                 assertTrue("File length not near equal to string length", sLine.length() + 2 >= nLength);
113cdf0e10cSrcweir                 aAccess.close();
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir             catch (java.io.FileNotFoundException e)
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 fail("Can't find file: " + sREADME + " - " + e.getMessage());
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir             catch (java.io.IOException e)
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 fail("IO Exception: " + e.getMessage());
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
checkOpenDocumentWithOffice()126cdf0e10cSrcweir     @Test public void checkOpenDocumentWithOffice()
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         // SOfficeFactory aFactory = new SOfficeFactory(getMSF());
129cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF());
130cdf0e10cSrcweir         final String sREADME = TestDocument.getUrl("README.txt");
131cdf0e10cSrcweir         try
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             XComponent aDocument = SOF.loadDocument(sREADME);
134cdf0e10cSrcweir             complex.junitskeleton.justatest.shortWait();
135cdf0e10cSrcweir             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, aDocument);
136cdf0e10cSrcweir             xClose.close(true);
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException ex)
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             fail("Illegal argument exception caught: " + ex.getMessage());
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir         catch (com.sun.star.io.IOException ex)
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             fail("IOException caught: " + ex.getMessage());
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             fail("Exception caught: " + ex.getMessage());
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     // marked as prepare for test, will call before every test
before()153cdf0e10cSrcweir     @Before public void before()
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             System.out.println("before()");
156cdf0e10cSrcweir             System.setProperty("THIS IS A TEST", "Hallo");
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     // marked as post for test, will call after every test
after()161cdf0e10cSrcweir     @After public void after()
162cdf0e10cSrcweir         {
163cdf0e10cSrcweir             System.out.println("after()");
164cdf0e10cSrcweir             String sValue = System.getProperty("THIS IS A TEST");
165cdf0e10cSrcweir             assertEquals(sValue, "Hallo");
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 
getMSF()169cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
172cdf0e10cSrcweir         return xMSF1;
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     // setup and close connections
setUpConnection()176cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
177cdf0e10cSrcweir         System.out.println("setUpConnection()");
178cdf0e10cSrcweir         connection.setUp();
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
tearDownConnection()181cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
182cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         System.out.println("tearDownConnection()");
185cdf0e10cSrcweir         connection.tearDown();
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
189cdf0e10cSrcweir 
190cdf0e10cSrcweir }
191