blob: 5d1d1f6d3238bb0210383bee33d605912782fd3e [file] [log] [blame]
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +02001// Copyright 2015 Google Inc. All Rights Reserved.
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
15package com.google.gitiles;
16
17import static com.google.common.truth.Truth.assertThat;
18
Dave Borowitz77cf5f22015-10-26 11:05:07 -040019import com.google.gitiles.CommitJsonData.Commit;
20import com.google.gitiles.CommitJsonData.Log;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020021import com.google.gitiles.DateFormatter.Format;
Dave Borowitz77cf5f22015-10-26 11:05:07 -040022import com.google.gson.reflect.TypeToken;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020023
24import org.eclipse.jgit.revwalk.RevCommit;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
29import java.util.ArrayList;
30
31/** Tests for {@link LogServlet}. */
32@RunWith(JUnit4.class)
33public class LogServletTest extends ServletTest {
Dave Borowitz77cf5f22015-10-26 11:05:07 -040034 private static final TypeToken<Log> LOG = new TypeToken<Log>() {};
35
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020036 @Test
37 public void basicLog() throws Exception {
38 RevCommit commit = repo.branch("HEAD").commit().create();
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020039
Dave Borowitza774f592015-10-26 11:41:27 -040040 Log response = buildJson(LOG, "/repo/+log");
Dave Borowitz77cf5f22015-10-26 11:05:07 -040041 assertThat(response.log).hasSize(1);
42 verifyJsonCommit(response.log.get(0), commit);
43 assertThat(response.log.get(0).treeDiff).isNull();
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020044 }
45
46 @Test
47 public void treeDiffLog() throws Exception {
48 String contents1 = "foo\n";
49 String contents2 = "foo\ncontents\n";
50 RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
51 RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020052
Dave Borowitza774f592015-10-26 11:41:27 -040053 Log response = buildJson(LOG, "/repo/+log/master", "name-status=1");
Dave Borowitz77cf5f22015-10-26 11:05:07 -040054 assertThat(response.log).hasSize(2);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020055
Dave Borowitz77cf5f22015-10-26 11:05:07 -040056 Commit jc2 = response.log.get(0);
57 verifyJsonCommit(jc2, c2);
58 assertThat(jc2.treeDiff).hasSize(1);
59 assertThat(jc2.treeDiff.get(0).type).isEqualTo("modify");
60 assertThat(jc2.treeDiff.get(0).oldPath).isEqualTo("foo");
61 assertThat(jc2.treeDiff.get(0).newPath).isEqualTo("foo");
62
63 Commit jc1 = response.log.get(1);
64 verifyJsonCommit(jc1, c1);
65 assertThat(jc1.treeDiff).hasSize(1);
66 assertThat(jc1.treeDiff.get(0).type).isEqualTo("add");
67 assertThat(jc1.treeDiff.get(0).oldPath).isEqualTo("/dev/null");
68 assertThat(jc1.treeDiff.get(0).newPath).isEqualTo("foo");
69 }
70
Dave Borowitz344c4dd2015-10-26 11:02:13 -040071 @Test
72 public void follow() throws Exception {
73 String contents = "contents";
Dave Borowitzcf38c032016-05-02 11:06:23 -040074 RevCommit c1 = repo.branch("master").commit().add("foo", contents).create();
75 RevCommit c2 = repo.branch("master").commit().rm("foo").add("bar", contents).create();
Dave Borowitz344c4dd2015-10-26 11:02:13 -040076 repo.getRevWalk().parseBody(c1);
77 repo.getRevWalk().parseBody(c2);
78
Dave Borowitz45dde172016-03-22 10:10:58 -040079 Log response = buildJson(LOG, "/repo/+log/master/bar", "follow=0");
Dave Borowitz344c4dd2015-10-26 11:02:13 -040080 assertThat(response.log).hasSize(1);
81 verifyJsonCommit(response.log.get(0), c2);
82
Dave Borowitz45dde172016-03-22 10:10:58 -040083 response = buildJson(LOG, "/repo/+log/master/bar");
Dave Borowitz344c4dd2015-10-26 11:02:13 -040084 assertThat(response.log).hasSize(2);
85 verifyJsonCommit(response.log.get(0), c2);
86 verifyJsonCommit(response.log.get(1), c1);
87 }
88
Dave Borowitz77cf5f22015-10-26 11:05:07 -040089 private void verifyJsonCommit(Commit jsonCommit, RevCommit commit) throws Exception {
90 repo.getRevWalk().parseBody(commit);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020091 GitilesAccess access = new TestGitilesAccess(repo.getRepository()).forRequest(null);
92 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020093 assertThat(jsonCommit.commit).isEqualTo(commit.name());
94 assertThat(jsonCommit.tree).isEqualTo(commit.getTree().name());
95
96 ArrayList<String> expectedParents = new ArrayList<>();
97 for (int i = 0; i < commit.getParentCount(); i++) {
98 expectedParents.add(commit.getParent(i).name());
99 }
100 assertThat(jsonCommit.parents).containsExactlyElementsIn(expectedParents);
101
102 assertThat(jsonCommit.author.name).isEqualTo(commit.getAuthorIdent().getName());
103 assertThat(jsonCommit.author.email).isEqualTo(commit.getAuthorIdent().getEmailAddress());
104 assertThat(jsonCommit.author.time).isEqualTo(df.format(commit.getAuthorIdent()));
105 assertThat(jsonCommit.committer.name).isEqualTo(commit.getCommitterIdent().getName());
106 assertThat(jsonCommit.committer.email).isEqualTo(commit.getCommitterIdent().getEmailAddress());
107 assertThat(jsonCommit.committer.time).isEqualTo(df.format(commit.getCommitterIdent()));
108 assertThat(jsonCommit.message).isEqualTo(commit.getFullMessage());
109 }
110}