| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package complex.api_internal; |
| 19 |
|
| 20 |
import static org.junit.Assert.assertNotNull; |
| 21 |
import static org.junit.Assert.assertTrue; |
| 22 |
import static org.junit.Assert.fail; |
| 23 |
import helper.OfficeProvider; |
| 24 |
import helper.ProcessHandler; |
| 25 |
|
| 26 |
import java.io.UnsupportedEncodingException; |
| 27 |
import java.util.StringTokenizer; |
| 28 |
import lib.TestParameters; |
| 29 |
|
| 30 |
import org.junit.AfterClass; |
| 31 |
import org.junit.Before; |
| 32 |
import org.junit.BeforeClass; |
| 33 |
import org.junit.Test; |
| 34 |
import org.openoffice.test.OfficeConnection; |
| 35 |
|
| 36 |
import com.sun.star.beans.NamedValue; |
| 37 |
import com.sun.star.beans.PropertyValue; |
| 38 |
import com.sun.star.lang.XMultiServiceFactory; |
| 39 |
import com.sun.star.task.XJob; |
| 40 |
import com.sun.star.uno.UnoRuntime; |
| 41 |
|
| 42 |
/** |
| 43 |
* This test executes the API tests internally in LibreOffice. Prerequisite is |
| 44 |
* that an OOoRunner.jar is registered inside of LibreOffice. Adjust the joblist |
| 45 |
* inside of the CheckAPI.props to determine which tests will be executed. |
| 46 |
*/ |
| 47 |
public class CheckAPI { |
| 48 |
|
| 49 |
/** |
| 50 |
* The test parameters |
| 51 |
*/ |
| 52 |
private TestParameters param = null; |
| 53 |
|
| 54 |
/** |
| 55 |
* |
| 56 |
*/ |
| 57 |
@Before public void before() |
| 58 |
{ |
| 59 |
param = new TestParameters(); |
| 60 |
} |
| 61 |
/** |
| 62 |
* Execute the API tests inside of the Office. If the Office crashes, it |
| 63 |
* will be restarted and the job will continue after the one that caused the crash. |
| 64 |
*/ |
| 65 |
@Test public void checkAPI() { |
| 66 |
System.out.println("Start with test"); |
| 67 |
// if test is idle for 5 minutes, assume that it hangs and kill it. |
| 68 |
XMultiServiceFactory xMSF = getMSF(); |
| 69 |
Object oObj = null; |
| 70 |
try { |
| 71 |
oObj = xMSF.createInstance("org.openoffice.RunnerService"); |
| 72 |
} |
| 73 |
catch(com.sun.star.uno.Exception e) { |
| 74 |
fail("Could not create Instance of 'org.openoffice.RunnerService'"); |
| 75 |
} |
| 76 |
assertNotNull("Cannot create 'org.openoffice.RunnerService'", oObj); |
| 77 |
|
| 78 |
// get the parameters for the internal test |
| 79 |
final NamedValue[] internalParams = new NamedValue[3]; |
| 80 |
internalParams[0] = new NamedValue(); |
| 81 |
internalParams[0].Name = "-OutProducer"; |
| 82 |
internalParams[0].Value = "stats.SimpleFileOutProducer"; |
| 83 |
internalParams[1] = new NamedValue(); |
| 84 |
internalParams[1].Name = "-OutputPath"; |
| 85 |
internalParams[1].Value = "/dev/null"; |
| 86 |
|
| 87 |
// do we have test jobs? |
| 88 |
final PropertyValue[] props = new PropertyValue[1]; |
| 89 |
props[0] = new PropertyValue(); |
| 90 |
props[0].Value = "sw.SwXTextTable"; |
| 91 |
|
| 92 |
System.out.println("Props length: "+ props.length); |
| 93 |
for (int i=0; i<props.length; i++) { |
| 94 |
XJob xJob = UnoRuntime.queryInterface(XJob.class, oObj); |
| 95 |
internalParams[2] = new NamedValue(); |
| 96 |
internalParams[2].Name = "-o"; |
| 97 |
internalParams[2].Value = props[i].Value; |
| 98 |
System.out.println("Executing: " + (String)props[i].Value); |
| 99 |
|
| 100 |
String erg = null; |
| 101 |
|
| 102 |
try { |
| 103 |
erg = (String)xJob.execute(internalParams); |
| 104 |
} |
| 105 |
catch(Throwable t) { |
| 106 |
// restart and go on with test!! |
| 107 |
t.printStackTrace(); |
| 108 |
fail("Test run '" + (String)props[i].Value +"' could not be executed: Office crashed and is killed!"); |
| 109 |
xMSF = null; |
| 110 |
ProcessHandler handler = (ProcessHandler)param.get("AppProvider"); |
| 111 |
handler.kill(); |
| 112 |
util.utils.pause(10000); |
| 113 |
OfficeProvider op = new OfficeProvider(); |
| 114 |
try { |
| 115 |
xMSF = (XMultiServiceFactory)op.getManager(param); |
| 116 |
param.put("ServiceFactory",xMSF); |
| 117 |
|
| 118 |
oObj = xMSF.createInstance("org.openoffice.RunnerService"); |
| 119 |
} |
| 120 |
catch(com.sun.star.uno.Exception e) { |
| 121 |
fail("Could not create Instance of 'org.openoffice.RunnerService'"); |
| 122 |
} |
| 123 |
catch (UnsupportedEncodingException e) { |
| 124 |
fail("Could not get Manager'"); |
| 125 |
} |
| 126 |
} |
| 127 |
System.out.println(erg); |
| 128 |
String processedErg = parseResult(erg); |
| 129 |
assertTrue("Run '" + (String)props[i].Value + "' has result '" + processedErg + "'", processedErg == null); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private String parseResult(String erg) { |
| 134 |
String lineFeed = System.getProperty("line.separator"); |
| 135 |
String processedErg = null; |
| 136 |
if (erg != null) { |
| 137 |
StringTokenizer token = new StringTokenizer(erg, lineFeed); |
| 138 |
String previousLine = null; |
| 139 |
while ( token.hasMoreTokens() ) { |
| 140 |
String line = token.nextToken(); |
| 141 |
// got a failure! |
| 142 |
if ( line.indexOf("FAILED") != -1 ) { |
| 143 |
processedErg = (processedErg == null)?"":processedErg + ";"; |
| 144 |
processedErg += previousLine + ":" + line; |
| 145 |
} |
| 146 |
if ( line.startsWith("Execute:") ) { |
| 147 |
previousLine = line; |
| 148 |
} |
| 149 |
else { |
| 150 |
previousLine += " " + line; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
return processedErg; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
private XMultiServiceFactory getMSF() |
| 159 |
{ |
| 160 |
return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); |
| 161 |
} |
| 162 |
|
| 163 |
// setup and close connections |
| 164 |
@BeforeClass public static void setUpConnection() throws Exception { |
| 165 |
System.out.println("setUpConnection()"); |
| 166 |
connection.setUp(); |
| 167 |
} |
| 168 |
|
| 169 |
@AfterClass public static void tearDownConnection() |
| 170 |
throws InterruptedException, com.sun.star.uno.Exception |
| 171 |
{ |
| 172 |
System.out.println("tearDownConnection()"); |
| 173 |
connection.tearDown(); |
| 174 |
} |
| 175 |
|
| 176 |
private static final OfficeConnection connection = new OfficeConnection(); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
|