diff --git a/changelog/src/main/org/apache/maven/cvslib/CvsConnection.java b/changelog/src/main/org/apache/maven/cvslib/CvsConnection.java index 42259d46..5eda9edc 100644 --- a/changelog/src/main/org/apache/maven/cvslib/CvsConnection.java +++ b/changelog/src/main/org/apache/maven/cvslib/CvsConnection.java @@ -212,7 +212,7 @@ public class CvsConnection * Lookup the password in the .cvspass file. This file is looked for in the * user.home directory if the option cvs.passfile is not set * - * @param CVSRoot the CVS root for which the password is being searched + * @param cvsRoot the CVS root for which the password is being searched * @return the password, scrambled */ private static String lookupPassword(String cvsRoot) @@ -229,26 +229,7 @@ public class CvsConnection try { reader = new BufferedReader(new FileReader(passFile)); - String line; - while ((line = reader.readLine()) != null) - { - if (line.startsWith("/")) - { - Vector cvspass = StringUtils.split(line, ' '); - if (cvspass.size() >= 3) - { - if (compareCvsRoot(cvsRoot, (String)cvspass.get(1))) { - password = (String)cvspass.get(2); - break; - } - } - } - else if (line.startsWith(cvsRoot)) - { - password = line.substring(cvsRoot.length() + 1); - break; - } - } + password = processCvspass(cvsRoot, reader); } catch (IOException e) { @@ -276,7 +257,40 @@ public class CvsConnection return password; } - static boolean compareCvsRoot(String cvsRoot, String target) + /** + * Read in a list of return delimited lines from .cvspass and retreive + * the password. Return null if the cvsRoot can't be found. + * + * @param cvsRoot the CVS root for which the password is being searched + * @param reader A buffered reader of lines of cvspass information + * @return The password, or null if it can't be found. + * @throws IOException + */ + static String processCvspass(String cvsRoot, BufferedReader reader) throws IOException { + String line; + String password = null; + while ((line = reader.readLine()) != null) + { + if (line.startsWith("/")) + { + Vector cvspass = StringUtils.split(line, ' '); + String cvspassRoot = (String)cvspass.get(1); + if (compareCvsRoot(cvsRoot, cvspassRoot)) { + int index = line.indexOf(cvspassRoot) + cvspassRoot.length()+1; + password = line.substring(index); + break; + } + } + else if (line.startsWith(cvsRoot)) + { + password = line.substring(cvsRoot.length() + 1); + break; + } + } + return password; + } + + static boolean compareCvsRoot(String cvsRoot, String target) { String s1 = completeCvsRootPort(cvsRoot); String s2 = completeCvsRootPort(target); diff --git a/changelog/src/test/org/apache/maven/cvslib/CvsConnectionTest.java b/changelog/src/test/org/apache/maven/cvslib/CvsConnectionTest.java index 3513dd35..7564cda7 100644 --- a/changelog/src/test/org/apache/maven/cvslib/CvsConnectionTest.java +++ b/changelog/src/test/org/apache/maven/cvslib/CvsConnectionTest.java @@ -17,12 +17,10 @@ package org.apache.maven.cvslib; * ==================================================================== */ -import java.io.FileInputStream; -import java.util.Collection; -import java.util.Iterator; -import junit.framework.TestCase; +import java.io.BufferedReader; +import java.io.StringReader; -import org.apache.maven.changelog.ChangeLogEntry; +import junit.framework.TestCase; /** @@ -85,9 +83,40 @@ public class CvsConnectionTest extends TestCase } - // Add test methods here, they have to start with 'test' name. - // for example: - // public void testHello() {} + /** + * Test of reading in .cvspass file processes different types of lines properly + * @throws Exception when there is an unexpected problem + */ + public void testProcessCvspass() throws Exception + { + String[] expectedResult = { + "A ", + null, + "Axxx ", + "Axxx xxx ", + "A ", + null, + "Axxx ", + "Axxx xxx " + }; + String[] cvspassData = { + ":pserver:user@server:/home/cvs A ", + ":ext:user@server:/home/cvs A ", + ":pserver:user@server:/home/cvs Axxx ", + ":pserver:user@server:/home/cvs Axxx xxx ", + "/1 :pserver:user@server:2401/home/cvs A ", + "/1 :ext:user@server:2401/home/cvs A ", + "/1 :pserver:user@server:2401/home/cvs Axxx ", + "/1 :pserver:user@server:2401/home/cvs Axxx xxx ", + }; + + for (int i = 4;i