test trigger updater
authorEugene Petrenko <eugene.petrenko@gmail.com>
Thu, 14 Jul 2011 21:15:40 +0000 (01:15 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Thu, 14 Jul 2011 21:15:40 +0000 (01:15 +0400)
nuget-server/src/META-INF/build-server-plugin-nuget.xml
nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommand.java
nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommandImpl.java [new file with mode: 0644]
nuget-server/src/jetbrains/buildServer/nuget/server/trigger/NamedPackagesUpdateChecker.java
nuget-tests/nuget-tests.iml
nuget-tests/src/jetbrains/buildServer/nuget/tests/server/ListPackagesCommandTest.java
nuget-tests/src/jetbrains/buildServer/nuget/tests/server/NamedPackagesUpdateCheckerTest.java [new file with mode: 0644]
nuget-tests/src/testng-nuget-tests..xml

index d72613813f086e22a9932abadced99e0c38a432f..a1ba186b065bb3e69ca5d14363bd48f89c0c72d7 100644 (file)
@@ -17,7 +17,7 @@
 \r
   <bean class="jetbrains.buildServer.nuget.server.exec.NuGetTeamCityProviderImpl"/>\r
   <bean class="jetbrains.buildServer.nuget.server.exec.NuGetExecutorImpl"/>\r
-  <bean class="jetbrains.buildServer.nuget.server.exec.ListPackagesCommand"/>\r
+  <bean class="jetbrains.buildServer.nuget.server.exec.ListPackagesCommandImpl"/>\r
 \r
   <bean class="jetbrains.buildServer.nuget.server.trigger.ThreadedBuildTriggetFactoryImpl"/>\r
   <bean class="jetbrains.buildServer.nuget.server.trigger.NamedPackagesUpdateChecker"/>\r
index 9d5d6ca53221755ebaae798f7b3a6645c2dd5b4a..f1a17a17a46caee92632f9a209b08a55730967bf 100644 (file)
 \r
 package jetbrains.buildServer.nuget.server.exec;\r
 \r
-import com.intellij.openapi.diagnostic.Logger;\r
-import jetbrains.buildServer.util.StringUtil;\r
 import org.jetbrains.annotations.NotNull;\r
 import org.jetbrains.annotations.Nullable;\r
 \r
 import java.io.File;\r
-import java.util.ArrayList;\r
 import java.util.Collection;\r
-import java.util.List;\r
 \r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
- * Date: 14.07.11 13:10\r
+ * Date: 14.07.11 19:04\r
  */\r
-public class ListPackagesCommand {\r
-  private static final Logger LOG = Logger.getInstance(ListPackagesCommand.class.getName());\r
-\r
-  private NuGetExecutor myExec;\r
-\r
-  public ListPackagesCommand(NuGetExecutor exec) {\r
-    myExec = exec;\r
-  }\r
-\r
+public interface ListPackagesCommand {\r
   @NotNull\r
-  public Collection<PackageInfo> checkForChanges(\r
-          @NotNull final File nugetPath,\r
-          @NotNull final String source,\r
-          @NotNull final String packageId,\r
-          @Nullable final String versionSpec) {\r
-    List<String> cmd = new ArrayList<String>();\r
-\r
-    cmd.add("TeamCity.List");\r
-    if (!StringUtil.isEmptyOrSpaces(source)) {\r
-      cmd.add("-Source");\r
-      cmd.add(source);\r
-    }\r
-    cmd.add("-Id");\r
-    cmd.add(packageId);\r
-\r
-    if (!StringUtil.isEmptyOrSpaces(versionSpec)) {\r
-      cmd.add("-Version");\r
-      cmd.add(versionSpec);\r
-    }\r
-\r
-    return myExec.executeNuGet(nugetPath, cmd, new ListPackagesCommandProcessor(source));\r
-  }\r
-\r
+  Collection<PackageInfo> checkForChanges(\r
+          @NotNull File nugetPath,\r
+          @Nullable String source,\r
+          @NotNull String packageId,\r
+          @Nullable String versionSpec);\r
 }\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommandImpl.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommandImpl.java
new file mode 100644 (file)
index 0000000..4bffd6c
--- /dev/null
@@ -0,0 +1,63 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.server.exec;\r
+\r
+import jetbrains.buildServer.util.StringUtil;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.jetbrains.annotations.Nullable;\r
+\r
+import java.io.File;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 14.07.11 13:10\r
+ */\r
+public class ListPackagesCommandImpl implements ListPackagesCommand {\r
+  private NuGetExecutor myExec;\r
+\r
+  public ListPackagesCommandImpl(NuGetExecutor exec) {\r
+    myExec = exec;\r
+  }\r
+\r
+  @NotNull\r
+  public Collection<PackageInfo> checkForChanges(\r
+          @NotNull final File nugetPath,\r
+          @Nullable final String source,\r
+          @NotNull final String packageId,\r
+          @Nullable final String versionSpec) {\r
+    List<String> cmd = new ArrayList<String>();\r
+\r
+    cmd.add("TeamCity.List");\r
+    if (!StringUtil.isEmptyOrSpaces(source)) {\r
+      cmd.add("-Source");\r
+      cmd.add(source);\r
+    }\r
+    cmd.add("-Id");\r
+    cmd.add(packageId);\r
+\r
+    if (!StringUtil.isEmptyOrSpaces(versionSpec)) {\r
+      cmd.add("-Version");\r
+      cmd.add(versionSpec);\r
+    }\r
+\r
+    return myExec.executeNuGet(nugetPath, cmd, new ListPackagesCommandProcessor(source));\r
+  }\r
+\r
+}\r
index 263c0b5ffa1ca0ddd3ee361f6bae18aac0b0d2fc..5ba574fe2decc71adc1f2c3a4d04f8a200ab48d4 100644 (file)
@@ -5,6 +5,7 @@ import jetbrains.buildServer.buildTriggers.BuildTriggerException;
 import jetbrains.buildServer.nuget.server.exec.ListPackagesCommand;\r
 import jetbrains.buildServer.nuget.server.exec.PackageInfo;\r
 import jetbrains.buildServer.serverSide.CustomDataStorage;\r
+import jetbrains.buildServer.util.StringUtil;\r
 import org.jetbrains.annotations.NotNull;\r
 \r
 import java.io.File;\r
@@ -29,11 +30,29 @@ public class NamedPackagesUpdateChecker implements TriggerUpdateChecker {
     final String version = descriptor.getProperties().get(TriggerConstants.VERSION);\r
     final String source = descriptor.getProperties().get(TriggerConstants.SOURCE);\r
 \r
-    Collection<PackageInfo> result = myCommand.checkForChanges(new File(path), source, pkgId, version);\r
+    if (StringUtil.isEmptyOrSpaces(path)) {\r
+      throw new BuildTriggerException("Path to NuGet.exe must be specified");\r
+    }\r
+\r
+    if (StringUtil.isEmptyOrSpaces(pkgId)) {\r
+      throw new BuildTriggerException("Package Id must be specified");\r
+    }\r
+\r
+    File nugetPath = new File(path);\r
+    if (!nugetPath.isFile()) {\r
+      throw new BuildTriggerException("Failed to find NuGet.exe at: " + nugetPath);\r
+    }\r
+\r
+    Collection<PackageInfo> result;\r
+    try {\r
+      result = myCommand.checkForChanges(nugetPath, source, pkgId, version);\r
+    } catch (Throwable t) {\r
+      throw new BuildTriggerException("Failed to check for package versions. " + t.getMessage(), t);\r
+    }\r
     final String hash = serializeHashcode(result);\r
 \r
     String oldHash = storage.getValue(KEY);\r
-    if (!hash.equals(oldHash)) {\r
+    if (oldHash != null && !hash.equals(oldHash)) {\r
       storage.putValue(KEY, hash);\r
       storage.flush();\r
       return new BuildStartReason("NuGet Package " + pkgId + " updated");\r
index 6c1b9c25cad7eacec379285b591761b7a9708ad6..88001f85eb1e41599d07355af3c7d112a5984db0 100644 (file)
@@ -21,6 +21,7 @@
     <orderEntry type="library" scope="RUNTIME" name="TeamCity server runtime" level="project" />\r
     <orderEntry type="library" scope="RUNTIME" name="TeamCity agent runtime" level="project" />\r
     <orderEntry type="library" name="Common-Impl" level="project" />\r
+    <orderEntry type="library" name="Server-Api" level="project" />\r
   </component>\r
 </module>\r
 \r
index 62af502ae20ed11338de486899221c3d82f9ca4a..1420f7c2a1774d3611bf163a49da08a03ddd43b9 100644 (file)
 package jetbrains.buildServer.nuget.tests.server;\r
 \r
 import jetbrains.buildServer.BaseTestCase;\r
-import jetbrains.buildServer.nuget.server.exec.ListPackagesCommand;\r
-import jetbrains.buildServer.nuget.server.exec.ListPackagesCommandProcessor;\r
-import jetbrains.buildServer.nuget.server.exec.NuGetExecutor;\r
-import jetbrains.buildServer.nuget.server.exec.PackageInfo;\r
+import jetbrains.buildServer.nuget.server.exec.*;\r
 import org.jmock.Expectations;\r
 import org.jmock.Mockery;\r
 import org.testng.annotations.BeforeMethod;\r
@@ -44,7 +41,7 @@ public class ListPackagesCommandTest extends BaseTestCase {
     super.setUp();\r
     m = new Mockery();\r
     exec = m.mock(NuGetExecutor.class);\r
-    cmd = new ListPackagesCommand(exec);\r
+    cmd = new ListPackagesCommandImpl(exec);\r
   }\r
 \r
   private void allowCommandLineCall(final String... cmd) {\r
diff --git a/nuget-tests/src/jetbrains/buildServer/nuget/tests/server/NamedPackagesUpdateCheckerTest.java b/nuget-tests/src/jetbrains/buildServer/nuget/tests/server/NamedPackagesUpdateCheckerTest.java
new file mode 100644 (file)
index 0000000..208e265
--- /dev/null
@@ -0,0 +1,119 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.tests.server;\r
+\r
+import jetbrains.buildServer.BaseTestCase;\r
+import jetbrains.buildServer.buildTriggers.BuildTriggerDescriptor;\r
+import jetbrains.buildServer.buildTriggers.BuildTriggerException;\r
+import jetbrains.buildServer.nuget.server.exec.ListPackagesCommand;\r
+import jetbrains.buildServer.nuget.server.exec.PackageInfo;\r
+import jetbrains.buildServer.nuget.server.trigger.NamedPackagesUpdateChecker;\r
+import jetbrains.buildServer.nuget.server.trigger.TriggerConstants;\r
+import jetbrains.buildServer.nuget.tests.integration.Paths;\r
+import jetbrains.buildServer.serverSide.CustomDataStorage;\r
+import junit.framework.Assert;\r
+import org.jmock.Expectations;\r
+import org.jmock.Mockery;\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+\r
+import java.io.File;\r
+import java.util.Arrays;\r
+import java.util.Map;\r
+import java.util.TreeMap;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 14.07.11 18:57\r
+ */\r
+public class NamedPackagesUpdateCheckerTest extends BaseTestCase {\r
+  private Mockery m;\r
+  private ListPackagesCommand cmd;\r
+  private NamedPackagesUpdateChecker checker;\r
+  private BuildTriggerDescriptor desr;\r
+  private CustomDataStorage store;\r
+  private Map<String, String> params;\r
+  private File nugetFakePath;\r
+\r
+  @BeforeMethod\r
+  @Override\r
+  protected void setUp() throws Exception {\r
+    super.setUp();\r
+\r
+    m = new Mockery();\r
+    cmd = m.mock(ListPackagesCommand.class);\r
+    desr = m.mock(BuildTriggerDescriptor.class);\r
+    store = m.mock(CustomDataStorage.class);\r
+    params = new TreeMap<String, String>();\r
+\r
+    checker = new NamedPackagesUpdateChecker(cmd);\r
+\r
+    m.checking(new Expectations(){{\r
+      allowing(desr).getProperties(); will(returnValue(params));\r
+    }});\r
+    nugetFakePath = Paths.getNuGetRunnerPath();\r
+\r
+    params.put(TriggerConstants.NUGET_EXE, nugetFakePath.getPath());\r
+    params.put(TriggerConstants.PACKAGE, "NUnit");\r
+  }\r
+\r
+  @Test\r
+  public void test_check_first_time_should_not_trigger() {\r
+    m.checking(new Expectations(){{\r
+      oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
+      will(returnValue(Arrays.asList(new PackageInfo("src", "pkg", "5.6.87"))));\r
+\r
+      oneOf(store).getValue("hash"); will(returnValue(null));\r
+      oneOf(store).putValue(with(equal("hash")), with(any(String.class)));\r
+      oneOf(store).flush();\r
+    }});\r
+    Assert.assertNull(checker.checkChanges(desr, store));\r
+  }\r
+\r
+  @Test\r
+  public void test_check_should_not_trigger_twice() {\r
+    m.checking(new Expectations(){{\r
+      oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
+      will(returnValue(Arrays.asList(new PackageInfo("src", "pkg", "5.6.87"))));\r
+\r
+      oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
+      will(returnValue(Arrays.asList(new PackageInfo("src", "pkg", "5.6.87"))));\r
+\r
+      oneOf(store).getValue("hash"); will(returnValue("aaa"));\r
+      oneOf(store).putValue("hash", "|s:src|p:pkg|v:5.6.87");\r
+      oneOf(store).getValue("hash"); will(returnValue("|s:src|p:pkg|v:5.6.87"));\r
+      oneOf(store).flush();\r
+    }});\r
+\r
+    Assert.assertNotNull(checker.checkChanges(desr, store));\r
+    Assert.assertNull(checker.checkChanges(desr, store));\r
+  }\r
+\r
+  @Test\r
+  public void test_check_should_fail_on_error() {\r
+    m.checking(new Expectations() {{\r
+      oneOf(cmd).checkForChanges(with(equal(nugetFakePath)), with(any(String.class)), with(any(String.class)), with(any(String.class)));\r
+      will(throwException(new RuntimeException("Failed to execute command")));\r
+    }});\r
+    try {\r
+      checker.checkChanges(desr, store);\r
+    } catch (BuildTriggerException e) {\r
+      return;\r
+    }\r
+    Assert.fail("should throw an exception");\r
+  }\r
+}\r
index 2e53378b45a416c8fef5380d971f9e5734d9e3f2..6b53facc6f78003e8f45af77b691d91fae57de06 100644 (file)
@@ -12,6 +12,8 @@
 \r
       <class name="jetbrains.buildServer.nuget.tests.server.ListPackagesCommandTest"/>\r
       <class name="jetbrains.buildServer.nuget.tests.server.ListPackagesCommandProcessorTest"/>\r
+\r
+      <class name="jetbrains.buildServer.nuget.tests.server.NamedPackagesUpdateCheckerTest"/>\r
     </classes>\r
   </test>\r
 \r