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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 List<Ref> getRefsByPrefixWithExclusions(String include, Set<String> excludes)
  74. throws IOException {
  75. if (failing) {
  76. throw new IOException("disk failed, no refs found");
  77. }
  78. return super.getRefsByPrefixWithExclusions(include, excludes);
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException {
  83. if (failing) {
  84. throw new IOException("disk failed, no refs found");
  85. }
  86. return super.getTipsWithSha1(id);
  87. }
  88. }
  89. }