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 helper;
28 
29 import complexlib.ComplexTestCase;
30 import util.DynamicClassLoader;
31 import share.DescEntry;
32 import share.DescGetter;
33 import share.ComplexTest;
34 import java.util.Vector;
35 import share.LogWriter;
36 
37 /**
38  *
39  */
40 public class ComplexDescGetter extends DescGetter
41 {
42 
43     ComplexTest testClass;
44 
45     /** Creates new ComplexDescGetter */
46     public ComplexDescGetter()
47     {
48         testClass = null;
49     }
50 
51     public DescEntry[] getDescriptionFor(String entry, String DescPath,
52             boolean debug)
53     {
54         // read scenario file
55         if (entry.startsWith("-sce"))
56         {
57             DescEntry[] entries = getScenario(entry.substring(5), null, debug);
58             return entries;
59         }
60         // one single job
61         else if (entry.startsWith("-o"))
62         {
63             DescEntry dEntry = getDescriptionForSingleJob(entry.substring(3), null, debug);
64             if (dEntry != null)
65             {
66                 return new DescEntry[]
67                         {
68                             dEntry
69                         };
70             }
71         }
72         System.out.println("Could not get a testjob with parameter '" + entry + "'");
73         // no job available
74         return null;
75     }
76 
77     protected DescEntry getDescriptionForSingleJob(String className, String descPath, boolean debug)
78     {
79         DynamicClassLoader dcl = new DynamicClassLoader();
80         String methodNames[] = null;
81 
82         if (debug)
83         {
84             System.out.println("Searching Class: " + className);
85         }
86 
87         int index = className.indexOf("::");
88         if (index != -1)
89         {
90             // case1: method()
91             // case2: method(param1,param2)
92             // case3: method1(param1,param2),method2(param1,param2)
93             String method = className.substring(index + 2);
94             className = className.substring(0, index);
95             Vector methods = new Vector();
96 
97             String[] split = method.split("(?<=\\)),(?=\\w+)");
98 
99             for (int i = 0; i < split.length; i++)
100             {
101                 String meth = split[i];
102 
103                 if (meth.endsWith("()"))
104                 {
105                     meth = meth.substring(0, meth.length() - 2);
106                 }
107 
108                 methods.add(meth);
109             }
110 
111             methodNames = new String[methods.size()];
112             methodNames = (String[]) methods.toArray(methodNames);
113         }
114 
115         // create an instance
116         try
117         {
118             testClass = (ComplexTestCase) dcl.getInstance(className);
119         }
120         catch (java.lang.IllegalArgumentException e)
121         {
122             System.out.println("Error while getting description for test '" + className + "' as a Complex test.");
123             return null;
124         }
125         catch (java.lang.ClassCastException e)
126         {
127             System.out.println("The given class '" + className + "' is not a Complex test.");
128             return null;
129         }
130 
131 
132         if (debug)
133         {
134             System.out.println("Got test: " + ((Object) testClass).toString());
135         }
136 
137         String testObjectName = className;
138         String[] testMethodNames = null;
139 
140         if (testMethodNames == null)
141         {
142             testMethodNames = testClass.getTestMethodNames();
143         }
144         if (methodNames != null)
145         {
146             testMethodNames = methodNames;
147         }
148 
149         DescEntry dEntry = createTestDesc(testObjectName, className, testMethodNames, null);
150 
151         return dEntry;
152     }
153 
154     /**
155      * Creates a description exntry for the given parameter
156      * @param testObjectName the name of the object
157      * @param className the class name of the class to load
158      * @param testMethodNames list of all methods to test
159      * @param log
160      * @return filled description entry
161      */
162     public DescEntry createTestDesc(String testObjectName, String className, String[] testMethodNames, LogWriter log)
163     {
164 
165         DescEntry dEntry = new DescEntry();
166 
167         dEntry.entryName = testObjectName;
168         dEntry.longName = className;
169         dEntry.isOptional = false;
170         dEntry.EntryType = "unit";
171         dEntry.isToTest = true;
172         dEntry.Logger = log;
173         dEntry.SubEntryCount = testMethodNames.length;
174         dEntry.SubEntries = new DescEntry[dEntry.SubEntryCount];
175         for (int i = 0; i < dEntry.SubEntryCount; i++)
176         {
177             DescEntry aEntry = new DescEntry();
178             aEntry.entryName = testMethodNames[i];
179             aEntry.longName = testObjectName + "::" + aEntry.entryName;
180             aEntry.isOptional = false;
181             aEntry.EntryType = "method";
182             aEntry.isToTest = true;
183             dEntry.SubEntries[i] = aEntry;
184             dEntry.Logger = log;
185         }
186 
187         return dEntry;
188     }
189 
190     protected String[] createScenario(String descPath, String job, boolean debug)
191     {
192         return new String[] {};
193     }
194 }
195