SCMUtil { public static String getConnectionString(final Properties versionInfo, final boolean hideConfidentialInformation) throws IOException { final String type = versionInfo.getProperty("type"); final StringBuilder connectionString = new StringBuilder(128); if (type != null && type.equals("git")) { final String repo = versionInfo.getProperty("repo"); if(StringUtils.isBlank(repo)) { if(! StringUtils.isBlank(versionInfo.getProperty("repo_1"))) { throw new IllegalStateException("Multipe git repositories provided. This use case is not supported. Provide only one git repository."); } throw new IllegalStateException("No git repository provided. "); } if(hideConfidentialInformation) { try { URI uri = new URI(repo); int port = uri.getPort(); if(port == -1) { final String scheme = uri.getScheme(); if(scheme != null && gitDefaultPorts.containsKey(scheme)) { port = gitDefaultPorts.get(scheme); } } connectionString.append(port); if(uri.getHost() != null) { connectionString.append(uri.getPath()); } } catch (URISyntaxException e) { throw new IllegalStateException(String.format("Invalid repository uri: %s", repo)); } } else { connectionString.append(PROTOCOL_PREFIX_GIT).append(repo); } } else { final String port = versionInfo.getProperty("port"); if(StringUtils.isBlank(port)) throw new IllegalStateException("No SCM port provided."); final String depotPath = versionInfo.getProperty("depotpath"); if(hideConfidentialInformation) { try { URI perforceUri = new URI("perforce: int _port = perforceUri.getPort(); if(_port == -1) { _port = PERFORCE_DEFAULT_PORT; } connectionString.append(_port); connectionString.append(depotPath); } catch (URISyntaxException e) { throw new IllegalStateException(String.format("Invalid port: %s", port)); } } else { if(StringUtils.isBlank(depotPath)) throw new IllegalStateException("No depot path provided."); connectionString.append(PROTOCOL_PREFIX_PERFORCE) .append(port).append(":") .append(getDepotPath(depotPath)); } } return connectionString.toString(); }  static String getConnectionString(final Properties versionInfo, final boolean hideConfidentialInformation); static String getRevision(final Properties versionInfo);  }
@Test public void testPerforceShowConfidentialInformation() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("port", "scm.example.com:1234"); versionInfo.setProperty("depotpath", " String connectionString = SCMUtil.getConnectionString(versionInfo, false); Assert.assertEquals("scm:perforce:scm.example.com:1234: }
@Test public void testPerforceHideConfidentialInformation() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("port", "scm.example.com:1234"); versionInfo.setProperty("depotpath", " String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("1234 }
@Test public void testGitShowConfidentialInformation() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "ssh: String connectionString = SCMUtil.getConnectionString(versionInfo, false); Assert.assertEquals("scm:git:ssh: }
@Test public void testGitHideConfidentialInformationPortNotSpecified() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "ssh: String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("29418/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformation() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "ssh: String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("1234/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformationWithoutPortProtocolSSH() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "ssh: String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("29418/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformationWithoutPortProtocolHTTP() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "http: String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("80/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformationWithoutPortProtocolHTTPS() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "https: String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("443/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformationWithoutPortProtocolGIT() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "git: String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("9418/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformationWithoutPortAndWithoutProtocol() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", " String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("-1/MyProject", connectionString); }
@Test public void testGitHideConfidentialInformationWithoutHostAndPortAndWithoutProtocol() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo", "scm.example.com/MyProject"); String connectionString = SCMUtil.getConnectionString(versionInfo, true); Assert.assertEquals("-1", connectionString); }
@Test(expected=IllegalStateException.class) public void testPerforceNoPort() throws Exception { Properties versionInfo = new Properties(); SCMUtil.getConnectionString(versionInfo, true); }
@Test(expected=IllegalStateException.class) public void testGitNoPort() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); SCMUtil.getConnectionString(versionInfo, true); }
@Test(expected=IllegalStateException.class) public void testPerforceShowConfidentialInformationDepotPathMissing() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("port", "scm.example.com:1234"); SCMUtil.getConnectionString(versionInfo, false); }
@Test(expected=IllegalStateException.class) public void testGitMultipleRepositories() throws Exception { Properties versionInfo = new Properties(); versionInfo.setProperty("type", "git"); versionInfo.setProperty("repo_1", "scm.example1.com:1234"); versionInfo.setProperty("repo_2", "scm.example2.com:1234"); SCMUtil.getConnectionString(versionInfo, false); }