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