commit | author | age
|
db653a
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit.tests;
|
|
17 |
|
7e8873
|
18 |
import java.util.List;
|
db653a
|
19 |
|
JM |
20 |
import org.eclipse.jgit.lib.Repository;
|
|
21 |
import org.eclipse.jgit.revwalk.RevCommit;
|
7e8873
|
22 |
import org.junit.Test;
|
db653a
|
23 |
|
f339f5
|
24 |
import com.gitblit.models.AnnotatedLine;
|
db653a
|
25 |
import com.gitblit.utils.DiffUtils;
|
cff352
|
26 |
import com.gitblit.utils.DiffUtils.DiffComparator;
|
a125cf
|
27 |
import com.gitblit.utils.DiffUtils.DiffOutputType;
|
db653a
|
28 |
import com.gitblit.utils.JGitUtils;
|
JM |
29 |
|
db4f6b
|
30 |
public class DiffUtilsTest extends GitblitUnitTest {
|
db653a
|
31 |
|
7e8873
|
32 |
@Test
|
f339f5
|
33 |
public void testDiffOutputTypes() throws Exception {
|
7e8873
|
34 |
assertEquals(DiffOutputType.PLAIN, DiffOutputType.forName("plain"));
|
025028
|
35 |
assertEquals(DiffOutputType.HTML, DiffOutputType.forName("html"));
|
7e8873
|
36 |
assertEquals(null, DiffOutputType.forName(null));
|
f339f5
|
37 |
}
|
85c2e6
|
38 |
|
7e8873
|
39 |
@Test
|
db653a
|
40 |
public void testParentCommitDiff() throws Exception {
|
JM |
41 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
|
42 |
RevCommit commit = JGitUtils.getCommit(repository,
|
|
43 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
310a80
|
44 |
String diff = DiffUtils.getCommitDiff(repository, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;
|
db653a
|
45 |
repository.close();
|
JM |
46 |
assertTrue(diff != null && diff.length() > 0);
|
|
47 |
String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
|
|
48 |
assertTrue(diff.indexOf(expected) > -1);
|
|
49 |
}
|
|
50 |
|
7e8873
|
51 |
@Test
|
db653a
|
52 |
public void testArbitraryCommitDiff() throws Exception {
|
JM |
53 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
|
54 |
RevCommit baseCommit = JGitUtils.getCommit(repository,
|
|
55 |
"8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
|
|
56 |
RevCommit commit = JGitUtils.getCommit(repository,
|
|
57 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
310a80
|
58 |
String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;
|
db653a
|
59 |
repository.close();
|
JM |
60 |
assertTrue(diff != null && diff.length() > 0);
|
|
61 |
String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
|
|
62 |
assertTrue(diff.indexOf(expected) > -1);
|
|
63 |
}
|
|
64 |
|
7e8873
|
65 |
@Test
|
db653a
|
66 |
public void testPlainFileDiff() throws Exception {
|
JM |
67 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
|
68 |
RevCommit commit = JGitUtils.getCommit(repository,
|
|
69 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
310a80
|
70 |
String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;
|
db653a
|
71 |
repository.close();
|
JM |
72 |
assertTrue(diff != null && diff.length() > 0);
|
|
73 |
String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
|
|
74 |
assertTrue(diff.indexOf(expected) > -1);
|
|
75 |
}
|
|
76 |
|
7e8873
|
77 |
@Test
|
db653a
|
78 |
public void testFilePatch() throws Exception {
|
JM |
79 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
|
80 |
RevCommit commit = JGitUtils.getCommit(repository,
|
|
81 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
|
82 |
String patch = DiffUtils.getCommitPatch(repository, null, commit, "java.java");
|
|
83 |
repository.close();
|
|
84 |
assertTrue(patch != null && patch.length() > 0);
|
|
85 |
String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
|
|
86 |
assertTrue(patch.indexOf(expected) > -1);
|
|
87 |
}
|
|
88 |
|
7e8873
|
89 |
@Test
|
db653a
|
90 |
public void testArbitraryFilePatch() throws Exception {
|
JM |
91 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
|
92 |
RevCommit baseCommit = JGitUtils.getCommit(repository,
|
|
93 |
"8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
|
|
94 |
RevCommit commit = JGitUtils.getCommit(repository,
|
|
95 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
|
96 |
String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, "java.java");
|
|
97 |
repository.close();
|
|
98 |
assertTrue(patch != null && patch.length() > 0);
|
|
99 |
String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
|
|
100 |
assertTrue(patch.indexOf(expected) > -1);
|
|
101 |
}
|
|
102 |
|
7e8873
|
103 |
@Test
|
db653a
|
104 |
public void testArbitraryCommitPatch() throws Exception {
|
JM |
105 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
|
106 |
RevCommit baseCommit = JGitUtils.getCommit(repository,
|
|
107 |
"8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
|
|
108 |
RevCommit commit = JGitUtils.getCommit(repository,
|
|
109 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
|
110 |
String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, null);
|
|
111 |
repository.close();
|
|
112 |
assertTrue(patch != null && patch.length() > 0);
|
|
113 |
String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
|
|
114 |
assertTrue(patch.indexOf(expected) > -1);
|
|
115 |
}
|
85c2e6
|
116 |
|
7e8873
|
117 |
@Test
|
f339f5
|
118 |
public void testBlame() throws Exception {
|
JM |
119 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
85c2e6
|
120 |
List<AnnotatedLine> lines = DiffUtils.blame(repository, "java.java",
|
JM |
121 |
"1d0c2933a4ae69c362f76797d42d6bd182d05176");
|
f339f5
|
122 |
repository.close();
|
JM |
123 |
assertTrue(lines.size() > 0);
|
7e8873
|
124 |
assertEquals("c6d31dccf5cc75e8e46299fc62d38f60ec6d41e0", lines.get(0).commitId);
|
f339f5
|
125 |
}
|
db653a
|
126 |
}
|