download nuget package test
authorEugene Petrenko <eugene.petrenko@gmail.com>
Fri, 12 Aug 2011 11:11:59 +0000 (15:11 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Fri, 12 Aug 2011 11:11:59 +0000 (15:11 +0400)
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/NuGetFeedReader.java
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/impl/FeedConstants.java
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/impl/NuGetFeedReaderImpl.java
nuget-tests/src/jetbrains/buildServer/nuget/tests/integration/FeedReaderTest.java

index 35901b3c99871fd17f5bc02d672a61fdfaf0dd66..37725088b749d6544f09fbf96d877f8439078c17 100644 (file)
@@ -18,6 +18,7 @@ package jetbrains.buildServer.nuget.server.feed.reader;
 \r
 import org.jetbrains.annotations.NotNull;\r
 \r
+import java.io.File;\r
 import java.io.IOException;\r
 import java.util.Collection;\r
 \r
@@ -29,4 +30,7 @@ public interface NuGetFeedReader {
   @NotNull\r
   Collection<FeedPackage> queryPackageVersions(@NotNull String feedUrl,\r
                                                @NotNull String packageId) throws IOException;\r
+\r
+  void downloadPackage(@NotNull FeedPackage pkg,\r
+                       @NotNull File destFile) throws IOException;\r
 }\r
index 103d36dd76cc0abc06bf7939121b6a751fb5bfb3..984e4122619eee4d745eb4a58b9e918ed6a89116 100644 (file)
@@ -22,4 +22,5 @@ package jetbrains.buildServer.nuget.server.feed.reader.impl;
  */\r
 public class FeedConstants {\r
   public static final String FEED_URL = "https://go.microsoft.com/fwlink/?LinkID=206669";\r
+  public static final String NUGET_COMMANDLINE = "NuGet.CommandLine";\r
 }\r
index bec0ffaf85530b59d226f451929ebe3c6ae7bcf6..a5bb9292f0267c7efde3538d78da30c3e0cbac02 100644 (file)
@@ -23,12 +23,14 @@ import jetbrains.buildServer.util.FileUtil;
 import jetbrains.buildServer.util.XmlUtil;\r
 import org.apache.http.HttpHeaders;\r
 import org.apache.http.HttpResponse;\r
+import org.apache.http.HttpStatus;\r
+import org.apache.http.StatusLine;\r
 import org.apache.http.client.methods.HttpGet;\r
 import org.jdom.Element;\r
 import org.jdom.JDOMException;\r
 import org.jetbrains.annotations.NotNull;\r
 \r
-import java.io.IOException;\r
+import java.io.*;\r
 import java.util.Collection;\r
 \r
 /**\r
@@ -51,7 +53,7 @@ public class NuGetFeedReaderImpl implements NuGetFeedReader {
 \r
   @NotNull\r
   public Collection<FeedPackage> queryPackageVersions(@NotNull String feedUrl,\r
-                           @NotNull String packageId) throws IOException {\r
+                                                      @NotNull String packageId) throws IOException {\r
     LOG.debug("Connecting to NuGet feed url: " + feedUrl);\r
     final Pair<String, HttpResponse> pair = myResolver.resolvePath(feedUrl);\r
     feedUrl = pair.first;\r
@@ -61,7 +63,7 @@ public class NuGetFeedReaderImpl implements NuGetFeedReader {
 \r
     final HttpGet get = myMethodFactory.createGet(feedUrl + "/Packages()",\r
             new Param("$filter", "Id eq '" + packageId + "'")\r
-            );\r
+    );\r
     get.setHeader(HttpHeaders.ACCEPT_ENCODING, "application/atom+xml");\r
 \r
     LOG.debug("Query for packages: " + get.getURI());\r
@@ -70,6 +72,28 @@ public class NuGetFeedReaderImpl implements NuGetFeedReader {
     return myParser.readPackages(toDocument(execute));\r
   }\r
 \r
+  public void downloadPackage(@NotNull FeedPackage pkg, @NotNull File file) throws IOException {\r
+    FileUtil.createParentDirs(file);\r
+    final String url = pkg.getDownloadUrl();\r
+\r
+    final HttpGet get = myMethodFactory.createGet(url);\r
+    final HttpResponse resp = myClient.getClient().execute(get);\r
+    final StatusLine statusLine = resp.getStatusLine();\r
+    if (statusLine.getStatusCode() != HttpStatus.SC_OK) {\r
+      throw new IOException("Failed to download package " + pkg + ". Server returned " + statusLine);\r
+    }\r
+\r
+    OutputStream os = null;\r
+    try {\r
+      os = new BufferedOutputStream(new FileOutputStream(file));\r
+      resp.getEntity().writeTo(os);\r
+    } catch (final IOException e) {\r
+      throw new IOException("Failed to download package " + pkg + ". " + e.getMessage()) {{ initCause(e); }};\r
+    } finally {\r
+      FileUtil.close(os);\r
+    }\r
+  }\r
+\r
   private Element toDocument(HttpResponse response) throws IOException {\r
     try {\r
       return FileUtil.parseDocument(response.getEntity().getContent(), false);\r
index 6ab0cd8d8a198dc7fa3552588c5330c02f20ead7..7beb8b372ac8792d0b945789f7bce9372e6f736c 100644 (file)
@@ -20,13 +20,19 @@ import jetbrains.buildServer.BaseTestCase;
 import jetbrains.buildServer.nuget.server.feed.reader.FeedPackage;\r
 import jetbrains.buildServer.nuget.server.feed.reader.NuGetFeedReader;\r
 import jetbrains.buildServer.nuget.server.feed.reader.impl.*;\r
+import jetbrains.buildServer.util.FileUtil;\r
 import org.testng.Assert;\r
 import org.testng.annotations.AfterMethod;\r
 import org.testng.annotations.BeforeMethod;\r
 import org.testng.annotations.Test;\r
 \r
+import java.io.BufferedInputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
 import java.io.IOException;\r
 import java.util.Collection;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipInputStream;\r
 \r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
@@ -64,4 +70,36 @@ public class FeedReaderTest extends BaseTestCase {
       System.out.println("feedPackage = " + feedPackage);\r
     }\r
   }\r
+\r
+  @Test\r
+  public void testDownloadLatest() throws IOException {\r
+    final Collection<FeedPackage> packages = myReader.queryPackageVersions(FeedConstants.FEED_URL, "NuGet.CommandLine");\r
+    FeedPackage latest = null;\r
+    for (FeedPackage aPackage : packages) {\r
+      if (aPackage.isLatestVersion()) {\r
+        latest = aPackage;\r
+      }\r
+    }\r
+    Assert.assertNotNull(latest, "there should be the latest package");\r
+\r
+    final File pkd = createTempFile();\r
+    myReader.downloadPackage(latest, pkd);\r
+\r
+    Assert.assertTrue(pkd.length() > 100);\r
+    boolean hasNuGetExe = false;\r
+    ZipInputStream zip = new ZipInputStream(new BufferedInputStream(new FileInputStream(pkd)));\r
+    try {\r
+      for (ZipEntry ze = zip.getNextEntry(); ze != null; ze = zip.getNextEntry()) {\r
+        String name = ze.getName();\r
+        System.out.println("ze = " + name);\r
+        name = name.toLowerCase();\r
+        hasNuGetExe |= name.endsWith("/NuGet.exe".toLowerCase());\r
+        hasNuGetExe |= name.endsWith("\\NuGet.exe".toLowerCase());\r
+      }\r
+    } finally {\r
+      FileUtil.close(zip);\r
+    }\r
+\r
+    Assert.assertTrue(hasNuGetExe, "package should contain nuget.exe");\r
+  }\r
 }\r