aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cummi...@apache.org
Subject svn commit: r1061772 - in /aries/trunk/application/application-modeller/src: main/java/org/apache/aries/application/modelling/impl/ test/java/org/apache/aries/application/modelling/utils/
Date Fri, 21 Jan 2011 11:59:31 GMT
Author: cumminsh
Date: Fri Jan 21 11:59:31 2011
New Revision: 1061772

URL: http://svn.apache.org/viewvc?rev=1061772&view=rev
Log:
ARIES-545: Equals methods for ImportedPackage and ExportedPackage and new unit tests.

Added:
    aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ExportedPackageTest.java
    aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ImportedPackageTest.java
Modified:
    aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ExportedPackageImpl.java
    aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ImportedPackageImpl.java

Modified: aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ExportedPackageImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ExportedPackageImpl.java?rev=1061772&r1=1061771&r2=1061772&view=diff
==============================================================================
--- aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ExportedPackageImpl.java
(original)
+++ aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ExportedPackageImpl.java
Fri Jan 21 11:59:31 2011
@@ -17,13 +17,14 @@
  * under the License.
  */
 package org.apache.aries.application.modelling.impl;
+import static org.apache.aries.application.modelling.ResourceType.PACKAGE;
 import static org.apache.aries.application.utils.AppConstants.LOG_ENTRY;
 import static org.apache.aries.application.utils.AppConstants.LOG_EXIT;
-import static org.apache.aries.application.modelling.ResourceType.PACKAGE;
 
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.aries.application.modelling.ExportedPackage;
 import org.apache.aries.application.modelling.ModelledResource;
@@ -33,9 +34,6 @@ import org.osgi.framework.Version;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
-
-
 public class ExportedPackageImpl implements ExportedPackage
 {
   
@@ -153,5 +151,72 @@ public class ExportedPackageImpl impleme
   public String toString() {
     return toDeploymentString();
   }
+  
+	@Override
+	public boolean equals(Object thing) {
+		if (thing == this) {
+			return true;
+		} else {
+			if (thing instanceof ExportedPackage) {
+				ExportedPackage otherPackage = (ExportedPackage) thing;
+				if (!this.getPackageName()
+						.equals(otherPackage.getPackageName())) {
+					return false;
+				}
+				if (!this.getVersion().equals(otherPackage.getVersion())) {
+					return false;
+				}
+				if (!this.getPackageName()
+						.equals(otherPackage.getPackageName())) {
+					return false;
+				}
+				
+				// We'll pick up the bundle comparisons in the attributes
+				Map<String, Object> otherAttributes = otherPackage
+						.getAttributes();
+
+				if (!attributesAreEqual(otherAttributes)) {
+					return false;
+				}
+			}
+			return true;
+
+		}
+	}
+
+	private boolean attributesAreEqual(Map<String, Object> otherAttributes) {
+		// We better have the same number of attributes
+		if (this.getAttributes().size() != otherAttributes.size()) {
+			return false;
+		}
+
+		for (Entry<String, Object> entry : getAttributes().entrySet()) {
+			String key = entry.getKey();
+			if (otherAttributes != null && otherAttributes.containsKey(key)) {
+				Object otherValue = otherAttributes.get(key);
+				Object value = entry.getValue();
+				if (value == null) {
+					if (otherValue != null) {
+						return false;
+					}
+				} else {
+					if (!value.equals(otherValue)) {
+						return false;
+					}
+				}
+			} else {
+				// We insist every value be present on both sides
+				return false;
+			}
+		}
+
+		return true;
+	}
+  
+  @Override
+  public int hashCode()
+  {
+	  return getPackageName().hashCode();
+  }
 
 }

Modified: aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ImportedPackageImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ImportedPackageImpl.java?rev=1061772&r1=1061771&r2=1061772&view=diff
==============================================================================
--- aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ImportedPackageImpl.java
(original)
+++ aries/trunk/application/application-modeller/src/main/java/org/apache/aries/application/modelling/impl/ImportedPackageImpl.java
Fri Jan 21 11:59:31 2011
@@ -28,8 +28,10 @@ import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.aries.application.InvalidAttributeException;
+import org.apache.aries.application.modelling.ExportedPackage;
 import org.apache.aries.application.modelling.ImportedPackage;
 import org.apache.aries.application.modelling.Provider;
 import org.apache.aries.application.modelling.ResourceType;
@@ -206,4 +208,72 @@ public class ImportedPackageImpl impleme
   public String toString() {
     return toDeploymentString();
   }
-}
+  
+	@Override
+	public boolean equals(Object thing) {
+		if (thing == this) {
+			return true;
+		} else {
+			if (thing instanceof ImportedPackage) {
+				ImportedPackage otherPackage = (ImportedPackage) thing;
+				if (!this.getPackageName()
+						.equals(otherPackage.getPackageName())) {
+					return false;
+				}
+				if (!this.getVersionRange().equals(otherPackage.getVersionRange())) {
+					return false;
+				}
+				if (!this.getPackageName()
+						.equals(otherPackage.getPackageName())) {
+					return false;
+				}
+				Map<String, String> otherAttributes = otherPackage
+						.getAttributes();
+
+				// Ignore the bundle, since the same package imported from
+				// different bundles should count as the same
+
+				if (!attributesAreEqual(otherAttributes)) {
+					return false;
+				}
+			}
+			return true;
+
+		}
+	}
+
+	private boolean attributesAreEqual(Map<String, String> otherAttributes) {
+		// We better have the same number of attributes
+		if (this.getAttributes().size() != otherAttributes.size()) {
+			return false;
+		}
+
+		for (Entry<String, String> entry : getAttributes().entrySet()) {
+			String key = entry.getKey();
+			if (otherAttributes != null && otherAttributes.containsKey(key)) {
+				Object otherValue = otherAttributes.get(key);
+				Object value = entry.getValue();
+				if (value == null) {
+					if (otherValue != null) {
+						return false;
+					}
+				} else {
+					if (!value.equals(otherValue)) {
+						return false;
+					}
+				}
+			} else {
+				// We insist every value be present on both sides
+				return false;
+			}
+		}
+
+		return true;
+	}
+  
+  @Override
+  public int hashCode()
+  {
+	  return getPackageName().hashCode();
+  }
+}
\ No newline at end of file

Added: aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ExportedPackageTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ExportedPackageTest.java?rev=1061772&view=auto
==============================================================================
--- aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ExportedPackageTest.java
(added)
+++ aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ExportedPackageTest.java
Fri Jan 21 11:59:31 2011
@@ -0,0 +1,77 @@
+package org.apache.aries.application.modelling.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.aries.application.modelling.ExportedPackage;
+import org.apache.aries.application.modelling.ModelledResource;
+import org.apache.aries.application.modelling.impl.ExportedPackageImpl;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ExportedPackageTest {
+
+	@Test
+	public void testEqualsForIdenticalPackages() {
+		String packageName = "package.name";
+		String version = "1.0.0";
+		ExportedPackage package1 = instantiatePackage(packageName, version);
+		// I should hope so!
+		assertEquals(package1, package1);
+	}
+
+
+	@Test
+	public void testEqualsForEqualTrivialPackages() {
+		String packageName = "package.name";
+		String version = "1.0.0";
+		ExportedPackage package1 = instantiatePackage(packageName, version);
+		ExportedPackage package2 = instantiatePackage(packageName, version);
+		assertEquals(package1, package2);
+	}
+
+	@Test
+	public void testEqualsForTrivialPackagesWithDifferentName() {
+		String version = "1.0.0";
+		ExportedPackage package1 = instantiatePackage("package.name", version);
+		ExportedPackage package2 = instantiatePackage("other.package.name", version);
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+	
+	@Test
+	public void testEqualsForTrivialPackagesWithDifferentVersion() {
+		String packageName = "package.name";
+		ExportedPackage package1 = instantiatePackage(packageName, "1.0.0");
+		ExportedPackage package2 = instantiatePackage(packageName, "1.0.1");
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+	
+	@Test
+	public void testEqualsForEqualPackagesWithDifferentAttributes() {
+		String packageName = "package.name";
+		String version = "1.0.0";
+		ExportedPackage package1 = instantiatePackage(packageName, version, "att=something");
+		ExportedPackage package2 = instantiatePackage(packageName, version, "att=something.else");
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+
+
+	private ExportedPackage instantiatePackage(String packageName,
+			String version, String ... attributes) {
+		ModelledResource mr = Skeleton.newMock(ModelledResource.class);
+		Map<String, Object> generatedAttributes = new HashMap<String, Object>();
+		generatedAttributes.put(Constants.VERSION_ATTRIBUTE, version);
+		for (String att : attributes)
+		{
+			String[] bits = att.split("=");
+			generatedAttributes.put(bits[0], bits[1]);
+		}
+		return new ExportedPackageImpl(mr, packageName, generatedAttributes);
+	}
+
+
+}

Added: aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ImportedPackageTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ImportedPackageTest.java?rev=1061772&view=auto
==============================================================================
--- aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ImportedPackageTest.java
(added)
+++ aries/trunk/application/application-modeller/src/test/java/org/apache/aries/application/modelling/utils/ImportedPackageTest.java
Fri Jan 21 11:59:31 2011
@@ -0,0 +1,94 @@
+package org.apache.aries.application.modelling.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.aries.application.InvalidAttributeException;
+import org.apache.aries.application.modelling.ImportedPackage;
+import org.apache.aries.application.modelling.impl.ImportedPackageImpl;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ImportedPackageTest {
+
+	@Test
+	public void testEqualsForIdenticalPackages() throws InvalidAttributeException {
+		String packageName = "package.name";
+		String version = "1.0.0";
+		ImportedPackage package1 = instantiatePackage(packageName, version);
+		// I should hope so!
+		assertEquals(package1, package1);
+	}
+
+
+	@Test
+	public void testEqualsForEqualTrivialPackages() throws InvalidAttributeException {
+		String packageName = "package.name";
+		String version = "1.0.0";
+		ImportedPackage package1 = instantiatePackage(packageName, version);
+		ImportedPackage package2 = instantiatePackage(packageName, version);
+		assertEquals(package1, package2);
+	}
+
+	@Test
+	public void testEqualsForEqualPackagesWithVersionRange() throws InvalidAttributeException
{
+		String packageName = "package.name";
+		String version = "[1.0.0, 1.6.3)";
+		ImportedPackage package1 = instantiatePackage(packageName, version);
+		ImportedPackage package2 = instantiatePackage(packageName, version);
+		assertEquals(package1, package2);
+	}
+
+
+	@Test
+	public void testEqualsForTrivialPackagesWithDifferentName() throws InvalidAttributeException
{
+		String version = "1.0.0";
+		ImportedPackage package1 = instantiatePackage("package.name", version);
+		ImportedPackage package2 = instantiatePackage("other.package.name", version);
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+	
+	@Test
+	public void testEqualsForTrivialPackagesWithDifferentVersion() throws InvalidAttributeException
{
+		String packageName = "package.name";
+		ImportedPackage package1 = instantiatePackage(packageName, "1.0.0");
+		ImportedPackage package2 = instantiatePackage(packageName, "1.0.1");
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+	
+	@Test
+	public void testEqualsForTrivialPackagesWithDifferentVersionRanges() throws InvalidAttributeException
{
+		String packageName = "package.name";
+		ImportedPackage package1 = instantiatePackage(packageName, "[1.0.0, 4.4.4)");
+		ImportedPackage package2 = instantiatePackage(packageName, "[1.0.0, 4.4.2)");
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+
+
+	@Test
+	public void testEqualsForEqualPackagesWithDifferentAttributes() throws InvalidAttributeException
{
+		String packageName = "package.name";
+		String version = "1.0.0";
+		ImportedPackage package1 = instantiatePackage(packageName, version, "att=something");
+		ImportedPackage package2 = instantiatePackage(packageName, version, "att=something.else");
+		assertFalse("Unexpectedly reported as equal" + package1 + "==" + package2, package1.equals(package2));
+	}
+
+
+	private ImportedPackage instantiatePackage(String packageName,
+			String version, String ... attributes) throws InvalidAttributeException {
+		Map<String, String> generatedAttributes = new HashMap<String, String>();
+		generatedAttributes.put(Constants.VERSION_ATTRIBUTE, version);
+		for (String att : attributes)
+		{
+			String[] bits = att.split("=");
+			generatedAttributes.put(bits[0], bits[1]);
+		}
+		return new ImportedPackageImpl(packageName, generatedAttributes);
+	}
+
+
+}



Mime
View raw message