|
| 1 | +package hudson.plugins.git.browser; |
| 2 | + |
| 3 | +import hudson.plugins.git.GitChangeLogParser; |
| 4 | +import hudson.plugins.git.GitChangeSet; |
| 5 | +import hudson.plugins.git.GitChangeSet.Path; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.MalformedURLException; |
| 10 | +import java.net.URL; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import junit.framework.TestCase; |
| 16 | + |
| 17 | +import org.xml.sax.SAXException; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Paul Nyheim (paul.nyheim@gmail.com) |
| 21 | + */ |
| 22 | +public class ViewGitWebTest extends TestCase { |
| 23 | + |
| 24 | + private static final String VIEWGIT_URL = "http://SERVER/viewgit"; |
| 25 | + private static final String PROJECT_NAME = "PROJECT"; |
| 26 | + private final ViewGitWeb viewGitWeb; |
| 27 | + |
| 28 | + { |
| 29 | + try { |
| 30 | + viewGitWeb = new ViewGitWeb(VIEWGIT_URL, PROJECT_NAME); |
| 31 | + } catch (MalformedURLException e) { |
| 32 | + throw new RuntimeException(e); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Test method for {@link hudson.plugins.git.browser.ViewGitWeb#getUrl()}. |
| 38 | + * |
| 39 | + * @throws MalformedURLException |
| 40 | + */ |
| 41 | + public void testGetUrl() throws MalformedURLException { |
| 42 | + assertEquals(String.valueOf(viewGitWeb.getUrl()), VIEWGIT_URL + "/"); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test method for {@link hudson.plugins.git.browser.ViewGitWeb#getUrl()}. |
| 47 | + * |
| 48 | + * @throws MalformedURLException |
| 49 | + */ |
| 50 | + public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException { |
| 51 | + assertEquals(String.valueOf(new ViewGitWeb(VIEWGIT_URL + "/", PROJECT_NAME).getUrl()), VIEWGIT_URL + "/"); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test method for |
| 56 | + * {@link hudson.plugins.git.browser.ViewGitWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)} |
| 57 | + * . |
| 58 | + * |
| 59 | + * @throws SAXException |
| 60 | + * @throws IOException |
| 61 | + */ |
| 62 | + public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException { |
| 63 | + final URL changeSetLink = viewGitWeb.getChangeSetLink(createChangeSet("rawchangelog")); |
| 64 | + assertEquals("http://SERVER/viewgit/?p=PROJECT&a=commit&h=396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test method for |
| 69 | + * {@link hudson.plugins.git.browser.ViewGitWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)} |
| 70 | + * . |
| 71 | + * |
| 72 | + * @throws SAXException |
| 73 | + * @throws IOException |
| 74 | + */ |
| 75 | + public void testGetDiffLinkPath() throws IOException, SAXException { |
| 76 | + final HashMap<String, Path> pathMap = createPathMap("rawchangelog"); |
| 77 | + final Path path1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java"); |
| 78 | + assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=396fc230a3db05c427737aa5c2eb7856ba72b05d#src%2Fmain%2Fjava%2Fhudson%2Fplugins%2Fgit%2Fbrowser%2FGithubWeb.java", viewGitWeb.getDiffLink(path1).toString()); |
| 79 | + final Path path2 = pathMap.get("src/test/java/hudson/plugins/git/browser/GithubWebTest.java"); |
| 80 | + assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=396fc230a3db05c427737aa5c2eb7856ba72b05d#src%2Ftest%2Fjava%2Fhudson%2Fplugins%2Fgit%2Fbrowser%2FGithubWebTest.java", viewGitWeb.getDiffLink(path2).toString()); |
| 81 | + final Path path3 = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file"); |
| 82 | + assertNull("Do not return a diff link for added files.", viewGitWeb.getDiffLink(path3)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test method for |
| 87 | + * {@link hudson.plugins.git.browser.ViewGitWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)} |
| 88 | + * . |
| 89 | + * |
| 90 | + * @throws SAXException |
| 91 | + * @throws IOException |
| 92 | + */ |
| 93 | + public void testGetFileLinkPath() throws IOException, SAXException { |
| 94 | + final HashMap<String, Path> pathMap = createPathMap("rawchangelog"); |
| 95 | + final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java"); |
| 96 | + final URL fileLink = viewGitWeb.getFileLink(path); |
| 97 | + assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=viewblob&h=3f28ad75f5ecd5e0ea9659362e2eef18951bd451&f=src/main/java/hudson/plugins/git/browser/GithubWeb.java", |
| 98 | + String.valueOf(fileLink)); |
| 99 | + } |
| 100 | + |
| 101 | + public void testGetDiffLinkForDeletedFile() throws Exception{ |
| 102 | + final HashMap<String, Path> pathMap = createPathMap("rawchangelog-with-deleted-file"); |
| 103 | + final Path path = pathMap.get("bar"); |
| 104 | + assertNull("Do not return a diff link for deleted files.", viewGitWeb.getDiffLink(path)); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test method for |
| 110 | + * {@link hudson.plugins.git.browser.ViewGitWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)} |
| 111 | + * . |
| 112 | + * |
| 113 | + * @throws SAXException |
| 114 | + * @throws IOException |
| 115 | + */ |
| 116 | + public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException { |
| 117 | + final HashMap<String, Path> pathMap = createPathMap("rawchangelog-with-deleted-file"); |
| 118 | + final Path path = pathMap.get("bar"); |
| 119 | + final URL fileLink = viewGitWeb.getFileLink(path); |
| 120 | + assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar", String.valueOf(fileLink)); |
| 121 | + } |
| 122 | + |
| 123 | + private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException { |
| 124 | + final File rawchangelog = new File(ViewGitWebTest.class.getResource(rawchangelogpath).getFile()); |
| 125 | + final GitChangeLogParser logParser = new GitChangeLogParser(false); |
| 126 | + final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs(); |
| 127 | + return changeSetList.get(0); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param changelog |
| 132 | + * @return |
| 133 | + * @throws IOException |
| 134 | + * @throws SAXException |
| 135 | + */ |
| 136 | + private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException { |
| 137 | + final HashMap<String, Path> pathMap = new HashMap<String, Path>(); |
| 138 | + final Collection<Path> changeSet = createChangeSet(changelog).getPaths(); |
| 139 | + for (final Path path : changeSet) { |
| 140 | + pathMap.put(path.getPath(), path); |
| 141 | + } |
| 142 | + return pathMap; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments