package hudson.plugins.git.browser;

import hudson.plugins.git.GitChangeSet;
import hudson.scm.EditType;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class GitBlitRepositoryBrowserTest {

    private final String repoUrl = "http://gitblit.example.com/";

    private final boolean useAuthorName;
    private final GitChangeSetSample sample;
    private final String projectName = "gitBlitProjectName";

    public GitBlitRepositoryBrowserTest(String useAuthorName) {
        this.useAuthorName = Boolean.valueOf(useAuthorName);
        sample = new GitChangeSetSample(this.useAuthorName);
    }

    @Parameterized.Parameters(name = "{0}")
    public static Collection permuteAuthorName() {
        List<Object[]> values = new ArrayList<Object[]>();
        String[] allowed = {"true", "false"};
        for (String authorName : allowed) {
            Object[] combination = {authorName};
            values.add(combination);
        }
        return values;
    }

    @Test
    public void testGetChangeSetLink() throws Exception {
        URL result = (new GitBlitRepositoryBrowser(repoUrl, projectName)).getChangeSetLink(sample.changeSet);
        assertEquals(new URL(repoUrl + "commit?r=" + projectName + "&h=" + sample.id), result);
    }

    @Test
    public void testGetDiffLink() throws Exception {
        GitBlitRepositoryBrowser gitblit = new GitBlitRepositoryBrowser(repoUrl, projectName);
        for (GitChangeSet.Path path : sample.changeSet.getPaths()) {
            EditType editType = path.getEditType();
            assertTrue("Unexpected edit type " + editType.getName(), editType == EditType.ADD || editType == EditType.EDIT || editType == EditType.DELETE);
            URL diffLink = gitblit.getDiffLink(path);
            URL expectedDiffLink = new URL(repoUrl + "blobdiff?r=" + projectName + "&h=" + sample.id + "&hb=" + sample.parent);
            String msg = "Wrong link for path: " + path.getPath() + ", edit type: " + editType.getName();
            assertEquals(msg, expectedDiffLink, diffLink);
        }
    }

    @Test
    public void testGetFileLink() throws Exception {
        GitBlitRepositoryBrowser gitblit = new GitBlitRepositoryBrowser(repoUrl, projectName);
    }

    @Test
    public void testGetProjectName() {
        GitBlitRepositoryBrowser gitblit = new GitBlitRepositoryBrowser(repoUrl, projectName);
        assertEquals(projectName, gitblit.getProjectName());
    }
}

@@ -119,16 +119,44 @@
 et.URL;%0A
+import java.net.URLEncoder;%0A
 import j
@@ -2445,24 +2445,863 @@
 ojectName);%0A
+        for (GitChangeSet.Path path : sample.changeSet.getPaths()) %7B%0A            URL fileLink = gitblit.getFileLink(path);%0A            EditType editType = path.getEditType();%0A            URL expectedFileLink = null;%0A            if (editType == EditType.ADD %7C%7C editType == EditType.EDIT) %7B%0A                expectedFileLink = new URL(repoUrl + %22blob?r=%22 + projectName + %22&h=%22 + sample.id + %22&f=%22 + URLEncoder.encode(path.getPath(), %22UTF-8%22).replaceAll(%22%5C%5C+%22, %22%2520%22));%0A            %7D else if (editType == EditType.DELETE) %7B%0A                expectedFileLink = null;%0A            %7D else %7B%0A                fail(%22Unexpected edit type %22 + editType.getName());%0A            %7D%0A            String msg = %22Wrong link for path: %22 + path.getPath() + %22, edit type: %22 + editType.getName();%0A            assertEquals(msg, expectedFileLink, fileLink);%0A        %7D%0A
     %7D%0A%0A    @

Add testGetFileLink implementation for GitBlit repo browser