xref: /AOO41X/main/framework/qa/complex/api_internal/CheckAPI.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 name: as default, start with complex
28*cdf0e10cSrcweir package complex.api_internal;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir // imports
31*cdf0e10cSrcweir import helper.OfficeProvider;
32*cdf0e10cSrcweir import helper.ProcessHandler;
33*cdf0e10cSrcweir import com.sun.star.task.XJob;
34*cdf0e10cSrcweir import com.sun.star.beans.XPropertyAccess;
35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
36*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38*cdf0e10cSrcweir import com.sun.star.beans.NamedValue;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir import java.util.Vector;
41*cdf0e10cSrcweir import java.util.StringTokenizer;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // ---------- junit imports -----------------
45*cdf0e10cSrcweir import lib.TestParameters;
46*cdf0e10cSrcweir import org.junit.After;
47*cdf0e10cSrcweir import org.junit.AfterClass;
48*cdf0e10cSrcweir import org.junit.Before;
49*cdf0e10cSrcweir import org.junit.BeforeClass;
50*cdf0e10cSrcweir import org.junit.Test;
51*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
52*cdf0e10cSrcweir import static org.junit.Assert.*;
53*cdf0e10cSrcweir // ------------------------------------------
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir /**
56*cdf0e10cSrcweir  * This test executes the API tests internally in StarOffice. Prerequiste is
57*cdf0e10cSrcweir  * that a OOoRunner.jar is registered inseide of StarOffice. Adjust the joblist
58*cdf0e10cSrcweir  * inside of the ChekAPI.props to determine which tetss will be executed.
59*cdf0e10cSrcweir  */
60*cdf0e10cSrcweir public class CheckAPI  {
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     // The name of the tested service
63*cdf0e10cSrcweir     private final String testName = "StarOfficeAPI";
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     /**
66*cdf0e10cSrcweir      * Return all test methods.
67*cdf0e10cSrcweir      * @return The test methods.
68*cdf0e10cSrcweir      */
69*cdf0e10cSrcweir //    public String[] getTestMethodNames() {
70*cdf0e10cSrcweir //        return new String[]{"checkAPI"};
71*cdf0e10cSrcweir //    }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     /**
74*cdf0e10cSrcweir      * The test parameters
75*cdf0e10cSrcweir      */
76*cdf0e10cSrcweir     private static TestParameters param = null;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     /**
79*cdf0e10cSrcweir      *
80*cdf0e10cSrcweir      */
81*cdf0e10cSrcweir     @Before public void before()
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir         param = new TestParameters();
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir     /**
86*cdf0e10cSrcweir      * Execute the API tests inside of the Office. If the Office crashes, it
87*cdf0e10cSrcweir      * will be restarted and the job will continue after the one that caused the crash.
88*cdf0e10cSrcweir      */
89*cdf0e10cSrcweir     @Test public void checkAPI() {
90*cdf0e10cSrcweir         System.out.println("Start with test");
91*cdf0e10cSrcweir         // if test is idle for 5 minutes, assume that it hangs and kill it.
92*cdf0e10cSrcweir         // param.put("TimeOut", new Integer("300000"));
93*cdf0e10cSrcweir /*        AppProvider office = (AppProvider)dcl.getInstance("helper.OfficeProvider");
94*cdf0e10cSrcweir         Object msf = office.getManager(param);
95*cdf0e10cSrcweir         if (msf == null) {
96*cdf0e10cSrcweir             failed("Could not connect an Office.");
97*cdf0e10cSrcweir         }
98*cdf0e10cSrcweir         param.put("ServiceFactory",msf); */
99*cdf0e10cSrcweir         XMultiServiceFactory xMSF = getMSF();
100*cdf0e10cSrcweir         Object oObj = null;
101*cdf0e10cSrcweir         try {
102*cdf0e10cSrcweir             oObj = xMSF.createInstance("org.openoffice.RunnerService");
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e) {
105*cdf0e10cSrcweir             fail("Could not create Instance of 'org.openoffice.RunnerService'");
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir         assertNotNull("Cannot create 'org.openoffice.RunnerService'", oObj);
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         // get the parameters for the internal test
110*cdf0e10cSrcweir         String paramList = (String)param.get("ParamList");
111*cdf0e10cSrcweir         Vector p = new Vector();
112*cdf0e10cSrcweir         StringTokenizer paramTokens = new StringTokenizer(paramList, " ");
113*cdf0e10cSrcweir         while(paramTokens.hasMoreTokens())
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             p.add(paramTokens.nextToken());
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         int length = p.size()/2+1;
118*cdf0e10cSrcweir         NamedValue[] internalParams = new NamedValue[length];
119*cdf0e10cSrcweir         for (int i=0; i<length-1; i++) {
120*cdf0e10cSrcweir             internalParams[i] = new NamedValue();
121*cdf0e10cSrcweir             internalParams[i].Name = (String)p.get(i*2);
122*cdf0e10cSrcweir             internalParams[i].Value = p.get(i*2+1);
123*cdf0e10cSrcweir             System.out.println("Name: "+internalParams[i].Name);
124*cdf0e10cSrcweir             System.out.println("Value: "+(String)internalParams[i].Value);
125*cdf0e10cSrcweir         }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         // do we have test jobs?
128*cdf0e10cSrcweir         String testJob = (String)param.get("job");
129*cdf0e10cSrcweir         PropertyValue[]props;
130*cdf0e10cSrcweir         if (testJob==null)
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             if ( param.get("job1")==null )
133*cdf0e10cSrcweir             {
134*cdf0e10cSrcweir                 // get all test jobs from runner service
135*cdf0e10cSrcweir                 XPropertyAccess xPropAcc = (XPropertyAccess)UnoRuntime.queryInterface(XPropertyAccess.class, oObj);
136*cdf0e10cSrcweir                 props = xPropAcc.getPropertyValues();
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir             else  {
139*cdf0e10cSrcweir                 int index=1;
140*cdf0e10cSrcweir                 p = new Vector();
141*cdf0e10cSrcweir                 while ( param.get("job"+index) != null ) {
142*cdf0e10cSrcweir                     p.add(param.get("job"+index));
143*cdf0e10cSrcweir                     index++;
144*cdf0e10cSrcweir                 }
145*cdf0e10cSrcweir                 props = new PropertyValue[p.size()];
146*cdf0e10cSrcweir                 for ( int i=0; i<props.length; i++ ) {
147*cdf0e10cSrcweir                     props[i] = new PropertyValue();
148*cdf0e10cSrcweir                     props[i].Value = p.get(i);
149*cdf0e10cSrcweir                 }
150*cdf0e10cSrcweir             }
151*cdf0e10cSrcweir         }
152*cdf0e10cSrcweir         else  {
153*cdf0e10cSrcweir             props = new PropertyValue[1];
154*cdf0e10cSrcweir             props[0] = new PropertyValue();
155*cdf0e10cSrcweir             props[0].Value = testJob;
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         System.out.println("Props length: "+ props.length);
159*cdf0e10cSrcweir         for (int i=0; i<props.length; i++) {
160*cdf0e10cSrcweir             XJob xJob = UnoRuntime.queryInterface(XJob.class, oObj);
161*cdf0e10cSrcweir             internalParams[length-1] = new NamedValue();
162*cdf0e10cSrcweir             internalParams[length-1].Name = "-o";
163*cdf0e10cSrcweir             internalParams[length-1].Value = props[i].Value;
164*cdf0e10cSrcweir             System.out.println("Executing: " + (String)props[i].Value);
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir             String erg = null;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             try {
169*cdf0e10cSrcweir                 erg = (String)xJob.execute(internalParams);
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir             catch(Throwable t) {
172*cdf0e10cSrcweir                 // restart and go on with test!!
173*cdf0e10cSrcweir                 t.printStackTrace();
174*cdf0e10cSrcweir                 fail("Test run '" + (String)props[i].Value +"' could not be executed: Office crashed and is killed!");
175*cdf0e10cSrcweir                 xMSF = null;
176*cdf0e10cSrcweir                 ProcessHandler handler = (ProcessHandler)param.get("AppProvider");
177*cdf0e10cSrcweir                 handler.kill();
178*cdf0e10cSrcweir                 try {
179*cdf0e10cSrcweir                     Thread.sleep(10000);
180*cdf0e10cSrcweir                 }
181*cdf0e10cSrcweir                 catch(java.lang.InterruptedException e) {}
182*cdf0e10cSrcweir                 OfficeProvider op = new OfficeProvider();
183*cdf0e10cSrcweir                 //                op.closeExistingOffice(param, true);
184*cdf0e10cSrcweir                 xMSF = (XMultiServiceFactory)op.getManager(param);
185*cdf0e10cSrcweir                 param.put("ServiceFactory",xMSF);
186*cdf0e10cSrcweir                 try {
187*cdf0e10cSrcweir                     oObj = xMSF.createInstance("org.openoffice.RunnerService");
188*cdf0e10cSrcweir                 }
189*cdf0e10cSrcweir                 catch(com.sun.star.uno.Exception e) {
190*cdf0e10cSrcweir                     fail("Could not create Instance of 'org.openoffice.RunnerService'");
191*cdf0e10cSrcweir                 }
192*cdf0e10cSrcweir             }
193*cdf0e10cSrcweir             System.out.println(erg);
194*cdf0e10cSrcweir             String processedErg = parseResult(erg);
195*cdf0e10cSrcweir             assertTrue("Run '" + (String)props[i].Value + "' has result '" + processedErg + "'", processedErg == null);
196*cdf0e10cSrcweir         }
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     private String parseResult(String erg) {
200*cdf0e10cSrcweir         String lineFeed = System.getProperty("line.separator");
201*cdf0e10cSrcweir         String processedErg = null;
202*cdf0e10cSrcweir 		if (erg != null) {
203*cdf0e10cSrcweir 			StringTokenizer token = new StringTokenizer(erg, lineFeed);
204*cdf0e10cSrcweir 			String previousLine = null;
205*cdf0e10cSrcweir 			while ( token.hasMoreTokens() ) {
206*cdf0e10cSrcweir 				String line = token.nextToken();
207*cdf0e10cSrcweir 				// got a failure!
208*cdf0e10cSrcweir 				if ( line.indexOf("FAILED") != -1 ) {
209*cdf0e10cSrcweir 					processedErg = (processedErg == null)?"":processedErg + ";";
210*cdf0e10cSrcweir 					processedErg += previousLine + ":" + line;
211*cdf0e10cSrcweir 				}
212*cdf0e10cSrcweir 				if ( line.startsWith("Execute:") ) {
213*cdf0e10cSrcweir 					previousLine = line;
214*cdf0e10cSrcweir 				}
215*cdf0e10cSrcweir 				else  {
216*cdf0e10cSrcweir 					previousLine += " " + line;
217*cdf0e10cSrcweir 				}
218*cdf0e10cSrcweir 			}
219*cdf0e10cSrcweir 		}
220*cdf0e10cSrcweir         return processedErg;
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
227*cdf0e10cSrcweir     {
228*cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
229*cdf0e10cSrcweir         return xMSF1;
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir     // setup and close connections
233*cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
234*cdf0e10cSrcweir         System.out.println("setUpConnection()");
235*cdf0e10cSrcweir         connection.setUp();
236*cdf0e10cSrcweir     }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
239*cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
240*cdf0e10cSrcweir     {
241*cdf0e10cSrcweir         System.out.println("tearDownConnection()");
242*cdf0e10cSrcweir         connection.tearDown();
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 
250