|
| 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 | +public class GitoriousWebTest extends TestCase { |
| 21 | + |
| 22 | + /** |
| 23 | + * |
| 24 | + */ |
| 25 | + private static final String GITORIOUS_URL = "https://SERVER/PROJECT"; |
| 26 | + private final GitoriousWeb gitoriousWeb; |
| 27 | + |
| 28 | + { |
| 29 | + try { |
| 30 | + gitoriousWeb = new GitoriousWeb(GITORIOUS_URL); |
| 31 | + } catch (MalformedURLException e) { |
| 32 | + throw new RuntimeException(e); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}. |
| 38 | + * @throws MalformedURLException |
| 39 | + */ |
| 40 | + public void testGetUrl() throws MalformedURLException { |
| 41 | + assertEquals(String.valueOf(gitoriousWeb.getUrl()), GITORIOUS_URL + "/"); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}. |
| 46 | + * @throws MalformedURLException |
| 47 | + */ |
| 48 | + public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException { |
| 49 | + assertEquals(String.valueOf(new GitoriousWeb(GITORIOUS_URL + "/").getUrl()), GITORIOUS_URL + "/"); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}. |
| 54 | + * @throws SAXException |
| 55 | + * @throws IOException |
| 56 | + */ |
| 57 | + public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException { |
| 58 | + final URL changeSetLink = gitoriousWeb.getChangeSetLink(createChangeSet("rawchangelog")); |
| 59 | + assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}. |
| 64 | + * @throws SAXException |
| 65 | + * @throws IOException |
| 66 | + */ |
| 67 | + public void testGetDiffLinkPath() throws IOException, SAXException { |
| 68 | + final HashMap<String, Path> pathMap = createPathMap("rawchangelog"); |
| 69 | + final Path modified1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java"); |
| 70 | + assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d/diffs?diffmode=sidebyside&fragment=1#src/main/java/hudson/plugins/git/browser/GithubWeb.java", gitoriousWeb.getDiffLink(modified1).toString()); |
| 71 | + // For added files returns a link to the commit. |
| 72 | + final Path added = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file"); |
| 73 | + assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d/diffs?diffmode=sidebyside&fragment=1#src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file", gitoriousWeb.getDiffLink(added).toString()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}. |
| 78 | + * @throws SAXException |
| 79 | + * @throws IOException |
| 80 | + */ |
| 81 | + public void testGetFileLinkPath() throws IOException, SAXException { |
| 82 | + final HashMap<String,Path> pathMap = createPathMap("rawchangelog"); |
| 83 | + final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java"); |
| 84 | + final URL fileLink = gitoriousWeb.getFileLink(path); |
| 85 | + assertEquals(GITORIOUS_URL + "/blobs/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java", String.valueOf(fileLink)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}. |
| 90 | + * @throws SAXException |
| 91 | + * @throws IOException |
| 92 | + */ |
| 93 | + public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException { |
| 94 | + final HashMap<String,Path> pathMap = createPathMap("rawchangelog-with-deleted-file"); |
| 95 | + final Path path = pathMap.get("bar"); |
| 96 | + final URL fileLink = gitoriousWeb.getFileLink(path); |
| 97 | + assertEquals(GITORIOUS_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f/diffs?diffmode=sidebyside&fragment=1#bar", String.valueOf(fileLink)); |
| 98 | + } |
| 99 | + |
| 100 | + private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException { |
| 101 | + final File rawchangelog = new File(GitoriousWebTest.class.getResource(rawchangelogpath).getFile()); |
| 102 | + final GitChangeLogParser logParser = new GitChangeLogParser(false); |
| 103 | + final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs(); |
| 104 | + return changeSetList.get(0); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param changelog |
| 109 | + * @return |
| 110 | + * @throws IOException |
| 111 | + * @throws SAXException |
| 112 | + */ |
| 113 | + private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException { |
| 114 | + final HashMap<String, Path> pathMap = new HashMap<String, Path>(); |
| 115 | + final Collection<Path> changeSet = createChangeSet(changelog).getPaths(); |
| 116 | + for (final Path path : changeSet) { |
| 117 | + pathMap.put(path.getPath(), path); |
| 118 | + } |
| 119 | + return pathMap; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +} |
0 commit comments