PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/javatests/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java

https://gitlab.com/chenfengxu/gerrit
Java | 299 lines | 253 code | 32 blank | 14 comment | 1 complexity | edf909f9eb51816647dcd875dcc1208f MD5 | raw file
  1. // Copyright (C) 2014 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. package com.google.gerrit.server.notedb;
  15. import static com.google.inject.Scopes.SINGLETON;
  16. import static java.util.concurrent.TimeUnit.SECONDS;
  17. import com.google.common.collect.ImmutableList;
  18. import com.google.gerrit.common.TimeUtil;
  19. import com.google.gerrit.common.data.SubmitRecord;
  20. import com.google.gerrit.extensions.config.FactoryModule;
  21. import com.google.gerrit.metrics.DisabledMetricMaker;
  22. import com.google.gerrit.metrics.MetricMaker;
  23. import com.google.gerrit.reviewdb.client.Account;
  24. import com.google.gerrit.reviewdb.client.Change;
  25. import com.google.gerrit.reviewdb.client.Comment;
  26. import com.google.gerrit.reviewdb.client.CommentRange;
  27. import com.google.gerrit.reviewdb.client.PatchSet;
  28. import com.google.gerrit.reviewdb.client.Project;
  29. import com.google.gerrit.reviewdb.server.ReviewDb;
  30. import com.google.gerrit.server.CurrentUser;
  31. import com.google.gerrit.server.GerritPersonIdent;
  32. import com.google.gerrit.server.IdentifiedUser;
  33. import com.google.gerrit.server.InternalUser;
  34. import com.google.gerrit.server.account.AccountCache;
  35. import com.google.gerrit.server.account.FakeRealm;
  36. import com.google.gerrit.server.account.GroupBackend;
  37. import com.google.gerrit.server.account.Realm;
  38. import com.google.gerrit.server.config.AllUsersName;
  39. import com.google.gerrit.server.config.AllUsersNameProvider;
  40. import com.google.gerrit.server.config.AnonymousCowardName;
  41. import com.google.gerrit.server.config.AnonymousCowardNameProvider;
  42. import com.google.gerrit.server.config.CanonicalWebUrl;
  43. import com.google.gerrit.server.config.DisableReverseDnsLookup;
  44. import com.google.gerrit.server.config.GerritServerConfig;
  45. import com.google.gerrit.server.config.GerritServerId;
  46. import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
  47. import com.google.gerrit.server.git.GitModule;
  48. import com.google.gerrit.server.git.GitRepositoryManager;
  49. import com.google.gerrit.server.group.SystemGroupBackend;
  50. import com.google.gerrit.server.project.ProjectCache;
  51. import com.google.gerrit.testing.ConfigSuite;
  52. import com.google.gerrit.testing.FakeAccountCache;
  53. import com.google.gerrit.testing.GerritBaseTests;
  54. import com.google.gerrit.testing.InMemoryRepositoryManager;
  55. import com.google.gerrit.testing.TestChanges;
  56. import com.google.gerrit.testing.TestTimeUtil;
  57. import com.google.gwtorm.server.OrmException;
  58. import com.google.gwtorm.server.SchemaFactory;
  59. import com.google.inject.Guice;
  60. import com.google.inject.Inject;
  61. import com.google.inject.Injector;
  62. import com.google.inject.TypeLiteral;
  63. import com.google.inject.util.Providers;
  64. import java.sql.Timestamp;
  65. import java.util.TimeZone;
  66. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  67. import org.eclipse.jgit.junit.TestRepository;
  68. import org.eclipse.jgit.lib.Config;
  69. import org.eclipse.jgit.lib.PersonIdent;
  70. import org.eclipse.jgit.revwalk.RevWalk;
  71. import org.junit.After;
  72. import org.junit.Before;
  73. import org.junit.Ignore;
  74. import org.junit.runner.RunWith;
  75. @Ignore
  76. @RunWith(ConfigSuite.class)
  77. public abstract class AbstractChangeNotesTest extends GerritBaseTests {
  78. @ConfigSuite.Default
  79. public static Config changeNotesLegacy() {
  80. Config cfg = new Config();
  81. cfg.setBoolean("notedb", null, "writeJson", false);
  82. return cfg;
  83. }
  84. @ConfigSuite.Config
  85. public static Config changeNotesJson() {
  86. Config cfg = new Config();
  87. cfg.setBoolean("notedb", null, "writeJson", true);
  88. return cfg;
  89. }
  90. @ConfigSuite.Parameter public Config testConfig;
  91. private static final TimeZone TZ = TimeZone.getTimeZone("America/Los_Angeles");
  92. protected Account.Id otherUserId;
  93. protected FakeAccountCache accountCache;
  94. protected IdentifiedUser changeOwner;
  95. protected IdentifiedUser otherUser;
  96. protected InMemoryRepository repo;
  97. protected InMemoryRepositoryManager repoManager;
  98. protected PersonIdent serverIdent;
  99. protected InternalUser internalUser;
  100. protected Project.NameKey project;
  101. protected RevWalk rw;
  102. protected TestRepository<InMemoryRepository> tr;
  103. @Inject protected IdentifiedUser.GenericFactory userFactory;
  104. @Inject protected NoteDbUpdateManager.Factory updateManagerFactory;
  105. @Inject protected AllUsersName allUsers;
  106. @Inject protected AbstractChangeNotes.Args args;
  107. @Inject @GerritServerId private String serverId;
  108. protected Injector injector;
  109. private String systemTimeZone;
  110. @Before
  111. public void setUpTestEnvironment() throws Exception {
  112. setTimeForTesting();
  113. serverIdent = new PersonIdent("Gerrit Server", "noreply@gerrit.com", TimeUtil.nowTs(), TZ);
  114. project = new Project.NameKey("test-project");
  115. repoManager = new InMemoryRepositoryManager();
  116. repo = repoManager.createRepository(project);
  117. tr = new TestRepository<>(repo);
  118. rw = tr.getRevWalk();
  119. accountCache = new FakeAccountCache();
  120. Account co = new Account(new Account.Id(1), TimeUtil.nowTs());
  121. co.setFullName("Change Owner");
  122. co.setPreferredEmail("change@owner.com");
  123. accountCache.put(co);
  124. Account ou = new Account(new Account.Id(2), TimeUtil.nowTs());
  125. ou.setFullName("Other Account");
  126. ou.setPreferredEmail("other@account.com");
  127. accountCache.put(ou);
  128. injector =
  129. Guice.createInjector(
  130. new FactoryModule() {
  131. @Override
  132. public void configure() {
  133. install(new GitModule());
  134. install(NoteDbModule.forTest(testConfig));
  135. bind(AllUsersName.class).toProvider(AllUsersNameProvider.class);
  136. bind(String.class).annotatedWith(GerritServerId.class).toInstance("gerrit");
  137. bind(GitRepositoryManager.class).toInstance(repoManager);
  138. bind(ProjectCache.class).toProvider(Providers.<ProjectCache>of(null));
  139. bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(testConfig);
  140. bind(String.class)
  141. .annotatedWith(AnonymousCowardName.class)
  142. .toProvider(AnonymousCowardNameProvider.class);
  143. bind(String.class)
  144. .annotatedWith(CanonicalWebUrl.class)
  145. .toInstance("http://localhost:8080/");
  146. bind(Boolean.class)
  147. .annotatedWith(DisableReverseDnsLookup.class)
  148. .toInstance(Boolean.FALSE);
  149. bind(Realm.class).to(FakeRealm.class);
  150. bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON);
  151. bind(AccountCache.class).toInstance(accountCache);
  152. bind(PersonIdent.class)
  153. .annotatedWith(GerritPersonIdent.class)
  154. .toInstance(serverIdent);
  155. bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
  156. bind(MetricMaker.class).to(DisabledMetricMaker.class);
  157. bind(ReviewDb.class).toProvider(Providers.<ReviewDb>of(null));
  158. MutableNotesMigration migration = MutableNotesMigration.newDisabled();
  159. migration.setFrom(NotesMigrationState.FINAL);
  160. bind(MutableNotesMigration.class).toInstance(migration);
  161. bind(NotesMigration.class).to(MutableNotesMigration.class);
  162. // Tests don't support ReviewDb at all, but bindings are required via NoteDbModule.
  163. bind(new TypeLiteral<SchemaFactory<ReviewDb>>() {})
  164. .toInstance(
  165. () -> {
  166. throw new UnsupportedOperationException();
  167. });
  168. bind(ChangeBundleReader.class)
  169. .toInstance(
  170. (db, id) -> {
  171. throw new UnsupportedOperationException();
  172. });
  173. }
  174. });
  175. injector.injectMembers(this);
  176. repoManager.createRepository(allUsers);
  177. changeOwner = userFactory.create(co.getId());
  178. otherUser = userFactory.create(ou.getId());
  179. otherUserId = otherUser.getAccountId();
  180. internalUser = new InternalUser();
  181. }
  182. private void setTimeForTesting() {
  183. systemTimeZone = System.setProperty("user.timezone", "US/Eastern");
  184. TestTimeUtil.resetWithClockStep(1, SECONDS);
  185. }
  186. @After
  187. public void resetTime() {
  188. TestTimeUtil.useSystemTime();
  189. System.setProperty("user.timezone", systemTimeZone);
  190. }
  191. protected Change newChange(boolean workInProgress) throws Exception {
  192. Change c = TestChanges.newChange(project, changeOwner.getAccountId());
  193. ChangeUpdate u = newUpdate(c, changeOwner);
  194. u.setChangeId(c.getKey().get());
  195. u.setBranch(c.getDest().get());
  196. u.setWorkInProgress(workInProgress);
  197. u.commit();
  198. return c;
  199. }
  200. protected Change newWorkInProgressChange() throws Exception {
  201. return newChange(true);
  202. }
  203. protected Change newChange() throws Exception {
  204. return newChange(false);
  205. }
  206. protected ChangeUpdate newUpdate(Change c, CurrentUser user) throws Exception {
  207. ChangeUpdate update = TestChanges.newUpdate(injector, c, user);
  208. update.setPatchSetId(c.currentPatchSetId());
  209. update.setAllowWriteToNewRef(true);
  210. return update;
  211. }
  212. protected ChangeNotes newNotes(Change c) throws OrmException {
  213. return new ChangeNotes(args, c).load();
  214. }
  215. protected static SubmitRecord submitRecord(
  216. String status, String errorMessage, SubmitRecord.Label... labels) {
  217. SubmitRecord rec = new SubmitRecord();
  218. rec.status = SubmitRecord.Status.valueOf(status);
  219. rec.errorMessage = errorMessage;
  220. if (labels.length > 0) {
  221. rec.labels = ImmutableList.copyOf(labels);
  222. }
  223. return rec;
  224. }
  225. protected static SubmitRecord.Label submitLabel(
  226. String name, String status, Account.Id appliedBy) {
  227. SubmitRecord.Label label = new SubmitRecord.Label();
  228. label.label = name;
  229. label.status = SubmitRecord.Label.Status.valueOf(status);
  230. label.appliedBy = appliedBy;
  231. return label;
  232. }
  233. protected Comment newComment(
  234. PatchSet.Id psId,
  235. String filename,
  236. String UUID,
  237. CommentRange range,
  238. int line,
  239. IdentifiedUser commenter,
  240. String parentUUID,
  241. Timestamp t,
  242. String message,
  243. short side,
  244. String commitSHA1,
  245. boolean unresolved) {
  246. Comment c =
  247. new Comment(
  248. new Comment.Key(UUID, filename, psId.get()),
  249. commenter.getAccountId(),
  250. t,
  251. side,
  252. message,
  253. serverId,
  254. unresolved);
  255. c.lineNbr = line;
  256. c.parentUuid = parentUUID;
  257. c.revId = commitSHA1;
  258. c.setRange(range);
  259. return c;
  260. }
  261. protected static Timestamp truncate(Timestamp ts) {
  262. return new Timestamp((ts.getTime() / 1000) * 1000);
  263. }
  264. protected static Timestamp after(Change c, long millis) {
  265. return new Timestamp(c.getCreatedOn().getTime() + millis);
  266. }
  267. }