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 
28 package lib;
29 
30 import java.util.Hashtable;
31 import util.PropertyName;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.uno.XComponentContext;
34 
35 //import com.sun.star.lang.XMultiServiceFactory;
36 
37 /**
38  * TestParameters describes a parameters (in a form of pairs: name, value) to
39  * be passed to tests and which may affect the test behaviour. That can be,
40  * for example, standard paths, connection strings, etc. The TestParameters
41  * also provides XMultiServiceFactory for the test (tests).
42  */
43 public class TestParameters extends Hashtable {
44 
45     /**
46      * The ConnectionString for Office Connection<br>
47      * default is 'socket,host=localhost,port=8100'
48      */
49 
50     public String ConnectionString="socket,host=localhost,port=8100";
51 
52     /**
53      * The AppProvider contains the Application Provider<br>
54      * to control the ServiceFactory.
55      */
56 
57     public Object AppProvider=null;
58 
59     /**
60      * The Process contains the Process handler<br>
61      * to control the Application.
62      */
63 
64     public Object ProcessHandler=null;
65 
66     /**
67      * The AppExecutionCmd contains the full qualified<br>
68      * command to an Application to be started.
69      */
70 
71     public String AppExecutionCommand="";
72 
73     /**
74      * If this parameter is <CODE>true</CODE> the <CODE>OfficeProvider</CODE> tries
75      * to get the URL to the binary of the office and to fill the
76      * <CODE>AppExecutionCommand</CODE> with usefull content if needet
77      */
78     public boolean AutoRestart = false;
79 
80     /**
81      * Shoert wait time for the Office: default is 500 milliseconds
82      */
83     public int ShortWait = 500;
84 
85 
86     /**
87      * The OfficeProvider contains the full qualified
88      * class that provides a connection to StarOffice<br>
89      * default is helper.OfficeProvider
90      */
91 
92     public String OfficeProvider = "helper.OfficeProvider";
93 
94     /**
95      * The Testbase to be executed by the runner<br>
96      * default is 'java_fat'
97      */
98 
99     public String TestBase="java_fat";
100 
101     /**
102      * The ServiceFactory to create instances
103      */
104 
105     public Object ServiceFactory;
106 
107     /**
108      * The Path to the component description
109      */
110 
111     public String DescriptionPath;
112 
113     /**
114      * The Path to the test documents that are loaded during the test <br>
115      */
116 
117     public String TestDocumentPath="unknown";
118 
119     /**
120      * 'true' is a log should be written, 'false' elsewhere <br>
121      * these will be provided by the testcases<br>
122      * default is true
123      */
124 
125     public boolean LoggingIsActive=true;
126 
127     /**
128      * 'true' is a debug information should be written, 'false' elsewhere
129      * these will be provided by the framework.<br>
130      * Debug information will always be written on standard out.<br>
131      * default is true
132      */
133 
134     public boolean DebugIsActive=false;
135 
136     /*
137      * This parameter contains the testjob to be executed<br>
138      * by the framework
139      */
140 
141     public Object TestJob;
142 
143     /*
144      * This parameter contains the class used<br>
145      * for Logging
146      */
147 
148     public String LogWriter="stats.SimpleLogWriter";
149 
150     /*
151      * This parameter contains the class used<br>
152      * for Logging
153      */
154 
155     public String OutProducer="stats.SimpleOutProducer";
156 
157     /*
158      * This parameter contains the timeout used<br>
159      * by the watcher
160      */
161     public Integer TimeOut = new Integer(3000000);
162 
163     /*
164      * This parameter contains the timeout used<br>
165      * by the complex tests
166      */
167     public Integer ThreadTimeOut = new Integer(3000000);
168 
169     /*
170      * This parameter contains the time which the office could use to close for
171      * itself before its destroyed. Default is 15000 ms
172      */
173     public Integer OfficeCloseTimeOut = new Integer(15000);
174 
175     /**
176      * Wraper around "get()" with some debug output
177      * @param key A key of this table.
178      * @return The value of this key.
179      * @see java.util.Hashtable
180      */
181     public Object get(Object key) {
182         Object val = super.get(key);
183         if (val == null && DebugIsActive) {
184             System.out.print("Have been asked for key \""+key.toString());
185             System.out.println("\" which is not part of params.");
186         }
187         return val;
188     }
189 
190     /**
191      * Special get method for boolean values: for convenience.
192      * Will return 'false' if the value is not of "Boolean" type.
193      * @param key A key of this table.
194      * @return The value of this key, castet to a boolean type.
195      */
196     public boolean getBool(Object key) {
197         Object val = super.get(key);
198         if (val != null) {
199             if (val instanceof String) {
200                 String sVal = (String)val;
201                 if (sVal.equalsIgnoreCase("true") ||
202                                                 sVal.equalsIgnoreCase("yes")) {
203                     return true;
204                 }
205                 else if (sVal.equalsIgnoreCase("false") ||
206                                                 sVal.equalsIgnoreCase("no")) {
207                     return false;
208                 }
209             }
210             if (val instanceof Boolean)
211                 return ((Boolean)val).booleanValue();
212         }
213         return false;
214     }
215 
216     /**
217      * Special get method for integer values: for convenience.
218      * Will return 0 if the value cannot be interpreted as Integer.
219      * @param key A key of this table.
220      * @return The value of this key, castet to an int type.
221      */
222     public int getInt(Object key) {
223         Object val = super.get(key);
224         if ( val != null ) {
225             if (val instanceof Integer) {
226                 return ((Integer)val).intValue();
227             }
228             else {
229                 try {
230                     if ( val instanceof String ) {
231                         Integer nr = new Integer((String)val);
232                         if (nr.intValue() > 0) return nr.intValue();
233                     }
234                 } catch ( java.lang.NumberFormatException nfe) {}
235             }
236         }
237         return 0;
238     }
239 
240 
241     /**
242      * Wraper around "put()"
243      * @param key A key of this table.
244      * @param val The value of the key.
245      * @return The value of this key.
246      * @see java.util.Hashtable
247      */
248     public Object put(Object key, Object val) {
249         return super.put(key,val);
250     }
251 
252     /**
253      * Constructor, defaults for Parameters are set.
254      */
255     public TestParameters() {
256         //fill the propertyset
257         String user = System.getProperty("user.name");
258         if ( user != null)
259         {
260             String PipeConnectionString = "pipe,name=" + user;
261             put(PropertyName.PIPE_CONNECTION_STRING,PipeConnectionString);
262             put(PropertyName.USE_PIPE_CONNECTION, Boolean.TRUE);
263         }
264         put(PropertyName.CONNECTION_STRING,ConnectionString);
265         put(PropertyName.TEST_BASE,TestBase);
266         put(PropertyName.TEST_DOCUMENT_PATH,TestDocumentPath);
267         put(PropertyName.LOGGING_IS_ACTIVE,LoggingIsActive?Boolean.TRUE:Boolean.FALSE);
268         put(PropertyName.DEBUG_IS_ACTIVE,DebugIsActive?Boolean.TRUE:Boolean.FALSE);
269         put(PropertyName.OUT_PRODUCER,OutProducer);
270         put(PropertyName.SHORT_WAIT,new Integer(ShortWait));
271         put(PropertyName.OFFICE_PROVIDER,OfficeProvider);
272         put(PropertyName.LOG_WRITER,LogWriter);
273         put(PropertyName.APP_EXECUTION_COMMAND,AppExecutionCommand);
274         put(PropertyName.TIME_OUT,TimeOut);
275         put(PropertyName.THREAD_TIME_OUT,ThreadTimeOut);
276         put(PropertyName.AUTO_RESTART,AutoRestart?Boolean.TRUE:Boolean.FALSE);
277         put(PropertyName.OFFICE_CLOSE_TIME_OUT, OfficeCloseTimeOut);
278 
279         // get the operating system
280         put(PropertyName.OPERATING_SYSTEM, getSOCompatibleOSName());
281 
282         //For compatibility Reasons
283         put("CNCSTR",ConnectionString);
284         put("DOCPTH",TestDocumentPath);
285         System.setProperty("DOCPTH",TestDocumentPath);
286     }
287 
288     /**
289      * @return a XMultiServiceFactory to be used by a test (tests).
290      */
291     public Object getMSF() {
292         Object ret = null;
293         ret = get("ServiceFactory");
294         return ret;
295     }
296 
297     public XComponentContext getComponentContext() {
298         Object context = get( "ComponentContext" );
299         if ( context == null )
300         {
301             XPropertySet factoryProps = (XPropertySet)com.sun.star.uno.UnoRuntime.queryInterface(
302                 XPropertySet.class, getMSF() );
303             try
304             {
305                 context = com.sun.star.uno.UnoRuntime.queryInterface(
306                     XComponentContext.class, factoryProps.getPropertyValue( "DefaultContext" ) );
307                 put( "ComponentContext", context );
308             }
309             catch( com.sun.star.beans.UnknownPropertyException e ) { }
310             catch( com.sun.star.lang.WrappedTargetException e ) { }
311         }
312         return (XComponentContext)context;
313     }
314 
315     /**
316      * Convert the system dependent operating system name to a name according
317      * to OOo rules.
318      * @return A valid OS name, or "" if the name is not known.
319      */
320     String getSOCompatibleOSName() {
321         String osname = System.getProperty ("os.name").toLowerCase ();
322         String osarch = System.getProperty ("os.arch");
323         String operatingSystem = "";
324         if (osname.indexOf ("windows")>-1) {
325             operatingSystem = PropertyName.WNTMSCI;
326         } else if (osname.indexOf ("linux")>-1) {
327             operatingSystem = PropertyName.UNXLNGI;
328         } else if (osname.indexOf ("sunos")>-1) {
329             if (osarch.equals ("x86")) {
330                 operatingSystem = PropertyName.UNXSOLI;
331             } else {
332                 operatingSystem = PropertyName.UNXSOLS;
333             }
334         } else if (osname.indexOf ("mac")>-1) {
335             operatingSystem = PropertyName.UNXMACXI;
336         } else {
337             System.out.println("ERROR: not supported platform: " + osname);
338             System.exit(1);
339         }
340         return operatingSystem;
341     }
342 
343 }// finish class TestParamenters
344