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.

RefsUnreadableInMemoryRepository.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2016, Google Inc. 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.http.test;
  11. import java.io.IOException;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  16. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  17. import org.eclipse.jgit.lib.ObjectId;
  18. import org.eclipse.jgit.lib.Ref;
  19. import org.eclipse.jgit.lib.RefDatabase;
  20. /**
  21. * An {@link InMemoryRepository} whose refs can be made unreadable for testing
  22. * purposes.
  23. */
  24. class RefsUnreadableInMemoryRepository extends InMemoryRepository {
  25. private final RefsUnreadableRefDatabase refs;
  26. private volatile boolean failing;
  27. RefsUnreadableInMemoryRepository(DfsRepositoryDescription repoDesc) {
  28. super(repoDesc);
  29. refs = new RefsUnreadableRefDatabase();
  30. failing = false;
  31. }
  32. /** {@inheritDoc} */
  33. @Override
  34. public RefDatabase getRefDatabase() {
  35. return refs;
  36. }
  37. /**
  38. * Make the ref database unable to scan its refs.
  39. * <p>
  40. * It may be useful to follow a call to startFailing with a call to
  41. * {@link RefDatabase#refresh()}, ensuring the next ref read fails.
  42. */
  43. void startFailing() {
  44. failing = true;
  45. }
  46. private class RefsUnreadableRefDatabase extends MemRefDatabase {
  47. /** {@inheritDoc} */
  48. @Override
  49. public Ref exactRef(String name) throws IOException {
  50. if (failing) {
  51. throw new IOException("disk failed, no refs found");
  52. }
  53. return super.exactRef(name);
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public Map<String, Ref> getRefs(String prefix) throws IOException {
  58. if (failing) {
  59. throw new IOException("disk failed, no refs found");
  60. }
  61. return super.getRefs(prefix);
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public List<Ref> getRefsByPrefix(String prefix) throws IOException {
  66. if (failing) {
  67. throw new IOException("disk failed, no refs found");
  68. }
  69. return super.getRefsByPrefix(prefix);
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException {
  74. if (failing) {
  75. throw new IOException("disk failed, no refs found");
  76. }
  77. return super.getTipsWithSha1(id);
  78. }
  79. }
  80. }