Line data Source code
1 : // Copyright (C) 2019 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.server.git.receive.testing; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.annotations.VisibleForTesting; 21 : import com.google.common.base.Splitter; 22 : import com.google.common.collect.ImmutableMap; 23 : import com.google.common.collect.ImmutableSet; 24 : import java.io.IOException; 25 : import java.util.HashMap; 26 : import java.util.HashSet; 27 : import java.util.List; 28 : import java.util.Map; 29 : import java.util.Set; 30 : import java.util.stream.StreamSupport; 31 : import org.eclipse.jgit.lib.ObjectId; 32 : import org.eclipse.jgit.lib.Ref; 33 : import org.eclipse.jgit.lib.Repository; 34 : import org.eclipse.jgit.transport.RefAdvertiser; 35 : 36 : /** Helper to collect advertised refs and additonal haves and verify them in tests. */ 37 : public class TestRefAdvertiser extends RefAdvertiser { 38 : 39 : @VisibleForTesting 40 : @AutoValue 41 1 : public abstract static class Result { 42 : public abstract ImmutableMap<String, Ref> allRefs(); 43 : 44 : public abstract ImmutableSet<ObjectId> additionalHaves(); 45 : 46 : public static Result create(Map<String, Ref> allRefs, Set<ObjectId> additionalHaves) { 47 1 : return new AutoValue_TestRefAdvertiser_Result( 48 1 : ImmutableMap.copyOf(allRefs), ImmutableSet.copyOf(additionalHaves)); 49 : } 50 : } 51 : 52 : private final Map<String, Ref> advertisedRefs; 53 : private final Set<ObjectId> additionalHaves; 54 : private final Repository repo; 55 : 56 1 : public TestRefAdvertiser(Repository repo) { 57 1 : advertisedRefs = new HashMap<>(); 58 1 : additionalHaves = new HashSet<>(); 59 1 : this.repo = repo; 60 1 : } 61 : 62 : @Override 63 : protected void writeOne(CharSequence line) throws IOException { 64 1 : List<String> lineParts = 65 1 : StreamSupport.stream(Splitter.on(' ').split(line).spliterator(), false) 66 1 : .map(String::trim) 67 1 : .collect(toImmutableList()); 68 1 : if (".have".equals(lineParts.get(1))) { 69 1 : additionalHaves.add(ObjectId.fromString(lineParts.get(0))); 70 : } else { 71 1 : ObjectId id = ObjectId.fromString(lineParts.get(0)); 72 1 : Ref ref = 73 1 : repo.getRefDatabase().getRefs().stream() 74 1 : .filter(r -> r.getObjectId().equals(id)) 75 1 : .findAny() 76 1 : .orElseThrow( 77 : () -> 78 0 : new RuntimeException( 79 0 : line.toString() + " does not conform to expected pattern")); 80 1 : advertisedRefs.put(lineParts.get(1), ref); 81 : } 82 1 : } 83 : 84 : @Override 85 1 : protected void end() {} 86 : 87 : public Result result() { 88 1 : return Result.create(advertisedRefs, additionalHaves); 89 : } 90 : }