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