| // Copyright (C) 2017 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package com.google.gerrit.server.update; |
| |
| import static org.junit.Assert.assertEquals; |
| |
| import com.google.gerrit.common.TimeUtil; |
| import com.google.gerrit.lifecycle.LifecycleManager; |
| import com.google.gerrit.reviewdb.client.Account; |
| import com.google.gerrit.reviewdb.client.Project; |
| import com.google.gerrit.reviewdb.server.ReviewDb; |
| import com.google.gerrit.server.CurrentUser; |
| import com.google.gerrit.server.IdentifiedUser; |
| import com.google.gerrit.server.account.AccountManager; |
| import com.google.gerrit.server.account.AuthRequest; |
| import com.google.gerrit.server.schema.SchemaCreator; |
| import com.google.gerrit.server.util.RequestContext; |
| import com.google.gerrit.server.util.ThreadLocalRequestContext; |
| import com.google.gerrit.testutil.InMemoryDatabase; |
| import com.google.gerrit.testutil.InMemoryModule; |
| import com.google.gerrit.testutil.InMemoryRepositoryManager; |
| import com.google.gwtorm.server.SchemaFactory; |
| import com.google.inject.Guice; |
| import com.google.inject.Inject; |
| import com.google.inject.Injector; |
| import com.google.inject.Provider; |
| import com.google.inject.util.Providers; |
| import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| import org.eclipse.jgit.junit.TestRepository; |
| import org.eclipse.jgit.revwalk.RevCommit; |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| |
| public class BatchUpdateTest { |
| @Inject private AccountManager accountManager; |
| @Inject private IdentifiedUser.GenericFactory userFactory; |
| @Inject private SchemaFactory<ReviewDb> schemaFactory; |
| @Inject private InMemoryRepositoryManager repoManager; |
| @Inject private SchemaCreator schemaCreator; |
| @Inject private ThreadLocalRequestContext requestContext; |
| @Inject private BatchUpdate.Factory batchUpdateFactory; |
| |
| // Only for use in setting up/tearing down injector; other users should use schemaFactory. |
| @Inject private InMemoryDatabase inMemoryDatabase; |
| |
| private LifecycleManager lifecycle; |
| private ReviewDb db; |
| private TestRepository<InMemoryRepository> repo; |
| private Project.NameKey project; |
| private IdentifiedUser user; |
| |
| @Before |
| public void setUp() throws Exception { |
| Injector injector = Guice.createInjector(new InMemoryModule()); |
| injector.injectMembers(this); |
| lifecycle = new LifecycleManager(); |
| lifecycle.add(injector); |
| lifecycle.start(); |
| |
| try (ReviewDb underlyingDb = inMemoryDatabase.getDatabase().open()) { |
| schemaCreator.create(underlyingDb); |
| } |
| db = schemaFactory.open(); |
| Account.Id userId = accountManager.authenticate(AuthRequest.forUser("user")).getAccountId(); |
| user = userFactory.create(userId); |
| |
| project = new Project.NameKey("test"); |
| |
| InMemoryRepository inMemoryRepo = repoManager.createRepository(project); |
| repo = new TestRepository<>(inMemoryRepo); |
| |
| requestContext.setContext( |
| new RequestContext() { |
| @Override |
| public CurrentUser getUser() { |
| return user; |
| } |
| |
| @Override |
| public Provider<ReviewDb> getReviewDbProvider() { |
| return Providers.of(db); |
| } |
| }); |
| } |
| |
| @After |
| public void tearDown() { |
| if (repo != null) { |
| repo.getRepository().close(); |
| } |
| if (lifecycle != null) { |
| lifecycle.stop(); |
| } |
| requestContext.setContext(null); |
| if (db != null) { |
| db.close(); |
| } |
| InMemoryDatabase.drop(inMemoryDatabase); |
| } |
| |
| @Test |
| public void addRefUpdateFromFastForwardCommit() throws Exception { |
| final RevCommit masterCommit = repo.branch("master").commit().create(); |
| final RevCommit branchCommit = repo.branch("branch").commit().parent(masterCommit).create(); |
| |
| try (BatchUpdate bu = batchUpdateFactory.create(db, project, user, TimeUtil.nowTs())) { |
| bu.addRepoOnlyOp( |
| new RepoOnlyOp() { |
| @Override |
| public void updateRepo(RepoContext ctx) throws Exception { |
| ctx.addRefUpdate(masterCommit.getId(), branchCommit.getId(), "refs/heads/master"); |
| } |
| }); |
| bu.execute(); |
| } |
| |
| assertEquals( |
| repo.getRepository().exactRef("refs/heads/master").getObjectId(), branchCommit.getId()); |
| } |
| } |