| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.platform; |
| 17 |
|
| 18 |
import org.apache.tools.ant.BuildFileTest; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 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 |
} |