| author | akozak | 2011-11-23 02:29:23 (EST) |
|---|---|---|
| committer | Winston Prakash | 2011-12-01 20:47:05 (EST) |
| commit | 4d65293ca31e89111afe917d6508d0b0bd6060dd (patch) (side-by-side diff) | |
| tree | 965f9d051b15bec643c3702955bb809ab9321eb8 | |
| parent | 28fd63add6c1d105e405dde8964b83fec87d7b9b (diff) | |
| download | org.eclipse.hudson.core-4d65293ca31e89111afe917d6508d0b0bd6060dd.zip org.eclipse.hudson.core-4d65293ca31e89111afe917d6508d0b0bd6060dd.tar.gz org.eclipse.hudson.core-4d65293ca31e89111afe917d6508d0b0bd6060dd.tar.bz2 | |
Implement equals, hashCode methods for tasks. Cover logic with unit-tests
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
8 files changed, 334 insertions, 27 deletions
diff --git a/hudson-core/src/main/java/hudson/tasks/Ant.java b/hudson-core/src/main/java/hudson/tasks/Ant.java index fc4f034..636e00b 100644 --- a/hudson-core/src/main/java/hudson/tasks/Ant.java +++ b/hudson-core/src/main/java/hudson/tasks/Ant.java @@ -1,6 +1,6 @@ /******************************************************************************* * - * Copyright (c) 2004-2010 Oracle Corporation. + * Copyright (c) 2004-2011 Oracle Corporation. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,7 +9,7 @@ * * Contributors: * - * Kohsuke Kawaguchi, Tom Huybrechts, Yahoo! Inc. + * Kohsuke Kawaguchi, Tom Huybrechts, Yahoo! Inc., Nikita Levyankov * * *******************************************************************************/ @@ -44,6 +44,9 @@ import hudson.util.VariableResolver; import hudson.util.FormValidation; import hudson.util.XStream2; import net.sf.json.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.QueryParameter; @@ -93,9 +96,9 @@ public class Ant extends Builder { public Ant(String targets,String antName, String antOpts, String buildFile, String properties) { this.targets = targets; this.antName = antName; - this.antOpts = Util.fixEmptyAndTrim(antOpts); - this.buildFile = Util.fixEmptyAndTrim(buildFile); - this.properties = Util.fixEmptyAndTrim(properties); + this.antOpts = StringUtils.trimToNull(antOpts); + this.buildFile = StringUtils.trimToNull(buildFile); + this.properties = StringUtils.trimToNull(properties); } public String getBuildFile() { @@ -452,4 +455,33 @@ public class Ant extends Builder { } } } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Ant that = (Ant) o; + return new EqualsBuilder() + .append(antName, that.antName) + .append(antOpts, that.antOpts) + .append(buildFile, that.buildFile) + .append(properties, that.properties) + .append(targets, that.targets) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder() + .append(targets) + .append(antName) + .append(antOpts) + .append(buildFile) + .append(properties) + .toHashCode(); + } } diff --git a/hudson-core/src/main/java/hudson/tasks/CommandInterpreter.java b/hudson-core/src/main/java/hudson/tasks/CommandInterpreter.java index eeceb5c..9ce027b 100644 --- a/hudson-core/src/main/java/hudson/tasks/CommandInterpreter.java +++ b/hudson-core/src/main/java/hudson/tasks/CommandInterpreter.java @@ -1,6 +1,6 @@ /******************************************************************************* * - * Copyright (c) 2004-2009 Oracle Corporation. + * Copyright (c) 2004-2011 Oracle Corporation. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,7 +9,7 @@ * * Contributors: * -* Kohsuke Kawaguchi, Tom Huybrechts +* Kohsuke Kawaguchi, Tom Huybrechts, Nikita Levyankov * * *******************************************************************************/ @@ -26,6 +26,7 @@ import hudson.model.TaskListener; import java.io.IOException; import java.util.Map; +import org.apache.commons.lang3.StringUtils; /** * Common part between {@link Shell} and {@link BatchFile}. @@ -90,6 +91,25 @@ public abstract class CommandInterpreter extends Builder { } } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + CommandInterpreter that = (CommandInterpreter) o; + return StringUtils.equalsIgnoreCase(command, that.command); + } + + @Override + public int hashCode() { + return command != null ? command.hashCode() : 0; + } + /** * Creates a script file in a temporary name in the specified directory. */ diff --git a/hudson-core/src/main/java/hudson/tasks/Maven.java b/hudson-core/src/main/java/hudson/tasks/Maven.java index 9cf3202..9cf462b 100644 --- a/hudson-core/src/main/java/hudson/tasks/Maven.java +++ b/hudson-core/src/main/java/hudson/tasks/Maven.java @@ -1,6 +1,6 @@ /******************************************************************************* * - * Copyright (c) 2004-2010 Oracle Corporation. + * Copyright (c) 2004-2011 Oracle Corporation. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,7 +9,7 @@ * * Contributors: * - * Kohsuke Kawaguchi, Jene Jasper, Stephen Connolly, Tom Huybrechts, Yahoo! Inc. + * Kohsuke Kawaguchi, Jene Jasper, Stephen Connolly, Tom Huybrechts, Yahoo! Inc., Nikita Levyankov * * *******************************************************************************/ @@ -48,6 +48,9 @@ import hudson.util.VariableResolver; import hudson.util.FormValidation; import hudson.util.XStream2; import net.sf.json.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.QueryParameter; @@ -122,9 +125,9 @@ public class Maven extends Builder { public Maven(String targets,String name, String pom, String properties, String jvmOptions, boolean usePrivateRepository) { this.targets = targets; this.mavenName = name; - this.pom = Util.fixEmptyAndTrim(pom); - this.properties = Util.fixEmptyAndTrim(properties); - this.jvmOptions = Util.fixEmptyAndTrim(jvmOptions); + this.pom = StringUtils.trimToNull(pom); + this.properties = StringUtils.trimToNull(properties); + this.jvmOptions = StringUtils.trimToNull(jvmOptions); this.usePrivateRepository = usePrivateRepository; } @@ -608,4 +611,35 @@ public class Maven extends Builder { MavenInstallation inferMavenInstallation(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Maven that = (Maven) o; + return new EqualsBuilder() + .append(usePrivateRepository, that.usePrivateRepository) + .append(jvmOptions, that.jvmOptions) + .append(mavenName, that.mavenName) + .append(pom, that.pom) + .append(properties, that.properties) + .append(targets, that.targets) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder() + .append(targets) + .append(mavenName) + .append(jvmOptions) + .append(pom) + .append(properties) + .append(usePrivateRepository) + .toHashCode(); + } } diff --git a/hudson-core/src/main/java/hudson/tasks/Shell.java b/hudson-core/src/main/java/hudson/tasks/Shell.java index 43a4b4a..553f32c 100644 --- a/hudson-core/src/main/java/hudson/tasks/Shell.java +++ b/hudson-core/src/main/java/hudson/tasks/Shell.java @@ -1,6 +1,6 @@ /******************************************************************************* * - * Copyright (c) 2004-2010 Oracle Corporation. + * Copyright (c) 2004-2011 Oracle Corporation. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,7 +9,7 @@ * * Contributors: * - * Kohsuke Kawaguchi, Jene Jasper, Yahoo! Inc. + * Kohsuke Kawaguchi, Jene Jasper, Yahoo! Inc., Nikita Levyankov * * *******************************************************************************/ @@ -48,19 +48,11 @@ public class Shell extends CommandInterpreter { private static String fixCrLf(String s) { // eliminate CR int idx; - while((idx=s.indexOf("\r\n"))!=-1) - s = s.substring(0,idx)+s.substring(idx+1); - - //// add CR back if this is for Windows - //if(isWindows()) { - // idx=0; - // while(true) { - // idx = s.indexOf('\n',idx); - // if(idx==-1) break; - // s = s.substring(0,idx)+'\r'+s.substring(idx); - // idx+=2; - // } - //} + if (null != s) { //avoid potential NullPointerException if command is null. + while ((idx = s.indexOf("\r\n")) != -1) { + s = s.substring(0, idx) + s.substring(idx + 1); + } + } return s; } diff --git a/hudson-core/src/test/java/hudson/tasks/AntEqualsHashCodeTest.java b/hudson-core/src/test/java/hudson/tasks/AntEqualsHashCodeTest.java new file mode 100644 index 0000000..50b7b24 --- a/dev/null +++ b/hudson-core/src/test/java/hudson/tasks/AntEqualsHashCodeTest.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * + * Copyright (c) 2011 Oracle Corporation. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * + * Nikita Levyankov + * + * + *******************************************************************************/ +package hudson.tasks; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; + +/** + * Verify equals and hashCode methods for Ant object. + * <p/> + * Date: 10/5/11 + * + * @author Nikita Levyankov + */ +public class AntEqualsHashCodeTest { + private Ant ant = new Ant("targets", "antName", "antOpts", "buildFile", "properties"); + + @Test + public void testHashCode() { + assertEquals(ant.hashCode(), new Ant("targets", "antName", "antOpts", "buildFile", "properties").hashCode()); + assertFalse(ant.hashCode() == new Ant("targets", "antName", "antOpts", "buildFile", "properties1").hashCode()); + assertFalse(ant.hashCode() == new Ant("targets", "antName", "antOpts", "buildFile1", "properties").hashCode()); + assertFalse(ant.hashCode() == new Ant("targets", "antName", "antOpts1", "buildFile", "properties").hashCode()); + assertFalse(ant.hashCode() == new Ant("targets", "antName1", "antOpts", "buildFile", "properties").hashCode()); + assertFalse(ant.hashCode() == new Ant("targets1", "antName", "antOpts", "buildFile", "properties").hashCode()); + assertFalse( + ant.hashCode() == new Ant("targets1", "antName1", "antOpts1", "buildFile1", "properties1").hashCode()); + } + + @Test + public void testEqual() { + assertEquals(ant, ant); + assertFalse(ant.equals(null)); + assertFalse(ant.equals(new Object())); + assertEquals(ant, new Ant("targets", "antName", "antOpts", "buildFile", "properties")); + assertFalse(ant.equals(new Ant("targets", "antName", "antOpts", "buildFile", "properties1"))); + assertFalse(ant.equals(new Ant("targets", "antName", "antOpts", "buildFile1", "properties"))); + assertFalse(ant.equals(new Ant("targets", "antName", "antOpts1", "buildFile", "properties"))); + assertFalse(ant.equals(new Ant("targets", "antName1", "antOpts", "buildFile", "properties"))); + assertFalse(ant.equals(new Ant("targets1", "antName", "antOpts", "buildFile", "properties"))); + assertFalse(ant.equals(new Ant("targets1", "antName1", "antOpts1", "buildFile1", "properties1"))); + } +} diff --git a/hudson-core/src/test/java/hudson/tasks/BatchFileEqualsHashCodeTest.java b/hudson-core/src/test/java/hudson/tasks/BatchFileEqualsHashCodeTest.java new file mode 100644 index 0000000..b21876d --- a/dev/null +++ b/hudson-core/src/test/java/hudson/tasks/BatchFileEqualsHashCodeTest.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * + * Copyright (c) 2011 Oracle Corporation. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * + * Nikita Levyankov + * + * + *******************************************************************************/ + +package hudson.tasks; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; + +/** + * Verify equals and hashCode methods for BatchFile object. + * <p/> + * Date: 10/5/11 + * + * @author Nikita Levyankov + */ +public class BatchFileEqualsHashCodeTest { + @Test + public void testHashCode() { + assertEquals(new BatchFile(null).hashCode(), new BatchFile(null).hashCode()); + assertEquals(new BatchFile("").hashCode(), new BatchFile("").hashCode()); + assertEquals(new BatchFile("echo").hashCode(), new BatchFile("echo").hashCode()); + assertFalse(new BatchFile("echo 'test'").hashCode() == new BatchFile("echo '123'").hashCode()); + assertFalse(new BatchFile(null).hashCode() == new BatchFile("echo '123'").hashCode()); + } + + @Test + public void testEqual() { + BatchFile echo = new BatchFile("echo"); + assertEquals(echo, echo); + assertFalse(new BatchFile("echo").equals(null)); + assertFalse(echo.equals(new Object())); + assertEquals(echo, new BatchFile("echo")); + assertEquals(new BatchFile(null), new BatchFile(null)); + assertEquals(new BatchFile(""), new BatchFile("")); + assertFalse(new BatchFile("echo 'test'").equals(new BatchFile("echo '123'"))); + assertFalse(new BatchFile(null).equals(new BatchFile("echo '123'"))); + } +}
\ No newline at end of file diff --git a/hudson-core/src/test/java/hudson/tasks/MavenEqualsHashCodeTest.java b/hudson-core/src/test/java/hudson/tasks/MavenEqualsHashCodeTest.java new file mode 100644 index 0000000..e1ad311 --- a/dev/null +++ b/hudson-core/src/test/java/hudson/tasks/MavenEqualsHashCodeTest.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * + * Copyright (c) 2011 Oracle Corporation. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * + * Nikita Levyankov + * + * + *******************************************************************************/ +package hudson.tasks; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; + +/** + * Verify equals and hashCode methods for Maven object. + * <p/> + * Date: 10/5/11 + * + * @author Nikita Levyankov + */ +public class MavenEqualsHashCodeTest { + + private Maven maven = new Maven("targets", "name", "pom", "properties", "jvmOptions", false); + + @Test + public void testHashCode() { + assertEquals(maven.hashCode(), + new Maven("targets", "name", "pom", "properties", "jvmOptions", false).hashCode()); + assertFalse( + maven.hashCode() == new Maven("targets", "name", "pom", "properties", "jvmOptions", true).hashCode()); + assertFalse( + maven.hashCode() == new Maven("targets", "name", "pom", "properties", "jvmOptions1", false).hashCode()); + assertFalse( + maven.hashCode() == new Maven("targets", "name", "pom", "properties1", "jvmOptions", false).hashCode()); + assertFalse( + maven.hashCode() == new Maven("targets", "name", "pom1", "properties", "jvmOptions", false).hashCode()); + assertFalse( + maven.hashCode() == new Maven("targets", "name1", "pom", "properties", "jvmOptions", false).hashCode()); + assertFalse( + maven.hashCode() == new Maven("targets1", "name", "pom", "properties", "jvmOptions", false).hashCode()); + } + + @Test + public void testEqual() { + assertEquals(maven, maven); + assertFalse(maven.equals(null)); + assertFalse(maven.equals(new Object())); + assertEquals(maven, new Maven("targets", "name", "pom", "properties", "jvmOptions", false)); + assertFalse(maven.equals(new Maven("targets", "name", "pom", "properties", "jvmOptions", true))); + assertFalse(maven.equals(new Maven("targets", "name", "pom", "properties", "jvmOptions1", false))); + assertFalse(maven.equals(new Maven("targets", "name", "pom1", "properties", "jvmOptions", false))); + assertFalse(maven.equals(new Maven("targets", "name1", "pom", "properties", "jvmOptions", false))); + assertFalse(maven.equals(new Maven("targets1", "name", "pom", "properties", "jvmOptions", false))); + assertFalse(maven.equals(new Maven("targets1", "name1", "pom1", "properties1", "jvmOptions1", true))); + } +} diff --git a/hudson-core/src/test/java/hudson/tasks/ShellEqualsHashCodeTest.java b/hudson-core/src/test/java/hudson/tasks/ShellEqualsHashCodeTest.java new file mode 100644 index 0000000..d5684e3 --- a/dev/null +++ b/hudson-core/src/test/java/hudson/tasks/ShellEqualsHashCodeTest.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * + * Copyright (c) 2011 Oracle Corporation. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * + * Nikita Levyankov + * + * + *******************************************************************************/ +package hudson.tasks; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; + +/** + * Verify equals and hashCode methods for Shell object. + * <p/> + * Date: 10/5/11 + * + * @author Nikita Levyankov + */ +public class ShellEqualsHashCodeTest { + + @Test + public void testHashCode() { + assertEquals(new Shell(null).hashCode(), new Shell(null).hashCode()); + assertEquals(new Shell("").hashCode(), new Shell("").hashCode()); + assertEquals(new Shell("echo").hashCode(), new Shell("echo").hashCode()); + assertFalse(new Shell("echo 'test'").hashCode() == new Shell("echo '123'").hashCode()); + assertFalse(new Shell(null).hashCode() == new Shell("echo '123'").hashCode()); + } + + @Test + public void testEqual() { + Shell echo = new Shell("echo"); + assertEquals(echo, echo); + assertFalse(new Shell("echo").equals(null)); + assertFalse(echo.equals(new Object())); + assertEquals(echo, new Shell("echo")); + assertEquals(new Shell(null), new Shell(null)); + assertEquals(new Shell(""), new Shell("")); + assertFalse(new Shell("echo 'test'").equals(new Shell("echo '123'"))); + assertFalse(new Shell(null).equals(new Shell("echo '123'"))); + } +} |

