|
5 | 5 | import gov.loc.repository.bagit.Bag; |
6 | 6 | import gov.loc.repository.bagit.BagFactory; |
7 | 7 | import gov.loc.repository.bagit.BagFile; |
| 8 | +import gov.loc.repository.bagit.transfer.dest.FileSystemFileDestination; |
| 9 | +import gov.loc.repository.bagit.transfer.fetch.HttpFetchProtocol; |
8 | 10 | import gov.loc.repository.bagit.utilities.ResourceHelper; |
9 | 11 | import gov.loc.repository.bagit.utilities.SimpleResult; |
10 | 12 |
|
|
18 | 20 | import org.jmock.States; |
19 | 21 | import org.jmock.integration.junit4.JMock; |
20 | 22 | import org.junit.After; |
| 23 | +import org.junit.Assert; |
21 | 24 | import org.junit.Before; |
22 | 25 | import org.junit.Test; |
23 | 26 | import org.junit.runner.RunWith; |
@@ -312,6 +315,59 @@ public void testMissingFetchTxt() throws Exception{ |
312 | 315 | this.unit.fetch(bag, mockDestinationFactory, false); |
313 | 316 |
|
314 | 317 | } |
| 318 | + |
| 319 | + /** |
| 320 | + * Fetching a payload file. This is correct according to the specs and works in the library. |
| 321 | + * |
| 322 | + * @link https://tools.ietf.org/html/draft-kunze-bagit#section-2.2.3 |
| 323 | + * @throws Exception |
| 324 | + */ |
| 325 | + @Test |
| 326 | + public void fetchToPayloadOk() throws Exception { |
| 327 | + File templateBag = new File("src/test/resources/bad-fetch-bag"); |
| 328 | + File bagFile = new File("target/fetchToPayloadOk"); |
| 329 | + FileUtils.deleteDirectory(bagFile); |
| 330 | + FileUtils.copyDirectory(templateBag, bagFile); |
| 331 | + BagFetcher fetcher = new BagFetcher(bagFactory); |
| 332 | + fetcher.registerProtocol("https", new HttpFetchProtocol()); |
| 333 | + Bag bag = bagFactory.createBag(bagFile, BagFactory.Version.V0_97, null); |
| 334 | + FileSystemFileDestination dest = new FileSystemFileDestination(bagFile); |
| 335 | + SimpleResult result = fetcher.fetch(bag, dest, false, true); |
| 336 | + Assert.assertTrue(result.isSuccess()); |
| 337 | + } |
| 338 | + |
| 339 | + /** |
| 340 | + * Fetching a non-payload file. This is not allowed according to the specs, at least the specs |
| 341 | + * only mention payload files. |
| 342 | + * |
| 343 | + * @throws Exception |
| 344 | + */ |
| 345 | + @Test |
| 346 | + public void fetchNonPayloadFileShouldFail() throws Exception { |
| 347 | + File templateBag = new File("src/test/resources/bad-fetch-bag"); |
| 348 | + File bagFile = new File("target/fetchNonPayloadFileShouldFail"); |
| 349 | + FileUtils.deleteDirectory(bagFile); |
| 350 | + FileUtils.copyDirectory(templateBag, bagFile); |
| 351 | + |
| 352 | + // Adding an extra line to the fetch.txt, to attempt to fetch a non-payload file |
| 353 | + FileUtils.writeStringToFile( |
| 354 | + new File(bagFile, "fetch.txt"), |
| 355 | + "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Wikipedia-logo-v2-nl.svg/135px-Wikipedia-logo-v2-nl.svg.png - metadata/wikipedia.png", |
| 356 | + true); |
| 357 | + |
| 358 | + BagFetcher fetcher = new BagFetcher(bagFactory); |
| 359 | + fetcher.registerProtocol("https", new HttpFetchProtocol()); |
| 360 | + Bag bag = bagFactory.createBag(bagFile, BagFactory.Version.V0_97, null); |
| 361 | + FileSystemFileDestination dest = new FileSystemFileDestination(bagFile); |
| 362 | + |
| 363 | + // Depending on |
| 364 | + SimpleResult result = fetcher.fetch(bag, dest, false, true); // Returns failure, but fetches metadata/wikipedia.png anyway |
| 365 | + //SimpleResult result = fetcher.fetch(bag, dest, true, true); // Returns success, but does not fetch metadata/wikipedia.png anyway |
| 366 | + Assert.assertFalse(result.isSuccess()); |
| 367 | + |
| 368 | + // However ... |
| 369 | + Assert.assertFalse(new File(bagFile, "metadata/wikipedia.png").exists()); |
| 370 | + } |
315 | 371 |
|
316 | 372 | private void expectDest(Expectations e, FetchedFileDestinationFactory destFactory, String path) throws Exception{ |
317 | 373 | FetchedFileDestination mockDestination = this.context.mock(FetchedFileDestination.class, "FetchedFileDestination:" + path); |
|
0 commit comments