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: package org.netbeans.xtest.harness;
042:
043: import org.apache.tools.ant.*;
044: import org.apache.tools.ant.types.*;
045: import org.apache.tools.ant.taskdefs.*;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.*;
050: import org.netbeans.xtest.xmlserializer.XMLSerializeException;
051:
052: /**
053: *
054: * @author lm97939
055: */
056: public class MTestConfigTask extends Task {
057:
058: private String config;
059: private String testtype;
060: private static Vector singletest = new Vector();
061: private HashSet passed_attribset = new HashSet();
062: private HashSet passed_patternattribs = new HashSet();
063: private HashSet passed_executors = new HashSet();
064: private HashSet passed_testbags = new HashSet();
065:
066: private Vector additionalPatterns = new Vector();
067:
068: private static MTestConfig mconfig;
069:
070: /** Creates a new instance of MTestConfigTask */
071: public MTestConfigTask() {
072: }
073:
074: // xml element
075: public void setTesttype(String s) {
076: testtype = s;
077: }
078:
079: // xml element
080: public void setTestattributes(String s) {
081: StringTokenizer stt = new StringTokenizer(s, ",");
082: while (stt.hasMoreTokens()) {
083: passed_attribset.add(stt.nextToken());
084: }
085: }
086:
087: // xml element
088: public void setPatternsetattribs(String s) {
089: StringTokenizer stt = new StringTokenizer(s, ",");
090: while (stt.hasMoreTokens()) {
091: passed_patternattribs.add(stt.nextToken());
092: }
093: }
094:
095: // xml element
096: public void setTestexecutors(String s) {
097: if (s == null || s.equals(""))
098: return;
099: StringTokenizer stt = new StringTokenizer(s, ",");
100: while (stt.hasMoreTokens()) {
101: passed_executors.add(stt.nextToken());
102: }
103: }
104:
105: // xml element
106: public void setTestbags(String s) {
107: if (s == null || s.equals(""))
108: return;
109: StringTokenizer stt = new StringTokenizer(s, ",");
110: while (stt.hasMoreTokens()) {
111: passed_testbags.add(stt.nextToken());
112: }
113: }
114:
115: // xml element
116: public void setTestconfig(String config) {
117: this .config = config;
118: }
119:
120: // xml element
121: public void setSingleTest(String s) {
122: if (s == null || s.equals(""))
123: return;
124: StringTokenizer stt = new StringTokenizer(s, ",");
125: while (stt.hasMoreTokens()) {
126: singletest.add(stt.nextToken());
127: }
128: }
129:
130: public PatternSet createPatternSet() {
131: PatternSet patterns = new PatternSet();
132: additionalPatterns.addElement(patterns);
133: return patterns;
134: }
135:
136: public static MTestConfig getMTestConfig() {
137: return mconfig;
138: }
139:
140: public static Testbag[] getTestbags() throws BuildException {
141: if (!singletest.isEmpty()) {
142: try {
143: return new Testbag[] { mconfig.createSingleTestbag(
144: mconfig.getTesttype() + "/src", singletest) };
145: } catch (XMLSerializeException e) {
146: throw new BuildException(e.getMessage(), e);
147: }
148: } else {
149: return mconfig.getFilteredTestbags();
150: }
151: }
152:
153: public void execute() throws BuildException {
154: File configfile;
155: if (testtype == null)
156: throw new BuildException("Attribute testtype is required.");
157: if (passed_attribset.isEmpty() && passed_testbags.isEmpty())
158: throw new BuildException(
159: "Either attribute testattributes or testbags is required.");
160: if (config != null && !config.equals("")) {
161: configfile = getProject().resolveFile(config);
162: if (!configfile.exists() || !configfile.isFile())
163: throw new BuildException("Config file "
164: + configfile.getAbsolutePath()
165: + " doesn't exist or is not a file.");
166: } else {
167: configfile = new File(getProject().getBaseDir(), "cfg-"
168: + testtype + ".xml");
169: }
170: try {
171: mconfig = MTestConfig.loadConfig(configfile,
172: passed_patternattribs, passed_attribset,
173: passed_executors, passed_testbags);
174: mconfig.setTesttype(testtype);
175: ArrayList includes_list = new ArrayList();
176: ArrayList excludes_list = new ArrayList();
177: Enumeration en = additionalPatterns.elements();
178: while (en.hasMoreElements()) {
179: PatternSet pattern = (PatternSet) en.nextElement();
180: String include[] = pattern
181: .getIncludePatterns(getProject());
182: String exclude[] = pattern
183: .getExcludePatterns(getProject());
184: if (include != null)
185: includes_list.addAll(Arrays.asList(include));
186: if (exclude != null)
187: excludes_list.addAll(Arrays.asList(exclude));
188: }
189: mconfig.setAdditionalIncludes((String[]) includes_list
190: .toArray(new String[0]));
191: mconfig.setAdditionalExcludes((String[]) excludes_list
192: .toArray(new String[0]));
193: } catch (XMLSerializeException e) {
194: throw new BuildException(e.getMessage(), e);
195: }
196: }
197: }
|