Line data Source code
1 : // Copyright (C) 2017 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.group.db.testing; 16 : 17 : import com.google.gerrit.server.config.AllUsersName; 18 : import com.google.gerrit.server.git.GitRepositoryManager; 19 : import org.eclipse.jgit.junit.TestRepository; 20 : import org.eclipse.jgit.lib.PersonIdent; 21 : import org.eclipse.jgit.lib.Ref; 22 : import org.eclipse.jgit.lib.Repository; 23 : import org.eclipse.jgit.revwalk.RevCommit; 24 : import org.eclipse.jgit.revwalk.RevWalk; 25 : 26 : /** Test utilities for low-level NoteDb groups. */ 27 : public class GroupTestUtil { 28 : public static void updateGroupFile( 29 : GitRepositoryManager repoManager, 30 : AllUsersName allUsersName, 31 : PersonIdent serverIdent, 32 : String refName, 33 : String fileName, 34 : String content) 35 : throws Exception { 36 1 : try (Repository repo = repoManager.openRepository(allUsersName)) { 37 1 : updateGroupFile(repo, serverIdent, refName, fileName, content); 38 : } 39 1 : } 40 : 41 : public static void updateGroupFile( 42 : Repository allUsersRepo, 43 : PersonIdent serverIdent, 44 : String refName, 45 : String fileName, 46 : String contents) 47 : throws Exception { 48 2 : try (RevWalk rw = new RevWalk(allUsersRepo); 49 2 : TestRepository<Repository> testRepository = new TestRepository<>(allUsersRepo, rw)) { 50 2 : TestRepository<Repository>.CommitBuilder builder = 51 : testRepository 52 2 : .branch(refName) 53 2 : .commit() 54 2 : .add(fileName, contents) 55 2 : .message("update group file") 56 2 : .author(serverIdent) 57 2 : .committer(serverIdent); 58 : 59 2 : Ref ref = allUsersRepo.exactRef(refName); 60 2 : if (ref != null) { 61 1 : RevCommit c = rw.parseCommit(ref.getObjectId()); 62 1 : if (c != null) { 63 1 : builder.parent(c); 64 : } 65 : } 66 2 : builder.create(); 67 : } 68 2 : } 69 : 70 : private GroupTestUtil() {} 71 : }