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 package complex.junitskeleton;
28 
29 import com.sun.star.io.IOException;
30 import com.sun.star.lang.IllegalArgumentException;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.util.XCloseable;
35 import java.io.File;
36 import java.io.RandomAccessFile;
37 
38 import lib.TestParameters;
39 
40 import util.SOfficeFactory;
41 
42 // ---------- junit imports -----------------
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.openoffice.test.OfficeConnection;
49 import static org.junit.Assert.*;
50 // ------------------------------------------
51 
52 public class Skeleton
53 {
54     /**
55      * The test parameters
56      */
57     private static TestParameters param = null;
58 
59     @Test public void check() {
60         assertTrue("Couldn't open document", open());
61         System.out.println("check");
62         assertTrue("Couldn't close document", close());
63         String tempDirURL = util.utils.getOfficeTemp/*Dir*/(getMSF());
64         System.out.println("temp dir URL is: " + tempDirURL);
65         String tempDir = graphical.FileHelper.getSystemPathFromFileURL(tempDirURL);
66         assertTrue("Temp directory doesn't exist.", new File(tempDir).exists());
67     }
68 
69     private boolean open()
70         {
71             System.out.println("open()");
72             // get multiservicefactory -----------------------------------------
73             final XMultiServiceFactory xMsf = getMSF();
74 
75             SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
76 
77             // some Tests need the qadevOOo TestParameters, it is like a Hashmap for Properties.
78             param = new TestParameters();
79             param.put("ServiceFactory", xMsf); // some qadevOOo functions need the ServiceFactory
80 
81             return true;
82         }
83 
84     private boolean close()
85         {
86             System.out.println("close()");
87             return true;
88         }
89 
90     // marked as test
91     @Test public void checkDocument()
92         {
93             System.out.println("checkDocument()");
94             final String sREADME = TestDocument.getUrl("README.txt");
95             System.out.println("README is in:" + sREADME);
96             File aFile = new File(sREADME);
97             if (! aFile.exists())
98             {
99                 // It is a little bit stupid that office urls not compatible to java file urls
100                 System.out.println("java.io.File can't access Office file urls.");
101                 String sREADMESystemPath = graphical.FileHelper.getSystemPathFromFileURL(sREADME);
102                 aFile = new File(sREADMESystemPath);
103                 assertTrue("File '" + sREADMESystemPath + "' doesn't exists.", aFile.exists());
104             }
105 
106             try
107             {
108                 RandomAccessFile aAccess = new RandomAccessFile(aFile, "r");
109                 long nLength = aAccess.length();
110                 System.out.println("File length: " + nLength);
111                 assertTrue("File length wrong", nLength > 0);
112                 String sLine = aAccess.readLine();
113                 assertTrue("Line must not be empty", sLine.length() > 0);
114                 System.out.println("       Line: '" + sLine + "'");
115                 System.out.println("     length: " + sLine.length());
116                 assertTrue("File length not near equal to string length", sLine.length() + 2 >= nLength);
117                 aAccess.close();
118             }
119             catch (java.io.FileNotFoundException e)
120             {
121                 fail("Can't find file: " + sREADME + " - " + e.getMessage());
122             }
123             catch (java.io.IOException e)
124             {
125                 fail("IO Exception: " + e.getMessage());
126             }
127 
128         }
129 
130     @Test public void checkOpenDocumentWithOffice()
131     {
132         // SOfficeFactory aFactory = new SOfficeFactory(getMSF());
133         SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF());
134         final String sREADME = TestDocument.getUrl("README.txt");
135         try
136         {
137             XComponent aDocument = SOF.loadDocument(sREADME);
138             complex.junitskeleton.justatest.shortWait();
139             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, aDocument);
140             xClose.close(true);
141         }
142         catch (com.sun.star.lang.IllegalArgumentException ex)
143         {
144             fail("Illegal argument exception caught: " + ex.getMessage());
145         }
146         catch (com.sun.star.io.IOException ex)
147         {
148             fail("IOException caught: " + ex.getMessage());
149         }
150         catch (com.sun.star.uno.Exception ex)
151         {
152             fail("Exception caught: " + ex.getMessage());
153         }
154     }
155 
156     // marked as prepare for test, will call before every test
157     @Before public void before()
158         {
159             System.out.println("before()");
160             System.setProperty("THIS IS A TEST", "Hallo");
161         }
162 
163 
164     // marked as post for test, will call after every test
165     @After public void after()
166         {
167             System.out.println("after()");
168             String sValue = System.getProperty("THIS IS A TEST");
169             assertEquals(sValue, "Hallo");
170     }
171 
172 
173     private XMultiServiceFactory getMSF()
174     {
175         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
176         return xMSF1;
177     }
178 
179     // setup and close connections
180     @BeforeClass public static void setUpConnection() throws Exception {
181         System.out.println("setUpConnection()");
182         connection.setUp();
183     }
184 
185     @AfterClass public static void tearDownConnection()
186         throws InterruptedException, com.sun.star.uno.Exception
187     {
188         System.out.println("tearDownConnection()");
189         connection.tearDown();
190     }
191 
192     private static final OfficeConnection connection = new OfficeConnection();
193 
194 }
195