File: RunTests.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (221 lines) | stat: -rw-r--r-- 8,606 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Copyright (c) 2004 Thomas Zander <zander@kde.org>

This file is part of Mauve.

Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Mauve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */

package gnu.anttask;

import gnu.testlet.*;
import java.io.*;
import java.util.*;

import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;

/**
 * Ant task to run Mauve test-suite.
 * <p>Before asking questions; always refer to the <a href="http://jakarta.apache.org/ant/manual/index.html">ant manual</a> first.</p>
    <p>Test classes are passed as child fileset tags, typically you simply pass the build
        dir for the project</p>
    <h3>Parameters</h3>
    <table border="1" cellpadding="2" cellspacing="0">
      <tr>
        <td valign="top"><b>Attribute</b></td>
        <td valign="top"><b>Description</b></td>
        <td align="center" valign="top"><b>Required</b></td>
      </tr>
      <tr>
        <td valign="top">debug</td>
        <td valign="top">Prints additional debug info for the tests</td>
        <td align="center" valign="top">No</td>
      </tr>
      <tr>
        <td valign="top">verbose</td>
        <td valign="top">Lets the test suite be more verbose; also showing passed-tests for example</td>
        <td align="center" valign="top">No</td>
      </tr>
      <tr>
        <td valign="top">haltonfailure</td>
        <td valign="top">make the testrun halt as soon as a failure is failed (see also failonerror)</td>
        <td align="center" valign="top">No</td>
      </tr>
      <tr>
        <td valign="top">srcdir</td>
        <td valign="top">provide the sources directory allowing the parsing of a "Tags:" comment in the header of the file.  If the testJDK or testJDBC are also supplied then tests that have tags specifying that their API comatibility are incompatible with the testJDK/testJDBC version will be skipped.</td>
        <td align="center" valign="top">No</td>
      </tr>
      <tr>
        <td valign="top">testJDK</td>
        <td valign="top">The version of the JDK you are running/simulating. Only tests that are compatible with this version will be run.</td>
        <td align="center" valign="top">No</td>
      </tr>
      <tr>
        <td valign="top">testJDBC</td>
        <td valign="top">The version of the JDBC API you are running/simulating. Only tests that are compatible with this version will be run.</td>
        <td align="center" valign="top">No</td>
      </tr>
      <tr>
        <td valign="top">failonerror</td>
        <td valign="top">If a test fails, should that be flagged as an error? Setting this and haltonfailure to true will stop the run as soon as a failure is found.</td>
        <td align="center" valign="top">No</td>
      </tr>
    </table>
 */
public class RunTests extends MatchingTask {
    private boolean verbose=false, debug=false, haltOnFailure=false, failOnError=true;
    private File sourceDir=null;
    private Vector filesets=new Vector();
    private double javaVersion=0d, JDBCVersion=0d;

    public void execute() throws BuildException {
        if(sourceDir == null)
            System.err.println("Warning; without 'srcdir' element no Tag checking will be done");
        MyTestHarness harness = new MyTestHarness (verbose, debug, haltOnFailure);

        Iterator iter = filesets.iterator();
        while(iter.hasNext()) {
            FileSet fs = (FileSet) iter.next();
            try {
                DirectoryScanner ds = fs.getDirectoryScanner(getProject());
                Iterator classNames = Arrays.asList(ds.getIncludedFiles()).iterator();
                while(classNames.hasNext()) {
                    String filename = (String) classNames.next();
                    if(! filename.endsWith(".class"))
                        continue; // only class files
                    if(filename.lastIndexOf("$") > filename.lastIndexOf(File.separator))
                        continue; // no inner classeS
                    filename=filename.substring(0, filename.length()-6);
                    filename=filename.replace(File.separatorChar, '.');
                    if(shouldRunTest(filename)) {
                        if(!verbose)
                            System.out.println("Run: "+ filename);
                        harness.runTest(filename);
                    }
                }
            } catch (BuildException be) {
                // directory doesn't exist or is not readable
                if (failOnError)
                    throw be;
                else
                    log(be.getMessage(),Project.MSG_WARN);
            }
        }
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public void setHaltOnFailure(boolean on) {
        haltOnFailure = on;
    }

    public void setFailOnError(boolean on) {
        failOnError = on;
    }

    public void addFileset(FileSet set) {
        filesets.add(set);
    }

    public void setSrcdir(File sourceDir) {
        this.sourceDir = sourceDir;
    }

    public void setTestJDK(String jdk) {
        jdk=jdk.toLowerCase().trim();
        if(jdk.startsWith("jdk")) {
            try {
                javaVersion = Double.parseDouble(jdk.substring(3));
                return;
            } catch(NumberFormatException e) {
            }
        }
        System.err.println("Failed to parse the testJDK argument; format is: 'JDK1.4'");
    }

    public void setTestJDBC(String jdbc) {
        jdbc=jdbc.toLowerCase().trim();
        if(jdbc.startsWith("jdbc")) {
            try {
                javaVersion = Double.parseDouble(jdbc.substring(4));
                return;
            } catch(NumberFormatException e) {
            }
        }
        System.err.println("Failed to parse the testJDBC argument; format is: 'JDBC2.0'");
    }

    private boolean shouldRunTest(String filename) {
        if(sourceDir == null)
            return true;
        File sourceFile = new File(sourceDir, filename.replace('.', File.separatorChar)+".java");
        if(! sourceFile.exists())
            return false;
        try {
            Reader reader = new FileReader(sourceFile);
            StringBuffer buf = new StringBuffer();
            int maxLines=30;
            while(maxLines > 0) {
                int character = reader.read();
                if(character == -1)
                    break;
                if(character == '\n') {
                    int index = buf.indexOf("Tags:") + 5; // 5 == length of string
                    if(index > 5 && buf.length() > index) {
                        String tags = buf.substring(index).trim().toLowerCase();
                        if("not-a-test".equals(tags))
                            return false;
                        try {
                            return new Tags(tags).isValid(javaVersion, JDBCVersion);
                        } catch(NumberFormatException e) {
                            System.err.println("Unreadable tags in class: "+ filename);
                            return false;
                        }
                    }
                    buf = new StringBuffer();
                    maxLines--;
                }
                else
                    buf.append((char) character);
            }
        } catch(IOException e) { }
        return false;
    }


    private static class MyTestHarness extends SimpleTestHarness {
        private boolean haltOnFailure;
        // extend it b/cause someone thought it should not have public constructors.
        public MyTestHarness(boolean verbose, boolean debug, boolean haltOnFailure) {
            super(verbose, debug, false, false, null);
            this.haltOnFailure = haltOnFailure;
        }

        protected void runTest (String name) throws BuildException {
            super.runtest(name);
            if(haltOnFailure && getFailures() > 0)
                throw new BuildException("Failures");
        }
    }
}