1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package complex.junitskeleton;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.io.IOException;
30*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
31*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
32*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
33*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34*cdf0e10cSrcweir import com.sun.star.util.XCloseable;
35*cdf0e10cSrcweir import java.io.File;
36*cdf0e10cSrcweir import java.io.RandomAccessFile;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import lib.TestParameters;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir import util.SOfficeFactory;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // ---------- junit imports -----------------
43*cdf0e10cSrcweir import org.junit.After;
44*cdf0e10cSrcweir import org.junit.AfterClass;
45*cdf0e10cSrcweir import org.junit.Before;
46*cdf0e10cSrcweir import org.junit.BeforeClass;
47*cdf0e10cSrcweir import org.junit.Test;
48*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
49*cdf0e10cSrcweir import static org.junit.Assert.*;
50*cdf0e10cSrcweir // ------------------------------------------
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir public class Skeleton
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     /**
55*cdf0e10cSrcweir      * The test parameters
56*cdf0e10cSrcweir      */
57*cdf0e10cSrcweir     private static TestParameters param = null;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     @Test public void check() {
60*cdf0e10cSrcweir         assertTrue("Couldn't open document", open());
61*cdf0e10cSrcweir         System.out.println("check");
62*cdf0e10cSrcweir         assertTrue("Couldn't close document", close());
63*cdf0e10cSrcweir         String tempDirURL = util.utils.getOfficeTemp/*Dir*/(getMSF());
64*cdf0e10cSrcweir         System.out.println("temp dir URL is: " + tempDirURL);
65*cdf0e10cSrcweir         String tempDir = graphical.FileHelper.getSystemPathFromFileURL(tempDirURL);
66*cdf0e10cSrcweir         assertTrue("Temp directory doesn't exist.", new File(tempDir).exists());
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     private boolean open()
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir             System.out.println("open()");
72*cdf0e10cSrcweir             // get multiservicefactory -----------------------------------------
73*cdf0e10cSrcweir             final XMultiServiceFactory xMsf = getMSF();
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir             SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir             // some Tests need the qadevOOo TestParameters, it is like a Hashmap for Properties.
78*cdf0e10cSrcweir             param = new TestParameters();
79*cdf0e10cSrcweir             param.put("ServiceFactory", xMsf); // some qadevOOo functions need the ServiceFactory
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir             return true;
82*cdf0e10cSrcweir         }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     private boolean close()
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir             System.out.println("close()");
87*cdf0e10cSrcweir             return true;
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     // marked as test
91*cdf0e10cSrcweir     @Test public void checkDocument()
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             System.out.println("checkDocument()");
94*cdf0e10cSrcweir             final String sREADME = TestDocument.getUrl("README.txt");
95*cdf0e10cSrcweir             System.out.println("README is in:" + sREADME);
96*cdf0e10cSrcweir             File aFile = new File(sREADME);
97*cdf0e10cSrcweir             if (! aFile.exists())
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 // It is a little bit stupid that office urls not compatible to java file urls
100*cdf0e10cSrcweir                 System.out.println("java.io.File can't access Office file urls.");
101*cdf0e10cSrcweir                 String sREADMESystemPath = graphical.FileHelper.getSystemPathFromFileURL(sREADME);
102*cdf0e10cSrcweir                 aFile = new File(sREADMESystemPath);
103*cdf0e10cSrcweir                 assertTrue("File '" + sREADMESystemPath + "' doesn't exists.", aFile.exists());
104*cdf0e10cSrcweir             }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             try
107*cdf0e10cSrcweir             {
108*cdf0e10cSrcweir                 RandomAccessFile aAccess = new RandomAccessFile(aFile, "r");
109*cdf0e10cSrcweir                 long nLength = aAccess.length();
110*cdf0e10cSrcweir                 System.out.println("File length: " + nLength);
111*cdf0e10cSrcweir                 assertTrue("File length wrong", nLength > 0);
112*cdf0e10cSrcweir                 String sLine = aAccess.readLine();
113*cdf0e10cSrcweir                 assertTrue("Line must not be empty", sLine.length() > 0);
114*cdf0e10cSrcweir                 System.out.println("       Line: '" + sLine + "'");
115*cdf0e10cSrcweir                 System.out.println("     length: " + sLine.length());
116*cdf0e10cSrcweir                 assertTrue("File length not near equal to string length", sLine.length() + 2 >= nLength);
117*cdf0e10cSrcweir                 aAccess.close();
118*cdf0e10cSrcweir             }
119*cdf0e10cSrcweir             catch (java.io.FileNotFoundException e)
120*cdf0e10cSrcweir             {
121*cdf0e10cSrcweir                 fail("Can't find file: " + sREADME + " - " + e.getMessage());
122*cdf0e10cSrcweir             }
123*cdf0e10cSrcweir             catch (java.io.IOException e)
124*cdf0e10cSrcweir             {
125*cdf0e10cSrcweir                 fail("IO Exception: " + e.getMessage());
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     @Test public void checkOpenDocumentWithOffice()
131*cdf0e10cSrcweir     {
132*cdf0e10cSrcweir         // SOfficeFactory aFactory = new SOfficeFactory(getMSF());
133*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF());
134*cdf0e10cSrcweir         final String sREADME = TestDocument.getUrl("README.txt");
135*cdf0e10cSrcweir         try
136*cdf0e10cSrcweir         {
137*cdf0e10cSrcweir             XComponent aDocument = SOF.loadDocument(sREADME);
138*cdf0e10cSrcweir             complex.junitskeleton.justatest.shortWait();
139*cdf0e10cSrcweir             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, aDocument);
140*cdf0e10cSrcweir             xClose.close(true);
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException ex)
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             fail("Illegal argument exception caught: " + ex.getMessage());
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir         catch (com.sun.star.io.IOException ex)
147*cdf0e10cSrcweir         {
148*cdf0e10cSrcweir             fail("IOException caught: " + ex.getMessage());
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             fail("Exception caught: " + ex.getMessage());
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     // marked as prepare for test, will call before every test
157*cdf0e10cSrcweir     @Before public void before()
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir             System.out.println("before()");
160*cdf0e10cSrcweir             System.setProperty("THIS IS A TEST", "Hallo");
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     // marked as post for test, will call after every test
165*cdf0e10cSrcweir     @After public void after()
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             System.out.println("after()");
168*cdf0e10cSrcweir             String sValue = System.getProperty("THIS IS A TEST");
169*cdf0e10cSrcweir             assertEquals(sValue, "Hallo");
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
176*cdf0e10cSrcweir         return xMSF1;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     // setup and close connections
180*cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
181*cdf0e10cSrcweir         System.out.println("setUpConnection()");
182*cdf0e10cSrcweir         connection.setUp();
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
186*cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         System.out.println("tearDownConnection()");
189*cdf0e10cSrcweir         connection.tearDown();
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir }
195