2 * Copyright 2000-2011 JetBrains s.r.o.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package jetbrains.buildServer.nuget.tests.integration;
\r
19 import com.intellij.execution.configurations.GeneralCommandLine;
\r
20 import jetbrains.buildServer.ExecResult;
\r
21 import jetbrains.buildServer.RunBuildException;
\r
22 import jetbrains.buildServer.SimpleCommandLineProcessRunner;
\r
23 import jetbrains.buildServer.agent.*;
\r
24 import jetbrains.buildServer.nuget.agent.commands.NuGetActionFactory;
\r
25 import jetbrains.buildServer.nuget.agent.commands.impl.CommandFactoryImpl;
\r
26 import jetbrains.buildServer.nuget.agent.commands.impl.LoggingNuGetActionFactoryImpl;
\r
27 import jetbrains.buildServer.nuget.agent.commands.impl.NuGetActionFactoryImpl;
\r
28 import jetbrains.buildServer.nuget.agent.dependencies.NuGetPackagesCollector;
\r
29 import jetbrains.buildServer.nuget.agent.dependencies.impl.NuGetPackagesCollectorImpl;
\r
30 import jetbrains.buildServer.nuget.agent.dependencies.PackageUsages;
\r
31 import jetbrains.buildServer.nuget.agent.dependencies.impl.NuGetPackagesConfigParser;
\r
32 import jetbrains.buildServer.nuget.agent.dependencies.impl.PackageUsagesImpl;
\r
33 import jetbrains.buildServer.nuget.agent.parameters.*;
\r
34 import jetbrains.buildServer.nuget.agent.util.BuildProcessBase;
\r
35 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;
\r
36 import jetbrains.buildServer.nuget.tests.util.BuildProcessTestCase;
\r
37 import org.jetbrains.annotations.NotNull;
\r
38 import org.jmock.Expectations;
\r
39 import org.jmock.Mockery;
\r
40 import org.testng.annotations.BeforeMethod;
\r
42 import java.io.File;
\r
43 import java.util.Collection;
\r
44 import java.util.HashMap;
\r
45 import java.util.Map;
\r
48 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
\r
49 * Date: 22.07.11 1:26
\r
51 public class IntegrationTestBase extends BuildProcessTestCase {
\r
52 private StringBuilder myCommandsOutput;
\r
53 protected File myRoot;
\r
54 protected Mockery m;
\r
55 protected AgentRunningBuild myBuild;
\r
56 protected BuildRunnerContext myContext;
\r
57 protected BuildProgressLogger myLogger;
\r
58 protected PackagesParametersFactory myParametersFactory;
\r
59 protected NuGetFetchParameters myNuGet;
\r
60 protected NuGetPackagesCollector myCollector;
\r
61 protected NuGetActionFactory myActionFactory;
\r
62 private BuildProcess myMockProcess;
\r
65 protected String getCommandsOutput() {
\r
66 return myCommandsOutput.toString();
\r
71 protected void setUp() throws Exception {
\r
73 myCommandsOutput = new StringBuilder();
\r
74 myRoot = createTempDir();
\r
76 myBuild = m.mock(AgentRunningBuild.class);
\r
77 myContext = m.mock(BuildRunnerContext.class);
\r
78 myLogger = m.mock(BuildProgressLogger.class);
\r
79 myParametersFactory = m.mock(PackagesParametersFactory.class);
\r
80 myMockProcess = m.mock(BuildProcess.class);
\r
81 myNuGet = m.mock(NuGetFetchParameters.class);
\r
83 m.checking(new Expectations() {{
\r
84 allowing(myContext).getBuild();
\r
85 will(returnValue(myBuild));
\r
86 allowing(myBuild).getBuildLogger();
\r
87 will(returnValue(myLogger));
\r
88 allowing(myBuild).getCheckoutDirectory();
\r
89 will(returnValue(myRoot));
\r
91 allowing(myMockProcess).start();
\r
92 allowing(myMockProcess).waitFor();
\r
93 will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));
\r
95 allowing(myLogger).message(with(any(String.class)));
\r
98 myCollector = new NuGetPackagesCollectorImpl();
\r
99 PackageUsages pu = new PackageUsagesImpl(
\r
101 new NuGetPackagesConfigParser()
\r
104 myActionFactory = new LoggingNuGetActionFactoryImpl(new NuGetActionFactoryImpl(executingFactory(), pu, new CommandFactoryImpl()));
\r
108 protected File getTestDataPath(final String path) {
\r
109 return Paths.getTestDataPath("integration/" + path);
\r
113 private CommandlineBuildProcessFactory executingFactory() {
\r
114 return new CommandlineBuildProcessFactory() {
\r
116 public BuildProcess executeCommandLine(@NotNull final BuildRunnerContext hostContext,
\r
117 @NotNull final File program,
\r
118 @NotNull final Collection<String> argz,
\r
119 @NotNull final File workingDir,
\r
120 @NotNull final Map<String, String> additionalEnvironment) throws RunBuildException {
\r
121 return new BuildProcessBase() {
\r
124 protected BuildFinishedStatus waitForImpl() throws RunBuildException {
\r
125 GeneralCommandLine cmd = new GeneralCommandLine();
\r
126 cmd.setExePath(program.getPath());
\r
127 for (String arg : argz) {
\r
128 cmd.addParameter(arg);
\r
130 cmd.setWorkingDirectory(workingDir);
\r
132 Map<String, String> env = new HashMap<String, String>();
\r
133 env.putAll(System.getenv());
\r
134 env.putAll(additionalEnvironment);
\r
135 cmd.setEnvParams(env);
\r
137 System.out.println("Run: " + cmd.getCommandLineString());
\r
139 ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);
\r
141 System.out.println(result.getStdout());
\r
142 System.out.println(result.getStderr());
\r
144 myCommandsOutput.append(result.getStdout()).append("\n\n").append(result.getStderr()).append("\n\n");
\r
146 return result.getExitCode() == 0
\r
147 ? BuildFinishedStatus.FINISHED_SUCCESS
\r
148 : BuildFinishedStatus.FINISHED_FAILED;
\r