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