2
* Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package net.sf.antcontrib.platform;
18
import org.apache.tools.ant.BuildFileTest;
21
* Testcase for <shellscript>
23
* @author Peter Reilly
25
public class ShellScriptTest extends BuildFileTest {
26
public ShellScriptTest(String name) {
31
configureProject("test/resources/platform/shellscript.xml");
35
public void testShHello() {
38
executeTarget("sh.hello");
39
assertTrue(getLog().indexOf("hello world") > -1);
42
public void testBashHello() {
45
executeTarget("bash.hello");
46
assertTrue(getLog().indexOf("hello world") > -1);
49
public void testShInputString() {
52
executeTarget("sh.inputstring");
53
assertTrue(getLog().indexOf("hello world") > -1);
56
public void testShProperty() {
59
executeTarget("sh.property");
60
assertTrue(getLog().indexOf("this is a property") > -1);
64
public void testPythonHello() {
67
executeTarget("python.hello");
68
assertTrue(getLog().indexOf("hello world") > -1);
71
public void testPerlHello() {
74
executeTarget("perl.hello");
75
assertTrue(getLog().indexOf("hello world") > -1);
78
public void testNoShell() {
79
expectBuildExceptionContaining(
80
"noshell", "Execute failed", "a shell that should not exist");
83
public void testSed() {
86
executeTarget("sed.test");
87
assertTrue(getLog().indexOf("BAR bar bar bar BAR bar") > -1);
90
public void testSetProperty() {
93
executeTarget("sh.set.property");
94
assertPropertyEquals("sh.set.property", "hello world");
97
public void testTmpSuffix() {
100
executeTarget("sh.tmp.suffix");
101
assertTrue(getLog().indexOf(".bat") > -1);
104
public void testCmd() {
107
executeTarget("cmd.test");
108
assertTrue(getLog().indexOf("hello world") > -1);
111
public void testDir() {
114
executeTarget("dir.test");
116
getProject().getProperty("dir.test.property")
117
.indexOf("subdir") > -1);
120
public void testCommand() {
121
expectBuildExceptionContaining(
122
"command.test", "Attribute failed",
123
"Attribute command is not supported");
126
private static boolean initialized = false;
127
private static boolean hasSh = false;
128
private static boolean hasBash = false;
129
private static boolean hasPython = false;
130
private static boolean hasPerl = false;
131
private static boolean hasSed = false;
132
private static boolean hasCmd = false;
133
private static Object staticMonitor = new Object();
136
* check if the env contains the shells
137
* sh, bash, python and perl
138
* assume cmd.exe exists for windows
140
private void staticInitialize() {
141
synchronized (staticMonitor) {
145
hasSh = hasShell("hassh");
146
hasBash = hasShell("hasbash");
147
hasPerl = hasShell("hasperl");
148
hasPython = hasShell("haspython");
149
hasSed = hasShell("hassed");
150
hasCmd = hasShell("hascmd");
155
private boolean hasShell(String target) {
157
executeTarget(target);
160
catch (Throwable t) {