A small change to reduce the complexity of the code (at least according
to PMD).
- only one return
- explicitly uses MSVSSConstants
I was actually looking for a bug (#20128) as I thought it'd be a trivial
fix, but I couldn't find it and got distracted by this.
Kev
Index: MSVSS.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/optional/vss/MSVSS.java,v
retrieving revision 1.42
diff -u -r1.42 MSVSS.java
--- MSVSS.java 17 Dec 2004 10:19:20 -0000 1.42
+++ MSVSS.java 22 Dec 2004 11:08:50 -0000
@@ -452,43 +452,36 @@
protected String getUser() {
return user != null ? FLAG_USER + user : "";
}
-
+
/**
* Gets the version string. This can be to-from "-VLbuild2~Lbuild1", from
* "~Lbuild1" or to "-VLbuild2".
* @return An empty string if neither tolabel or fromlabel are set.
*/
protected String getVersionLabel() {
- if (fromLabel == null && toLabel == null) {
- return "";
- }
- if (fromLabel != null && toLabel != null) {
- if (fromLabel.length() > 31) {
+ StringBuffer sb = new StringBuffer();
+ if (toLabel != null) {
+ if (toLabel.length() > 31) {
+ toLabel = toLabel.substring(0, 30);
+ log("ToLabel is longer than 31 characters, truncated to: "
+ + toLabel, Project.MSG_WARN);
+ }
+ sb.append(MSVSSConstants.FLAG_VERSION_LABEL);
+ sb.append(toLabel);
+ }
+ if (fromLabel != null) {
+ if (fromLabel.length() > 31) {
fromLabel = fromLabel.substring(0, 30);
log("FromLabel is longer than 31 characters, truncated to: "
- + fromLabel, Project.MSG_WARN);
+ + fromLabel, Project.MSG_WARN);
}
- if (toLabel.length() > 31) {
- toLabel = toLabel.substring(0, 30);
- log("ToLabel is longer than 31 characters, truncated to: "
- + toLabel, Project.MSG_WARN);
- }
- return FLAG_VERSION_LABEL + toLabel + VALUE_FROMLABEL + fromLabel;
- } else if (fromLabel != null) {
- if (fromLabel.length() > 31) {
- fromLabel = fromLabel.substring(0, 30);
- log("FromLabel is longer than 31 characters, truncated to: "
- + fromLabel, Project.MSG_WARN);
- }
- return FLAG_VERSION + VALUE_FROMLABEL + fromLabel;
- } else {
- if (toLabel.length() > 31) {
- toLabel = toLabel.substring(0, 30);
- log("ToLabel is longer than 31 characters, truncated to: "
- + toLabel, Project.MSG_WARN);
- }
- return FLAG_VERSION_LABEL + toLabel;
- }
+ if (toLabel == null) {
+ sb.append(MSVSSConstants.FLAG_VERSION);
+ }
+ sb.append(MSVSSConstants.VALUE_FROMLABEL);
+ sb.append(fromLabel);
+ }
+ return sb.toString();
}
/**
Index: MSVSSTest.java
===================================================================
RCS file: MSVSSTest.java
diff -N MSVSSTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MSVSSTest.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,142 @@
+/*
+ * Created on Dec 22, 2004
+ *
+ */
+package org.apache.tools.ant.taskdefs.optional.vss;
+
+import org.apache.tools.ant.Project;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * @author kj
+ *
+ */
+public class MSVSSTest extends TestCase {
+
+ private String date = "2002";
+ private String version = "1";
+
+ private String toLabel;
+ private String fromLabel;
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Gets the version string. This can be to-from "-VLbuild2~Lbuild1", from
+ * "~Lbuild1" or to "-VLbuild2".
+ * @return An empty string if neither tolabel or fromlabel are set.
+ */
+ public String getVersionLabelTwo() {
+ StringBuffer sb = new StringBuffer();
+ if (toLabel != null) {
+ if (toLabel.length() > 31) {
+ toLabel = toLabel.substring(0, 30);
+ }
+ sb.append(MSVSSConstants.FLAG_VERSION_LABEL);
+ sb.append(toLabel);
+ }
+ if (fromLabel != null) {
+ if (fromLabel.length() > 31) {
+ fromLabel = fromLabel.substring(0, 30);
+ }
+ if (toLabel == null) {
+ sb.append(MSVSSConstants.FLAG_VERSION);
+ }
+ sb.append(MSVSSConstants.VALUE_FROMLABEL);
+ sb.append(fromLabel);
+ }
+ return sb.toString();
+ }
+
+ //old code
+ protected String getVersionLabel() {
+ if (fromLabel == null && toLabel == null) {
+ return "";
+ }
+ if (fromLabel != null && toLabel != null) {
+ if (fromLabel.length() > 31) {
+ fromLabel = fromLabel.substring(0, 30);
+ }
+ if (toLabel.length() > 31) {
+ toLabel = toLabel.substring(0, 30);
+ }
+ return MSVSSConstants.FLAG_VERSION_LABEL + toLabel + MSVSSConstants.VALUE_FROMLABEL + fromLabel;
+ } else if (fromLabel != null) {
+ if (fromLabel.length() > 31) {
+ fromLabel = fromLabel.substring(0, 30);
+ }
+ return MSVSSConstants.FLAG_VERSION + MSVSSConstants.VALUE_FROMLABEL + fromLabel;
+ } else {
+ if (toLabel.length() > 31) {
+ toLabel = toLabel.substring(0, 30);
+ }
+ return MSVSSConstants.FLAG_VERSION_LABEL + toLabel;
+ }
+ }
+
+ /**
+ * Constructor for MSVSSTest.
+ * @param arg0
+ */
+ public MSVSSTest(String arg0) {
+ super(arg0);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new MSVSSTest("testGetVersionLabelFromOnly"));
+ suite.addTest(new MSVSSTest("testGetVersionLabelToOnly"));
+ suite.addTest(new MSVSSTest("testGetVersionLabelEmpty"));
+ return suite;
+ }
+
+ public void testGetVersionLabelFromOnly() {
+ try {
+ this.fromLabel = "fromLabel";
+ this.toLabel = "";
+ assertEquals(getVersionLabel(), getVersionLabelTwo());
+ System.out.println(getVersionLabelTwo());
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testGetVersionLabelToOnly() {
+ try {
+ this.fromLabel = "";
+ this.toLabel = "toLabel";
+ assertEquals(getVersionLabel(), getVersionLabelTwo());
+ System.out.println(getVersionLabelTwo());
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testGetVersionLabelEmpty() {
+ try {
+ this.fromLabel = "";
+ this.toLabel = "";
+ assertEquals(getVersionLabel(), getVersionLabelTwo());
+ System.out.println(getVersionLabelTwo());
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]