Extract base class from ChangeNotesTest Change-Id: I037c2b8d9f80413a313056c13efd47c493b1339a
diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java new file mode 100644 index 0000000..415825b --- /dev/null +++ b/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java
@@ -0,0 +1,231 @@ +// Copyright (C) 2014 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.notedb; + +import static com.google.inject.Scopes.SINGLETON; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import com.google.common.collect.ImmutableList; +import com.google.gerrit.common.data.SubmitRecord; +import com.google.gerrit.reviewdb.client.Account; +import com.google.gerrit.reviewdb.client.Change; +import com.google.gerrit.reviewdb.client.CommentRange; +import com.google.gerrit.reviewdb.client.Patch; +import com.google.gerrit.reviewdb.client.PatchLineComment; +import com.google.gerrit.reviewdb.client.PatchSet; +import com.google.gerrit.reviewdb.client.Project; +import com.google.gerrit.reviewdb.client.RevId; +import com.google.gerrit.server.GerritPersonIdent; +import com.google.gerrit.server.IdentifiedUser; +import com.google.gerrit.server.account.AccountCache; +import com.google.gerrit.server.account.CapabilityControl; +import com.google.gerrit.server.account.GroupBackend; +import com.google.gerrit.server.account.Realm; +import com.google.gerrit.server.config.AllUsersNameProvider; +import com.google.gerrit.server.config.AnonymousCowardName; +import com.google.gerrit.server.config.AnonymousCowardNameProvider; +import com.google.gerrit.server.config.CanonicalWebUrl; +import com.google.gerrit.server.config.FactoryModule; +import com.google.gerrit.server.config.GerritServerConfig; +import com.google.gerrit.server.extensions.events.GitReferenceUpdated; +import com.google.gerrit.server.git.GitModule; +import com.google.gerrit.server.git.GitRepositoryManager; +import com.google.gerrit.server.group.SystemGroupBackend; +import com.google.gerrit.server.project.ProjectCache; +import com.google.gerrit.server.util.TimeUtil; +import com.google.gerrit.testutil.FakeAccountCache; +import com.google.gerrit.testutil.FakeRealm; +import com.google.gerrit.testutil.InMemoryRepositoryManager; +import com.google.gerrit.testutil.TestChanges; +import com.google.gwtorm.client.KeyUtil; +import com.google.gwtorm.server.OrmException; +import com.google.gwtorm.server.StandardKeyEncoder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.util.Providers; + +import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.lib.PersonIdent; +import org.joda.time.DateTime; +import org.joda.time.DateTimeUtils; +import org.joda.time.DateTimeUtils.MillisProvider; +import org.junit.After; +import org.junit.Before; + +import java.sql.Timestamp; +import java.util.TimeZone; +import java.util.concurrent.atomic.AtomicLong; + +public class AbstractChangeNotesTest { + private static final TimeZone TZ = + TimeZone.getTimeZone("America/Los_Angeles"); + + protected Account.Id otherUserId; + protected FakeAccountCache accountCache; + protected IdentifiedUser changeOwner; + protected IdentifiedUser.GenericFactory userFactory; + protected IdentifiedUser otherUser; + protected InMemoryRepository repo; + protected PersonIdent serverIdent; + protected Project.NameKey project; + + private AllUsersNameProvider allUsers; + private Injector injector; + private InMemoryRepositoryManager repoManager; + private String systemTimeZone; + private volatile long clockStepMs; + + @Before + public void setUp() throws Exception { + setTimeForTesting(); + KeyUtil.setEncoderImpl(new StandardKeyEncoder()); + + serverIdent = new PersonIdent( + "Gerrit Server", "noreply@gerrit.com", TimeUtil.nowTs(), TZ); + project = new Project.NameKey("test-project"); + repoManager = new InMemoryRepositoryManager(); + repo = repoManager.createRepository(project); + accountCache = new FakeAccountCache(); + Account co = new Account(new Account.Id(1), TimeUtil.nowTs()); + co.setFullName("Change Owner"); + co.setPreferredEmail("change@owner.com"); + accountCache.put(co); + Account ou = new Account(new Account.Id(2), TimeUtil.nowTs()); + ou.setFullName("Other Account"); + ou.setPreferredEmail("other@account.com"); + accountCache.put(ou); + + injector = Guice.createInjector(new FactoryModule() { + @Override + public void configure() { + install(new GitModule()); + bind(NotesMigration.class).toInstance(NotesMigration.allEnabled()); + bind(GitRepositoryManager.class).toInstance(repoManager); + bind(ProjectCache.class).toProvider(Providers.<ProjectCache> of(null)); + bind(CapabilityControl.Factory.class) + .toProvider(Providers.<CapabilityControl.Factory> of(null)); + bind(Config.class).annotatedWith(GerritServerConfig.class) + .toInstance(new Config()); + bind(String.class).annotatedWith(AnonymousCowardName.class) + .toProvider(AnonymousCowardNameProvider.class); + bind(String.class).annotatedWith(CanonicalWebUrl.class) + .toInstance("http://localhost:8080/"); + bind(Realm.class).to(FakeRealm.class); + bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON); + bind(AccountCache.class).toInstance(accountCache); + bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class) + .toInstance(serverIdent); + bind(GitReferenceUpdated.class) + .toInstance(GitReferenceUpdated.DISABLED); + } + }); + + userFactory = injector.getInstance(IdentifiedUser.GenericFactory.class); + allUsers = injector.getInstance(AllUsersNameProvider.class); + repoManager.createRepository(allUsers.get()); + changeOwner = userFactory.create(co.getId()); + otherUser = userFactory.create(ou.getId()); + otherUserId = otherUser.getAccountId(); + } + + private void setTimeForTesting() { + systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); + clockStepMs = MILLISECONDS.convert(1, SECONDS); + final AtomicLong clockMs = new AtomicLong( + new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); + + DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { + @Override + public long getMillis() { + return clockMs.getAndAdd(clockStepMs); + } + }); + } + + @After + public void resetTime() { + DateTimeUtils.setCurrentMillisSystem(); + System.setProperty("user.timezone", systemTimeZone); + } + + protected Change newChange() { + return TestChanges.newChange(project, changeOwner); + } + + protected ChangeUpdate newUpdate(Change c, IdentifiedUser user) + throws OrmException { + return TestChanges.newUpdate(injector, repoManager, c, allUsers, user); + } + + protected ChangeNotes newNotes(Change c) throws OrmException { + return new ChangeNotes(repoManager, allUsers, c).load(); + } + + protected static SubmitRecord submitRecord(String status, + String errorMessage, SubmitRecord.Label... labels) { + SubmitRecord rec = new SubmitRecord(); + rec.status = SubmitRecord.Status.valueOf(status); + rec.errorMessage = errorMessage; + if (labels.length > 0) { + rec.labels = ImmutableList.copyOf(labels); + } + return rec; + } + + protected static SubmitRecord.Label submitLabel(String name, String status, + Account.Id appliedBy) { + SubmitRecord.Label label = new SubmitRecord.Label(); + label.label = name; + label.status = SubmitRecord.Label.Status.valueOf(status); + label.appliedBy = appliedBy; + return label; + } + + protected PatchLineComment newPublishedPatchLineComment(PatchSet.Id psId, + String filename, String UUID, CommentRange range, int line, + IdentifiedUser commenter, String parentUUID, Timestamp t, + String message, short side, String commitSHA1) { + return newPatchLineComment(psId, filename, UUID, range, line, commenter, + parentUUID, t, message, side, commitSHA1, + PatchLineComment.Status.PUBLISHED); + } + + protected PatchLineComment newPatchLineComment(PatchSet.Id psId, + String filename, String UUID, CommentRange range, int line, + IdentifiedUser commenter, String parentUUID, Timestamp t, + String message, short side, String commitSHA1, + PatchLineComment.Status status) { + PatchLineComment comment = new PatchLineComment( + new PatchLineComment.Key( + new Patch.Key(psId, filename), UUID), + line, commenter.getAccountId(), parentUUID, t); + comment.setSide(side); + comment.setMessage(message); + comment.setRange(range); + comment.setRevId(new RevId(commitSHA1)); + comment.setStatus(status); + return comment; + } + + protected static Timestamp truncate(Timestamp ts) { + return new Timestamp((ts.getTime() / 1000) * 1000); + } + + protected static Timestamp after(Change c, long millis) { + return new Timestamp(c.getCreatedOn().getTime() + millis); + } +}
diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/notedb/ChangeNotesTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/notedb/ChangeNotesTest.java index 53735c6..ba039a3 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/notedb/ChangeNotesTest.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/notedb/ChangeNotesTest.java
@@ -17,10 +17,7 @@ import static com.google.gerrit.server.notedb.ReviewerState.CC; import static com.google.gerrit.server.notedb.ReviewerState.REVIEWER; import static com.google.gerrit.testutil.TestChanges.incrementPatchSet; -import static com.google.inject.Scopes.SINGLETON; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -38,47 +35,17 @@ import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.ChangeMessage; import com.google.gerrit.reviewdb.client.CommentRange; -import com.google.gerrit.reviewdb.client.Patch; import com.google.gerrit.reviewdb.client.PatchLineComment; import com.google.gerrit.reviewdb.client.PatchLineComment.Status; import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.PatchSetApproval; -import com.google.gerrit.reviewdb.client.Project; -import com.google.gerrit.reviewdb.client.RevId; -import com.google.gerrit.server.GerritPersonIdent; -import com.google.gerrit.server.IdentifiedUser; -import com.google.gerrit.server.account.AccountCache; -import com.google.gerrit.server.account.CapabilityControl; -import com.google.gerrit.server.account.GroupBackend; -import com.google.gerrit.server.account.Realm; -import com.google.gerrit.server.config.AllUsersNameProvider; -import com.google.gerrit.server.config.AnonymousCowardName; -import com.google.gerrit.server.config.AnonymousCowardNameProvider; -import com.google.gerrit.server.config.CanonicalWebUrl; -import com.google.gerrit.server.config.FactoryModule; -import com.google.gerrit.server.config.GerritServerConfig; -import com.google.gerrit.server.extensions.events.GitReferenceUpdated; -import com.google.gerrit.server.git.GitModule; -import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.VersionedMetaData.BatchMetaDataUpdate; -import com.google.gerrit.server.group.SystemGroupBackend; -import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.util.TimeUtil; -import com.google.gerrit.testutil.FakeAccountCache; -import com.google.gerrit.testutil.FakeRealm; -import com.google.gerrit.testutil.InMemoryRepositoryManager; import com.google.gerrit.testutil.TestChanges; -import com.google.gwtorm.client.KeyUtil; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.StandardKeyEncoder; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.util.Providers; -import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.lib.BatchRefUpdate; import org.eclipse.jgit.lib.CommitBuilder; -import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.PersonIdent; @@ -86,11 +53,6 @@ import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.transport.ReceiveCommand; -import org.joda.time.DateTime; -import org.joda.time.DateTimeUtils; -import org.joda.time.DateTimeUtils.MillisProvider; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import java.io.IOException; @@ -99,99 +61,8 @@ import java.util.Date; import java.util.List; import java.util.TimeZone; -import java.util.concurrent.atomic.AtomicLong; -public class ChangeNotesTest { - private static final TimeZone TZ = - TimeZone.getTimeZone("America/Los_Angeles"); - - private PersonIdent serverIdent; - private Project.NameKey project; - private InMemoryRepositoryManager repoManager; - private InMemoryRepository repo; - private FakeAccountCache accountCache; - private IdentifiedUser.GenericFactory userFactory; - private AllUsersNameProvider allUsers; - private IdentifiedUser changeOwner; - private IdentifiedUser otherUser; - private Account.Id otherUserId; - private Injector injector; - private String systemTimeZone; - private volatile long clockStepMs; - - @Before - public void setUp() throws Exception { - setTimeForTesting(); - KeyUtil.setEncoderImpl(new StandardKeyEncoder()); - - serverIdent = new PersonIdent( - "Gerrit Server", "noreply@gerrit.com", TimeUtil.nowTs(), TZ); - project = new Project.NameKey("test-project"); - repoManager = new InMemoryRepositoryManager(); - repo = repoManager.createRepository(project); - accountCache = new FakeAccountCache(); - Account co = new Account(new Account.Id(1), TimeUtil.nowTs()); - co.setFullName("Change Owner"); - co.setPreferredEmail("change@owner.com"); - accountCache.put(co); - Account ou = new Account(new Account.Id(2), TimeUtil.nowTs()); - ou.setFullName("Other Account"); - ou.setPreferredEmail("other@account.com"); - accountCache.put(ou); - - injector = Guice.createInjector(new FactoryModule() { - @Override - public void configure() { - install(new GitModule()); - bind(NotesMigration.class).toInstance(NotesMigration.allEnabled()); - bind(GitRepositoryManager.class).toInstance(repoManager); - bind(ProjectCache.class).toProvider(Providers.<ProjectCache> of(null)); - bind(CapabilityControl.Factory.class) - .toProvider(Providers.<CapabilityControl.Factory> of(null)); - bind(Config.class).annotatedWith(GerritServerConfig.class) - .toInstance(new Config()); - bind(String.class).annotatedWith(AnonymousCowardName.class) - .toProvider(AnonymousCowardNameProvider.class); - bind(String.class).annotatedWith(CanonicalWebUrl.class) - .toInstance("http://localhost:8080/"); - bind(Realm.class).to(FakeRealm.class); - bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON); - bind(AccountCache.class).toInstance(accountCache); - bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class) - .toInstance(serverIdent); - bind(GitReferenceUpdated.class) - .toInstance(GitReferenceUpdated.DISABLED); - } - }); - - userFactory = injector.getInstance(IdentifiedUser.GenericFactory.class); - allUsers = injector.getInstance(AllUsersNameProvider.class); - repoManager.createRepository(allUsers.get()); - changeOwner = userFactory.create(co.getId()); - otherUser = userFactory.create(ou.getId()); - otherUserId = otherUser.getAccountId(); - } - - private void setTimeForTesting() { - systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); - clockStepMs = MILLISECONDS.convert(1, SECONDS); - final AtomicLong clockMs = new AtomicLong( - new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); - - DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { - @Override - public long getMillis() { - return clockMs.getAndAdd(clockStepMs); - } - }); - } - - @After - public void resetTime() { - DateTimeUtils.setCurrentMillisSystem(); - System.setProperty("user.timezone", systemTimeZone); - } - +public class ChangeNotesTest extends AbstractChangeNotesTest { @Test public void approvalsCommitFormatSimple() throws Exception { Change c = TestChanges.newChange(project, changeOwner, 1); @@ -1457,69 +1328,4 @@ assertEquals(commentForBase, Iterables.getOnlyElement(commentsForBase.get(psId))); } - - private Change newChange() { - return TestChanges.newChange(project, changeOwner); - } - - private PatchLineComment newPublishedPatchLineComment(PatchSet.Id psId, - String filename, String UUID, CommentRange range, int line, - IdentifiedUser commenter, String parentUUID, Timestamp t, - String message, short side, String commitSHA1) { - return newPatchLineComment(psId, filename, UUID, range, line, commenter, - parentUUID, t, message, side, commitSHA1, Status.PUBLISHED); - } - - private PatchLineComment newPatchLineComment(PatchSet.Id psId, - String filename, String UUID, CommentRange range, int line, - IdentifiedUser commenter, String parentUUID, Timestamp t, - String message, short side, String commitSHA1, Status status) { - PatchLineComment comment = new PatchLineComment( - new PatchLineComment.Key( - new Patch.Key(psId, filename), UUID), - line, commenter.getAccountId(), parentUUID, t); - comment.setSide(side); - comment.setMessage(message); - comment.setRange(range); - comment.setRevId(new RevId(commitSHA1)); - comment.setStatus(status); - return comment; - } - - private ChangeUpdate newUpdate(Change c, IdentifiedUser user) - throws OrmException { - return TestChanges.newUpdate(injector, repoManager, c, allUsers, user); - } - - private ChangeNotes newNotes(Change c) throws OrmException { - return new ChangeNotes(repoManager, allUsers, c).load(); - } - - private static Timestamp truncate(Timestamp ts) { - return new Timestamp((ts.getTime() / 1000) * 1000); - } - - private static Timestamp after(Change c, long millis) { - return new Timestamp(c.getCreatedOn().getTime() + millis); - } - - private static SubmitRecord submitRecord(String status, - String errorMessage, SubmitRecord.Label... labels) { - SubmitRecord rec = new SubmitRecord(); - rec.status = SubmitRecord.Status.valueOf(status); - rec.errorMessage = errorMessage; - if (labels.length > 0) { - rec.labels = ImmutableList.copyOf(labels); - } - return rec; - } - - private static SubmitRecord.Label submitLabel(String name, String status, - Account.Id appliedBy) { - SubmitRecord.Label label = new SubmitRecord.Label(); - label.label = name; - label.status = SubmitRecord.Label.Status.valueOf(status); - label.appliedBy = appliedBy; - return label; - } }