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.git.testing; 16 : 17 : import static com.google.common.truth.Truth.assertAbout; 18 : import static java.util.concurrent.TimeUnit.SECONDS; 19 : 20 : import com.google.common.truth.FailureMetadata; 21 : import com.google.common.truth.Subject; 22 : import java.sql.Timestamp; 23 : import org.eclipse.jgit.lib.ObjectId; 24 : import org.eclipse.jgit.revwalk.RevCommit; 25 : 26 : /** Subject over JGit {@link RevCommit}s. */ 27 : public class CommitSubject extends Subject { 28 : 29 : /** 30 : * Constructs a new subject. 31 : * 32 : * @param commit the commit. 33 : * @return a new subject over the commit. 34 : */ 35 : public static CommitSubject assertThat(RevCommit commit) { 36 0 : return assertAbout(CommitSubject::new).that(commit); 37 : } 38 : 39 : /** 40 : * Performs some common assertions over a single commit. 41 : * 42 : * @param commit the commit. 43 : * @param expectedCommitMessage exact expected commit message. 44 : * @param expectedCommitTimestamp expected commit timestamp, to the tolerance specified in {@link 45 : * #hasCommitTimestamp(Timestamp)}. 46 : * @param expectedSha1 expected commit SHA-1. 47 : */ 48 : public static void assertCommit( 49 : RevCommit commit, 50 : String expectedCommitMessage, 51 : Timestamp expectedCommitTimestamp, 52 : ObjectId expectedSha1) { 53 0 : CommitSubject commitSubject = assertThat(commit); 54 0 : commitSubject.hasCommitMessage(expectedCommitMessage); 55 0 : commitSubject.hasCommitTimestamp(expectedCommitTimestamp); 56 0 : commitSubject.hasSha1(expectedSha1); 57 0 : } 58 : 59 : private final RevCommit commit; 60 : 61 : private CommitSubject(FailureMetadata metadata, RevCommit commit) { 62 0 : super(metadata, commit); 63 0 : this.commit = commit; 64 0 : } 65 : 66 : /** 67 : * Asserts that the commit has the given commit message. 68 : * 69 : * @param expectedCommitMessage exact expected commit message. 70 : */ 71 : public void hasCommitMessage(String expectedCommitMessage) { 72 0 : isNotNull(); 73 0 : check("getFullMessage()").that(commit.getFullMessage()).isEqualTo(expectedCommitMessage); 74 0 : } 75 : 76 : /** 77 : * Asserts that the commit has the given commit message, up to skew of at most 1 second. 78 : * 79 : * @param expectedCommitTimestamp expected commit timestamp. 80 : */ 81 : public void hasCommitTimestamp(Timestamp expectedCommitTimestamp) { 82 0 : isNotNull(); 83 0 : long timestampDiffMs = 84 0 : Math.abs(commit.getCommitTime() * 1000L - expectedCommitTimestamp.getTime()); 85 0 : check("commitTimestampDiff()").that(timestampDiffMs).isAtMost(SECONDS.toMillis(1)); 86 0 : } 87 : 88 : /** 89 : * Asserts that the commit has the given SHA-1. 90 : * 91 : * @param expectedSha1 expected commit SHA-1. 92 : */ 93 : public void hasSha1(ObjectId expectedSha1) { 94 0 : isNotNull(); 95 0 : check("sha1()").that(commit).isEqualTo(expectedSha1); 96 0 : } 97 : }