Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 1 | // Copyright (C) 2013 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
David Ostrovsky | daf5cce | 2017-09-20 00:19:04 +0200 | [diff] [blame] | 15 | package com.google.gerrit.testing; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 16 | |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 17 | import com.google.common.collect.Sets; |
David Ostrovsky | b03a6e9 | 2019-05-26 14:11:47 +0200 | [diff] [blame] | 18 | import com.google.gerrit.entities.Project; |
Marija Savtchouk | 6a88e31 | 2021-08-20 16:50:26 +0100 | [diff] [blame] | 19 | import com.google.gerrit.entities.Project.NameKey; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 20 | import com.google.gerrit.server.git.GitRepositoryManager; |
| 21 | import com.google.gerrit.server.git.RepositoryCaseMismatchException; |
Dave Borowitz | c7078ab | 2017-04-24 13:42:19 +0200 | [diff] [blame] | 22 | import com.google.inject.Inject; |
Edwin Kempin | fe105c2 | 2021-12-22 15:22:00 +0100 | [diff] [blame] | 23 | import java.util.Collections; |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 24 | import java.util.HashMap; |
| 25 | import java.util.Map; |
Edwin Kempin | fe105c2 | 2021-12-22 15:22:00 +0100 | [diff] [blame] | 26 | import java.util.NavigableSet; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 27 | import org.eclipse.jgit.errors.RepositoryNotFoundException; |
Shawn Pearce | 95c5dee | 2013-03-18 09:19:15 -0700 | [diff] [blame] | 28 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 29 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 30 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 31 | |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 32 | /** Repository manager that uses in-memory repositories. */ |
| 33 | public class InMemoryRepositoryManager implements GitRepositoryManager { |
Dave Borowitz | 826a7b6 | 2013-12-05 15:44:21 -0800 | [diff] [blame] | 34 | public static InMemoryRepository newRepository(Project.NameKey name) { |
Dave Borowitz | 24b3e67 | 2017-07-28 12:55:47 -0400 | [diff] [blame] | 35 | return new Repo(name); |
Dave Borowitz | 826a7b6 | 2013-12-05 15:44:21 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Dave Borowitz | 7ffd9a8 | 2015-05-18 14:41:30 -0700 | [diff] [blame] | 38 | public static class Description extends DfsRepositoryDescription { |
| 39 | private final Project.NameKey name; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 40 | |
| 41 | private Description(Project.NameKey name) { |
| 42 | super(name.get()); |
Dave Borowitz | 7ffd9a8 | 2015-05-18 14:41:30 -0700 | [diff] [blame] | 43 | this.name = name; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 44 | } |
Dave Borowitz | 7ffd9a8 | 2015-05-18 14:41:30 -0700 | [diff] [blame] | 45 | |
| 46 | public Project.NameKey getProject() { |
| 47 | return name; |
| 48 | } |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Dave Borowitz | 7ffd9a8 | 2015-05-18 14:41:30 -0700 | [diff] [blame] | 51 | public static class Repo extends InMemoryRepository { |
Shawn Pearce | 1ac21e9 | 2016-11-14 12:57:34 -0800 | [diff] [blame] | 52 | private String description; |
| 53 | |
Dave Borowitz | 24b3e67 | 2017-07-28 12:55:47 -0400 | [diff] [blame] | 54 | private Repo(Project.NameKey name) { |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 55 | super(new Description(name)); |
Dave Borowitz | 24b3e67 | 2017-07-28 12:55:47 -0400 | [diff] [blame] | 56 | setPerformsAtomicTransactions(true); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Description getDescription() { |
| 61 | return (Description) super.getDescription(); |
| 62 | } |
Shawn Pearce | 1ac21e9 | 2016-11-14 12:57:34 -0800 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | public String getGitwebDescription() { |
| 66 | return description; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void setGitwebDescription(String d) { |
| 71 | description = d; |
| 72 | } |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Dave Borowitz | c7078ab | 2017-04-24 13:42:19 +0200 | [diff] [blame] | 75 | private final Map<String, Repo> repos; |
| 76 | |
Dave Borowitz | c7078ab | 2017-04-24 13:42:19 +0200 | [diff] [blame] | 77 | @Inject |
Dave Borowitz | 24b3e67 | 2017-07-28 12:55:47 -0400 | [diff] [blame] | 78 | public InMemoryRepositoryManager() { |
Dave Borowitz | c7078ab | 2017-04-24 13:42:19 +0200 | [diff] [blame] | 79 | this.repos = new HashMap<>(); |
| 80 | } |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 81 | |
| 82 | @Override |
Marija Savtchouk | 6a88e31 | 2021-08-20 16:50:26 +0100 | [diff] [blame] | 83 | public synchronized Status getRepositoryStatus(NameKey name) { |
| 84 | try { |
| 85 | get(name); |
| 86 | return Status.ACTIVE; |
| 87 | } catch (RepositoryNotFoundException e) { |
| 88 | return Status.NON_EXISTENT; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Override |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 93 | public synchronized Repo openRepository(Project.NameKey name) throws RepositoryNotFoundException { |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 94 | return get(name); |
| 95 | } |
| 96 | |
| 97 | @Override |
Dave Borowitz | 7ffd9a8 | 2015-05-18 14:41:30 -0700 | [diff] [blame] | 98 | public synchronized Repo createRepository(Project.NameKey name) |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 99 | throws RepositoryCaseMismatchException, RepositoryNotFoundException { |
| 100 | Repo repo; |
| 101 | try { |
| 102 | repo = get(name); |
| 103 | if (!repo.getDescription().getRepositoryName().equals(name.get())) { |
| 104 | throw new RepositoryCaseMismatchException(name); |
| 105 | } |
| 106 | } catch (RepositoryNotFoundException e) { |
Dave Borowitz | 24b3e67 | 2017-07-28 12:55:47 -0400 | [diff] [blame] | 107 | repo = new Repo(name); |
Dave Borowitz | 64c2340 | 2016-07-12 18:21:35 -0400 | [diff] [blame] | 108 | repos.put(normalize(name), repo); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 109 | } |
| 110 | return repo; |
| 111 | } |
| 112 | |
| 113 | @Override |
Edwin Kempin | fe105c2 | 2021-12-22 15:22:00 +0100 | [diff] [blame] | 114 | public synchronized NavigableSet<Project.NameKey> list() { |
| 115 | NavigableSet<Project.NameKey> names = Sets.newTreeSet(); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 116 | for (DfsRepository repo : repos.values()) { |
Dave Borowitz | 5c9a650 | 2019-04-19 09:14:35 -0700 | [diff] [blame] | 117 | names.add(Project.nameKey(repo.getDescription().getRepositoryName())); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 118 | } |
Edwin Kempin | fe105c2 | 2021-12-22 15:22:00 +0100 | [diff] [blame] | 119 | return Collections.unmodifiableNavigableSet(names); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Dave Borowitz | 64c2340 | 2016-07-12 18:21:35 -0400 | [diff] [blame] | 122 | public synchronized void deleteRepository(Project.NameKey name) { |
| 123 | repos.remove(normalize(name)); |
| 124 | } |
| 125 | |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 126 | private synchronized Repo get(Project.NameKey name) throws RepositoryNotFoundException { |
Dave Borowitz | 64c2340 | 2016-07-12 18:21:35 -0400 | [diff] [blame] | 127 | Repo repo = repos.get(normalize(name)); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 128 | if (repo != null) { |
David Pursehouse | d545c29 | 2017-02-01 22:47:34 +0900 | [diff] [blame] | 129 | repo.incrementOpen(); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 130 | return repo; |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 131 | } |
David Pursehouse | 3232472 | 2016-06-01 21:20:02 +0900 | [diff] [blame] | 132 | throw new RepositoryNotFoundException(name.get()); |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 133 | } |
Dave Borowitz | 64c2340 | 2016-07-12 18:21:35 -0400 | [diff] [blame] | 134 | |
| 135 | private static String normalize(Project.NameKey name) { |
| 136 | return name.get().toLowerCase(); |
| 137 | } |
Dave Borowitz | 6445559 | 2013-02-21 12:28:01 -0800 | [diff] [blame] | 138 | } |