1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package helper;
24 
25 import complexlib.ComplexTestCase;
26 import util.DynamicClassLoader;
27 import share.DescEntry;
28 import share.DescGetter;
29 import share.ComplexTest;
30 import java.util.Vector;
31 import share.LogWriter;
32 
33 /**
34  *
35  */
36 public class ComplexDescGetter extends DescGetter
37 {
38 
39     ComplexTest testClass;
40 
41     /** Creates new ComplexDescGetter */
ComplexDescGetter()42     public ComplexDescGetter()
43     {
44         testClass = null;
45     }
46 
getDescriptionFor(String entry, String DescPath, boolean debug)47     public DescEntry[] getDescriptionFor(String entry, String DescPath,
48             boolean debug)
49     {
50         // read scenario file
51         if (entry.startsWith("-sce"))
52         {
53             DescEntry[] entries = getScenario(entry.substring(5), null, debug);
54             return entries;
55         }
56         // one single job
57         else if (entry.startsWith("-o"))
58         {
59             DescEntry dEntry = getDescriptionForSingleJob(entry.substring(3), null, debug);
60             if (dEntry != null)
61             {
62                 return new DescEntry[]
63                         {
64                             dEntry
65                         };
66             }
67         }
68         System.out.println("Could not get a testjob with parameter '" + entry + "'");
69         // no job available
70         return null;
71     }
72 
getDescriptionForSingleJob(String className, String descPath, boolean debug)73     protected DescEntry getDescriptionForSingleJob(String className, String descPath, boolean debug)
74     {
75         DynamicClassLoader dcl = new DynamicClassLoader();
76         String methodNames[] = null;
77 
78         if (debug)
79         {
80             System.out.println("Searching Class: " + className);
81         }
82 
83         int index = className.indexOf("::");
84         if (index != -1)
85         {
86             // case1: method()
87             // case2: method(param1,param2)
88             // case3: method1(param1,param2),method2(param1,param2)
89             String method = className.substring(index + 2);
90             className = className.substring(0, index);
91             Vector methods = new Vector();
92 
93             String[] split = method.split("(?<=\\)),(?=\\w+)");
94 
95             for (int i = 0; i < split.length; i++)
96             {
97                 String meth = split[i];
98 
99                 if (meth.endsWith("()"))
100                 {
101                     meth = meth.substring(0, meth.length() - 2);
102                 }
103 
104                 methods.add(meth);
105             }
106 
107             methodNames = new String[methods.size()];
108             methodNames = (String[]) methods.toArray(methodNames);
109         }
110 
111         // create an instance
112         try
113         {
114             testClass = (ComplexTestCase) dcl.getInstance(className);
115         }
116         catch (java.lang.IllegalArgumentException e)
117         {
118             System.out.println("Error while getting description for test '" + className + "' as a Complex test.");
119             return null;
120         }
121         catch (java.lang.ClassCastException e)
122         {
123             System.out.println("The given class '" + className + "' is not a Complex test.");
124             return null;
125         }
126 
127 
128         if (debug)
129         {
130             System.out.println("Got test: " + ((Object) testClass).toString());
131         }
132 
133         String testObjectName = className;
134         String[] testMethodNames = null;
135 
136         if (testMethodNames == null)
137         {
138             testMethodNames = testClass.getTestMethodNames();
139         }
140         if (methodNames != null)
141         {
142             testMethodNames = methodNames;
143         }
144 
145         DescEntry dEntry = createTestDesc(testObjectName, className, testMethodNames, null);
146 
147         return dEntry;
148     }
149 
150     /**
151      * Creates a description exntry for the given parameter
152      * @param testObjectName the name of the object
153      * @param className the class name of the class to load
154      * @param testMethodNames list of all methods to test
155      * @param log
156      * @return filled description entry
157      */
createTestDesc(String testObjectName, String className, String[] testMethodNames, LogWriter log)158     public DescEntry createTestDesc(String testObjectName, String className, String[] testMethodNames, LogWriter log)
159     {
160 
161         DescEntry dEntry = new DescEntry();
162 
163         dEntry.entryName = testObjectName;
164         dEntry.longName = className;
165         dEntry.isOptional = false;
166         dEntry.EntryType = "unit";
167         dEntry.isToTest = true;
168         dEntry.Logger = log;
169         dEntry.SubEntryCount = testMethodNames.length;
170         dEntry.SubEntries = new DescEntry[dEntry.SubEntryCount];
171         for (int i = 0; i < dEntry.SubEntryCount; i++)
172         {
173             DescEntry aEntry = new DescEntry();
174             aEntry.entryName = testMethodNames[i];
175             aEntry.longName = testObjectName + "::" + aEntry.entryName;
176             aEntry.isOptional = false;
177             aEntry.EntryType = "method";
178             aEntry.isToTest = true;
179             dEntry.SubEntries[i] = aEntry;
180             dEntry.Logger = log;
181         }
182 
183         return dEntry;
184     }
185 
createScenario(String descPath, String job, boolean debug)186     protected String[] createScenario(String descPath, String job, boolean debug)
187     {
188         return new String[] {};
189     }
190 }
191