aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
blob: d54b8f28eae59eec162d05bd4a31f414f31cc9e7 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.apache.tools.ant.taskdefs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.input.DefaultInputHandler;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.TeeOutputStream;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;

import static org.apache.tools.ant.AntAssert.assertContains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * stress out java task
 * */
public class JavaTest {

    @Rule
    public BuildFileRule buildRule = new BuildFileRule();

    private static final int TIME_TO_WAIT = 1;
    // wait 1 second extra to allow for java to start ...
    // this time was OK on a Win NT machine and on nagoya
    private static final int SECURITY_MARGIN = 2000;

    /** Utilities used for file operations */
    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();

    private boolean runFatalTests=false;


    /**
     * configure the project.
     * if the property junit.run.fatal.tests is set we run
     * the fatal tests
     */
    @Before
    public void setUp() {
        buildRule.configureProject("src/etc/testcases/taskdefs/java.xml");
        buildRule.executeTarget("setUp");

        //final String propname="tests-classpath.value";
        //String testClasspath=System.getProperty(propname);
        //System.out.println("Test cp="+testClasspath);
        String runFatal=System.getProperty("junit.run.fatal.tests");
        if(runFatal!=null)
            runFatalTests=true;
    }

    @Test
    public void testNoJarNoClassname(){
        try {
            buildRule.executeTarget("testNoJarNoClassname");
            fail("Build exception should have been thrown - parameter validation");
        } catch (BuildException ex) {
            assertContains("Classname must not be null.", ex.getMessage());
        }
    }

    @Test
    public void testJarNoFork() {
        try {
            buildRule.executeTarget("testJarNoFork");
            fail("Build exception should have been thrown - parameter validation");
        } catch (BuildException ex) {
            assertContains("Cannot execute a jar in non-forked mode. Please set fork='true'. ", ex.getMessage());
        }
    }

    @Test
    public void testJarAndClassName() {
        try {
            buildRule.executeTarget("testJarAndClassName");
            fail("Build exception should have been thrown - both classname and JAR are not allowed");
        } catch (BuildException ex) {
            assertEquals("Cannot use 'jar' and 'classname' attributes in same command", ex.getMessage());
        }
    }

    @Test
    public void testClassnameAndJar() {
        try {
            buildRule.executeTarget("testClassnameAndJar");
            fail("Build exception should have been thrown - both classname and JAR are not allowed");
        } catch (BuildException ex) {
            assertEquals("Cannot use 'jar' and 'classname' attributes in same command.", ex.getMessage());
        }
    }

    @Test
    public void testRun() {
        buildRule.executeTarget("testRun");
    }



    /** this test fails but we ignore the return value;
     *  we verify that failure only matters when failonerror is set
     */
    @Test
    public void testRunFail() {
        Assume.assumeTrue("Fatal tests have not been set to run", runFatalTests);
        buildRule.executeTarget("testRunFail");
    }

    @Test
    public void testRunFailFoe() {
        Assume.assumeTrue("Fatal tests have not been set to run", runFatalTests);
        try {
            buildRule.executeTarget("testRunFailFoe");
            fail("Build exception should have been thrown - " + "java failures being propagated");
        } catch (BuildException ex) {
            assertContains("Java returned:", ex.getMessage());
        }
    }

    @Test
    public void testRunFailFoeFork() {
        try {
            buildRule.executeTarget("testRunFailFoeFork");
            fail("Build exception should have been thrown - " + "java failures being propagated");
        } catch (BuildException ex) {
            assertContains("Java returned:", ex.getMessage());
        }
    }

    @Test
    public void testExcepting() {
        buildRule.executeTarget("testExcepting");
        assertContains("Exception raised inside called program", buildRule.getLog());
    }

    @Test
    public void testExceptingFork() {
        buildRule.executeTarget("testExceptingFork");
        assertContains("Java Result:", buildRule.getLog());
    }

    @Test
    public void testExceptingFoe() {
        try {
            buildRule.executeTarget("testExceptingFoe");
            fail("Build exception should have been thrown - " + "passes exception through");
        } catch (BuildException ex) {
            assertContains("Exception raised inside called program", ex.getMessage());
        }
    }

    @Test
    public void testExceptingFoeFork() {
        try {
            buildRule.executeTarget("testExceptingFoeFork");
            fail("Build exception should have been thrown - " + "exceptions turned into error codes");
        } catch (BuildException ex) {
            assertContains("Java returned:", ex.getMessage());
        }
    }

    @Test
    public void testResultPropertyZero() {
        buildRule.executeTarget("testResultPropertyZero");
        assertEquals("0", buildRule.getProject().getProperty("exitcode"));
    }

    @Test
    public void testResultPropertyNonZero() {
        buildRule.executeTarget("testResultPropertyNonZero");
        assertEquals("2", buildRule.getProject().getProperty("exitcode"));
    }

    @Test
    public void testResultPropertyZeroNoFork() {
        buildRule.executeTarget("testResultPropertyZeroNoFork");
        assertEquals("0", buildRule.getProject().getProperty("exitcode"));
    }

    @Test
    public void testResultPropertyNonZeroNoFork() {
        buildRule.executeTarget("testResultPropertyNonZeroNoFork");
         assertEquals("-1", buildRule.getProject().getProperty("exitcode"));
     }

    @Test
    public void testRunFailWithFailOnError() {
        try {
            buildRule.executeTarget("testRunFailWithFailOnError");
            fail("Build exception should have been thrown - " + "non zero return code");
        } catch (BuildException ex) {
            assertContains("Java returned:", ex.getMessage());
        }
    }

    @Test
    public void testRunSuccessWithFailOnError() {
        buildRule.executeTarget("testRunSuccessWithFailOnError");
    }

    @Test
    public void testSpawn() throws InterruptedException {
        File logFile = FILE_UTILS.createTempFile("spawn", "log",
                new File(buildRule.getProject().getProperty("output")), false, false);
        // this is guaranteed by FileUtils#createTempFile
        assertTrue("log file not existing", !logFile.exists());
        buildRule.getProject().setProperty("logFile", logFile.getAbsolutePath());
        buildRule.getProject().setProperty("timeToWait", Long.toString(TIME_TO_WAIT));
        buildRule.getProject().executeTarget("testSpawn");

        Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN);


        // let's be nice with the next generation of developers
        if (!logFile.exists()) {
            System.out.println("suggestion: increase the constant"
            + " SECURITY_MARGIN to give more time for java to start.");
        }
        assertTrue("log file exists", logFile.exists());
    }

    @Test
    public void testRedirect1() {
        buildRule.executeTarget("redirect1");
    }

    @Test
    public void testRedirect2() {
        buildRule.executeTarget("redirect2");
    }

    @Test
    public void testRedirect3() {
        buildRule.executeTarget("redirect3");
    }

    @Test
    public void testRedirector1() {
        buildRule.executeTarget("redirector1");
    }

    @Test
    public void testRedirector2() {
        buildRule.executeTarget("redirector2");
    }

    @Test
    public void testReleasedInput() throws Exception {
        PipedOutputStream out = new PipedOutputStream();
        final PipedInputStream in = new PipedInputStream(out);
        buildRule.getProject().setInputHandler(new DefaultInputHandler() {
            protected InputStream getInputStream() {
                return in;
            }
        });
        buildRule.getProject().setDefaultInputStream(in);

        Java java = new Java();
        java.setProject(buildRule.getProject());
        java.setClassname("org.apache.tools.ant.Main");
        java.setArgs("-version");
        java.setFork(true);
        // note: due to the missing classpath it will fail, but the input stream
        // reader will be read
        java.execute();

        Thread inputThread = new Thread(new Runnable() {
            public void run() {
                Input input = new Input();
                input.setProject(buildRule.getProject());
                input.setAddproperty("input.value");
                input.execute();
            }
        });
        inputThread.start();

        // wait a little bit for the task to wait for input
        Thread.sleep(100);

        // write some stuff in the input stream to be catched by the input task
        out.write("foo\n".getBytes());
        out.flush();
        try {
            out.write("bar\n".getBytes());
            out.flush();
        } catch (IOException x) {
            // "Pipe closed" on XP; ignore?
        }

        inputThread.join(2000);

        assertEquals("foo", buildRule.getProject().getProperty("input.value"));
    }

    @Test
    public void testFlushedInput() throws Exception {
        final PipedOutputStream out = new PipedOutputStream();
        final PipedInputStream in = new PipedInputStream(out);
        buildRule.getProject().setInputHandler(new DefaultInputHandler() {
            protected InputStream getInputStream() {
                return in;
            }
        });
        buildRule.getProject().setDefaultInputStream(in);

        final boolean[] timeout = new boolean[1];
        timeout[0] = false;

        Thread writingThread = new Thread(new Runnable() {
            public void run() {
                try {
                    // wait a little bit to have the target executed
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new AssumptionViolatedException("Thread interrupted", e);
                }
                try {
                    out.write("foo-FlushedInput\n".getBytes());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        writingThread.setDaemon(true);

        writingThread.start();
        buildRule.executeTarget("flushedInput");
    }

    /**
     * entry point class with no dependencies other
     * than normal JRE runtime
     */
    public static class EntryPoint {

    /**
     * this entry point is used by the java.xml tests to
     * generate failure strings to handle
     * argv[0] = exit code (optional)
     * argv[1] = string to print to System.out (optional)
     * argv[1] = string to print to System.err (optional)
     */
        public static void main(String[] argv) {
            int exitCode=0;
            if(argv.length>0) {
                try {
                    exitCode=Integer.parseInt(argv[0]);
                } catch(NumberFormatException nfe) {
                    exitCode=-1;
                }
            }
            if(argv.length>1) {
                System.out.println(argv[1]);
            }
            if(argv.length>2) {
                System.err.println(argv[2]);
            }
            if(exitCode!=0) {
                System.exit(exitCode);
            }
        }
    }

    /**
     * entry point class with no dependencies other
     * than normal JRE runtime
     */
    public static class ExceptingEntryPoint {

        /**
         * throw a run time exception which does not need
         * to be in the signature of the entry point
         */
        public static void main(String[] argv) {
            throw new NullPointerException("Exception raised inside called program");
        }
    }
    /**
     * test class for spawn
     */
    public static class SpawnEntryPoint {
        public static void main(String [] argv) throws InterruptedException {
            int sleepTime = 10;
            String logFile = "spawn.log";
            if (argv.length >= 1) {
                sleepTime = Integer.parseInt(argv[0]);
            }
            if (argv.length >= 2)
            {
                logFile = argv[1];
            }
            OutputStreamWriter out = null;
            Thread.sleep(sleepTime * 1000);

            try {
                File dest = new File(logFile);
                FileOutputStream fos = new FileOutputStream(dest);
                out = new OutputStreamWriter(fos);
                out.write("bye bye\n");
            } catch (Exception ex) {}
            finally {
                try {out.close();} catch (IOException ioe) {}}

        }
    }

    /**
     * entry point class to pipe System.in to the specified stream:
     * "out", "err", or "both".  If none specified, swallow the input.
     */
    public static class PipeEntryPoint {

        /**
         * pipe input to specified output
         */
        public static void main(String[] args) throws InterruptedException {
            OutputStream os = null;
            if (args.length > 0) {
                if ("out".equalsIgnoreCase(args[0])) {
                    os = System.out;
                } else if ("err".equalsIgnoreCase(args[0])) {
                    os = System.err;
                } else if ("both".equalsIgnoreCase(args[0])) {
                    os = new TeeOutputStream(System.out, System.err);
                }
            }
            if (os != null) {
                Thread t = new Thread(new StreamPumper(System.in, os, true));
                t.setName("PipeEntryPoint " + args[0]);
                t.start();
                t.join();
            }
        }
    }

    public static class ReadPoint {
        public static void main(String[] args) throws IOException {
            String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
            System.out.println(line);
        }
    }

}