| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 19 | import com.google.gitiles.CommitJsonData.Commit; |
| 20 | import com.google.gitiles.CommitJsonData.Log; |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 21 | import com.google.gitiles.DateFormatter.Format; |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 22 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 23 | import java.util.ArrayList; |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 24 | import org.eclipse.jgit.revwalk.RevCommit; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 29 | /** Tests for {@link LogServlet}. */ |
| 30 | @RunWith(JUnit4.class) |
| 31 | public class LogServletTest extends ServletTest { |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 32 | private static final TypeToken<Log> LOG = new TypeToken<Log>() {}; |
| 33 | |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 34 | @Test |
| 35 | public void basicLog() throws Exception { |
| 36 | RevCommit commit = repo.branch("HEAD").commit().create(); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 37 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 38 | Log response = buildJson(LOG, "/repo/+log"); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 39 | assertThat(response.log).hasSize(1); |
| 40 | verifyJsonCommit(response.log.get(0), commit); |
| 41 | assertThat(response.log.get(0).treeDiff).isNull(); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 42 | } |
| 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, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 50 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 51 | Log response = buildJson(LOG, "/repo/+log/master", "name-status=1"); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 52 | assertThat(response.log).hasSize(2); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 53 | |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 54 | 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 Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 69 | @Test |
| Alex Spradlin | 284a35a | 2019-07-09 16:02:55 -0700 | [diff] [blame] | 70 | public void firstParentLog() throws Exception { |
| 71 | RevCommit p1 = repo.update("master", repo.commit().add("foo", "foo\n")); |
| 72 | RevCommit p2 = repo.update("master", repo.commit().add("foo", "foo2\n")); |
| 73 | RevCommit c = repo.update("master", repo.commit().parent(p1).parent(p2).add("foo", "foo3\n")); |
| 74 | |
| 75 | Log response = buildJson(LOG, "/repo/+log/master", "first-parent"); |
| 76 | assertThat(response.log).hasSize(2); |
| 77 | |
| 78 | verifyJsonCommit(response.log.get(0), c); |
| 79 | verifyJsonCommit(response.log.get(1), p1); |
| 80 | } |
| 81 | |
| 82 | @Test |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 83 | public void follow() throws Exception { |
| 84 | String contents = "contents"; |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 85 | RevCommit c1 = repo.branch("master").commit().add("foo", contents).create(); |
| 86 | RevCommit c2 = repo.branch("master").commit().rm("foo").add("bar", contents).create(); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 87 | repo.getRevWalk().parseBody(c1); |
| 88 | repo.getRevWalk().parseBody(c2); |
| 89 | |
| Dave Borowitz | 45dde17 | 2016-03-22 10:10:58 -0400 | [diff] [blame] | 90 | Log response = buildJson(LOG, "/repo/+log/master/bar", "follow=0"); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 91 | assertThat(response.log).hasSize(1); |
| 92 | verifyJsonCommit(response.log.get(0), c2); |
| 93 | |
| Dave Borowitz | 45dde17 | 2016-03-22 10:10:58 -0400 | [diff] [blame] | 94 | response = buildJson(LOG, "/repo/+log/master/bar"); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 95 | assertThat(response.log).hasSize(2); |
| 96 | verifyJsonCommit(response.log.get(0), c2); |
| 97 | verifyJsonCommit(response.log.get(1), c1); |
| 98 | } |
| 99 | |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 100 | private void verifyJsonCommit(Commit jsonCommit, RevCommit commit) throws Exception { |
| 101 | repo.getRevWalk().parseBody(commit); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 102 | GitilesAccess access = new TestGitilesAccess(repo.getRepository()).forRequest(null); |
| 103 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 104 | assertThat(jsonCommit.commit).isEqualTo(commit.name()); |
| 105 | assertThat(jsonCommit.tree).isEqualTo(commit.getTree().name()); |
| 106 | |
| 107 | ArrayList<String> expectedParents = new ArrayList<>(); |
| 108 | for (int i = 0; i < commit.getParentCount(); i++) { |
| 109 | expectedParents.add(commit.getParent(i).name()); |
| 110 | } |
| 111 | assertThat(jsonCommit.parents).containsExactlyElementsIn(expectedParents); |
| 112 | |
| 113 | assertThat(jsonCommit.author.name).isEqualTo(commit.getAuthorIdent().getName()); |
| 114 | assertThat(jsonCommit.author.email).isEqualTo(commit.getAuthorIdent().getEmailAddress()); |
| 115 | assertThat(jsonCommit.author.time).isEqualTo(df.format(commit.getAuthorIdent())); |
| 116 | assertThat(jsonCommit.committer.name).isEqualTo(commit.getCommitterIdent().getName()); |
| 117 | assertThat(jsonCommit.committer.email).isEqualTo(commit.getCommitterIdent().getEmailAddress()); |
| 118 | assertThat(jsonCommit.committer.time).isEqualTo(df.format(commit.getCommitterIdent())); |
| 119 | assertThat(jsonCommit.message).isEqualTo(commit.getFullMessage()); |
| 120 | } |
| 121 | } |