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 basicrunner;
28 
29 import lib.TestResult;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import share.DescEntry;
33 import share.LogWriter;
34 
35 import com.sun.star.lang.XMultiServiceFactory;
36 
37 import com.sun.star.beans.PropertyValue;
38 
39 
40 /**
41  * The BASIC interface test
42  */
43 public class BasicIfcTest {
44     /** The BasicHandler **/
45     static BasicHandler oBasicHandler = null;
46     /** The result orf the test **/
47     protected TestResult tRes;
48     /** the name of the test **/
49     protected String testName;
50 
51     /** Constructor with test name.
52      * @param name The name of the test.
53      */
54     public BasicIfcTest(String name) {
55         testName = name;
56     }
57 
58     /**
59      * Let the test run.
60      * @param xTestedEntry Informaton about the interface to test.
61      * @param tEnv The environment of the test.
62      * @param tParam The test parameters.
63      * @return A result of the test.
64      */
65     public TestResult run(DescEntry xTestedEntry, TestEnvironment tEnv,
66             TestParameters tParam) {
67 
68         String sResult = "";
69 
70         this.tRes = new TestResult();
71         LogWriter log = xTestedEntry.Logger;
72 
73         // Get Handler, that was created during object creation.
74         try {
75             oBasicHandler = (BasicHandler)tEnv.getObjRelation("BasicHandler");
76         } catch (java.lang.NullPointerException e) {
77             log.println("No Component created");
78             return null;
79         }
80 
81         if (!oBasicHandler.isUptodate((XMultiServiceFactory)tParam.getMSF())) {
82             // If Handler uses old MSF (in case of Office's GPF) then don't test
83             // interface.
84             return null;
85         }
86 
87         boolean objectWasCreated = ((Boolean)tEnv.getObjRelation("objectCreated")).booleanValue();
88 
89         if (objectWasCreated) {
90             oBasicHandler.setTestedInterface(this, log);
91 
92             DescEntry methods[] = xTestedEntry.SubEntries;
93 
94             String  names[] = new String[methods.length + 1];
95             boolean isOpt[] = new boolean[methods.length + 1];
96             String other[] = new String[1];
97 
98             String aName = xTestedEntry.longName;
99             aName = aName.substring(aName.indexOf("::")+2);
100             int oldIndex = 0;
101             int index = aName.indexOf("::");
102             names[0] = "";
103             while(index!=-1) {
104                 names[0] += aName.substring(oldIndex,index) + ".";
105                 oldIndex=index+2;
106                 index=aName.indexOf("::", oldIndex);
107             }
108             names[0] += aName.substring(oldIndex);
109             isOpt[0] = xTestedEntry.isOptional;
110 
111             for (int i = 1; i < names.length; i++) {
112                 names[i] = methods[i - 1].entryName;
113                 isOpt[i] = methods[i - 1].isOptional;
114             }
115 
116             // for reasons of compatibility with JSuite we change the first
117             // character of EntryType to upper case.
118             String eType = xTestedEntry.EntryType;
119             other[0] = eType.toUpperCase().charAt(0)+eType.substring(1);
120 
121             Object params[] = {names, isOpt, other};
122 
123             try {
124                 PropertyValue Res = oBasicHandler.perform("testInterface", params);
125                 sResult = (String)Res.Value;
126             } catch (BasicException e) {
127                 log.println(e.info);
128                 sResult = "SKIPPED.FAILED";
129             }
130         } else { // if object was not created...
131             sResult = "SKIPPED.FAILED";
132         }
133 
134         // now tRes has all substates: collect them
135         DescEntry[] subs = xTestedEntry.SubEntries;
136         for (int i = 0; i < subs.length ; i++) {
137             if (sResult.equals("SKIPPED.FAILED"))
138                 subs[i].State = "SKIPPED.FAILED";
139             else if (sResult.equals("SKIPPED.OK"))
140                 subs[i].State = "SKIPPED.OK";
141             else
142                 if (tRes.getStatusFor(subs[i].entryName) == null) {
143                     subs[i].State = "SKIPPED.FAILED";
144                 } else {
145                     subs[i].State = tRes.getStatusFor(
146                                                 subs[i].entryName).toString();
147                 }
148         }
149 
150         xTestedEntry.State = sResult;
151         return null;
152     }
153 
154     /**
155      * Set the result of the method that is tested.
156      * @param methodName The name of the method.
157      * @param bResult The result of the test.
158      */
159     public void methodTested(String methodName, boolean bResult) {
160         tRes.tested(methodName, bResult);
161     }
162 
163     /**
164      * @return The name of the interface or the service tested.
165      */
166     String getTestedClassName() {
167         return testName;
168     }
169 }
170