From 6521d8efcee3c65a3055a57dfd81148e7a221bf5 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 2 Apr 2009 23:29:15 +0000 Subject: [PATCH] added a new assertion method git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@16839 71c3de6d-444a-0410-be80-ed276b4c234a --- .../org/jvnet/hudson/test/HudsonTestCase.java | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index f8ce0f112c..e79f85519c 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -79,7 +79,6 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; -import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Arrays; @@ -88,6 +87,7 @@ import java.util.logging.Filter; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import java.beans.PropertyDescriptor; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; @@ -96,9 +96,8 @@ import junit.framework.TestCase; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.beanutils.PropertyUtils; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; import org.jvnet.hudson.test.HudsonHomeLoader.CopyExisting; import org.jvnet.hudson.test.recipes.Recipe; @@ -121,7 +120,6 @@ import org.w3c.css.sac.CSSException; import org.w3c.css.sac.CSSParseException; import org.w3c.css.sac.ErrorHandler; import org.xml.sax.SAXException; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import com.gargoylesoftware.htmlunit.AjaxController; import com.gargoylesoftware.htmlunit.BrowserVersion; @@ -132,7 +130,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlButton; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlElement; -import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine; import com.gargoylesoftware.htmlunit.javascript.host.Stylesheet; import com.gargoylesoftware.htmlunit.javascript.host.XMLHttpRequest; @@ -559,6 +556,39 @@ public abstract class HudsonTestCase extends TestCase { return new StreamTaskListener(new CloseProofOutputStream(System.out)); } + /** + * Asserts that two JavaBeans are equal as far as the given list of properties are concerned. + * + *

+ * This method takes two objects that have properties (getXyz, isXyz, or just the public xyz field), + * and makes sure that the property values for each given property are equals (by using {@link #assertEquals(Object, Object)}) + * + *

+ * Property values can be null on both objects, and that is OK, but passing in a property that doesn't + * exist will fail an assertion. + * + *

+ * This method is very convenient for comparing a large number of properties on two objects, + * for example to verify that the configuration is identical after a config screen roundtrip. + * + * @param lhs + * One of the two objects to be compared. + * @param rhs + * The other object to be compared + * @param properties + * ','-separated list of property names that are compared. + * @since 1.297 + */ + public void assertEqualBeans(Object lhs, Object rhs, String properties) throws Exception { + for (String p : properties.split(",")) { + PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(lhs, p); + assertNotNull("No such property "+p+" on "+lhs.getClass(),pd); + Object lp = PropertyUtils.getProperty(lhs, p); + Object rp = PropertyUtils.getProperty(rhs, p); + assertEquals("Property "+p+" is different",lp,rp); + } + } + // // recipe methods. Control the test environments.