blob: 06ddaf43c0ed3d7fa37decc91291a08c5cbf77e3 [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;
Dave Borowitz3b744b12016-08-19 16:11:10 -040023import java.util.ArrayList;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020024import org.eclipse.jgit.revwalk.RevCommit;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020029/** Tests for {@link LogServlet}. */
30@RunWith(JUnit4.class)
31public class LogServletTest extends ServletTest {
Dave Borowitz77cf5f22015-10-26 11:05:07 -040032 private static final TypeToken<Log> LOG = new TypeToken<Log>() {};
33
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020034 @Test
35 public void basicLog() throws Exception {
36 RevCommit commit = repo.branch("HEAD").commit().create();
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020037
Dave Borowitza774f592015-10-26 11:41:27 -040038 Log response = buildJson(LOG, "/repo/+log");
Dave Borowitz77cf5f22015-10-26 11:05:07 -040039 assertThat(response.log).hasSize(1);
40 verifyJsonCommit(response.log.get(0), commit);
41 assertThat(response.log.get(0).treeDiff).isNull();
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020042 }
43
44 @Test
45 public void treeDiffLog() throws Exception {
46 String contents1 = "foo\n";
47 String contents2 = "foo\ncontents\n";
48 RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
49 RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020050
Dave Borowitza774f592015-10-26 11:41:27 -040051 Log response = buildJson(LOG, "/repo/+log/master", "name-status=1");
Dave Borowitz77cf5f22015-10-26 11:05:07 -040052 assertThat(response.log).hasSize(2);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020053
Dave Borowitz77cf5f22015-10-26 11:05:07 -040054 Commit jc2 = response.log.get(0);
55 verifyJsonCommit(jc2, c2);
56 assertThat(jc2.treeDiff).hasSize(1);
57 assertThat(jc2.treeDiff.get(0).type).isEqualTo("modify");
58 assertThat(jc2.treeDiff.get(0).oldPath).isEqualTo("foo");
59 assertThat(jc2.treeDiff.get(0).newPath).isEqualTo("foo");
60
61 Commit jc1 = response.log.get(1);
62 verifyJsonCommit(jc1, c1);
63 assertThat(jc1.treeDiff).hasSize(1);
64 assertThat(jc1.treeDiff.get(0).type).isEqualTo("add");
65 assertThat(jc1.treeDiff.get(0).oldPath).isEqualTo("/dev/null");
66 assertThat(jc1.treeDiff.get(0).newPath).isEqualTo("foo");
67 }
68
Dave Borowitz344c4dd2015-10-26 11:02:13 -040069 @Test
70 public void follow() throws Exception {
71 String contents = "contents";
Dave Borowitzcf38c032016-05-02 11:06:23 -040072 RevCommit c1 = repo.branch("master").commit().add("foo", contents).create();
73 RevCommit c2 = repo.branch("master").commit().rm("foo").add("bar", contents).create();
Dave Borowitz344c4dd2015-10-26 11:02:13 -040074 repo.getRevWalk().parseBody(c1);
75 repo.getRevWalk().parseBody(c2);
76
Dave Borowitz45dde172016-03-22 10:10:58 -040077 Log response = buildJson(LOG, "/repo/+log/master/bar", "follow=0");
Dave Borowitz344c4dd2015-10-26 11:02:13 -040078 assertThat(response.log).hasSize(1);
79 verifyJsonCommit(response.log.get(0), c2);
80
Dave Borowitz45dde172016-03-22 10:10:58 -040081 response = buildJson(LOG, "/repo/+log/master/bar");
Dave Borowitz344c4dd2015-10-26 11:02:13 -040082 assertThat(response.log).hasSize(2);
83 verifyJsonCommit(response.log.get(0), c2);
84 verifyJsonCommit(response.log.get(1), c1);
85 }
86
Dave Borowitz77cf5f22015-10-26 11:05:07 -040087 private void verifyJsonCommit(Commit jsonCommit, RevCommit commit) throws Exception {
88 repo.getRevWalk().parseBody(commit);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020089 GitilesAccess access = new TestGitilesAccess(repo.getRepository()).forRequest(null);
90 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020091 assertThat(jsonCommit.commit).isEqualTo(commit.name());
92 assertThat(jsonCommit.tree).isEqualTo(commit.getTree().name());
93
94 ArrayList<String> expectedParents = new ArrayList<>();
95 for (int i = 0; i < commit.getParentCount(); i++) {
96 expectedParents.add(commit.getParent(i).name());
97 }
98 assertThat(jsonCommit.parents).containsExactlyElementsIn(expectedParents);
99
100 assertThat(jsonCommit.author.name).isEqualTo(commit.getAuthorIdent().getName());
101 assertThat(jsonCommit.author.email).isEqualTo(commit.getAuthorIdent().getEmailAddress());
102 assertThat(jsonCommit.author.time).isEqualTo(df.format(commit.getAuthorIdent()));
103 assertThat(jsonCommit.committer.name).isEqualTo(commit.getCommitterIdent().getName());
104 assertThat(jsonCommit.committer.email).isEqualTo(commit.getCommitterIdent().getEmailAddress());
105 assertThat(jsonCommit.committer.time).isEqualTo(df.format(commit.getCommitterIdent()));
106 assertThat(jsonCommit.message).isEqualTo(commit.getFullMessage());
107 }
108}