| 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; |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 23 | |
| 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 | |
| 29 | import java.util.ArrayList; |
| 30 | |
| 31 | /** Tests for {@link LogServlet}. */ |
| 32 | @RunWith(JUnit4.class) |
| 33 | public class LogServletTest extends ServletTest { |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 34 | private static final TypeToken<Log> LOG = new TypeToken<Log>() {}; |
| 35 | |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 36 | @Test |
| 37 | public void basicLog() throws Exception { |
| 38 | RevCommit commit = repo.branch("HEAD").commit().create(); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 39 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame^] | 40 | Log response = buildJson(LOG, "/repo/+log"); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 41 | assertThat(response.log).hasSize(1); |
| 42 | verifyJsonCommit(response.log.get(0), commit); |
| 43 | assertThat(response.log.get(0).treeDiff).isNull(); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 44 | } |
| 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, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 52 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame^] | 53 | Log response = buildJson(LOG, "/repo/+log/master", "name-status=1"); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 54 | assertThat(response.log).hasSize(2); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 55 | |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 56 | 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 | |
| 71 | private void verifyJsonCommit(Commit jsonCommit, RevCommit commit) throws Exception { |
| 72 | repo.getRevWalk().parseBody(commit); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 73 | GitilesAccess access = new TestGitilesAccess(repo.getRepository()).forRequest(null); |
| 74 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 75 | assertThat(jsonCommit.commit).isEqualTo(commit.name()); |
| 76 | assertThat(jsonCommit.tree).isEqualTo(commit.getTree().name()); |
| 77 | |
| 78 | ArrayList<String> expectedParents = new ArrayList<>(); |
| 79 | for (int i = 0; i < commit.getParentCount(); i++) { |
| 80 | expectedParents.add(commit.getParent(i).name()); |
| 81 | } |
| 82 | assertThat(jsonCommit.parents).containsExactlyElementsIn(expectedParents); |
| 83 | |
| 84 | assertThat(jsonCommit.author.name).isEqualTo(commit.getAuthorIdent().getName()); |
| 85 | assertThat(jsonCommit.author.email).isEqualTo(commit.getAuthorIdent().getEmailAddress()); |
| 86 | assertThat(jsonCommit.author.time).isEqualTo(df.format(commit.getAuthorIdent())); |
| 87 | assertThat(jsonCommit.committer.name).isEqualTo(commit.getCommitterIdent().getName()); |
| 88 | assertThat(jsonCommit.committer.email).isEqualTo(commit.getCommitterIdent().getEmailAddress()); |
| 89 | assertThat(jsonCommit.committer.time).isEqualTo(df.format(commit.getCommitterIdent())); |
| 90 | assertThat(jsonCommit.message).isEqualTo(commit.getFullMessage()); |
| 91 | } |
| 92 | } |