~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/optional/sitraka/Coverage.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 *
 
14
 * 2. Redistributions in binary form must reproduce the above copyright
 
15
 *    notice, this list of conditions and the following disclaimer in
 
16
 *    the documentation and/or other materials provided with the
 
17
 *    distribution.
 
18
 *
 
19
 * 3. The end-user documentation included with the redistribution, if
 
20
 *    any, must include the following acknowlegement:
 
21
 *       "This product includes software developed by the
 
22
 *        Apache Software Foundation (http://www.apache.org/)."
 
23
 *    Alternately, this acknowlegement may appear in the software itself,
 
24
 *    if and wherever such third-party acknowlegements normally appear.
 
25
 *
 
26
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 
27
 *    Foundation" must not be used to endorse or promote products derived
 
28
 *    from this software without prior written permission. For written
 
29
 *    permission, please contact apache@apache.org.
 
30
 *
 
31
 * 5. Products derived from this software may not be called "Apache"
 
32
 *    nor may "Apache" appear in their names without prior written
 
33
 *    permission of the Apache Group.
 
34
 *
 
35
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
36
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
37
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
38
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 
39
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
40
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
41
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 
42
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
43
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
44
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
45
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
49
 * This software consists of voluntary contributions made by many
 
50
 * individuals on behalf of the Apache Software Foundation.  For more
 
51
 * information on the Apache Software Foundation, please see
 
52
 * <http://www.apache.org/>.
 
53
 */
 
54
 
 
55
package org.apache.tools.ant.taskdefs.optional.sitraka;
 
56
 
 
57
import java.io.PrintWriter;
 
58
import java.io.StringWriter;
 
59
import java.io.FileWriter;
 
60
import java.io.File;
 
61
import java.io.OutputStream;
 
62
import java.io.IOException;
 
63
import java.util.Vector;
 
64
import java.util.Random;
 
65
import java.util.Hashtable;
 
66
import org.apache.tools.ant.Project;
 
67
import org.apache.tools.ant.Task;
 
68
import org.apache.tools.ant.BuildException;
 
69
import org.apache.tools.ant.types.CommandlineJava;
 
70
import org.apache.tools.ant.types.Commandline;
 
71
import org.apache.tools.ant.types.EnumeratedAttribute;
 
72
import org.apache.tools.ant.types.Path;
 
73
import org.apache.tools.ant.types.FileSet;
 
74
import org.apache.tools.ant.taskdefs.Execute;
 
75
import org.apache.tools.ant.taskdefs.LogStreamHandler;
 
76
 
 
77
/**
 
78
 * Convenient task to run Sitraka JProbe Coverage from Ant.
 
79
 * Options are pretty numerous, you'd better check the manual for a full
 
80
 * descriptions of options. (not that simple since they differ from the online
 
81
 * help, from the usage command line and from the examples...)
 
82
 * <p>
 
83
 * For additional information, visit <a href="http://www.sitraka.com">www.sitraka.com</a>
 
84
 *
 
85
 * @author <a href="sbailliez@imediation.com">Stephane Bailliez</a>
 
86
 */
 
87
public class Coverage extends Task {
 
88
 
 
89
        protected File home;
 
90
 
 
91
        protected Commandline cmdl = new Commandline();
 
92
 
 
93
        protected CommandlineJava cmdlJava = new CommandlineJava();
 
94
 
 
95
        protected String function = "coverage";
 
96
 
 
97
        protected String seedName;
 
98
 
 
99
        protected File inputFile;
 
100
 
 
101
        protected File javaExe;
 
102
 
 
103
        protected String vm;
 
104
 
 
105
        protected boolean applet = false;
 
106
 
 
107
        /** this is a somewhat annoying thing, set it to never */
 
108
        protected String exitPrompt = "never";
 
109
 
 
110
        protected Filters filters = new Filters();
 
111
 
 
112
        protected Triggers triggers;
 
113
 
 
114
        protected String finalSnapshot = "coverage";
 
115
 
 
116
        protected String recordFromStart = "coverage";
 
117
 
 
118
        protected File snapshotDir;
 
119
 
 
120
        protected File workingDir;
 
121
 
 
122
        protected boolean trackNatives = false;
 
123
 
 
124
        protected Socket socket;
 
125
 
 
126
        protected int warnLevel = 0;
 
127
 
 
128
        protected Vector filesets = new Vector();
 
129
 
 
130
        //--------- setters used via reflection --
 
131
 
 
132
        /** set the coverage home directory where are libraries, jars and jplauncher */
 
133
        public void setHome(File value){
 
134
                home = value;
 
135
        }
 
136
 
 
137
        /** seed name for snapshot file. can be null, default to snap */
 
138
        public void setSeedname(String value){
 
139
                seedName = value;
 
140
        }
 
141
 
 
142
        public void setInputfile(File value){
 
143
                inputFile = value;
 
144
        }
 
145
 
 
146
        public void setJavaexe(File value){
 
147
                javaExe = value;
 
148
        }
 
149
 
 
150
        public static class Javavm extends EnumeratedAttribute {
 
151
                public String[] getValues(){
 
152
                        return new String[]{"java2", "jdk118", "jdk117"};
 
153
                }
 
154
        }
 
155
        /** jdk117, jdk118 or java2, can be null, default to java2 */
 
156
        public void setVm(Javavm value) {
 
157
                vm = value.getValue();
 
158
        }
 
159
 
 
160
        /** default to false unless file is htm or html */
 
161
        public void setApplet(boolean value){
 
162
                applet = value;
 
163
        }
 
164
 
 
165
        /** always, error, never */
 
166
        public void setExitprompt(String value){
 
167
                exitPrompt = value;
 
168
        }
 
169
 
 
170
        public Filters createFilters(){
 
171
                return filters;
 
172
        }
 
173
 
 
174
        public Triggers createTriggers(){
 
175
                if (triggers == null) {
 
176
                        triggers = new Triggers();
 
177
                }
 
178
                return triggers;
 
179
        }
 
180
 
 
181
        public Socket createSocket(){
 
182
                if (socket == null ) {
 
183
                        socket = new Socket();
 
184
                }
 
185
                return socket;
 
186
        }
 
187
 
 
188
        public static class Finalsnapshot extends EnumeratedAttribute {
 
189
                public String[] getValues(){
 
190
                        return new String[]{"coverage", "none", "all"};
 
191
                }
 
192
        }
 
193
 
 
194
        /** none, coverage, all. can be null, default to none */
 
195
        public void setFinalsnapshot(String value){
 
196
                finalSnapshot = value;
 
197
        }
 
198
 
 
199
        public static class Recordfromstart extends EnumeratedAttribute {
 
200
                public String[] getValues(){
 
201
                        return new String[]{"coverage", "none", "all"};
 
202
                }
 
203
        }
 
204
        /** all, coverage, none */
 
205
        public void setRecordfromstart(Recordfromstart value) {
 
206
                recordFromStart = value.getValue();
 
207
        }
 
208
 
 
209
        public void setWarnlevel(Integer value){
 
210
                warnLevel = value.intValue();
 
211
        }
 
212
 
 
213
        public void setSnapshotdir(File value){
 
214
                snapshotDir = value;
 
215
        }
 
216
 
 
217
        public void setWorkingdir(File value){
 
218
                workingDir = value;
 
219
        }
 
220
 
 
221
        public void setTracknatives(boolean value){
 
222
                trackNatives = value;
 
223
        }
 
224
 
 
225
        //
 
226
 
 
227
        /** the jvm arguments */
 
228
    public Commandline.Argument createJvmarg() {
 
229
        return cmdlJava.createVmArgument();
 
230
    }
 
231
 
 
232
        /** the command arguments */
 
233
    public Commandline.Argument createArg() {
 
234
        return cmdlJava.createArgument();
 
235
    }
 
236
 
 
237
        /** classpath to run the files */
 
238
    public Path createClasspath() {
 
239
        return cmdlJava.createClasspath(project).createPath();
 
240
    }
 
241
 
 
242
        /** classname to run as standalone or runner for filesets */
 
243
        public void setClassname(String value){
 
244
                cmdlJava.setClassname(value);
 
245
        }
 
246
 
 
247
        /** the classnames to execute */
 
248
        public void addFileset(FileSet fs){
 
249
                filesets.addElement(fs);
 
250
        }
 
251
 
 
252
 
 
253
        //---------------- the tedious job begins here
 
254
 
 
255
        public Coverage(){
 
256
        }
 
257
 
 
258
        /** execute the jplauncher by providing a parameter file */
 
259
        public void execute() throws BuildException {
 
260
                File paramfile = null;  
 
261
                // if an input file is used, all other options are ignored...   
 
262
                if (inputFile == null){
 
263
                        checkOptions();
 
264
                        paramfile = createParamFile();
 
265
                } else {
 
266
                        paramfile = inputFile;
 
267
                }
 
268
                try {
 
269
                        // we need to run Coverage from his directory due to dll/jar issues
 
270
                        cmdl.setExecutable( new File(home, "jplauncher").getAbsolutePath() );
 
271
                        cmdl.createArgument().setValue("-jp_input=" + paramfile.getAbsolutePath());
 
272
                        
 
273
                        // use the custom handler for stdin issues
 
274
                        LogStreamHandler handler = new CoverageStreamHandler(this);
 
275
                        Execute exec = new Execute( handler );
 
276
                        log(cmdl.toString(), Project.MSG_VERBOSE);
 
277
                        exec.setCommandline(cmdl.getCommandline());
 
278
                        int exitValue = exec.execute();
 
279
                        if (exitValue != 0) {
 
280
                                throw new BuildException("JProbe Coverage failed (" + exitValue + ")");
 
281
                        }
 
282
                } catch (IOException e){
 
283
                        throw new BuildException("Failed to execute JProbe Coverage.", e);
 
284
                } finally {
 
285
                        //@todo should be removed once switched to JDK1.2
 
286
                        if (inputFile == null && paramfile != null){
 
287
                                paramfile.delete();
 
288
                        }
 
289
                }
 
290
        }
 
291
 
 
292
        /** wheck what is necessary to check, Coverage will do the job for us */
 
293
        protected void checkOptions() throws BuildException {
 
294
                // check coverage home
 
295
                if (home == null || !home.isDirectory() ) {
 
296
                        throw new BuildException("Invalid home directory. Must point to JProbe home directory");
 
297
                }
 
298
                home = new File(home,"Coverage");
 
299
                File jar = new File(home, "coverage.jar");
 
300
                if (!jar.exists()) {
 
301
                        throw new BuildException("Cannot find Coverage directory: " + home);
 
302
                }
 
303
 
 
304
                // make sure snapshot dir exists and is resolved
 
305
                if (snapshotDir == null) {
 
306
                        snapshotDir = new File(".");
 
307
                }
 
308
                snapshotDir = project.resolveFile(snapshotDir.getPath());
 
309
                if (!snapshotDir.isDirectory() || !snapshotDir.exists()) {
 
310
                        throw new BuildException("Snapshot directory does not exists :" + snapshotDir);
 
311
                }
 
312
                if (workingDir == null) {
 
313
                        workingDir = new File(".");
 
314
                }
 
315
                workingDir = project.resolveFile(workingDir.getPath());
 
316
 
 
317
                // check for info, do your best to select the java executable.
 
318
                // JProbe 3.0 fails if there is no javaexe option. So
 
319
                if (javaExe == null && ( vm == null || "java2".equals(vm) ) ) {
 
320
                        String version = System.getProperty("java.version");
 
321
                        // make we are using 1.2+, if it is, then do your best to
 
322
                        // get a javaexe
 
323
                        if ( !version.startsWith("1.1") ){
 
324
                                if (vm == null){
 
325
                                        vm = "java2";
 
326
                                }
 
327
                                // if we are here obviously it is java2
 
328
                                String home = System.getProperty("java.home");
 
329
                                boolean isUnix = File.separatorChar == '/';
 
330
                                javaExe = isUnix ? new File(home, "bin/java") : new File(home,"/bin/java.exe");
 
331
                        }                       
 
332
                }               
 
333
        }
 
334
 
 
335
        /**
 
336
         * return the command line parameters. Parameters can either be passed
 
337
         * to the command line and stored to a file (then use the -jp_input=<filename>)
 
338
         * if they are too numerous.
 
339
         */
 
340
        protected String[] getParameters(){
 
341
                Vector params = new Vector();
 
342
                params.addElement("-jp_function=" + function);
 
343
                if (vm != null) {
 
344
                        params.addElement("-jp_vm=" + vm);
 
345
                }
 
346
                if (javaExe != null) {
 
347
                        params.addElement("-jp_java_exe=" + project.resolveFile(javaExe.getPath()));
 
348
                }
 
349
                params.addElement("-jp_working_dir=" + workingDir.getPath() );
 
350
                params.addElement("-jp_snapshot_dir=" + snapshotDir.getPath() );
 
351
                params.addElement("-jp_record_from_start=" + recordFromStart);
 
352
                params.addElement("-jp_warn=" + warnLevel);
 
353
                if (seedName != null) {
 
354
                        params.addElement("-jp_output_file=" + seedName);
 
355
                }
 
356
                params.addElement("-jp_filter=" + filters.toString() );
 
357
                if (triggers != null) {
 
358
                        params.addElement("-jp_trigger=" + triggers.toString() );
 
359
                }
 
360
                if (finalSnapshot != null) {
 
361
                        params.addElement("-jp_final_snapshot=" + finalSnapshot);
 
362
                }
 
363
        params.addElement("-jp_exit_prompt=" + exitPrompt);
 
364
                //params.addElement("-jp_append=" + append);
 
365
                params.addElement("-jp_track_natives=" + trackNatives);
 
366
                //.... now the jvm
 
367
                // arguments
 
368
                String[] vmargs = cmdlJava.getVmCommand().getArguments();
 
369
                for (int i = 0; i < vmargs.length; i++) {
 
370
                        params.addElement(vmargs[i]);
 
371
                }
 
372
                // classpath
 
373
                Path classpath = cmdlJava.getClasspath();
 
374
                if (classpath != null && classpath.size() > 0) {
 
375
                        params.addElement("-classpath " + classpath.toString());
 
376
                }
 
377
                // classname (runner or standalone)
 
378
                if (cmdlJava.getClassname() != null) {
 
379
                        params.addElement(cmdlJava.getClassname());
 
380
                }
 
381
                // arguments for classname
 
382
                String[] args = cmdlJava.getJavaCommand().getArguments();
 
383
                for (int i = 0; i < args.length; i++) {
 
384
                        params.addElement(args[i]);
 
385
                }
 
386
                
 
387
                String[] array = new String[params.size()];
 
388
                params.copyInto(array);
 
389
                return array;
 
390
        }
 
391
        
 
392
        
 
393
        /**
 
394
         * create the parameter file from the given options. The file is
 
395
         * created with a random name in the current directory.
 
396
         * @return the file object where are written the configuration to run
 
397
         * JProbe Coverage
 
398
         * @throws BuildException thrown if something bad happens while writing
 
399
         * the arguments to the file.
 
400
         */
 
401
        protected File createParamFile() throws BuildException {
 
402
                //@todo change this when switching to JDK 1.2 and use File.createTmpFile()
 
403
                File file = createTmpFile();
 
404
                log("Creating parameter file: " + file, Project.MSG_VERBOSE);
 
405
                
 
406
                // options need to be one per line in the parameter file
 
407
                // so write them all in a single string
 
408
                StringWriter sw = new StringWriter();
 
409
                PrintWriter pw = new PrintWriter(sw);
 
410
                String[] params = getParameters();
 
411
                for (int i = 0; i < params.length; i++){
 
412
                        pw.println(params[i]);
 
413
                }
 
414
                pw.flush();
 
415
                log("JProbe Coverage parameters:\n" + sw.toString(), Project.MSG_VERBOSE);
 
416
                
 
417
                // now write them to the file
 
418
                FileWriter fw = null;
 
419
                try {
 
420
                        fw = new FileWriter(file);
 
421
                        fw.write(sw.toString());
 
422
                        fw.flush();
 
423
                } catch (IOException e){
 
424
                        throw new BuildException("Could not write parameter file " + file, e);
 
425
                } finally {
 
426
                        if (fw != null) {
 
427
                                try {
 
428
                                        fw.close();
 
429
                                } catch (IOException ignored){}
 
430
                        }
 
431
                }
 
432
                return file;
 
433
        }
 
434
 
 
435
        /** create a temporary file in the current dir (For JDK1.1 support) */
 
436
        protected File createTmpFile(){
 
437
                final long rand = (new Random(System.currentTimeMillis())).nextLong();
 
438
                File file = new File("jpcoverage" + rand + ".tmp");
 
439
                return file;
 
440
        }
 
441
 
 
442
        /** specific pumper to avoid those nasty stdin issues */
 
443
        static class CoverageStreamHandler extends LogStreamHandler {
 
444
                CoverageStreamHandler(Task task){
 
445
                        super(task, Project.MSG_INFO, Project.MSG_WARN);
 
446
                }
 
447
                /**
 
448
                 * there are some issues concerning all JProbe executable
 
449
                 * In our case a 'Press ENTER to close this window..." will
 
450
                 * be displayed in the current window waiting for enter.
 
451
                 * So I'm closing the stream right away to avoid problems.
 
452
                 */
 
453
            public void setProcessInputStream(OutputStream os) {
 
454
                try {
 
455
                        os.close();
 
456
                } catch (IOException ignored){
 
457
                }
 
458
            }
 
459
        }
 
460
 
 
461
}