Author: bentmann Date: Fri Jun 26 18:02:19 2009 New Revision: 788798 URL: http://svn.apache.org/viewvc?rev=788798&view=rev Log: [MNG-4215] Review and fix hashCode/equals methods of ArtifactRepository implementations Submitted by: Igor Fedorenko
Added: maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java (with props) Modified: maven/components/trunk/maven-compat/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java maven/components/trunk/maven-core/src/main/java/org/apache/maven/ReactorArtifactRepository.java maven/components/trunk/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java Modified: maven/components/trunk/maven-compat/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-compat/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java?rev=788798&r1=788797&r2=788798&view=diff ============================================================================== --- maven/components/trunk/maven-compat/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java (original) +++ maven/components/trunk/maven-compat/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java Fri Jun 26 18:02:19 2009 @@ -327,7 +327,7 @@ { final int prime = 31; int result = 1; - result = prime * result + ( ( id == null ) ? 0 : id.hashCode() ); + result = prime * result + ( ( getId() == null ) ? 0 : getId().hashCode() ); return result; } @@ -345,21 +345,15 @@ { return false; } - + ArtifactRepository other = (ArtifactRepository) obj; - - if ( id == null ) - { - if ( other.getId() != null ) - { - return false; - } - } - else if ( !id.equals( other.getId() ) ) - { - return false; - } - - return true; - } + + return eq( getId(), other.getId() ); + } + + protected static <T> boolean eq( T s1, T s2 ) + { + return s1 != null ? s1.equals( s2 ) : s2 == null; + } + } Added: maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java?rev=788798&view=auto ============================================================================== --- maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java (added) +++ maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java Fri Jun 26 18:02:19 2009 @@ -0,0 +1,59 @@ +package org.apache.maven.artifact.repository; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +import junit.framework.TestCase; + +public class MavenArtifactRepositoryTest + extends TestCase +{ + private static class MavenArtifactRepositorySubclass extends MavenArtifactRepository + { + String id; + + public MavenArtifactRepositorySubclass(String id) + { + this.id = id; + } + + @Override + public String getId() + { + return id; + } + } + + public void testHashCodeEquals() + { + MavenArtifactRepositorySubclass r1 = new MavenArtifactRepositorySubclass( "foo" ); + MavenArtifactRepositorySubclass r2 = new MavenArtifactRepositorySubclass( "foo" ); + MavenArtifactRepositorySubclass r3 = new MavenArtifactRepositorySubclass( "bar" ); + + assertTrue( r1.hashCode() == r2.hashCode() ); + assertFalse( r1.hashCode() == r3.hashCode() ); + + assertTrue( r1.equals( r2 ) ); + assertTrue( r2.equals( r1 ) ); + + assertFalse( r1.equals( r3 ) ); + assertFalse( r3.equals( r1 ) ); + } +} Propchange: maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/components/trunk/maven-compat/src/test/java/org/apache/maven/artifact/repository/MavenArtifactRepositoryTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/ReactorArtifactRepository.java URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/ReactorArtifactRepository.java?rev=788798&r1=788797&r2=788798&view=diff ============================================================================== --- maven/components/trunk/maven-core/src/main/java/org/apache/maven/ReactorArtifactRepository.java (original) +++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/ReactorArtifactRepository.java Fri Jun 26 18:02:19 2009 @@ -179,4 +179,30 @@ return buffer.toString(); } + @Override + public int hashCode() + { + return reactorProjects != null ? reactorProjects.hashCode() : 0; + } + + @Override + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( obj == null ) + { + return false; + } + if ( getClass() != obj.getClass() ) + { + return false; + } + + ReactorArtifactRepository other = (ReactorArtifactRepository) obj; + + return eq( reactorProjects, other.reactorProjects ); + } } Modified: maven/components/trunk/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java?rev=788798&r1=788797&r2=788798&view=diff ============================================================================== --- maven/components/trunk/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java (original) +++ maven/components/trunk/maven-repository/src/main/java/org/apache/maven/repository/DelegatingLocalArtifactRepository.java Fri Jun 26 18:02:19 2009 @@ -132,4 +132,37 @@ return userLocalArtifactRepository.getUrl(); } + @Override + public int hashCode() + { + int hash = 17; + hash = hash * 31 + ( buildReactor == null ? 0 : buildReactor.hashCode() ); + hash = hash * 31 + ( ideWorkspace == null ? 0 : ideWorkspace.hashCode() ); + hash = hash * 31 + ( userLocalArtifactRepository == null ? 0 : userLocalArtifactRepository.hashCode() ); + + return hash; + } + + @Override + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( obj == null ) + { + return false; + } + if ( getClass() != obj.getClass() ) + { + return false; + } + + DelegatingLocalArtifactRepository other = (DelegatingLocalArtifactRepository) obj; + + return eq( buildReactor, other.buildReactor ) + && eq( ideWorkspace, other.ideWorkspace ) + && eq( userLocalArtifactRepository, other.userLocalArtifactRepository ); + } }