LCOV - code coverage report
Current view: top level - testing - InMemoryRepositoryManager.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 36 41 87.8 %
Date: 2022-11-19 15:00:39 Functions: 12 15 80.0 %

          Line data    Source code
       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             : 
      15             : package com.google.gerrit.testing;
      16             : 
      17             : import com.google.common.collect.Sets;
      18             : import com.google.gerrit.entities.Project;
      19             : import com.google.gerrit.entities.Project.NameKey;
      20             : import com.google.gerrit.server.git.GitRepositoryManager;
      21             : import com.google.gerrit.server.git.RepositoryCaseMismatchException;
      22             : import com.google.inject.Inject;
      23             : import java.util.Collections;
      24             : import java.util.HashMap;
      25             : import java.util.Map;
      26             : import java.util.NavigableSet;
      27             : import org.eclipse.jgit.errors.RepositoryNotFoundException;
      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;
      31             : 
      32             : /** Repository manager that uses in-memory repositories. */
      33             : public class InMemoryRepositoryManager implements GitRepositoryManager {
      34             :   public static InMemoryRepository newRepository(Project.NameKey name) {
      35           0 :     return new Repo(name);
      36             :   }
      37             : 
      38             :   public static class Description extends DfsRepositoryDescription {
      39             :     private final Project.NameKey name;
      40             : 
      41             :     private Description(Project.NameKey name) {
      42         147 :       super(name.get());
      43         147 :       this.name = name;
      44         147 :     }
      45             : 
      46             :     public Project.NameKey getProject() {
      47           1 :       return name;
      48             :     }
      49             :   }
      50             : 
      51             :   public static class Repo extends InMemoryRepository {
      52             :     private String description;
      53             : 
      54             :     private Repo(Project.NameKey name) {
      55         147 :       super(new Description(name));
      56         147 :       setPerformsAtomicTransactions(true);
      57         147 :     }
      58             : 
      59             :     @Override
      60             :     public Description getDescription() {
      61         147 :       return (Description) super.getDescription();
      62             :     }
      63             : 
      64             :     @Override
      65             :     public String getGitwebDescription() {
      66           0 :       return description;
      67             :     }
      68             : 
      69             :     @Override
      70             :     public void setGitwebDescription(String d) {
      71         141 :       description = d;
      72         141 :     }
      73             :   }
      74             : 
      75             :   private final Map<String, Repo> repos;
      76             : 
      77             :   @Inject
      78         148 :   public InMemoryRepositoryManager() {
      79         148 :     this.repos = new HashMap<>();
      80         148 :   }
      81             : 
      82             :   @Override
      83             :   public synchronized Status getRepositoryStatus(NameKey name) {
      84             :     try {
      85           1 :       get(name);
      86           1 :       return Status.ACTIVE;
      87         141 :     } catch (RepositoryNotFoundException e) {
      88         141 :       return Status.NON_EXISTENT;
      89             :     }
      90             :   }
      91             : 
      92             :   @Override
      93             :   public synchronized Repo openRepository(Project.NameKey name) throws RepositoryNotFoundException {
      94         146 :     return get(name);
      95             :   }
      96             : 
      97             :   @Override
      98             :   public synchronized Repo createRepository(Project.NameKey name)
      99             :       throws RepositoryCaseMismatchException, RepositoryNotFoundException {
     100             :     Repo repo;
     101             :     try {
     102           1 :       repo = get(name);
     103           1 :       if (!repo.getDescription().getRepositoryName().equals(name.get())) {
     104           0 :         throw new RepositoryCaseMismatchException(name);
     105             :       }
     106         147 :     } catch (RepositoryNotFoundException e) {
     107         147 :       repo = new Repo(name);
     108         147 :       repos.put(normalize(name), repo);
     109           1 :     }
     110         147 :     return repo;
     111             :   }
     112             : 
     113             :   @Override
     114             :   public synchronized NavigableSet<Project.NameKey> list() {
     115         141 :     NavigableSet<Project.NameKey> names = Sets.newTreeSet();
     116         141 :     for (DfsRepository repo : repos.values()) {
     117         141 :       names.add(Project.nameKey(repo.getDescription().getRepositoryName()));
     118         141 :     }
     119         141 :     return Collections.unmodifiableNavigableSet(names);
     120             :   }
     121             : 
     122             :   public synchronized void deleteRepository(Project.NameKey name) {
     123           0 :     repos.remove(normalize(name));
     124           0 :   }
     125             : 
     126             :   private synchronized Repo get(Project.NameKey name) throws RepositoryNotFoundException {
     127         147 :     Repo repo = repos.get(normalize(name));
     128         147 :     if (repo != null) {
     129         146 :       repo.incrementOpen();
     130         146 :       return repo;
     131             :     }
     132         147 :     throw new RepositoryNotFoundException(name.get());
     133             :   }
     134             : 
     135             :   private static String normalize(Project.NameKey name) {
     136         147 :     return name.get().toLowerCase();
     137             :   }
     138             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750