summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/server/change/IncludedInResolverTest.java
blob: dca2dcb23bafede4706f784c8eb9cef7d6a20612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2013 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.change;

import static com.google.common.truth.Truth.assertThat;

import java.io.IOException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class IncludedInResolverTest extends RepositoryTestCase {

  // Branch names
  private static final String BRANCH_MASTER = "master";
  private static final String BRANCH_1_0 = "rel-1.0";
  private static final String BRANCH_1_3 = "rel-1.3";
  private static final String BRANCH_2_0 = "rel-2.0";
  private static final String BRANCH_2_5 = "rel-2.5";

  // Tag names
  private static final String TAG_1_0 = "1.0";
  private static final String TAG_1_0_1 = "1.0.1";
  private static final String TAG_1_3 = "1.3";
  private static final String TAG_2_0_1 = "2.0.1";
  private static final String TAG_2_0 = "2.0";
  private static final String TAG_2_5 = "2.5";
  private static final String TAG_2_5_ANNOTATED = "2.5-annotated";
  private static final String TAG_2_5_ANNOTATED_TWICE = "2.5-annotated_twice";

  // Commits
  private RevCommit commit_initial;
  private RevCommit commit_v1_3;
  private RevCommit commit_v2_5;

  private RevWalk revWalk;

  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    /*- The following graph will be created.

     o   tag 2.5, 2.5_annotated, 2.5_annotated_twice
     |\
     | o tag 2.0.1
     | o tag 2.0
     o | tag 1.3
     |/
     o   c3

     | o tag 1.0.1
     |/
     o   tag 1.0
     o   c2
     o   c1

    */

    // TODO(dborowitz): Use try/finally when this doesn't double-close the repo.
    @SuppressWarnings("resource")
    Git git = new Git(db);
    revWalk = new RevWalk(db);
    // Version 1.0
    commit_initial = git.commit().setMessage("c1").call();
    git.commit().setMessage("c2").call();
    RevCommit commit_v1_0 = git.commit().setMessage("version 1.0").call();
    git.tag().setName(TAG_1_0).setObjectId(commit_v1_0).call();
    RevCommit c3 = git.commit().setMessage("c3").call();
    // Version 1.01
    createAndCheckoutBranch(commit_v1_0, BRANCH_1_0);
    RevCommit commit_v1_0_1 = git.commit().setMessage("verREFS_HEADS_RELsion 1.0.1").call();
    git.tag().setName(TAG_1_0_1).setObjectId(commit_v1_0_1).call();
    // Version 1.3
    createAndCheckoutBranch(c3, BRANCH_1_3);
    commit_v1_3 = git.commit().setMessage("version 1.3").call();
    git.tag().setName(TAG_1_3).setObjectId(commit_v1_3).call();
    // Version 2.0
    createAndCheckoutBranch(c3, BRANCH_2_0);
    RevCommit commit_v2_0 = git.commit().setMessage("version 2.0").call();
    git.tag().setName(TAG_2_0).setObjectId(commit_v2_0).call();
    RevCommit commit_v2_0_1 = git.commit().setMessage("version 2.0.1").call();
    git.tag().setName(TAG_2_0_1).setObjectId(commit_v2_0_1).call();

    // Version 2.5
    createAndCheckoutBranch(commit_v1_3, BRANCH_2_5);
    git.merge()
        .include(commit_v2_0_1)
        .setCommit(false)
        .setFastForward(FastForwardMode.NO_FF)
        .call();
    commit_v2_5 = git.commit().setMessage("version 2.5").call();
    git.tag().setName(TAG_2_5).setObjectId(commit_v2_5).setAnnotated(false).call();
    Ref ref_tag_2_5_annotated =
        git.tag().setName(TAG_2_5_ANNOTATED).setObjectId(commit_v2_5).setAnnotated(true).call();
    RevTag tag_2_5_annotated = revWalk.parseTag(ref_tag_2_5_annotated.getObjectId());
    git.tag()
        .setName(TAG_2_5_ANNOTATED_TWICE)
        .setObjectId(tag_2_5_annotated)
        .setAnnotated(true)
        .call();
  }

  @Override
  @After
  public void tearDown() throws Exception {
    revWalk.close();
    super.tearDown();
  }

  @Test
  public void resolveLatestCommit() throws Exception {
    // Check tip commit
    IncludedInResolver.Result detail = resolve(commit_v2_5);

    // Check that only tags and branches which refer the tip are returned
    assertThat(detail.tags()).containsExactly(TAG_2_5, TAG_2_5_ANNOTATED, TAG_2_5_ANNOTATED_TWICE);
    assertThat(detail.branches()).containsExactly(BRANCH_2_5);
  }

  @Test
  public void resolveFirstCommit() throws Exception {
    // Check first commit
    IncludedInResolver.Result detail = resolve(commit_initial);

    // Check whether all tags and branches are returned
    assertThat(detail.tags())
        .containsExactly(
            TAG_1_0,
            TAG_1_0_1,
            TAG_1_3,
            TAG_2_0,
            TAG_2_0_1,
            TAG_2_5,
            TAG_2_5_ANNOTATED,
            TAG_2_5_ANNOTATED_TWICE);
    assertThat(detail.branches())
        .containsExactly(BRANCH_MASTER, BRANCH_1_0, BRANCH_1_3, BRANCH_2_0, BRANCH_2_5);
  }

  @Test
  public void resolveBetwixtCommit() throws Exception {
    // Check a commit somewhere in the middle
    IncludedInResolver.Result detail = resolve(commit_v1_3);

    // Check whether all succeeding tags and branches are returned
    assertThat(detail.tags())
        .containsExactly(TAG_1_3, TAG_2_5, TAG_2_5_ANNOTATED, TAG_2_5_ANNOTATED_TWICE);
    assertThat(detail.branches()).containsExactly(BRANCH_1_3, BRANCH_2_5);
  }

  private IncludedInResolver.Result resolve(RevCommit commit) throws Exception {
    return IncludedInResolver.resolve(db, revWalk, commit);
  }

  private void createAndCheckoutBranch(ObjectId objectId, String branchName) throws IOException {
    String fullBranchName = "refs/heads/" + branchName;
    super.createBranch(objectId, fullBranchName);
    super.checkoutBranch(fullBranchName);
  }
}