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.server;
\r
19 import jetbrains.buildServer.BaseTestCase;
\r
20 import jetbrains.buildServer.buildTriggers.BuildTriggerDescriptor;
\r
21 import jetbrains.buildServer.buildTriggers.BuildTriggerException;
\r
22 import jetbrains.buildServer.nuget.server.exec.ListPackagesCommand;
\r
23 import jetbrains.buildServer.nuget.server.exec.SourcePackageInfo;
\r
24 import jetbrains.buildServer.nuget.server.toolRegistry.NuGetToolManager;
\r
25 import jetbrains.buildServer.nuget.server.trigger.NamedPackagesUpdateChecker;
\r
26 import jetbrains.buildServer.nuget.server.trigger.TriggerConstants;
\r
27 import jetbrains.buildServer.nuget.tests.integration.Paths;
\r
28 import jetbrains.buildServer.serverSide.CustomDataStorage;
\r
29 import junit.framework.Assert;
\r
30 import org.jmock.Expectations;
\r
31 import org.jmock.Mockery;
\r
32 import org.testng.annotations.BeforeMethod;
\r
33 import org.testng.annotations.Test;
\r
35 import java.io.File;
\r
36 import java.util.Arrays;
\r
37 import java.util.Map;
\r
38 import java.util.TreeMap;
\r
41 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
\r
42 * Date: 14.07.11 18:57
\r
44 public class NamedPackagesUpdateCheckerTest extends BaseTestCase {
\r
46 private ListPackagesCommand cmd;
\r
47 private NamedPackagesUpdateChecker checker;
\r
48 private BuildTriggerDescriptor desr;
\r
49 private CustomDataStorage store;
\r
50 private NuGetToolManager manager;
\r
51 private Map<String, String> params;
\r
52 private File nugetFakePath;
\r
56 protected void setUp() throws Exception {
\r
60 cmd = m.mock(ListPackagesCommand.class);
\r
61 desr = m.mock(BuildTriggerDescriptor.class);
\r
62 store = m.mock(CustomDataStorage.class);
\r
63 params = new TreeMap<String, String>();
\r
64 manager = m.mock(NuGetToolManager.class);
\r
66 checker = new NamedPackagesUpdateChecker(cmd, manager);
\r
67 nugetFakePath = Paths.getNuGetRunnerPath();
\r
68 final String path = nugetFakePath.getPath();
\r
70 m.checking(new Expectations(){{
\r
71 allowing(desr).getProperties(); will(returnValue(params));
\r
72 allowing(manager).getNuGetPath(path); will(returnValue(path));
\r
75 params.put(TriggerConstants.NUGET_EXE, path);
\r
76 params.put(TriggerConstants.PACKAGE, "NUnit");
\r
80 public void test_check_first_time_should_not_trigger() {
\r
81 m.checking(new Expectations(){{
\r
82 oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);
\r
83 will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));
\r
85 oneOf(store).getValue("hash"); will(returnValue(null));
\r
86 oneOf(store).putValue(with(equal("hash")), with(any(String.class)));
\r
87 oneOf(store).flush();
\r
89 Assert.assertNull(checker.checkChanges(desr, store));
\r
91 m.assertIsSatisfied();
\r
95 public void test_should_trigger_on_new_package() {
\r
96 final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";
\r
98 params.put(TriggerConstants.PACKAGE, "Common");
\r
99 params.put(TriggerConstants.SOURCE, source);
\r
100 m.checking(new Expectations(){{
\r
101 oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);
\r
102 will(returnValue(Arrays.asList(new SourcePackageInfo(source, "Common", "1.0.0.21"))));
\r
104 oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);
\r
105 will(returnValue(Arrays.asList(
\r
106 new SourcePackageInfo(source, "Common", "1.0.0.21"),
\r
107 new SourcePackageInfo(source, "Common", "2.0.0.22")
\r
110 final String hash1 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21";
\r
111 final String hash2 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:2.0.0.22";
\r
113 oneOf(store).getValue("hash"); will(returnValue(null));
\r
114 oneOf(store).putValue("hash", hash1);
\r
115 oneOf(store).flush();
\r
117 oneOf(store).getValue("hash"); will(returnValue(hash1));
\r
118 oneOf(store).putValue("hash", hash2);
\r
119 oneOf(store).flush();
\r
122 Assert.assertNull(checker.checkChanges(desr, store));
\r
125 Assert.assertNotNull(checker.checkChanges(desr, store));
\r
127 m.assertIsSatisfied();
\r
131 public void test_should_trigger_on_new_package2() {
\r
132 final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";
\r
134 params.put(TriggerConstants.PACKAGE, "Common");
\r
135 params.put(TriggerConstants.SOURCE, source);
\r
136 m.checking(new Expectations(){{
\r
137 oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);
\r
138 will(returnValue(Arrays.asList(new SourcePackageInfo(source, "Common", "1.0.0.21"))));
\r
140 oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);
\r
141 will(returnValue(Arrays.asList(
\r
142 new SourcePackageInfo(source, "Common", "1.0.0.21"),
\r
143 new SourcePackageInfo(source, "Common", "2.0.0.22")
\r
146 final String hash1 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21";
\r
147 final String hash2 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:2.0.0.22";
\r
149 oneOf(store).getValue("hash"); will(returnValue(hash1));
\r
150 oneOf(store).getValue("hash"); will(returnValue(hash1));
\r
151 oneOf(store).putValue("hash", hash2);
\r
152 oneOf(store).flush();
\r
155 Assert.assertNull(checker.checkChanges(desr, store));
\r
158 Assert.assertNotNull(checker.checkChanges(desr, store));
\r
160 m.assertIsSatisfied();
\r
164 public void test_should_sort_packages() {
\r
165 final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";
\r
167 params.put(TriggerConstants.PACKAGE, "Common");
\r
168 params.put(TriggerConstants.SOURCE, source);
\r
169 m.checking(new Expectations(){{
\r
170 oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);
\r
171 will(returnValue(Arrays.asList(
\r
172 new SourcePackageInfo(source, "Common", "1.0.0.21"),
\r
173 new SourcePackageInfo(null, "Common", "2.0.0.22"),
\r
174 new SourcePackageInfo("s2", "C3ommon", "2.0.0.22"),
\r
175 new SourcePackageInfo(null, "C3ommon", "2.0.0.22"),
\r
176 new SourcePackageInfo(null, "C3o4mmon", "2.0.0.22"),
\r
177 new SourcePackageInfo("s4", "C3o3mmon", "2.0.0.22"),
\r
178 new SourcePackageInfo(null, "C3omm5on", "2.0.0.22")
\r
181 final String hash1 = "|p:Common|v:2.0.0.22|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21|s:s2|p:C3ommon|v:2.0.0.22|s:s4|p:C3o3mmon|v:2.0.0.22|p:C3ommon|v:2.0.0.22|p:C3o4mmon|v:2.0.0.22|p:C3omm5on|v:2.0.0.22";
\r
182 oneOf(store).getValue("hash"); will(returnValue("foo"));
\r
183 oneOf(store).putValue("hash", hash1);
\r
184 oneOf(store).flush();
\r
187 Assert.assertNotNull(checker.checkChanges(desr, store));
\r
189 m.assertIsSatisfied();
\r
193 public void test_check_should_not_trigger_twice() {
\r
194 m.checking(new Expectations(){{
\r
195 oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);
\r
196 will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));
\r
198 oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);
\r
199 will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));
\r
201 oneOf(store).getValue("hash"); will(returnValue("aaa"));
\r
202 oneOf(store).putValue("hash", "|s:src|p:pkg|v:5.6.87");
\r
203 oneOf(store).getValue("hash"); will(returnValue("|s:src|p:pkg|v:5.6.87"));
\r
204 oneOf(store).flush();
\r
207 Assert.assertNotNull(checker.checkChanges(desr, store));
\r
208 Assert.assertNull(checker.checkChanges(desr, store));
\r
210 m.assertIsSatisfied();
\r
214 public void test_check_should_fail_on_error() {
\r
215 m.checking(new Expectations() {{
\r
216 oneOf(cmd).checkForChanges(with(equal(nugetFakePath)), with(any(String.class)), with(any(String.class)), with(any(String.class)));
\r
217 will(throwException(new RuntimeException("Failed to execute command")));
\r
220 checker.checkChanges(desr, store);
\r
221 Assert.fail("should throw an exception");
\r
222 } catch (BuildTriggerException e) {
\r
226 m.assertIsSatisfied();
\r