You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadTest.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lfs.server.fs;
  11. import static org.apache.http.HttpStatus.SC_NOT_FOUND;
  12. import static org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertThrows;
  15. import java.io.IOException;
  16. import java.nio.file.Path;
  17. import java.nio.file.Paths;
  18. import java.text.MessageFormat;
  19. import org.apache.http.client.ClientProtocolException;
  20. import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
  21. import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
  22. import org.eclipse.jgit.util.FileUtils;
  23. import org.junit.Test;
  24. public class DownloadTest extends LfsServerTest {
  25. @Test
  26. public void testDownload() throws Exception {
  27. String TEXT = "test";
  28. AnyLongObjectId id = putContent(TEXT);
  29. Path f = Paths.get(getTempDirectory().toString(), "download");
  30. long len = getContent(id, f);
  31. assertEquals(TEXT.length(), len);
  32. FileUtils.delete(f.toFile(), FileUtils.RETRY);
  33. }
  34. @Test
  35. public void testDownloadInvalidPathInfo()
  36. throws ClientProtocolException, IOException {
  37. String TEXT = "test";
  38. String id = putContent(TEXT).name().substring(0, 60);
  39. Path f = Paths.get(getTempDirectory().toString(), "download");
  40. String error = String.format(
  41. "Invalid pathInfo: '/%s' does not match '/{SHA-256}'", id);
  42. assertThrows(formatErrorMessage(SC_UNPROCESSABLE_ENTITY, error),
  43. RuntimeException.class, () -> getContent(id, f));
  44. }
  45. @Test
  46. public void testDownloadInvalidId()
  47. throws ClientProtocolException, IOException {
  48. String TEXT = "test";
  49. String id = putContent(TEXT).name().replace('f', 'z');
  50. Path f = Paths.get(getTempDirectory().toString(), "download");
  51. String error = String.format("Invalid id: %s", id);
  52. assertThrows(formatErrorMessage(SC_UNPROCESSABLE_ENTITY, error),
  53. RuntimeException.class, () -> getContent(id, f));
  54. }
  55. @Test
  56. public void testDownloadNotFound() {
  57. String TEXT = "test";
  58. AnyLongObjectId id = LongObjectIdTestUtils.hash(TEXT);
  59. Path f = Paths.get(getTempDirectory().toString(), "download");
  60. String error = String.format("Object '%s' not found", id.getName());
  61. assertThrows(formatErrorMessage(SC_NOT_FOUND, error),
  62. RuntimeException.class, () -> getContent(id, f));
  63. }
  64. @SuppressWarnings("boxing")
  65. @Test
  66. public void testLargeFileDownload() throws Exception {
  67. Path f = Paths.get(getTempDirectory().toString(), "largeRandomFile");
  68. long expectedLen = createPseudoRandomContentFile(f, 5 * MiB);
  69. AnyLongObjectId id = putContent(f);
  70. Path f2 = Paths.get(getTempDirectory().toString(), "download");
  71. long start = System.nanoTime();
  72. long len = getContent(id, f2);
  73. System.out.println(
  74. MessageFormat.format("downloaded 10 MiB random data in {0}ms",
  75. (System.nanoTime() - start) / 1e6));
  76. assertEquals(expectedLen, len);
  77. FileUtils.delete(f.toFile(), FileUtils.RETRY);
  78. }
  79. @SuppressWarnings("boxing")
  80. private String formatErrorMessage(int status, String message) {
  81. return String.format("Status: %d {\"message\":\"%s\"}", status,
  82. message);
  83. }
  84. }