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