1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package helper;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import complexlib.ComplexTestCase;
30*cdf0e10cSrcweir import util.DynamicClassLoader;
31*cdf0e10cSrcweir import share.DescEntry;
32*cdf0e10cSrcweir import share.DescGetter;
33*cdf0e10cSrcweir import share.ComplexTest;
34*cdf0e10cSrcweir import java.util.Vector;
35*cdf0e10cSrcweir import share.LogWriter;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir /**
38*cdf0e10cSrcweir  *
39*cdf0e10cSrcweir  */
40*cdf0e10cSrcweir public class ComplexDescGetter extends DescGetter
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir     ComplexTest testClass;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir     /** Creates new ComplexDescGetter */
46*cdf0e10cSrcweir     public ComplexDescGetter()
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         testClass = null;
49*cdf0e10cSrcweir     }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     public DescEntry[] getDescriptionFor(String entry, String DescPath,
52*cdf0e10cSrcweir             boolean debug)
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         // read scenario file
55*cdf0e10cSrcweir         if (entry.startsWith("-sce"))
56*cdf0e10cSrcweir         {
57*cdf0e10cSrcweir             DescEntry[] entries = getScenario(entry.substring(5), null, debug);
58*cdf0e10cSrcweir             return entries;
59*cdf0e10cSrcweir         }
60*cdf0e10cSrcweir         // one single job
61*cdf0e10cSrcweir         else if (entry.startsWith("-o"))
62*cdf0e10cSrcweir         {
63*cdf0e10cSrcweir             DescEntry dEntry = getDescriptionForSingleJob(entry.substring(3), null, debug);
64*cdf0e10cSrcweir             if (dEntry != null)
65*cdf0e10cSrcweir             {
66*cdf0e10cSrcweir                 return new DescEntry[]
67*cdf0e10cSrcweir                         {
68*cdf0e10cSrcweir                             dEntry
69*cdf0e10cSrcweir                         };
70*cdf0e10cSrcweir             }
71*cdf0e10cSrcweir         }
72*cdf0e10cSrcweir         System.out.println("Could not get a testjob with parameter '" + entry + "'");
73*cdf0e10cSrcweir         // no job available
74*cdf0e10cSrcweir         return null;
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     protected DescEntry getDescriptionForSingleJob(String className, String descPath, boolean debug)
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         DynamicClassLoader dcl = new DynamicClassLoader();
80*cdf0e10cSrcweir         String methodNames[] = null;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         if (debug)
83*cdf0e10cSrcweir         {
84*cdf0e10cSrcweir             System.out.println("Searching Class: " + className);
85*cdf0e10cSrcweir         }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         int index = className.indexOf("::");
88*cdf0e10cSrcweir         if (index != -1)
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             // case1: method()
91*cdf0e10cSrcweir             // case2: method(param1,param2)
92*cdf0e10cSrcweir             // case3: method1(param1,param2),method2(param1,param2)
93*cdf0e10cSrcweir             String method = className.substring(index + 2);
94*cdf0e10cSrcweir             className = className.substring(0, index);
95*cdf0e10cSrcweir             Vector methods = new Vector();
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir             String[] split = method.split("(?<=\\)),(?=\\w+)");
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir             for (int i = 0; i < split.length; i++)
100*cdf0e10cSrcweir             {
101*cdf0e10cSrcweir                 String meth = split[i];
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir                 if (meth.endsWith("()"))
104*cdf0e10cSrcweir                 {
105*cdf0e10cSrcweir                     meth = meth.substring(0, meth.length() - 2);
106*cdf0e10cSrcweir                 }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir                 methods.add(meth);
109*cdf0e10cSrcweir             }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir             methodNames = new String[methods.size()];
112*cdf0e10cSrcweir             methodNames = (String[]) methods.toArray(methodNames);
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         // create an instance
116*cdf0e10cSrcweir         try
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             testClass = (ComplexTestCase) dcl.getInstance(className);
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir         catch (java.lang.IllegalArgumentException e)
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             System.out.println("Error while getting description for test '" + className + "' as a Complex test.");
123*cdf0e10cSrcweir             return null;
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir         catch (java.lang.ClassCastException e)
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             System.out.println("The given class '" + className + "' is not a Complex test.");
128*cdf0e10cSrcweir             return null;
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         if (debug)
133*cdf0e10cSrcweir         {
134*cdf0e10cSrcweir             System.out.println("Got test: " + ((Object) testClass).toString());
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         String testObjectName = className;
138*cdf0e10cSrcweir         String[] testMethodNames = null;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         if (testMethodNames == null)
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir             testMethodNames = testClass.getTestMethodNames();
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir         if (methodNames != null)
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             testMethodNames = methodNames;
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         DescEntry dEntry = createTestDesc(testObjectName, className, testMethodNames, null);
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         return dEntry;
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     /**
155*cdf0e10cSrcweir      * Creates a description exntry for the given parameter
156*cdf0e10cSrcweir      * @param testObjectName the name of the object
157*cdf0e10cSrcweir      * @param className the class name of the class to load
158*cdf0e10cSrcweir      * @param testMethodNames list of all methods to test
159*cdf0e10cSrcweir      * @param log
160*cdf0e10cSrcweir      * @return filled description entry
161*cdf0e10cSrcweir      */
162*cdf0e10cSrcweir     public DescEntry createTestDesc(String testObjectName, String className, String[] testMethodNames, LogWriter log)
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         DescEntry dEntry = new DescEntry();
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         dEntry.entryName = testObjectName;
168*cdf0e10cSrcweir         dEntry.longName = className;
169*cdf0e10cSrcweir         dEntry.isOptional = false;
170*cdf0e10cSrcweir         dEntry.EntryType = "unit";
171*cdf0e10cSrcweir         dEntry.isToTest = true;
172*cdf0e10cSrcweir         dEntry.Logger = log;
173*cdf0e10cSrcweir         dEntry.SubEntryCount = testMethodNames.length;
174*cdf0e10cSrcweir         dEntry.SubEntries = new DescEntry[dEntry.SubEntryCount];
175*cdf0e10cSrcweir         for (int i = 0; i < dEntry.SubEntryCount; i++)
176*cdf0e10cSrcweir         {
177*cdf0e10cSrcweir             DescEntry aEntry = new DescEntry();
178*cdf0e10cSrcweir             aEntry.entryName = testMethodNames[i];
179*cdf0e10cSrcweir             aEntry.longName = testObjectName + "::" + aEntry.entryName;
180*cdf0e10cSrcweir             aEntry.isOptional = false;
181*cdf0e10cSrcweir             aEntry.EntryType = "method";
182*cdf0e10cSrcweir             aEntry.isToTest = true;
183*cdf0e10cSrcweir             dEntry.SubEntries[i] = aEntry;
184*cdf0e10cSrcweir             dEntry.Logger = log;
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         return dEntry;
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     protected String[] createScenario(String descPath, String job, boolean debug)
191*cdf0e10cSrcweir     {
192*cdf0e10cSrcweir         return new String[] {};
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir }
195