|
1 | 1 | package com.coravy.hudson.plugins.github;
|
2 | 2 |
|
3 |
| -import static org.junit.Assert.assertEquals; |
| 3 | +import com.tngtech.java.junit.dataprovider.DataProvider; |
| 4 | +import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
| 5 | +import com.tngtech.java.junit.dataprovider.UseDataProvider; |
4 | 6 | import hudson.MarkupText;
|
5 |
| - |
| 7 | +import hudson.plugins.git.GitChangeSet; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Random; |
| 10 | +import org.junit.Before; |
6 | 11 | import org.junit.Test;
|
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import static java.lang.String.format; |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | +import static org.hamcrest.Matchers.is; |
7 | 16 |
|
| 17 | +@RunWith(DataProviderRunner.class) |
8 | 18 | public class GithubLinkAnnotatorTest {
|
9 | 19 |
|
10 |
| - private final static String GITHUB_URL = "http://github.com/juretta/iphone-project-tools/"; |
| 20 | + private final static String GITHUB_URL = "http://github.com/juretta/iphone-project-tools"; |
| 21 | + private final static String SHA1 = "badbeef136cd854f4dd6fa40bf94c0c657681dd5"; |
| 22 | + private final static Random RANDOM = new Random(); |
| 23 | + private final String expectedChangeSetAnnotation = " (" |
| 24 | + + "<a href='" + GITHUB_URL + "/commit/" + SHA1 + "'>" |
| 25 | + + "commit: " + SHA1.substring(0, 7) |
| 26 | + + "</a>)"; |
| 27 | + private static GitChangeSet changeSet; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void createChangeSet() throws Exception { |
| 31 | + ArrayList<String> lines = new ArrayList<String>(); |
| 32 | + lines.add("commit " + SHA1); |
| 33 | + lines.add("tree 66236cf9a1ac0c589172b450ed01f019a5697c49"); |
| 34 | + lines.add("parent e74a24e995305bd67a180f0ebc57927e2b8783ce"); |
| 35 | + lines.add("author Author Name <author.name@nospam.com> 1363879004 +0100"); |
| 36 | + lines.add("committer Committer Name <committer.name@nospam.com> 1364199539 -0400"); |
| 37 | + lines.add(""); |
| 38 | + lines.add(" Committer and author are different in this commit."); |
| 39 | + lines.add(""); |
| 40 | + changeSet = new GitChangeSet(lines, true); |
| 41 | + } |
| 42 | + |
| 43 | + private static Object[] genActualAndExpected(String keyword) { |
| 44 | + int issueNumber = RANDOM.nextInt(1000000); |
| 45 | + final String innerText = keyword + " #" + issueNumber; |
| 46 | + final String startHREF = "<a href='" + GITHUB_URL + "/issues/" + issueNumber + "/find'>"; |
| 47 | + final String endHREF = "</a>"; |
| 48 | + final String annotatedText = startHREF + innerText + endHREF; |
| 49 | + return new Object[]{ |
| 50 | + // Input text to the annotate method |
| 51 | + format("An issue %s link", innerText), |
| 52 | + // Expected result from the annotate method |
| 53 | + format("An issue %s link", annotatedText) |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + @DataProvider |
| 58 | + public static Object[][] annotations() { |
| 59 | + return new Object[][]{ |
| 60 | + genActualAndExpected("Closes"), |
| 61 | + genActualAndExpected("Close"), |
| 62 | + genActualAndExpected("closes"), |
| 63 | + genActualAndExpected("close") |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @UseDataProvider("annotations") |
| 69 | + public void inputIsExpected(String input, String expected) throws Exception { |
| 70 | + assertThat(format("For input '%s'", input), |
| 71 | + annotate(input, null), |
| 72 | + is(expected)); |
| 73 | + } |
11 | 74 |
|
12 | 75 | @Test
|
13 |
| - public final void testAnnotateStringMarkupText() { |
14 |
| - assertAnnotatedTextEquals("An issue Closes #1 link", |
15 |
| - "An issue <a href='" + GITHUB_URL |
16 |
| - + "issues/1/find'>Closes #1</a> link"); |
17 |
| - assertAnnotatedTextEquals("An issue Close #1 link", |
18 |
| - "An issue <a href='" + GITHUB_URL |
19 |
| - + "issues/1/find'>Close #1</a> link"); |
20 |
| - assertAnnotatedTextEquals("An issue closes #123 link", |
21 |
| - "An issue <a href='" + GITHUB_URL |
22 |
| - + "issues/123/find'>closes #123</a> link"); |
23 |
| - assertAnnotatedTextEquals("An issue close #9876 link", |
24 |
| - "An issue <a href='" + GITHUB_URL |
25 |
| - + "issues/9876/find'>close #9876</a> link"); |
| 76 | + @UseDataProvider("annotations") |
| 77 | + public void inputIsExpectedWithChangeSet(String input, String expected) throws Exception { |
| 78 | + assertThat(format("For changeset input '%s'", input), |
| 79 | + annotate(input, changeSet), |
| 80 | + is(expected + expectedChangeSetAnnotation)); |
26 | 81 | }
|
27 | 82 |
|
28 |
| - private void assertAnnotatedTextEquals(final String originalText, |
29 |
| - final String expectedAnnotatedText) { |
| 83 | + private String annotate(final String originalText, GitChangeSet changeSet) { |
30 | 84 | MarkupText markupText = new MarkupText(originalText);
|
31 | 85 |
|
32 | 86 | GithubLinkAnnotator annotator = new GithubLinkAnnotator();
|
33 |
| - annotator.annotate(new GithubUrl(GITHUB_URL), markupText, null); |
| 87 | + annotator.annotate(new GithubUrl(GITHUB_URL), markupText, changeSet); |
34 | 88 |
|
35 |
| - assertEquals(expectedAnnotatedText, markupText.toString()); |
| 89 | + return markupText.toString(true); |
36 | 90 | }
|
37 | 91 | }
|
0 commit comments