001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * Testbag.java
044: *
045: * Created on 25th March 2003, 15:46
046: */
047:
048: package org.netbeans.xtest.harness;
049:
050: import java.util.HashSet;
051: import java.util.StringTokenizer;
052: import java.util.Vector;
053: import org.netbeans.xtest.xmlserializer.*;
054:
055: /**
056: *
057: * @author lm97939
058: */
059:
060: public class Testbag implements XMLSerializable {
061:
062: private String name;
063: private String testattribs;
064: private String executor;
065: private String compiler;
066:
067: private String pluginName;
068:
069: private TestSetup testSetup;
070:
071: private String resultsprocessor;
072: private Integer prio;
073: private Integer timeout;
074:
075: private Testset testsets[];
076: private TestProperty testProperties[];
077:
078: private MTestConfig.AntExecType ant_executor;
079: private MTestConfig.AntExecType ant_compiler;
080: private MTestConfig.AntExecType ant_resultsprocessor;
081:
082: private MTestConfig parent;
083:
084: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
085: Testbag.class);
086: static {
087: try {
088: // register this class
089: classMappingRegistry.registerSimpleField("name",
090: ClassMappingRegistry.ATTRIBUTE, "name");
091: classMappingRegistry.registerSimpleField("testattribs",
092: ClassMappingRegistry.ATTRIBUTE, "testattribs");
093: classMappingRegistry.registerSimpleField("executor",
094: ClassMappingRegistry.ATTRIBUTE, "executor");
095: classMappingRegistry.registerSimpleField("compiler",
096: ClassMappingRegistry.ATTRIBUTE, "compiler");
097: classMappingRegistry.registerSimpleField("pluginName",
098: ClassMappingRegistry.ATTRIBUTE, "plugin");
099: classMappingRegistry.registerSimpleField(
100: "resultsprocessor", ClassMappingRegistry.ATTRIBUTE,
101: "resultsprocessor");
102: classMappingRegistry.registerSimpleField("prio",
103: ClassMappingRegistry.ATTRIBUTE, "prio");
104: classMappingRegistry.registerSimpleField("timeout",
105: ClassMappingRegistry.ATTRIBUTE, "timeout");
106: classMappingRegistry.registerContainerField("testsets",
107: "testset", ClassMappingRegistry.DIRECT);
108: classMappingRegistry.registerContainerField(
109: "testProperties", "testproperty",
110: ClassMappingRegistry.DIRECT);
111: //classMappingRegistry.registerContainerField("setupAndTeardown","setup", ClassMappingRegistry.DIRECT);
112: classMappingRegistry.registerSimpleField("testSetup",
113: ClassMappingRegistry.ELEMENT, "testsetup");
114:
115: } catch (MappingException me) {
116: me.printStackTrace();
117: classMappingRegistry = null;
118: }
119: }
120:
121: public ClassMappingRegistry registerXMLMapping() {
122: return classMappingRegistry;
123: }
124:
125: public String getName() {
126: return name;
127: }
128:
129: protected void setParent(MTestConfig p) {
130: parent = p;
131: }
132:
133: public MTestConfig getParent() {
134: return parent;
135: }
136:
137: protected void setName(String name) {
138: this .name = name;
139: }
140:
141: public String getTestattribs() {
142: return testattribs;
143: }
144:
145: public Integer getPrio() {
146: return prio;
147: }
148:
149: public Integer getTimeout() {
150: return timeout;
151: }
152:
153: public TestProperty[] getTestProperties() {
154: return testProperties;
155: }
156:
157: public String getExecutorName() {
158: return executor;
159: }
160:
161: public String getCompilerName() {
162: return compiler;
163: }
164:
165: public String getResultsprocessorName() {
166: return resultsprocessor;
167: }
168:
169: public MTestConfig.AntExecType getExecutor() {
170: return ant_executor;
171: }
172:
173: public MTestConfig.AntExecType getCompiler() {
174: return ant_compiler;
175: }
176:
177: public MTestConfig.AntExecType getResultsprocessor() {
178: return ant_resultsprocessor;
179: }
180:
181: public String getPluginName() {
182: return pluginName;
183: }
184:
185: public String getSetupDir() {
186: if (testSetup != null) {
187: return testSetup.dir;
188: }
189: return null;
190: }
191:
192: public String getSetUpClassName() {
193: if (testSetup != null) {
194: if (testSetup.setupDescriptor != null) {
195: return testSetup.setupDescriptor.className;
196: }
197: }
198: // else
199: return null;
200: }
201:
202: public String getSetUpMethodName() {
203: if (testSetup != null) {
204: if (testSetup.setupDescriptor != null) {
205: return testSetup.setupDescriptor.methodName;
206: }
207: }
208: // else
209: return null;
210: }
211:
212: public String getTearDownClassName() {
213: if (testSetup != null) {
214: if (testSetup.teardownDescriptor != null) {
215: return testSetup.teardownDescriptor.className;
216: }
217: }
218: // else
219: return null;
220: }
221:
222: public String getTearDownMethodName() {
223: if (testSetup != null) {
224: if (testSetup.teardownDescriptor != null) {
225: return testSetup.teardownDescriptor.methodName;
226: }
227: }
228: // else
229: return null;
230: }
231:
232: public Testset[] getTestsets() {
233: return testsets;
234: }
235:
236: public void setTestsets(Testset[] testsets) {
237: this .testsets = testsets;
238: }
239:
240: protected void setAntExecutor(MTestConfig.AntExecType type) {
241: ant_executor = type;
242: }
243:
244: protected void setAntCompiler(MTestConfig.AntExecType type) {
245: ant_compiler = type;
246: }
247:
248: protected void setAntResultsprocessor(MTestConfig.AntExecType type) {
249: ant_resultsprocessor = type;
250: }
251:
252: protected void validate(HashSet passed_patternset)
253: throws XMLSerializeException {
254: if (name == null)
255: throw new XMLSerializeException(
256: "Attribute name is required for element testbag.");
257: if (testattribs == null)
258: throw new XMLSerializeException(
259: "Attribute testattribs is required for element testbag.");
260: if (testsets == null || testsets.length == 0)
261: throw new XMLSerializeException(
262: "Al least one element testset is required under element testbag.");
263: if (timeout != null && timeout.intValue() < 0)
264: throw new XMLSerializeException(
265: "Value of testbag's attribute timeout can't be negative.");
266: if (prio != null && prio.intValue() < 0)
267: throw new XMLSerializeException(
268: "Value of testbag's attribute prio must be positive.");
269: for (int i = 0; i < testsets.length; i++) {
270: testsets[i].setParent(this );
271: testsets[i].validate();
272: testsets[i].filterPatternsets(passed_patternset);
273: }
274: if (testProperties != null) {
275: for (int i = 0; i < testProperties.length; i++) {
276: testProperties[i].validate();
277: }
278: }
279: // validate testsetup
280: if (testSetup != null) {
281: testSetup.validate();
282: }
283: // deprecate resultprocessor
284: /*
285: if (getResultsprocessorName() != null) {
286: System.out.println("!!! Attribute resultprocessor is no longer in use. Please don't use it.");
287: }
288: */
289: }
290:
291: // public inner classes
292:
293: public static class TestProperty implements XMLSerializable {
294: private String name;
295: private String value;
296:
297: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
298: Testbag.TestProperty.class);
299: static {
300: try {
301: // register this class
302: classMappingRegistry.registerSimpleField("name",
303: ClassMappingRegistry.ATTRIBUTE, "name");
304: classMappingRegistry.registerSimpleField("value",
305: ClassMappingRegistry.ATTRIBUTE, "value");
306: } catch (MappingException me) {
307: me.printStackTrace();
308: classMappingRegistry = null;
309: }
310: }
311:
312: public ClassMappingRegistry registerXMLMapping() {
313: return classMappingRegistry;
314: }
315:
316: public String getName() {
317: return name;
318: }
319:
320: public String getValue() {
321: return value;
322: }
323:
324: protected void validate() throws XMLSerializeException {
325: if (name == null)
326: throw new XMLSerializeException(
327: "Attribute name is required for element testproperty");
328: if (name == null)
329: throw new XMLSerializeException(
330: "Attribute value is required for element testproperty");
331: }
332: }
333:
334: public static class Testset implements XMLSerializable {
335: private String dir;
336: private Patternset patternsets[];
337: private Patternset filtered_patternsets[];
338: private Testbag parent;
339:
340: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
341: Testbag.Testset.class);
342: static {
343: try {
344: // register this class
345: classMappingRegistry.registerSimpleField("dir",
346: ClassMappingRegistry.ATTRIBUTE, "dir");
347: classMappingRegistry.registerContainerField(
348: "patternsets", "patternset",
349: ClassMappingRegistry.DIRECT);
350: } catch (MappingException me) {
351: me.printStackTrace();
352: classMappingRegistry = null;
353: }
354: }
355:
356: public ClassMappingRegistry registerXMLMapping() {
357: return classMappingRegistry;
358: }
359:
360: public String getDir() {
361: return dir;
362: }
363:
364: protected void setDir(String dir) {
365: this .dir = dir;
366: }
367:
368: protected void setParent(Testbag p) {
369: parent = p;
370: }
371:
372: public Testbag getParent() {
373: return parent;
374: }
375:
376: public Patternset[] getAllPatternset() {
377: return patternsets;
378: }
379:
380: public Patternset[] getPatternset() {
381: return filtered_patternsets;
382: }
383:
384: public void setPatternsets(Patternset[] patternsets) {
385: this .patternsets = patternsets;
386: }
387:
388: public String[][] getIncludes() {
389: Vector vector = new Vector();
390: if (patternsets != null) {
391: for (int j = 0; j < patternsets.length; j++) {
392: InExclude includes[] = patternsets[j].getIncludes();
393: if (includes != null) {
394: for (int k = 0; k < includes.length; k++) {
395: vector.add(new String[] {
396: includes[k].getName(),
397: includes[k].getExpectedFail() });
398: }
399: }
400: }
401: }
402: /*
403: System.out.println("include Parent"+getParent());
404: System.out.println("include grand Parent"+getParent().getParent());
405: System.out.println("include grand Parent additional includes"+getParent().getParent().getAdditionalIncludes());
406: */
407: if (getParent().getParent().getAdditionalIncludes() != null)
408: for (int i = 0; i < getParent().getParent()
409: .getAdditionalIncludes().length; i++)
410: vector
411: .add(new String[] {
412: getParent().getParent()
413: .getAdditionalIncludes()[i],
414: null });
415: return (String[][]) vector.toArray(new String[0][0]);
416: }
417:
418: public String[][] getExcludes() {
419: Vector vector = new Vector();
420: if (patternsets != null) {
421: for (int j = 0; j < patternsets.length; j++) {
422: InExclude excludes[] = patternsets[j].getExcludes();
423: if (excludes != null) {
424: for (int k = 0; k < excludes.length; k++) {
425: vector.add(new String[] {
426: excludes[k].getName(),
427: excludes[k].getExpectedFail() });
428: }
429: }
430: }
431: }
432: if (getParent().getParent().getAdditionalExcludes() != null)
433: for (int i = 0; i < getParent().getParent()
434: .getAdditionalExcludes().length; i++)
435: vector
436: .add(new String[] {
437: getParent().getParent()
438: .getAdditionalExcludes()[i],
439: null });
440: return (String[][]) vector.toArray(new String[0][0]);
441: }
442:
443: protected void validate() throws XMLSerializeException {
444: if (dir == null)
445: throw new XMLSerializeException(
446: "Attribute dir is required for element testset");
447: if (patternsets != null)
448: for (int i = 0; i < patternsets.length; i++)
449: patternsets[i].validate();
450: }
451:
452: protected void filterPatternsets(HashSet passed_patternset) {
453: if (patternsets != null) {
454: if (passed_patternset == null
455: || passed_patternset.isEmpty()) {
456: filtered_patternsets = patternsets;
457: } else {
458: Vector patterns = new Vector();
459: for (int i = 0; i < patternsets.length; i++) {
460: boolean include = false;
461: if (patternsets[i].getPatternattribs() != null) {
462: StringTokenizer tokenizer = new StringTokenizer(
463: patternsets[i].getPatternattribs(),
464: ",");
465: while (tokenizer.hasMoreTokens()) {
466: String attr = tokenizer.nextToken();
467: if (passed_patternset.contains(attr))
468: include = true;
469: if (passed_patternset.contains("!"
470: + attr)) {
471: include = false;
472: break;
473: }
474: }
475: if (include)
476: patterns.add(patternsets[i]);
477: }
478: }
479: filtered_patternsets = (Patternset[]) patterns
480: .toArray(new Patternset[0]);
481: }
482: }
483: }
484:
485: }
486:
487: public static class Patternset implements XMLSerializable {
488: private InExclude includes[];
489: private InExclude excludes[];
490: private String patternattribs;
491:
492: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
493: Testbag.Patternset.class);
494: static {
495: try {
496: // register this class
497: classMappingRegistry.registerSimpleField(
498: "patternattribs",
499: ClassMappingRegistry.ATTRIBUTE, "attribs");
500: classMappingRegistry.registerContainerField("includes",
501: "include", ClassMappingRegistry.DIRECT);
502: classMappingRegistry.registerContainerField("excludes",
503: "exclude", ClassMappingRegistry.DIRECT);
504: } catch (MappingException me) {
505: me.printStackTrace();
506: classMappingRegistry = null;
507: }
508: }
509:
510: public ClassMappingRegistry registerXMLMapping() {
511: return classMappingRegistry;
512: }
513:
514: public InExclude[] getIncludes() {
515: return includes;
516: }
517:
518: public void setIncludes(InExclude[] includes) {
519: this .includes = includes;
520: }
521:
522: public InExclude[] getExcludes() {
523: return excludes;
524: }
525:
526: public void setExcludes(InExclude[] excludes) {
527: this .excludes = excludes;
528: }
529:
530: public String getPatternattribs() {
531: return patternattribs;
532: }
533:
534: protected void validate() throws XMLSerializeException {
535: if (includes != null)
536: for (int i = 0; i < includes.length; i++)
537: includes[i].validate();
538: if (excludes != null)
539: for (int i = 0; i < excludes.length; i++)
540: excludes[i].validate();
541: }
542: }
543:
544: public static class InExclude implements XMLSerializable {
545: private String name;
546: private String expectedFail;
547:
548: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
549: Testbag.InExclude.class);
550: static {
551: try {
552: // register this class
553: classMappingRegistry.registerSimpleField(
554: "expectedFail", ClassMappingRegistry.ATTRIBUTE,
555: "expectedFail");
556: classMappingRegistry.registerSimpleField("name",
557: ClassMappingRegistry.ATTRIBUTE, "name");
558: } catch (MappingException me) {
559: me.printStackTrace();
560: classMappingRegistry = null;
561: }
562: }
563:
564: public ClassMappingRegistry registerXMLMapping() {
565: return classMappingRegistry;
566: }
567:
568: public String getName() {
569: return name;
570: }
571:
572: public String getExpectedFail() {
573: return expectedFail;
574: }
575:
576: public void setName(String name) {
577: this .name = name;
578: }
579:
580: protected void validate() throws XMLSerializeException {
581: if (name == null)
582: throw new XMLSerializeException(
583: "Attribute name is required for element include/exclude.");
584: }
585:
586: }
587:
588: // TestBag setup inner class
589: public static class TestSetup implements XMLSerializable {
590:
591: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
592: Testbag.TestSetup.class);
593: static {
594: try {
595: // register this class
596: classMappingRegistry.registerSimpleField("dir",
597: ClassMappingRegistry.ATTRIBUTE, "dir");
598: classMappingRegistry.registerSimpleField(
599: "setupDescriptor",
600: ClassMappingRegistry.ELEMENT, "setup");
601: classMappingRegistry.registerSimpleField(
602: "teardownDescriptor",
603: ClassMappingRegistry.ELEMENT, "teardown");
604: } catch (MappingException me) {
605: me.printStackTrace();
606: classMappingRegistry = null;
607: }
608: }
609:
610: public ClassMappingRegistry registerXMLMapping() {
611: return classMappingRegistry;
612: }
613:
614: private String dir;
615: private SetupDescriptor setupDescriptor;
616: private SetupDescriptor teardownDescriptor;
617:
618: protected void validate() throws XMLSerializeException {
619: if ((dir == null)) {
620: throw new XMLSerializeException(
621: "When using testsetup, dir attribute have to be defined.");
622: }
623: if ((setupDescriptor == null)
624: && (teardownDescriptor == null)) {
625: throw new XMLSerializeException(
626: "Testsetup have to contain either setup or teardown elements.");
627: }
628: if (setupDescriptor != null) {
629: setupDescriptor.validate();
630: }
631: if (teardownDescriptor != null) {
632: teardownDescriptor.validate();
633: }
634: }
635: }
636:
637: public static class SetupDescriptor implements XMLSerializable {
638:
639: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
640: Testbag.SetupDescriptor.class);
641:
642: static {
643: try {
644: classMappingRegistry.registerSimpleField("className",
645: ClassMappingRegistry.ATTRIBUTE, "class");
646: classMappingRegistry.registerSimpleField("methodName",
647: ClassMappingRegistry.ATTRIBUTE, "method");
648: } catch (MappingException me) {
649: me.printStackTrace();
650: classMappingRegistry = null;
651: }
652: }
653:
654: public ClassMappingRegistry registerXMLMapping() {
655: return classMappingRegistry;
656: }
657:
658: private String className;
659: private String methodName;
660:
661: protected void validate() throws XMLSerializeException {
662: if ((className == null) || (methodName == null))
663: throw new XMLSerializeException(
664: "class and method attributes are required for setup/teardown.");
665: }
666: }
667: }
|