587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.core.plexus.tests;
|
|
18 |
|
628
|
19 |
import static org.junit.Assert.assertTrue;
|
|
20 |
import static org.junit.Assert.fail;
|
587
|
21 |
import hidden.org.codehaus.plexus.interpolation.os.Os;
|
|
22 |
|
|
23 |
import java.util.Hashtable;
|
|
24 |
|
|
25 |
import org.junit.Test;
|
|
26 |
|
|
27 |
import com.nokia.helium.core.plexus.CommandBase;
|
|
28 |
import com.nokia.helium.core.plexus.StreamRecorder;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Unittests for the CommandBase class.
|
|
32 |
*
|
|
33 |
*/
|
|
34 |
public class TestCommandBase {
|
|
35 |
|
|
36 |
/**
|
|
37 |
* The simplest possible implementation possible.
|
|
38 |
*
|
|
39 |
*/
|
|
40 |
public class CommandImpl extends CommandBase<Exception> {
|
|
41 |
|
|
42 |
@Override
|
|
43 |
protected String getExecutable() {
|
|
44 |
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
|
45 |
return "echo";
|
|
46 |
} else {
|
|
47 |
return System.getProperty("testdir") + "/tests/echo.sh";
|
|
48 |
}
|
|
49 |
}
|
|
50 |
|
|
51 |
@Override
|
|
52 |
protected void throwException(String message, Throwable t)
|
|
53 |
throws Exception {
|
|
54 |
throw new Exception(message, t);
|
|
55 |
}
|
|
56 |
|
|
57 |
}
|
|
58 |
|
|
59 |
@Test
|
|
60 |
public void simpleExecution() {
|
|
61 |
CommandImpl cmd = new CommandImpl();
|
|
62 |
try {
|
|
63 |
cmd.execute(null);
|
|
64 |
} catch (Exception e) {
|
|
65 |
fail("Exception should not happen.");
|
|
66 |
}
|
|
67 |
|
|
68 |
}
|
|
69 |
|
|
70 |
@Test
|
|
71 |
public void simpleExecutionWithArgs() {
|
|
72 |
CommandImpl cmd = new CommandImpl();
|
|
73 |
String args[] = new String[2];
|
|
74 |
args[0] = "foo";
|
|
75 |
args[1] = "bar";
|
|
76 |
try {
|
|
77 |
cmd.execute(args);
|
|
78 |
} catch (Exception e) {
|
|
79 |
fail("Exception should not happen.");
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
@Test
|
|
84 |
public void simpleExecutionWithArgsAndRecorder() throws Exception {
|
|
85 |
CommandImpl cmd = new CommandImpl();
|
|
86 |
String args[] = new String[2];
|
|
87 |
args[0] = "foo";
|
|
88 |
args[1] = "bar";
|
|
89 |
StreamRecorder rec = new StreamRecorder();
|
|
90 |
cmd.execute(args, rec);
|
|
91 |
assertTrue(rec.getBuffer().toString().startsWith("foo bar"));
|
|
92 |
}
|
|
93 |
|
|
94 |
@Test
|
|
95 |
public void simpleExecutionWithArgsAndRecorderAsOutputHandler() throws Exception {
|
|
96 |
CommandImpl cmd = new CommandImpl();
|
|
97 |
StreamRecorder rec = new StreamRecorder();
|
|
98 |
cmd.addOutputLineHandler(rec);
|
|
99 |
String args[] = new String[2];
|
|
100 |
args[0] = "foo";
|
|
101 |
args[1] = "bar";
|
|
102 |
cmd.execute(args, rec);
|
|
103 |
assertTrue(rec.getBuffer().toString().startsWith("foo bar"));
|
|
104 |
}
|
|
105 |
|
|
106 |
@Test
|
|
107 |
public void simpleExecutionWithEnv() throws Exception {
|
|
108 |
CommandImpl cmd = new CommandImpl();
|
|
109 |
StreamRecorder rec = new StreamRecorder();
|
|
110 |
cmd.addOutputLineHandler(rec);
|
|
111 |
String args[] = new String[2];
|
|
112 |
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
|
113 |
args[0] = "%TEST_FOO%";
|
|
114 |
args[1] = "%TEST_BAR%";
|
|
115 |
} else {
|
|
116 |
args[0] = "$TEST_FOO";
|
|
117 |
args[1] = "$TEST_BAR";
|
|
118 |
}
|
|
119 |
Hashtable<String, String> env = new Hashtable<String, String>();
|
|
120 |
env.put("TEST_FOO", "foo");
|
|
121 |
env.put("TEST_BAR", "bar");
|
|
122 |
cmd.execute(args, env, rec);
|
|
123 |
assertTrue(rec.getBuffer().toString().startsWith("foo bar"));
|
|
124 |
}
|
|
125 |
|
|
126 |
}
|