|
| 1 | +package hudson.scm.browsers; |
| 2 | + |
| 3 | +import hudson.model.FreeStyleProject; |
| 4 | +import hudson.scm.CVSChangeLogSet; |
| 5 | +import hudson.scm.CVSSCM; |
| 6 | +import hudson.scm.CvsModule; |
| 7 | +import hudson.scm.CvsRepository; |
| 8 | +import hudson.scm.CvsRepositoryItem; |
| 9 | +import hudson.scm.CvsRepositoryLocation; |
| 10 | +import hudson.scm.ExcludedRegion; |
| 11 | +import hudson.scm.VisibleJenkinsRule; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.MalformedURLException; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertNull; |
| 24 | +import static org.junit.Assert.assertSame; |
| 25 | + |
| 26 | +public class CvsFacadeRepositoryBrowserTest { |
| 27 | + |
| 28 | + @Rule |
| 29 | + public VisibleJenkinsRule jenkinsRule = new VisibleJenkinsRule(); |
| 30 | + |
| 31 | + private CvsFacadeRepositoryBrowser testCase; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() { |
| 35 | + testCase = new CvsFacadeRepositoryBrowser(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testResolveBrowser() throws MalformedURLException { |
| 40 | + ViewCVS browser = new ViewCVS(new URL("http://localhost/viewcvs/viewcvs.cgi?cvsroot=repo")); |
| 41 | + CvsRepository repository = new CvsRepository(":pserver:host:port/path/to/repo", false, null, new ArrayList<CvsRepositoryItem>(), new ArrayList<ExcludedRegion>(), -1, browser); |
| 42 | + |
| 43 | + CVSChangeLogSet.CVSChangeLog changelog = new CVSChangeLogSet.CVSChangeLog(); |
| 44 | + changelog.setRepository(repository); |
| 45 | + assertSame(browser, testCase.resolveRepositoryBrowser(changelog)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testResolveOldFile() throws MalformedURLException { |
| 50 | + CVSChangeLogSet.CVSChangeLog changelog = new CVSChangeLogSet.CVSChangeLog(); |
| 51 | + assertNull(testCase.resolveRepositoryBrowser(changelog)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testResolveParameterisedRepository() throws IOException { |
| 56 | + ViewCVS browser = new ViewCVS(new URL("http://localhost/viewcvs/viewcvs.cgi?cvsroot=repo")); |
| 57 | + FreeStyleProject p = jenkinsRule.createFreeStyleProject(); |
| 58 | + p = jenkinsRule.createFreeStyleProject("useful project"); |
| 59 | + p.setScm(new CVSSCM(Arrays.asList(new CvsRepository(":pserver:user@host:${otherPort}/path/to/repo", false, null, new ArrayList<CvsRepositoryItem>(), new ArrayList<ExcludedRegion>(), -1, browser)), false, false, false, false, false, false, false)); |
| 60 | + |
| 61 | + |
| 62 | + CvsRepository repository = new CvsRepository(":pserver:host:${port}/path/to/repo", false, null, new ArrayList<CvsRepositoryItem>(), new ArrayList<ExcludedRegion>(), -1, null); |
| 63 | + |
| 64 | + |
| 65 | + CVSChangeLogSet.CVSChangeLog changelog = new CVSChangeLogSet.CVSChangeLog(); |
| 66 | + changelog.setRepository(repository); |
| 67 | + |
| 68 | + //we don't expect a browser match, and don't expect an error |
| 69 | + assertNull(testCase.resolveRepositoryBrowser(changelog)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testResolveBrowserFromAlternativeJob() throws IOException { |
| 74 | + // create a few projects with browsers |
| 75 | + for (int k = 0; k < 10; k++) { |
| 76 | + FreeStyleProject p = jenkinsRule.createFreeStyleProject("job " + k); |
| 77 | + List<CvsRepository> repositoryList = new ArrayList<CvsRepository>(); |
| 78 | + for (int j = 0; j < 5; j++) { |
| 79 | + CvsModule[] modules = new CvsModule[10]; |
| 80 | + for (int i = 0; i < modules.length; i++) { |
| 81 | + modules[i] = new CvsModule("remote" + i, "remote" + i); |
| 82 | + } |
| 83 | + CvsRepositoryItem item = new CvsRepositoryItem(new CvsRepositoryLocation.HeadRepositoryLocation(), modules); |
| 84 | + repositoryList.add(new CvsRepository(":pserver:user@host:" + j + "/path/to/repo", false, null, Arrays.asList(item), new ArrayList<ExcludedRegion>(), -1, new ViewCVS(new URL("http://host:" + j + "/viewcvs/viewcvs.cgi?cvsroot=root")))); |
| 85 | + } |
| 86 | + p.setScm(new CVSSCM(repositoryList, false, false, false, false, false, false, false)); |
| 87 | + } |
| 88 | + |
| 89 | + FreeStyleProject p = jenkinsRule.createFreeStyleProject("null browser job"); |
| 90 | + |
| 91 | + // now add a job with no browser |
| 92 | + List<CvsRepository> repositoryList = new ArrayList<CvsRepository>(); |
| 93 | + for (int j = 0; j < 5; j++) { |
| 94 | + CvsModule[] modules = new CvsModule[10]; |
| 95 | + for (int i = 0; i < modules.length; i++) { |
| 96 | + modules[i] = new CvsModule("remote" + i, "remote" + i); |
| 97 | + } |
| 98 | + CvsRepositoryItem item = new CvsRepositoryItem(new CvsRepositoryLocation.HeadRepositoryLocation(), modules); |
| 99 | + repositoryList.add(new CvsRepository(":pserver:user@host:" + j + "/path/to/repo", false, null, Arrays.asList(item), new ArrayList<ExcludedRegion>(), -1, null)); |
| 100 | + } |
| 101 | + p.setScm(new CVSSCM(repositoryList, false, false, false, false, false, false, false)); |
| 102 | + |
| 103 | + // add a job with the browser we expect to get, and a cvs root that matches our test root |
| 104 | + ViewCVS browser = new ViewCVS(new URL("http://localhost/viewcvs/viewcvs.cgi?cvsroot=repo")); |
| 105 | + p = jenkinsRule.createFreeStyleProject("useful project"); |
| 106 | + p.setScm(new CVSSCM(Arrays.asList(new CvsRepository(":pserver:host:10/path/to/repo", false, null, new ArrayList<CvsRepositoryItem>(), new ArrayList<ExcludedRegion>(), -1, browser)), false, false, false, false, false, false, false)); |
| 107 | + |
| 108 | + |
| 109 | + CvsRepository repository = new CvsRepository(":pserver:host:10/path/to/repo", false, null, new ArrayList<CvsRepositoryItem>(), new ArrayList<ExcludedRegion>(), -1, null); |
| 110 | + |
| 111 | + CVSChangeLogSet.CVSChangeLog changelog = new CVSChangeLogSet.CVSChangeLog(); |
| 112 | + changelog.setRepository(repository); |
| 113 | + assertSame(browser, testCase.resolveRepositoryBrowser(changelog)); |
| 114 | + |
| 115 | + //cal again to check caching hasn't broken anything |
| 116 | + assertSame(browser, testCase.resolveRepositoryBrowser(changelog)); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +} |
0 commit comments