blob: cfc74ee13e441432df828d82653465355728772f [file] [log] [blame]
Dave Borowitzea9bba12014-07-09 16:45:40 -07001// 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
15package com.google.gitiles;
16
17import static com.google.common.collect.Iterables.getOnlyElement;
Dave Borowitzfde41fd2015-09-16 15:14:38 -040018import static com.google.common.truth.Truth.assertThat;
Dave Borowitzea9bba12014-07-09 16:45:40 -070019import static java.nio.charset.StandardCharsets.UTF_8;
Dave Borowitzea9bba12014-07-09 16:45:40 -070020
21import com.google.common.collect.ImmutableList;
22import com.google.common.io.BaseEncoding;
23
24import org.eclipse.jgit.diff.DiffEntry;
25import org.eclipse.jgit.diff.DiffEntry.ChangeType;
26import org.eclipse.jgit.diff.DiffEntry.Side;
27import org.eclipse.jgit.diff.Edit;
28import org.eclipse.jgit.diff.Edit.Type;
29import org.eclipse.jgit.diff.RawText;
Dave Borowitzea9bba12014-07-09 16:45:40 -070030import org.eclipse.jgit.patch.FileHeader;
31import org.eclipse.jgit.patch.Patch;
32import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzea9bba12014-07-09 16:45:40 -070033import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080034import org.junit.runner.RunWith;
35import org.junit.runners.JUnit4;
Dave Borowitzea9bba12014-07-09 16:45:40 -070036
Dave Borowitz3dc854f2014-11-04 16:19:37 -080037@RunWith(JUnit4.class)
Nodir Turakulov4bc26002015-08-18 18:24:37 -070038public class DiffServletTest extends ServletTest {
Dave Borowitzea9bba12014-07-09 16:45:40 -070039 @Test
Dave Borowitz3e8299c2014-07-29 18:04:40 -070040 public void diffFileOneParentHtml() throws Exception {
41 String contents1 = "foo\n";
42 String contents2 = "foo\ncontents\n";
43 RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
44 RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));
45
Nodir Turakulov4bc26002015-08-18 18:24:37 -070046 String actual = buildHtml("/repo/+diff/" + c2.name() + "^!/foo", false);
Dave Borowitz3e8299c2014-07-29 18:04:40 -070047
48 String diffHeader = String.format(
Nodir Turakulov4bc26002015-08-18 18:24:37 -070049 "diff --git <a href=\"/b/repo/+/%s/foo\">a/foo</a> <a href=\"/b/repo/+/%s/foo\">b/foo</a>",
Dave Borowitz3e8299c2014-07-29 18:04:40 -070050 c1.name(), c2.name());
Dave Borowitzfde41fd2015-09-16 15:14:38 -040051 assertThat(actual).contains(diffHeader);
Dave Borowitz3e8299c2014-07-29 18:04:40 -070052 }
53
54 @Test
55 public void diffFileNoParentsText() throws Exception {
Dave Borowitzea9bba12014-07-09 16:45:40 -070056 String contents = "foo\ncontents\n";
57 RevCommit c = repo.update("master", repo.commit().add("foo", contents));
58
Nodir Turakulov4bc26002015-08-18 18:24:37 -070059 FakeHttpServletResponse res = buildText("/repo/+diff/" + c.name() + "^!/foo");
Dave Borowitzea9bba12014-07-09 16:45:40 -070060
61 Patch p = parsePatch(res.getActualBody());
62 FileHeader f = getOnlyElement(p.getFiles());
Dave Borowitzfde41fd2015-09-16 15:14:38 -040063 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 Borowitzea9bba12014-07-09 16:45:40 -070066
67 RawText rt = new RawText(contents.getBytes(UTF_8));
68 Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList());
Dave Borowitzfde41fd2015-09-16 15:14:38 -040069 assertThat(e.getType()).isEqualTo(Type.INSERT);
70 assertThat(rt.getString(e.getBeginB(), e.getEndB(), false)).isEqualTo(contents);
Dave Borowitzea9bba12014-07-09 16:45:40 -070071 }
72
73 @Test
Dave Borowitz3e8299c2014-07-29 18:04:40 -070074 public void diffFileOneParentText() throws Exception {
Dave Borowitzea9bba12014-07-09 16:45:40 -070075 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 Turakulov4bc26002015-08-18 18:24:37 -070080 FakeHttpServletResponse res = buildText("/repo/+diff/" + c2.name() + "^!/foo");
Dave Borowitzea9bba12014-07-09 16:45:40 -070081
82 Patch p = parsePatch(res.getActualBody());
83 FileHeader f = getOnlyElement(p.getFiles());
Dave Borowitzfde41fd2015-09-16 15:14:38 -040084 assertThat(f.getChangeType()).isEqualTo(ChangeType.MODIFY);
85 assertThat(f.getPath(Side.OLD)).isEqualTo("foo");
86 assertThat(f.getPath(Side.NEW)).isEqualTo("foo");
Dave Borowitzea9bba12014-07-09 16:45:40 -070087
88 RawText rt2 = new RawText(contents2.getBytes(UTF_8));
89 Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList());
Dave Borowitzfde41fd2015-09-16 15:14:38 -040090 assertThat(e.getType()).isEqualTo(Type.INSERT);
91 assertThat(rt2.getString(e.getBeginB(), e.getEndB(), false)).isEqualTo("contents\n");
Dave Borowitzea9bba12014-07-09 16:45:40 -070092 }
93
94 @Test
Dave Borowitz3e8299c2014-07-29 18:04:40 -070095 public void diffDirectoryText() throws Exception {
Dave Borowitzea9bba12014-07-09 16:45:40 -070096 String contents = "contents\n";
97 RevCommit c = repo.update("master", repo.commit()
98 .add("dir/foo", contents)
99 .add("dir/bar", contents)
100 .add("baz", contents));
101
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700102 FakeHttpServletResponse res = buildText("/repo/+diff/" + c.name() + "^!/dir");
Dave Borowitzea9bba12014-07-09 16:45:40 -0700103
104 Patch p = parsePatch(res.getActualBody());
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400105 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 Borowitzea9bba12014-07-09 16:45:40 -0700108 }
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 Borowitzfde41fd2015-09-16 15:14:38 -0400114 assertThat(p.getErrors()).isEqualTo(ImmutableList.of());
Dave Borowitzea9bba12014-07-09 16:45:40 -0700115 return p;
116 }
117}