Debian Codegraph @

test/src/net/sf/antcontrib/platform/ShellScriptTest.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package net.sf.antcontrib.platform;
17
18 import org.apache.tools.ant.BuildFileTest;
19
20 /**
21 * Testcase for <shellscript>
22 *
23 * @author Peter Reilly
24 */
25 public class ShellScriptTest extends BuildFileTest {
26 public ShellScriptTest(String name) {
27 super(name);
28 }
29
30 public void setUp() {
31 configureProject("test/resources/platform/shellscript.xml");
32 staticInitialize();
33 }
34
35 public void testShHello() {
36 if (! hasSh)
37 return;
38 executeTarget("sh.hello");
39 assertTrue(getLog().indexOf("hello world") > -1);
40 }
41
42 public void testBashHello() {
43 if (! hasBash)
44 return;
45 executeTarget("bash.hello");
46 assertTrue(getLog().indexOf("hello world") > -1);
47 }
48
49 public void testShInputString() {
50 if (! hasSh)
51 return;
52 executeTarget("sh.inputstring");
53 assertTrue(getLog().indexOf("hello world") > -1);
54 }
55
56 public void testShProperty() {
57 if (! hasSh)
58 return;
59 executeTarget("sh.property");
60 assertTrue(getLog().indexOf("this is a property") > -1);
61 }
62
63
64 public void testPythonHello() {
65 if (! hasPython)
66 return;
67 executeTarget("python.hello");
68 assertTrue(getLog().indexOf("hello world") > -1);
69 }
70
71 public void testPerlHello() {
72 if (! hasPerl)
73 return;
74 executeTarget("perl.hello");
75 assertTrue(getLog().indexOf("hello world") > -1);
76 }
77
78 public void testNoShell() {
79 expectBuildExceptionContaining(
80 "noshell", "Execute failed", "a shell that should not exist");
81 }
82
83 public void testSed() {
84 if (! hasSed)
85 return;
86 executeTarget("sed.test");
87 assertTrue(getLog().indexOf("BAR bar bar bar BAR bar") > -1);
88 }
89
90 public void testSetProperty() {
91 if (! hasSh)
92 return;
93 executeTarget("sh.set.property");
94 assertPropertyEquals("sh.set.property", "hello world");
95 }
96
97 public void testTmpSuffix() {
98 if (! hasSh)
99 return;
100 executeTarget("sh.tmp.suffix");
101 assertTrue(getLog().indexOf(".bat") > -1);
102 }
103
104 public void testCmd() {
105 if (! hasCmd)
106 return;
107 executeTarget("cmd.test");
108 assertTrue(getLog().indexOf("hello world") > -1);
109 }
110
111 public void testDir() {
112 if (! hasBash)
113 return;
114 executeTarget("dir.test");
115 assertTrue(
116 getProject().getProperty("dir.test.property")
117 .indexOf("subdir") > -1);
118 }
119
120 public void testCommand() {
121 expectBuildExceptionContaining(
122 "command.test", "Attribute failed",
123 "Attribute command is not supported");
124 }
125
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();
134
135 /**
136 * check if the env contains the shells
137 * sh, bash, python and perl
138 * assume cmd.exe exists for windows
139 */
140 private void staticInitialize() {
141 synchronized (staticMonitor) {
142 if (initialized)
143 return;
144 initialized = true;
145 hasSh = hasShell("hassh");
146 hasBash = hasShell("hasbash");
147 hasPerl = hasShell("hasperl");
148 hasPython = hasShell("haspython");
149 hasSed = hasShell("hassed");
150 hasCmd = hasShell("hascmd");
151
152 }
153 }
154
155 private boolean hasShell(String target) {
156 try {
157 executeTarget(target);
158 return true;
159 }
160 catch (Throwable t) {
161 return false;
162 }
163 }
164
165 }