| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 1 | // Copyright (C) 2014 The Android Open Source Project |
| 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.collect.Iterables.getOnlyElement; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 19 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 20 | |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.io.BaseEncoding; |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 23 | import org.eclipse.jgit.diff.DiffEntry; |
| 24 | import org.eclipse.jgit.diff.DiffEntry.ChangeType; |
| 25 | import org.eclipse.jgit.diff.DiffEntry.Side; |
| 26 | import org.eclipse.jgit.diff.Edit; |
| 27 | import org.eclipse.jgit.diff.Edit.Type; |
| 28 | import org.eclipse.jgit.diff.RawText; |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.patch.FileHeader; |
| 30 | import org.eclipse.jgit.patch.Patch; |
| 31 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 32 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 35 | |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 36 | @RunWith(JUnit4.class) |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 37 | public class DiffServletTest extends ServletTest { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 38 | @Test |
| Dave Borowitz | 3e8299c | 2014-07-29 18:04:40 -0700 | [diff] [blame] | 39 | public void diffFileOneParentHtml() throws Exception { |
| 40 | String contents1 = "foo\n"; |
| 41 | String contents2 = "foo\ncontents\n"; |
| 42 | RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1)); |
| 43 | RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2)); |
| 44 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 45 | String actual = buildHtml("/repo/+diff/" + c2.name() + "^!/foo", false); |
| Dave Borowitz | 3e8299c | 2014-07-29 18:04:40 -0700 | [diff] [blame] | 46 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 47 | String diffHeader = |
| 48 | String.format( |
| 49 | "diff --git <a href=\"/b/repo/+/%s/foo\">a/foo</a> <a href=\"/b/repo/+/%s/foo\">b/foo</a>", |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 50 | c1.name(), c2.name()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 51 | assertThat(actual).contains(diffHeader); |
| Dave Borowitz | 3e8299c | 2014-07-29 18:04:40 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void diffFileNoParentsText() throws Exception { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 56 | String contents = "foo\ncontents\n"; |
| 57 | RevCommit c = repo.update("master", repo.commit().add("foo", contents)); |
| 58 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 59 | FakeHttpServletResponse res = buildText("/repo/+diff/" + c.name() + "^!/foo"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 60 | |
| 61 | Patch p = parsePatch(res.getActualBody()); |
| 62 | FileHeader f = getOnlyElement(p.getFiles()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 63 | assertThat(f.getChangeType()).isEqualTo(ChangeType.ADD); |
| 64 | assertThat(f.getPath(Side.OLD)).isEqualTo(DiffEntry.DEV_NULL); |
| 65 | assertThat(f.getPath(Side.NEW)).isEqualTo("foo"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 66 | |
| 67 | RawText rt = new RawText(contents.getBytes(UTF_8)); |
| 68 | Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 69 | assertThat(e.getType()).isEqualTo(Type.INSERT); |
| 70 | assertThat(rt.getString(e.getBeginB(), e.getEndB(), false)).isEqualTo(contents); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| Dave Borowitz | 3e8299c | 2014-07-29 18:04:40 -0700 | [diff] [blame] | 74 | public void diffFileOneParentText() throws Exception { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 75 | String contents1 = "foo\n"; |
| 76 | String contents2 = "foo\ncontents\n"; |
| 77 | RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1)); |
| 78 | RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2)); |
| 79 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 80 | FakeHttpServletResponse res = buildText("/repo/+diff/" + c2.name() + "^!/foo"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 81 | |
| 82 | Patch p = parsePatch(res.getActualBody()); |
| 83 | FileHeader f = getOnlyElement(p.getFiles()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 84 | assertThat(f.getChangeType()).isEqualTo(ChangeType.MODIFY); |
| 85 | assertThat(f.getPath(Side.OLD)).isEqualTo("foo"); |
| 86 | assertThat(f.getPath(Side.NEW)).isEqualTo("foo"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 87 | |
| 88 | RawText rt2 = new RawText(contents2.getBytes(UTF_8)); |
| 89 | Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 90 | assertThat(e.getType()).isEqualTo(Type.INSERT); |
| 91 | assertThat(rt2.getString(e.getBeginB(), e.getEndB(), false)).isEqualTo("contents\n"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test |
| Dave Borowitz | 3e8299c | 2014-07-29 18:04:40 -0700 | [diff] [blame] | 95 | public void diffDirectoryText() throws Exception { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 96 | String contents = "contents\n"; |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 97 | RevCommit c = |
| 98 | repo.update( |
| 99 | "master", |
| 100 | repo.commit().add("dir/foo", contents).add("dir/bar", contents).add("baz", contents)); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 101 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 102 | FakeHttpServletResponse res = buildText("/repo/+diff/" + c.name() + "^!/dir"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 103 | |
| 104 | Patch p = parsePatch(res.getActualBody()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 105 | assertThat(p.getFiles().size()).isEqualTo(2); |
| 106 | assertThat(p.getFiles().get(0).getPath(Side.NEW)).isEqualTo("dir/bar"); |
| 107 | assertThat(p.getFiles().get(1).getPath(Side.NEW)).isEqualTo("dir/foo"); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private static Patch parsePatch(byte[] enc) { |
| 111 | byte[] buf = BaseEncoding.base64().decode(new String(enc, UTF_8)); |
| 112 | Patch p = new Patch(); |
| 113 | p.parse(buf, 0, buf.length); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 114 | assertThat(p.getErrors()).isEqualTo(ImmutableList.of()); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 115 | return p; |
| 116 | } |
| 117 | } |