4138c84d47df57b0ab05df0104706b665533a5da
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / integration / IntegrationTestBase.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\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
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.tests.integration;\r
18 \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
41 \r
42 import java.io.File;\r
43 import java.util.Collection;\r
44 \r
45 /**\r
46  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
47  * Date: 22.07.11 1:26\r
48  */\r
49 public class IntegrationTestBase extends BuildProcessTestCase {\r
50   private StringBuilder myCommandsOutput;\r
51   protected File myRoot;\r
52   protected Mockery m;\r
53   protected AgentRunningBuild myBuild;\r
54   protected BuildRunnerContext myContext;\r
55   protected BuildProgressLogger myLogger;\r
56   protected PackagesParametersFactory myParametersFactory;\r
57   protected NuGetFetchParameters myNuGet;\r
58   protected NuGetPackagesCollector myCollector;\r
59   protected NuGetActionFactory myActionFactory;\r
60   private BuildProcess myMockProcess;\r
61 \r
62   @NotNull\r
63   protected String getCommandsOutput() {\r
64     return myCommandsOutput.toString();\r
65   }\r
66 \r
67   @BeforeMethod\r
68   @Override\r
69   protected void setUp() throws Exception {\r
70     super.setUp();\r
71     myCommandsOutput = new StringBuilder();\r
72     myRoot = createTempDir();\r
73     m = new Mockery();\r
74     myBuild = m.mock(AgentRunningBuild.class);\r
75     myContext = m.mock(BuildRunnerContext.class);\r
76     myLogger = m.mock(BuildProgressLogger.class);\r
77     myParametersFactory = m.mock(PackagesParametersFactory.class);\r
78     myMockProcess = m.mock(BuildProcess.class);\r
79     myNuGet = m.mock(NuGetFetchParameters.class);\r
80 \r
81     m.checking(new Expectations() {{\r
82       allowing(myContext).getBuild();\r
83       will(returnValue(myBuild));\r
84       allowing(myBuild).getBuildLogger();\r
85       will(returnValue(myLogger));\r
86       allowing(myBuild).getCheckoutDirectory();\r
87       will(returnValue(myRoot));\r
88 \r
89       allowing(myMockProcess).start();\r
90       allowing(myMockProcess).waitFor();\r
91       will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));\r
92 \r
93       allowing(myLogger).message(with(any(String.class)));\r
94     }});\r
95 \r
96     myCollector = new NuGetPackagesCollectorImpl();\r
97     PackageUsages pu = new PackageUsagesImpl(\r
98             myCollector,\r
99             new NuGetPackagesConfigParser()\r
100     );\r
101 \r
102     myActionFactory = new LoggingNuGetActionFactoryImpl(new NuGetActionFactoryImpl(executingFactory(), pu, new CommandFactoryImpl()));\r
103   }\r
104 \r
105   @NotNull\r
106   protected File getTestDataPath(final String path) {\r
107     return Paths.getTestDataPath("integration/" + path);\r
108   }\r
109 \r
110   @NotNull\r
111   private CommandlineBuildProcessFactory executingFactory() {\r
112     return new CommandlineBuildProcessFactory() {\r
113       public BuildProcess executeCommandLine(@NotNull final BuildRunnerContext hostContext,\r
114                                              @NotNull final File program,\r
115                                              @NotNull final Collection<String> argz,\r
116                                              @NotNull final File workingDir) throws RunBuildException {\r
117         return new BuildProcessBase() {\r
118           @NotNull\r
119           @Override\r
120           protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
121             GeneralCommandLine cmd = new GeneralCommandLine();\r
122             cmd.setExePath(program.getPath());\r
123             for (String arg : argz) {\r
124               cmd.addParameter(arg);\r
125             }\r
126             cmd.setWorkingDirectory(workingDir);\r
127 \r
128             System.out.println("Run: " + cmd.getCommandLineString());\r
129 \r
130             ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);\r
131 \r
132             System.out.println(result.getStdout());\r
133             System.out.println(result.getStderr());\r
134 \r
135             myCommandsOutput.append(result.getStdout()).append("\n\n").append(result.getStderr()).append("\n\n");\r
136 \r
137             return result.getExitCode() == 0\r
138                     ? BuildFinishedStatus.FINISHED_SUCCESS\r
139                     : BuildFinishedStatus.FINISHED_FAILED;\r
140           }\r
141         };\r
142       }\r
143     };\r
144   }\r
145 }\r