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