[jboss-svn-commits] JBoss Common SVN: r3525 - in tmpdpl/trunk/impl-vdf/src: test/java/org/jboss/tmpdpl/impl/vdf and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Sep 15 22:31:52 EDT 2009
Author: ALRubinger
Date: 2009-09-15 22:31:52 -0400 (Tue, 15 Sep 2009)
New Revision: 3525
Modified:
tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java
tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java
Log:
[TMPDPL-6] Put back the hashCode/equals contract where equal if created from equal Archives
Modified: tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java
===================================================================
--- tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java 2009-09-15 23:33:49 UTC (rev 3524)
+++ tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java 2009-09-16 02:31:52 UTC (rev 3525)
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.ref.SoftReference;
import java.net.URI;
import java.net.URL;
import java.security.AccessController;
@@ -71,6 +72,12 @@
*/
private Deployment deployment;
+ /**
+ * The possibly-null archive from which this Deployable was
+ * created. Used in determining equals() comparison.
+ */
+ private SoftReference<Archive<?>> createdFromArchive;
+
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -107,9 +114,15 @@
// Export to ZIP
final InputStream zipStream = ZipExporter.exportZip(archive);
+ // Create
+ final String name = archive.getName();
+ VfsVdfDeployableImpl deployable = (VfsVdfDeployableImpl) create(name, zipStream);
+
+ // Set created from
+ deployable.setCreatedFromArchive(new SoftReference<Archive<?>>(archive));
+
// Return
- final String name = archive.getName();
- return create(name, zipStream);
+ return deployable;
}
/**
@@ -147,7 +160,7 @@
}
if (!tmpDir.isDirectory())
{
- throw new IllegalStateException("Temp location mus tbe a directory: " + tmpDir.getAbsolutePath());
+ throw new IllegalStateException("Temp location must be a directory: " + tmpDir.getAbsolutePath());
}
final File tmpFile = new File(tmpDir, name);
tmpFile.deleteOnExit();
@@ -301,11 +314,23 @@
@Override
public int hashCode()
{
- return this.deployment.hashCode();
+ final int prime = 31;
+ int result = 1;
+
+ // If we have been created from an archive, use the archive hashCode
+ if (createdFromArchive != null)
+ {
+ return prime * result + createdFromArchive.get().hashCode();
+ }
+
+ // Use the deployment hash code
+ result = prime * result + ((deployment == null) ? 0 : deployment.hashCode());
+ return result;
}
/**
- * Deployables with equal underlying deployments are equal
+ * Deployables with equal underlying deployments, or
+ * created from archives are equal
* {@inheritDoc}
* @see java.lang.Object#equals(java.lang.Object)
*/
@@ -319,6 +344,23 @@
if (getClass() != obj.getClass())
return false;
final VfsVdfDeployableImpl other = (VfsVdfDeployableImpl) obj;
+
+ // Equal if the archives created from are equal
+ if (createdFromArchive != null)
+ {
+ final SoftReference<Archive<?>> otherCreatedFromArchive = other.getCreatedFromArchive();
+ if (otherCreatedFromArchive == null)
+ {
+ return false;
+ }
+ else
+ {
+ if (createdFromArchive.get().equals(otherCreatedFromArchive.get()))
+ {
+ return true;
+ }
+ }
+ }
if (deployment == null)
{
if (other.deployment != null)
@@ -326,6 +368,7 @@
}
else if (!deployment.equals(other.deployment))
return false;
+
return true;
}
@@ -341,4 +384,20 @@
this.deployment = deployment;
}
+ /**
+ * @return the createdFromArchive
+ */
+ private SoftReference<Archive<?>> getCreatedFromArchive()
+ {
+ return createdFromArchive;
+ }
+
+ /**
+ * @param createdFromArchive the createdFromArchive to set
+ */
+ private void setCreatedFromArchive(SoftReference<Archive<?>> createdFromArchive)
+ {
+ this.createdFromArchive = createdFromArchive;
+ }
+
}
Modified: tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java
===================================================================
--- tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java 2009-09-15 23:33:49 UTC (rev 3524)
+++ tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java 2009-09-16 02:31:52 UTC (rev 3525)
@@ -87,10 +87,11 @@
// Make some test Deployables from the archives
final Deployable deployable1 = VfsVdfDeployableImpl.create(archive1);
final Deployable deployable2 = VfsVdfDeployableImpl.create(archive2);
+ final Deployable deployable3 = VfsVdfDeployableImpl.create(archive1);
// Test equals
- Assert
- .assertTrue("Deployables pointing to same itself should be equal by value", deployable1.equals(deployable1));
+ Assert.assertTrue("Deployables made from same archive should be equal by value", deployable1.equals(deployable3));
+ Assert.assertTrue("Deployables must be equal by symmetry; a == b then b == a", deployable3.equals(deployable1));
Assert.assertFalse("Deployables pointing to different archives should not be equal by value", deployable1
.equals(deployable2));
Assert.assertFalse("Deployables must not be equal to null value", deployable1.equals(null));
@@ -98,8 +99,11 @@
// Get hashCodes
final int deployable1HashCode = deployable1.hashCode();
final int deployable1HashCodeAgain = deployable1.hashCode();
+ final int deployable3HashCode = deployable3.hashCode();
// Test hashCodes
+ Assert.assertEquals("Deployables with equal values must have equal hash codes", deployable1HashCode,
+ deployable3HashCode);
Assert.assertEquals("hash code op must be indempotent", deployable1HashCode, deployable1HashCodeAgain);
/*
More information about the jboss-svn-commits
mailing list